Repository navigation
Reject cyclic $ref in schema validation - #434
Merged
Merged
Conversation
A schema whose $ref leads back to itself without consuming any of the
object, such as {"$ref": "#"}, made ucl_schema_validate() recurse
until the stack was exhausted.
Keep a chain of the $refs being followed, on the stack, and fail with
UCL_SCHEMA_INVALID_SCHEMA when a $ref resolves to a schema that is
already being followed for the same object. Recursive schemas that
descend into the object, like the root pointer ref tests, are not
affected.
Fixes vstakhov#389
Contributor
Author
|
@vstakhov gentle ping on this one when you have a moment. It still merges cleanly. Happy to change anything. |
A schema reached through a $ref is evaluated under the root its local refs resolve against, and that root changes when the chain crosses an external ref. A schema object shared between the root document and an ext_refs entry can therefore be reached twice for the same object under different roots without any cycle, which the (schema, obj) key wrongly rejected. Include the root in the frame; the set of roots is still finite, so cycles are still caught. Add a C regression test for the shared case and for a real cycle that alternates between two roots, and align the new refs parameters with clang-format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #389.
A
$refthat leads back to a schema already being followed for the same object, like{"$ref": "#"}or two definitions pointing at each other, madeucl_schema_validate()recurse until the stack overflowed. Following a$refconsumes none of the object, so nothing ever ended the recursion.ucl_schema_validate()and the three helpers that call it now pass along a chain of the$refs being followed, kept on the stack. When a$refresolves to a schema that is already in the chain for the same object, validation fails withUCL_SCHEMA_INVALID_SCHEMA("cyclic reference: #"). Recursion that descends into the object is unaffected: the "root pointer ref" cases inref.json, wherefoorefers back to#, still validate nested data.Two cases added to
tests/schema/ref.json: a self reference, and two definitions that refer to each other (valid when the data never reaches them, invalid when it does). Without the changetest_schema < tests/schema/ref.jsonsegfaults; with it, every file intests/schema/passes.