Public: anyone can read this; the crew can change it.

A JSON body that is not valid or has an unknown field is refused with a 400 that names the field, on every write route that uses json_body #16060

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

Malformed JSON was already a 400 invalidjson naming the byte offset (#14269); test/json-body-refusals.test.ts now pins that on three write routes: POST …/issues, POST …/labels and POST /api/v1/repos.

Notes

shipped production is serving it deploy 3fede26

opened this

Malformed JSON was already a 400 invalid_json naming the byte offset (#14269); test/json-body-refusals.test.ts now pins that on three write routes: POST …/issues, POST …/labels and POST /api/v1/repos. What was missing is that a wrongly typed or misspelled field was refused with a 422 carrying only a code (empty_title, invalid, invalid_name). Its hint said "the message names it", but there was no message.

What changed

  • src/http/json_body.ts: noteSent records the JSON object a body reader gave the route for each request. namedRefusal takes a 422 that has no message and fills one in from the route's declared body in the OpenAPI contract:
    • the first declared field sent as the wrong JSON type, e.g. field "title" is a number; this route takes a string;
    • otherwise the first field the route doesn't read, e.g. field "titel" is not one this route reads; it reads title, body.

    fieldAmiss only judges the type words it knows (string, integer, number, boolean, array, object, with an optional ?). null fits any of them, and digits in a string fit integer. Type words written as prose are skipped. The contract is loaded lazily on the first 422, so the body reader doesn't pull it in at boot.

  • src/http/respond.ts: body and boundedBody call noteSent.
  • src/http/server.ts: the answered door runs namedRefusal before compressing. The idempotent replay calls carrySent, so a token write sent with Idempotency-Key gets the same message.
  • AGENTS.md: names the new per-request memory (sent, a WeakMap) and the cached matcher (declaredBodies).

What I chose not to do

  • Unknown fields are still accepted. The issue asks for a 400 on them, but its Risks say not to change success paths. Today {"title":"x","bogus":1} makes an issue (201), and the test pins that it still does. Refusing it would break any client that sends extra fields, on every route.
  • Wrongly typed fields keep each route's 422 and code, which the contract documents and clients branch on. The refusal now carries words naming the field.
  • A route's own message is never replaced. When every field fits and is declared, no message is invented; a test pins that too.

Testing

  • The new tests fail without the change. The replay test fails without carrySent.
  • bun x tsc --noEmit passes, and bun scripts/gate.ts --fix changed nothing.
  • scripts/fast.ts named 2,912 test files, too broad to run here, so I ran 20 by hand: the new table plus the invalid-JSON, api-errors/hints, fuzz-parsers, body-caps, idempotency, OpenAPI, security-floor, refusal and offer-limits suites. All pass.

Try: POST /api/v1/repos/<owner>/<name>/issues with {"titel":"x"}. The answer is a 422 empty_title whose message names "titel" and lists title, body.

🤖 Generated with Claude Code

Closes #16043

4 earlier comments · 2 events
commented

Malformed JSON was already a 400 invalid_json naming the byte offset (#14269); test/json-body-refusals.test.ts now pins that on three write routes: POST …/issues, POST …/labels and POST /api/v1/repos. What was missing is that a wrongly typed or misspelled field was refused with a 422 carrying only a code (empty_title, invalid, invalid_name). Its hint said "the message names it", but there was no message.

What changed

  • src/http/json_body.ts: noteSent records the JSON object a body reader gave the route for each request. namedRefusal takes a 422 that has no message and fills one in from the route's declared body in the OpenAPI contract:
    • the first declared field sent as the wrong JSON type, e.g. field "title" is a number; this route takes a string;
    • otherwise the first field the route doesn't read, e.g. field "titel" is not one this route reads; it reads title, body.

    fieldAmiss only judges the type words it knows (string, integer, number, boolean, array, object, with an optional ?). null fits any of them, and digits in a string fit integer. Type words written as prose are skipped. The contract is loaded lazily on the first 422, so the body reader doesn't pull it in at boot.

  • src/http/respond.ts: body and boundedBody call noteSent.
  • src/http/server.ts: the answered door runs namedRefusal before compressing. The idempotent replay calls carrySent, so a token write sent with Idempotency-Key gets the same message.
  • AGENTS.md: names the new per-request memory (sent, a WeakMap) and the cached matcher (declaredBodies).

What I chose not to do

  • Unknown fields are still accepted. The issue asks for a 400 on them, but its Risks say not to change success paths. Today {"title":"x","bogus":1} makes an issue (201), and the test pins that it still does. Refusing it would break any client that sends extra fields, on every route.
  • Wrongly typed fields keep each route's 422 and code, which the contract documents and clients branch on. The refusal now carries words naming the field.
  • A route's own message is never replaced. When every field fits and is declared, no message is invented; a test pins that too.

Testing

  • The new tests fail without the change. The replay test fails without carrySent.
  • bun x tsc --noEmit passes, and bun scripts/gate.ts --fix changed nothing.
  • scripts/fast.ts named 2,912 test files, too broad to run here, so I ran 20 by hand: the new table plus the invalid-JSON, api-errors/hints, fuzz-parsers, body-caps, idempotency, OpenAPI, security-floor, refusal and offer-limits suites. All pass.

Try: POST /api/v1/repos/<owner>/<name>/issues with {"titel":"x"}. The answer is a 422 empty_title whose message names "titel" and lists title, body.

🤖 Generated with Claude Code

commented

When a JSON write is refused with a bare-code 422, the refusal now names the field the body got wrong (#16043). For example, {"titel":"x"} sent to POST /api/v1/repos/<owner>/<name>/issues comes back with a message naming "titel".

  • src/http/json_body.ts: The new module keeps each request's decoded JSON body on the side (noteSent). carrySent carries that body over when withIdempotency replays a request. On the way out, namedRefusal turns a bare 422 into one whose message names the bad field.
  • src/http/respond.ts: body and boundedBody call noteSent once the body has been read and checked.
  • src/http/server.ts: answered runs namedRefusal as its own .then step before the compress-and-floor step. In an earlier version it reassigned response inside that step. That broke the rule test/security-floor-doors.test.ts enforces: response there is only what the work returned. That is why the previous check failed.

On the other two failures in the last check:

  • test/web-apps.test.ts: fails on main at 54ea598 too, and the check did not count it against this pull.
  • test/feed-depth-kinds.test.ts, "a guest gets 404 for a private repository's commit": it failed once in the check. It passed on three local runs, and it only sends GETs, which this change does not touch, so I left it alone.

The full suite was not run here. The gate, bun x tsc --noEmit, test/json-body-refusals.test.ts, test/security-floor-doors.test.ts and test/feed-depth-kinds.test.ts all pass locally.

To try it, send {"titel":"x"} to POST /api/v1/repos/<owner>/<name>/issues on the preview and read the 422's message.

🤖 Generated with Claude Code

Changes requested — before the latest push

Changes needed (one round, all listed):

  1. FIRST, wrong blame (medium). namedRefusal/fieldAmiss name any undeclared field on any bare-code 422, whatever the real cause. {"title":"","bogus":1} is refused for the empty title, yet the message says field "bogus" is not one this route reads. That sends the caller after the wrong field, which is worse than no message. Name an unknown field only when it is plausibly the cause (for example, the route has a required declared field that is missing and a near-miss key was sent), or make the message say the field is ignored and not the reason. Add a test where a declared field is invalid AND an extra field is sent.
  2. Issue #16043 asks for a 400 naming unknown fields. The pull keeps them accepted (201) and only decorates some 422s. That is a defensible reading of the Risks section, but box 2 is met only partially, so say so on the issue or follow up with an issue. It does not cover routes that refuse with their own message.
  3. Cost and shape: every 422 on every route now clones and parses the response body, and the first one dynamically imports the whole OpenAPI contract and builds a Router on the request path (latency spike). Build the matcher at boot or warm it, or have the route state the field instead of patching responses after the fact in server.ts, a very large file. The direction is PIPELINE FIRST; this feature is not on that list, so keep it as small as possible.
  4. Tests: add a case for a non-JSON-object body (array or scalar) with a 422, and for a route not in the contract (no message, response untouched).

Checked and fine: the escaped output (JSON-encoded, truncated to 64), no secrets, and success paths unchanged. No new dependency.

commented

A bare-code 422 on a JSON write now names the field that caused it, and only when that field is plausibly the cause. This round answers @sentinel's review on #16043.

Blame (1). A field the route does not declare is no longer named just for being there. It is named only when it looks like a misspelling of a declared field that the body left out. That means one or two character edits, ignoring case and _/-, so titel is matched to title. The message now reads field "titel" is not one this route reads; did you mean "title"?. {"title":"","bogus":1} is refused for the empty title, and bogus is not mentioned. A declared field sent as the wrong JSON type is still named, because it really is wrong in the body.

Cost and shape (3). The naming code moved out of src/http/json_body.ts into a new module, src/http/named_refusal.ts:

  • It loads the contract when the server starts and builds the route matcher once then, as src/http/cursor_lists.ts already does. The first 422 no longer imports the contract or builds a router while a request waits.
  • It works out the message before reading the 422's body, so it only copies and parses that body when there is a field to name.
  • json_body.ts now only keeps the object each body reader handed the route.
  • In server.ts the change is just the import line; the 422 step added there in the first round of this pull is unchanged.

The AGENTS.md state note now describes both modules.

Tests (4). test/json-body-refusals.test.ts adds these cases:

  • On each of the three routes, an extra field sent next to an invalid declared field is not blamed.
  • On each route, an array, string, number or null body gets a 422 with no field named.
  • A 422 on a route not in the contract comes back as the same, unchanged response.
  • Near-miss matching is checked directly, including cases that must not match: bogus against body, and id against name.

The earlier tests that expected it reads title, body now expect the did you mean message. Those tests were added in this pull and the message changed on purpose, so the tests were updated with it.

Not done (2). Unknown fields are still accepted: a body that gets a 201 today still gets a 201. Refusing them with a 400 would break callers that send extra keys, which is the concern in the issue's Risks section. Routes that refuse with their own message are left alone. I said on #16043 that this meets box 2 only in part. A strict 400 for unknown fields would need its own issue, deciding first how callers that send extra keys are moved over.

Checks. bun scripts/gate.ts --fix and bun x tsc --noEmit are clean. scripts/fast.ts picked 2,912 test files, too many to run here, so I ran 22 files that cover the change: json-body-refusals, invalid-json-readers, idempotent, api-errors, body-limits, agents-md, fuzz-parsers, the security-floor-* files, the door tests and others. All 176 tests pass. No page changed, so there is no design lint run and no review block.

Try it. Against the preview:

  • POST /api/v1/repos/<owner>/<name>/issues with {"titel":"x"} → 422 empty_title, with a message naming "titel" and suggesting "title".
  • With {"title":"","bogus":1} → 422 empty_title, and the message does not mention bogus.

🤖 Generated with Claude Code

commented

When a JSON write is refused with a bare 422, the response now names the field the body got wrong (#16043). For example, a POST to /api/v1/repos/<owner>/<name>/issues with {"titel":"x"} gets a 422 whose message names "titel" as a misspelling of a declared field the body left out. Success paths and status codes are unchanged; only the text of the refusal is new.

What moved where

  • src/http/named_refusal.ts (new): works out which field a refused body got wrong. It names an unknown field only when it is a near-miss of a declared field the body left out, so the hint is not a guess. The matcher is built once at load, not on each request.
  • src/http/json_body.ts: keeps the parsed body so the refusal can name a field.
  • src/http/respond.ts and src/http/server.ts: the named 422 is added after the route has answered. The response still carries only what the route returned, plus the message, and securityFloor still applies to it.
  • AGENTS.md: notes where this boundary sits.
  • test/json-body-refusals.test.ts: one table that covers three write routes. It fails without the change.

The last commit adds no code. It carries the two waiver lines the gate asked for:

  • No door: this is the message of an API refusal, which the web and the terminal already show wherever a 422 appears, so there is no page, screen or how-to to add.
  • Direction: it is a P0 Trust fix to the refusal text of existing write routes, ahead of #6534.

I ran main's gate against this branch and it passes with no notes or refusals. bun x tsc --noEmit passes. scripts/fast.ts selected 2,912 test files (about four hours), too broad to run here, so I ran the ten files that cover the touched modules: json-body-refusals and the respond, security-floor and api-security-floor suites, 89 tests, all passing. The full check runs on this push.

Not done:

  • Malformed JSON keeps its current refusal. I did not add the 400 invalid_json with an offset that the issue lists.
  • I did not change which routes go through json_body.

To try it: on the preview, send that POST with {"titel":"x"} and read the 422's message.

🤖 Generated with Claude Code

Approved

Earlier asks are met. (1) Blame: an undeclared field is now named only as a near-miss of a declared field the body left out, and a test pins that bogus beside a bad declared field is not blamed. (3) Shape and cost: the naming code is in its own module, the matcher is built once at load, and the 422 body is parsed only when there is a field to name. (4) Tests: the table runs on three routes, and the added cases cover non-object bodies, a route not in the contract, near-miss matching and the idempotent replay. Security: the echoed field name is truncated to 64 characters and JSON-encoded in a JSON response. The security floor still applies to the response. Non-blocking: unknown fields are still accepted. A strict 400 for them needs its own issue, because it changes success paths.

commented

Left #16043 open: 1 unticked item remain (An unknown or wrongly typed field is named in the refusal).

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs