Repository navigation
Validate request.body.schema as a bounded JSON Schema 2020-12 profile (#845) - #860
Merged
Merged
Conversation
…20-12 profile with Ajv (#845) request.body.schema moves from URLCode's private interpreted subset to an explicit JSON Schema 2020-12 profile, compiled once at load by Ajv (ajv/dist/2020). The private interpreter is deleted; there is no old-dialect translator or compatibility mode. - Profile (body-validation.ts): a keyword allowlist with $schema (2020-12 only) and $defs at the root, local #/$defs/<name> $ref only (acyclic, never resolved remotely or fetched), type lists incl. "null", allOf/anyOf/oneOf/not, scalar enum/const, string/number/array/object constraints, prefixItems, propertyNames, patternProperties and format: uuid (no format library ships). Anything else fails validation/activation with a bounded diagnostic naming the keyword and the JSON pointer inside the schema; a $ref value is never echoed. - Regex admission: pattern and patternProperties keys go through assertSafePattern; pattern needs maxLength <= 128 and patternProperties needs propertyNames.maxLength <= 128 on the same schema, which Ajv checks first. - Limits: 8 levels, 256 nodes, 32 $ref uses, 32 $defs, 1024 nodes once refs are expanded, uniqueItems only with maxItems <= 64. - Ajv options: strict, allErrors false (first failure only), ownProperties, messages off. The 422 keeps its shape; pointers show declared names only, [] for array positions and * for other client keys; anyOf/oneOf branch failures fold into one issue. The answer now names the first failure instead of up to eight (documented in docs/HTTP.md). - Cloudflare: the build emits body-validators.js (Ajv standalone ESM with the same options); the Worker refuses to start without it. Ran on real workerd via npm run test:workerd: 21/21 responses identical to the server, including five 2020-12 requests. - Identity-before-validation order is unchanged and now tested. - mcp compiles inputSchema/outputSchema with the same public @jimhoyd/urlcode/body-schema code at activation. - Shared fixtures in test/fixtures/body-schema/profile.json run on Node and on the Worker build; examples/body-validation gains a 2020-12 route. Co-Authored-By: Claude Opus 5.5 <[email protected]>
jimhoyd
enabled auto-merge (squash)
September 28, 2026 20:11
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
First deliverable of #845.
request.body.schemais now an explicit, bounded JSON Schema 2020-12 profile, compiled once at load by Ajv (ajv/dist/2020). The private schema interpreter is deleted, with no dialect translator and no compatibility mode.Profile
The full allowlist is in docs/HTTP.md and
bodySchemaProfile.$schema(2020-12 only) and$defs, plus local#/$defs/<name>refs. Cycles are refused and nothing remote is fetched."null"; scalarenum/const;allOf/anyOf/oneOf/notformat: uuidonly; there is noajv-formats.$id,$anchor,$dynamicRef,$vocabulary,definitions,nullable,default,readOnly,writeOnly, and any other keyword off the allowlist. Each refusal is a bounded diagnostic with a JSON pointer, and it never echoes a$refvalue.patternandpatternPropertieskeys go through the existingassertSafePattern. They also need a same-nodemaxLength(orpropertyNames.maxLength) of at most 128, so an overlong value never reaches the regex.uniqueItemsonly withmaxItems≤ 64, and 64 properties per object.HTTP contract change
The 422 shape and its 4096-byte cap are unchanged. A 422 now names only the first failure, because Ajv runs with
allErrors: falseon untrusted input; before, it could list up to 8 issues. Client-chosen keys are redacted in pointers. Documented in docs/HTTP.md.Targets
body-validators.js), generated with the same options. A Worker whose body-schema validator is missing refuses to start. The Worker path never imports Ajv, and a test asserts the generated code contains nonew Function/eval.Evidence (macOS, Node 26)
npm run verifypasses, including the workspace suites and the proof.npm test: 1115/1115.npm run test:packagepasses. mcp tests: 42/42.npm run test:workerd: 21/21 parity. It ran just before three final edits, which touch Node-side files only.test/fixtures/body-schema/profile.jsonrun on Node and through the Worker build: objects, nullable unions, local refs, invalid input, unsupported keywords, remote/$id/draft-07 refs, recursive refs, ReDoS patterns, unboundeduniqueItemsand unsupported formats.examples/body-validationadds a 2020-12/contactsroute, with 19/19 fixtures passing.Not in this PR
Filed as follow-ups: the store and forms vocabularies, OpenAPI export, one source schema shared by HTTP, MCP and forms, per-method body rules, more formats, and running the workerd parity check in CI.
🤖 Generated with Claude Code