Repository navigation
Recursion guard - #306
Merged
Merged
Recursion guard#306
Conversation
With recycled validators, SchemaValidator.Validate and the allOf, anyOf, oneOf and not loops of schemaPropsValidator cleared the slot of a child only after the child's Validate returned. A child redeems itself in a deferred call, so when its Validate panicked (e.g. a $ref under a property that cannot be expanded), the parent's deferred redeemChildren redeemed it a second time. The pool then hands the same object to two later borrowers. A server survives the panic (net/http recovers handler panics), so later requests could share validator state and return wrong results. Clear the slot before calling Validate. TestPools_NoDoubleRedeemOnPanic reproduces it with -tags poolsdebug, which reports the double redeem. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Frédéric BIDON <fredbi@yahoo.com>
A circular $ref through a composition keyword, such as
A: {allOf: [{$ref: A}]}, made schema validation overflow the goroutine
stack: a fatal error that recover cannot catch.
go-openapi/spec bounds each expansion, and leaves a circular $ref in
place. But newSchemaPropsValidator builds the allOf, anyOf, oneOf and
not validators along with their parent, and each one expands its $ref
again, one level down, in a new expansion. Nothing bounded that loop.
With two branches, A: {allOf: [{$ref: A}, {$ref: A}]}, it builds
2^depth validators.
Thread a compositionBudget through validator construction. It caps the
nesting at maxCompositionDepth (128) and the count at
maxCompositionValidators (10,000) per schema validator. Going over
either cap panics with "Invalid schema provided to SchemaValidator",
like any other schema that cannot be expanded. Only schemas with
composition keywords create a budget, and it stays on the stack: no
pre-walk, no allocation. Validators for properties and items are built
as Validate walks the data, and start their own budget, so recursive
schemas such as trees are not affected.
SpecValidator no longer panics on such a schema when it validates
defaults and examples (reached with ContinueOnErrors, since the
circular ancestry check stops it otherwise): validateValueAgainstSchema
returns the panic as an error at the location of the value.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Frédéric BIDON <fredbi@yahoo.com>
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.
Change type
Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update
Short description
Fixes
Full description
Checklist