Skip to content

Bound schema composition nesting to mitigate deep-composition DoS - #305

Closed
fredbi with Copilot wants to merge 1 commit into
masterfrom
copilot/fix-validation-recursion-depth
Closed

fredbi with Copilot wants to merge 1 commit into
masterfrom
copilot/fix-validation-recursion-depth

Conversation

Copilot AI commented Oct 19, 2026

Copy link
Copy Markdown

validate.Spec and validate.AgainstSchema previously accepted unbounded allOf/anyOf/oneOf/not nesting, so small untrusted documents could trigger disproportionate CPU and memory usage. This change introduces a fail-fast composition-depth guard with deterministic error locations.

  • Configurable depth limit (safe default)

    • Adds WithMaxSchemaCompositionDepth(limit int) to schema validation options.
    • Sets a default maximum depth of 256; non-positive values fall back to this default.
  • Shared depth-check implementation

    • Adds an iterative composition walker (schema_composition_depth.go) that traverses only allOf, anyOf, oneOf, and not.
    • Tracks depth explicitly and reports the first over-limit path deterministically.
  • Enforcement at both validation entry points

    • Applies the guard in schema validation (AgainstSchema / NewSchemaValidator) before deep validator construction.
    • Applies the guard in spec validation (SpecValidator.Validate) as an early pass across schema-bearing locations (definitions, parameters, responses, and path/operation schema slots).
  • Error behavior

    • Adds a dedicated validation message: schema composition nesting exceeds limit.
    • Surfaces this as standard validation errors, preserving normal error reporting semantics.
  • Regression coverage

    • Adds tests for:
      • AgainstSchema: over-limit fails fast, higher limit succeeds.
      • Spec: deeply nested definition composition fails fast, higher limit succeeds.
      • Option behavior: default and round-trip handling for WithMaxSchemaCompositionDepth.
err := validate.AgainstSchema(schema, data, strfmt.Default,
	validate.WithMaxSchemaCompositionDepth(64),
)
// err contains: "<path> schema composition nesting exceeds limit of 64"

Co-authored-by: fredbi <14262513+fredbi@users.noreply.github.com>
@fredbi fredbi closed this Oct 20, 2026
@fredbi
fredbi deleted the copilot/fix-validation-recursion-depth branch October 20, 2026 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants