Plan: Size bounds on untyped request JSON
Status
Done (2026-11-21) — every free-form JSON request field carries a size bound the extractor enforces (craig_validation::json_bounds, seven named bounds), or is named in DTO_UNTYPED_BOUNDS with the bound it has (the two IV-E inputs, the two mock-server bodies); the dto-length untyped ceiling is replaced by that table, and the NUL-only JSON bindings are deleted. Three deviations are recorded as errata below.
Closes: #2633, on the last step.
Tree: written against main at 63e1f4f70.
Ruling (maintainer, 2026-11-21, on #2633): the next plan to draft; per-field size models, starting from a measurement of what shipped rulesets, fixtures and seeded rows reach.
Erratum (2026-11-21): step 3 does not bind CreateReportRequest::raw_submission, so its ceiling is 12, not 11. The field carries the whole intake body, whose free-form narrative the intake edge bounds only per string (up to 32,768 bytes) and by the parser’s recursion limit, with no value count; RECORD_JSON would make craig-cases refuse a report the edge accepted. It is bound after the edge bounds narrative, in a step of its own before the close.
Erratum (2026-11-21): SendExchangeRequest::payload takes a fifth bound, BATCH_JSON (32 levels, 100,000 values, 65,536 bytes), not RECORD_JSON: one payload carries a batch of records (record_count), and no real payload exists to measure, so the numbers are judgment. Step 4’s ceiling is 5 (step 3’s 12 less seven fields).
Erratum (2026-11-21): the step the first erratum deferred. The intake edge bounds narrative with SUBMISSION_NARRATIVE_JSON (16 levels, 5,000 values, 32,768 bytes, the sanitizer’s own raw ceiling), and craig-cases binds raw_submission with SUBMISSION_JSON (32 levels, 10,000 values, 32,768 bytes), sized to hold anything the edge forwards; an intake test builds the largest admitted submission and checks it fits. The ceiling is then 4.
Erratum (2026-11-21): delivered in seven merge requests, not five: the plan page, the four planned steps, the submission step the first erratum deferred, and the close.
Context
-
The dto-length lint (
xtask/src/cmd/lints/dto_extractors.rs) follows every request type to its fields and requires a length cap on each string. A field whose type is underserde_jsonis an "untyped leaf": counted, never checked, and pinned byDTO_OUT_OF_MODEL_CEILINGS(xtask/src/cmd/lints.rs) at 25. -
The 25 are 23 production request fields and 2 craig-mock-server handler bodies (census on #2633). Beyond the service’s request body limit (2 MiB by default,
craig_common::settings), today:-
2 fields carry a full size model the lint cannot see:
CreateIveDeterminationRequest::case_inputsandchild_inputs(validate_inputs_documentincrates/craig-cases-contracts/src/ive_determination.rs: depth 8, 256 nodes, 64-byte keys, 12 children). -
4 are parsed into a closed shape by their handler after extraction:
RegisterSignerKeyRequest::public_key_jwk, the twoauth_config, andUserDeltaBody::user_delta. -
17 have the NUL check (
craig_validation::validate_json_no_nul) or nothing: rule-setcontent(5 types),EvaluateRequest::input, reportchildren/adults/raw_submission, court-orderfindings(2), reviewfindings/remediation_plan, agreementdata_elements(2),SendExchangeRequest::payload,OverrideDelta::delta.
-
-
Since #2662 the shared extractors run each type’s garde rules before the handler, so a garde rule on the field is enforced on every route that extracts it.
-
Measured (notes on #2633): across the four layers' 153 rule-set documents the largest is 314,010 bytes, depth 8, 329 entries in one container, longest string 5,616 bytes, 5,155 nodes.
-
Measured on a seeded devstack (2026-11-21, read-only SQL, byte length of each stored document as text):
Column Rows Largest (bytes) rule_sets.content147
247,122
rule_evaluations.input75,046
610
security_reviews.findings/remediation_plan17 / 17
67 / 81
data_sharing_agreements.data_elements33
58
exchange_partners.auth_config24
29
court_orders.findings5
50
reports.raw_submission/children/adults14 each
1,641 / 505 / 413 (encrypted envelopes, so ciphertext sizes)
exchange_transactions.request_payload,composition_overrides.delta0
no seeded row
What this does and does not show. A document’s byte length is an upper limit on its node count and on its longest string, so every seeded document outside the rule sets is far inside the node and string bounds below. Depth was not measured for stored rows, the report columns show ciphertext only, two columns have no seeded row, and the seed is synthetic: it is no evidence of what a jurisdiction’s real findings or partner payloads reach. No seeded row is near any bound, so no stored document is known to become un-resendable.
Design
One bound type, one walk
New module crates/craig-validation/src/json_bounds.rs, behind the crate’s existing json feature:
-
pub struct JsonBounds { pub depth: u16, pub nodes: u32, pub string_bytes: u32 }.depthcounts containers on the path to a value (a scalar at top level is depth 0).nodescounts every value, including each array element and each object member.string_bytescaps each string value and each object key, in UTF-8 bytes. -
pub enum JsonExcess { Depth, Nodes, StringBytes, Nul }andpub fn json_excess(value: &Value, bounds: &JsonBounds) → Option<JsonExcess>. One iterative walk with its own stack, asjson_has_nulis, which stops at the first excess. It also refuses a NUL, so a bounded field needs no second rule. -
Garde bindings behind
garde+json, in the closure-returning form the tree already uses (custom(each_child_once())):pub fn json_within(bounds: &'static JsonBounds) → impl Fn(&Value, &()) → garde::Resultandjson_option_withinforOption<Value>. The message is value-free and names the limit, for examplemust not nest deeper than 16 levels; the NUL message staysJSON_NUL_REFUSAL. -
The refusal is the house 422
validation-failednaming the field, produced by the extractor. No new problem type.
The bounds
Four named constants in the same module. Rule: each is at least four times the largest value measured for any field of its class, and a measurement that breaks that rule raises the constant before its step lands.
| Constant | depth | nodes | string bytes | Fields |
|---|---|---|---|---|
|
32 |
50,000 |
65,536 |
the five rule-set |
|
16 |
5,000 |
16,384 |
|
|
16 |
2,000 |
16,384 |
|
|
4 |
32 |
1,024 |
|
Each field’s existing custom(validate_json_no_nul) / validate_json_option_no_nul / skip is replaced by custom(json_within(&X)) or custom(json_option_within(&X)). Handler-side checks stay as they are (the children count, the JWK and auth_config parses, the override dry run, the IV-E walker); the bound runs first. No field is retyped: that would move today’s typed 400s to deserialize-time 422s for no gain in the bound.
Stored rows are not touched. A stored document over its bound can still be read; re-sending it in an update is refused. The seeded-row measurement shows whether any exists.
The lint
In dto_extractors.rs, WireField gains the field’s attributes, and a field whose leaf resolves Untyped is not counted when its garde attribute holds custom(json_within(…)) or custom(json_option_within(…)). The untyped ceiling then falls with each step (it is shrink-only, so each step lowers it). The last step removes the untyped ceiling entry and adds DTO_UNTYPED_BOUNDS, a table in the shape of DTO_OPAQUE_BOUNDS, reconciled both ways, naming the four sites that stay unbound by the shared walk with the bound each has: the two IV-E fields (their own walker and its pinning tests) and the two craig-mock-server bodies (devstack emulator; bounded by its body limit only). A new untyped request field then fails lints dto-length until it carries json_within or a table row.
Scope
Steps, one MR each:
-
The walk and the lint.
json_bounds.rswith its tests; the lint’s attribute rule and its unit tests; no field changes, ceiling still 25. -
craig-rules-contracts.
content×5 andinput; a test that every rule-set document of the locked layers fitsRULE_SET_JSON; ceiling 25 → 19. -
craig-cases-contracts and craig-security-contracts.
findings×2,children,adults,raw_submission; reviewfindings,remediation_plan,public_key_jwk; ceiling → 11. -
craig-exchange and craig-composition.
data_elements×2,auth_config×2,payload,delta,user_delta; ceiling → 4. -
Close.
DTO_UNTYPED_BOUNDSreplaces the ceiling entry;craig_validation::validate_json_no_nulandvalidate_json_option_no_nul, their per-cratetext_guardsre-exports and their tests are deleted once no field binds them (json_has_nulstays: the walk and the override handler use it);quality-gates.adoc; plan archived.
Out of scope: retyping any field; the 17 Multipart sites (#2632); response types; stored data; the BFFs' own forms; a per-deployment override of the bounds.
Tests
| Category | Test |
|---|---|
Property |
craig-validation |
Unit (boundaries) |
|
Contract (wire: the 422) |
craig-common extractor test |
Regression (shipped data fits) |
Step 2: |
Devstack-gated (car only) |
One per service touched: a body one level past the field’s depth bound answers 422 naming the field, and the same body at the bound is accepted. Existing API tests stay green unchanged. |
Lint unit |
|
Red proof |
Step 1: make the walk return |
Risks
| Risk | Mode | Mitigation |
|---|---|---|
A bound is tighter than real data, so a legitimate request is refused |
fail-closed |
The four-times rule over measured maxima; the locked-layer test; each step’s car runs every API test and the seed. A refusal is a 422 naming the field, so it is visible. |
A stored document exceeds its bound and can no longer be re-sent in an update |
fail-closed |
The seeded-row measurement before approval; the Ops clause tells an operator the limits. |
A field keeps a NUL-only rule and is assumed bound |
fail-open |
The lint counts it until it carries |
The walk miscounts (off by one, keys not capped) |
fail-open |
The boundary unit test per limit and the oracle property test. |
The lint accepts a |
silent |
The rule matches the two binding names only, by their last path segment; the unit test covers a look-alike name. |
The bound runs before a handler’s typed 400 (JWK, |
fail-closed |
Only a body over the bound moves, to 422; recorded in the release note. |
Bound text in |
silent |
Four rows only, each naming its pinning test, as |
Delivery
-
Doc page:
docs/modules/ROOT/pages/quality-gates.adoc(the dto-length section: the bound, the table); the four constants are also stated indocs/modules/ROOT/pages/shared-crates.adoc§ craig-validation. -
CHANGELOG entry: one
changelog.d/2633.<slug>.changed.adocfragment per step 2–4, each with the fields bounded and the limits; Ops clause: a request whose JSON document exceeds the stated depth, node or string limit is now refused with 422validation-failed. -
Served text: each bounded field’s doc comment states its limits, so
cargo xtask api-docsis regenerated in the same step, and the craig-cases golden is re-blessed if its slice changes. -
Gates before each push: fmt, clippy, machete, nextest for the touched crates,
cargo xtask validate --profile fast,cargo xtask test --doc. -
Close:
Closes #2633on step 5; plan archived with the measured maxima and the final table; a note with merge evidence.
Review log
| Round | Vendor | Resolved model | Verdict |
|---|---|---|---|
1 |
Anthropic (Vertex) |
|
CLEAN: all four questions pass; one note (say whether the NUL-only JSON bindings are deleted), answered in the Scope’s close step. |
2 |
Google (Vertex) |
|
CLEAN: all four questions pass. |