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

A malformed JSON body is answered invalid_json, not the first field's validation error #14291

opened this

Check · Review · Land · Live — Its check is red. Next: builder, its check is red.

Ready to land

A malformed JSON body is now answered 400 invalidjson on every route that reads one, instead of being read as {} and failing on the first field's validation. The new test, the other API and contract suites I ran, tsc and the gate pass.

Notes · 5 files · +180 −19

stopped failed before it answered — 19157 passed, 4 failed: a person who cannot write, and a repository no longer stuck, get nothing done, fuzz: the API's JSON bodies > body() is always a plain object, whatever the bytes, the keeper's answer and its page carry both, with the counts, and 1 more deploy de31989

Check failed: a person who cannot write, and a repository no longer stuck, get nothing doneCheck of de31989checked against main at 59903fafast tier: red in 6m15s

opened this

A malformed JSON body is now answered 400 invalid_json on every route that reads one, instead of being read as {} and failing on the first field's validation. The new test, the other API and contract suites I ran, tsc and the gate pass. I didn't run the full suite: scripts/fast.ts named 2620 of 3734 files, so I ran 162 related ones.

What changed

  • Reader: src/http/json_body.ts is new. Both body readers in src/http/respond.ts (body and boundedBody) now go through it, so there is one parse point. A parse failure throws InvalidJson, which the server's failure handler in src/http/server.ts answers, the same way it answers BodyTooLarge. The security headers are kept on that response.
  • Answer: {bad gets 400 {error:"invalid_json", message:"the body is not valid JSON: bad token \"bad}\" at byte 8", hint:"send a JSON object with content-type application/json, e.g. {\"name\":\"value\"}"}. The offset is in bytes, from a small scanner that finds the first bad character. Bun's own parse message names neither a token nor an offset.
  • Contract: openapi.ts adds an invalid_json refusal and a 400 response to every route that takes a body (POST, PUT or PATCH). It is therefore in GET /contract/openapi.json for all of them. I found no separate error table elsewhere in src/contract, so the OpenAPI refusals are the contract's table.
  • Routes covered: every route reading a body through body() or boundedBody(). That includes /api/v1/start, /api/v1/users, /api/v1/first-admin, tokens, reports, offers, conversation, runners, maintenance, business plan and the OAuth endpoints. The checkbox list the issue asked for is not in this description.
  • Test: test/api-errors.test.ts posts a malformed body to /api/v1/start, /api/v1/users and /api/v1/first-admin, and each answers invalid_json with the offset. It also checks the offset scanner and the contract entry. On main these routes answer 422 invalid_name and similar.

Left as is

  • An empty body is still {}, and valid JSON that isn't an object (an array or a number) is still {}, so the route's own validation answers it.
  • displays.ts and boards.ts already answer their own bad_json 400. I didn't rename them, to avoid a second contract change.
  • Clients that matched on the old 422 for a malformed body now get 400. That is the change the issue asks for.

Try it: curl -i -X POST $BASE/api/v1/start -H 'content-type: application/json' -d '{bad'

The pre-push hook wanted a "Try:" commit, so there is an empty commit with that line.

Closes #14269

edited this
commented

A malformed JSON body is now answered 400 invalid_json on every route that reads one, instead of being read as {} and failing on the first field's validation. The new test, the other API and contract suites I ran, tsc and the gate pass. I didn't run the full suite: scripts/fast.ts named 2620 of 3734 files, so I ran 162 related ones.

What changed

  • Reader: src/http/json_body.ts is new. Both body readers in src/http/respond.ts (body and boundedBody) now go through it, so there is one parse point. A parse failure throws InvalidJson, which the server's failure handler in src/http/server.ts answers, the same way it answers BodyTooLarge. The security headers are kept on that response.
  • Answer: {bad gets 400 {error:"invalid_json", message:"the body is not valid JSON: bad token \"bad}\" at byte 8", hint:"send a JSON object with content-type application/json, e.g. {\"name\":\"value\"}"}. The offset is in bytes, from a small scanner that finds the first bad character. Bun's own parse message names neither a token nor an offset.
  • Contract: openapi.ts adds an invalid_json refusal and a 400 response to every route that takes a body (POST, PUT or PATCH). It is therefore in GET /contract/openapi.json for all of them. I found no separate error table elsewhere in src/contract, so the OpenAPI refusals are the contract's table.
  • Routes covered: every route reading a body through body() or boundedBody(). That includes /api/v1/start, /api/v1/users, /api/v1/first-admin, tokens, reports, offers, conversation, runners, maintenance, business plan and the OAuth endpoints. The checkbox list the issue asked for is not in this description.
  • Test: test/api-errors.test.ts posts a malformed body to /api/v1/start, /api/v1/users and /api/v1/first-admin, and each answers invalid_json with the offset. It also checks the offset scanner and the contract entry. On main these routes answer 422 invalid_name and similar.

Left as is

  • An empty body is still {}, and valid JSON that isn't an object (an array or a number) is still {}, so the route's own validation answers it.
  • displays.ts and boards.ts already answer their own bad_json 400. I didn't rename them, to avoid a second contract change.
  • Clients that matched on the old 422 for a malformed body now get 400. That is the change the issue asks for.

Try it: curl -i -X POST $BASE/api/v1/start -H 'content-type: application/json' -d '{bad'

The pre-push hook wanted a "Try:" commit, so there is an empty commit with that line.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs