Skip to content

Commit

Permalink
Detect certain JSON reference cycles while parsing schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanpenman committed Dec 22, 2024
1 parent ae0112b commit b151d1e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
12 changes: 12 additions & 0 deletions include/valijson/schema_cache.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <map>
#include <string>

#include <valijson/subschema.hpp>

namespace valijson {

typedef std::map<std::string, const Subschema *> SchemaCache;

} // namespace valijson
15 changes: 8 additions & 7 deletions include/valijson/schema_parser.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <stdexcept>
#include <functional>
#include <iostream>
#include <vector>
#include <memory>
#include <functional>

#include <valijson/constraints/concrete_constraints.hpp>
#include <valijson/internal/adapter.hpp>
Expand All @@ -14,6 +12,7 @@
#include <valijson/internal/uri.hpp>
#include <valijson/constraint_builder.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_cache.hpp>
#include <valijson/exceptions.hpp>

namespace valijson {
Expand Down Expand Up @@ -109,8 +108,8 @@ class SchemaParser
void populateSchema(
const AdapterType &node,
Schema &schema,
typename FunctionPtrs<AdapterType>::FetchDoc fetchDoc = nullptr ,
typename FunctionPtrs<AdapterType>::FreeDoc freeDoc = nullptr )
typename FunctionPtrs<AdapterType>::FetchDoc fetchDoc = nullptr,
typename FunctionPtrs<AdapterType>::FreeDoc freeDoc = nullptr)
{
if ((fetchDoc == nullptr ) ^ (freeDoc == nullptr)) {
throwRuntimeError("Remote document fetching can't be enabled without both fetch and free functions");
Expand Down Expand Up @@ -148,8 +147,6 @@ class SchemaParser
typedef std::map<std::string, const DocumentType*> Type;
};

typedef std::map<std::string, const Subschema *> SchemaCache;

/**
* @brief Free memory used by fetched documents
*
Expand Down Expand Up @@ -478,6 +475,10 @@ class SchemaParser

}

if (std::find(newCacheKeys.begin(), newCacheKeys.end(), queryKey) != newCacheKeys.end()) {
throwRuntimeError("found cycle while resolving JSON reference");
}

// JSON References in nested schema will be resolved relative to the
// current document
const AdapterType &referencedAdapter =
Expand Down

0 comments on commit b151d1e

Please sign in to comment.