Skip to content

Validate request.body.schema as a bounded JSON Schema 2020-12 profile (#845) - #860

Merged
jimhoyd merged 2 commits into
mainfrom
claude/body-schema-2020-12
Sep 28, 2026
Merged

jimhoyd merged 2 commits into
mainfrom
claude/body-schema-2020-12

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

First deliverable of #845. request.body.schema is 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.

  • Supported:
    • $schema (2020-12 only) and $defs, plus local #/$defs/<name> refs. Cycles are refused and nothing remote is fetched.
    • type lists including "null"; scalar enum/const; allOf/anyOf/oneOf/not
    • object, array, string and number constraints
    • format: uuid only; there is no ajv-formats.
  • Refused at load: $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 $ref value.
  • Regex admission: pattern and patternProperties keys go through the existing assertSafePattern. They also need a same-node maxLength (or propertyNames.maxLength) of at most 128, so an overlong value never reaches the regex.
  • Limits: depth 8, 256 nodes, 32 refs, 1024 expanded nodes, uniqueItems only with maxItems ≤ 64, and 64 properties per object.
  • Order unchanged: identity and access checks still run before body validation, and a test covers it.

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: false on untrusted input; before, it could list up to 8 issues. Client-chosen keys are redacted in pointers. Documented in docs/HTTP.md.

Targets

  • Node, AWS and Vercel compile with Ajv at load.
  • Cloudflare ships Ajv's standalone ESM output (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 no new Function/eval.
  • The workerd parity run was made with real workerd (wrangler dev --local, macOS arm64): all 21 responses matched Node, including 5 new 2020-12 cases.

Evidence (macOS, Node 26)

  • npm run verify passes, including the workspace suites and the proof.
  • npm test: 1115/1115. npm run test:package passes. mcp tests: 42/42.
  • npm run test:workerd: 21/21 parity. It ran just before three final edits, which touch Node-side files only.
  • The fixtures in test/fixtures/body-schema/profile.json run 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, unbounded uniqueItems and unsupported formats.
  • examples/body-validation adds a 2020-12 /contacts route, 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

…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]>
Comment thread test/capability-query.test.ts Fixed
@jimhoyd
jimhoyd merged commit b1531e9 into main Sep 28, 2026
30 checks passed
@jimhoyd
jimhoyd deleted the claude/body-schema-2020-12 branch September 28, 2026 20:20
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