Skip to content

Answer unparseable request bodies with a 400 JSON error - #553

Merged
evantahler merged 1 commit into
mainfrom
claude-evan/elegant-cori-houxov
Oct 20, 2026
Merged

evantahler merged 1 commit into
mainfrom
claude-evan/elegant-cori-houxov

Conversation

@evantahler

@evantahler evantahler commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

closes #552

Problem

A non-GET request with Content-Type: application/json and an empty or malformed body failed inside parseRequestParams. handleWebAction handled only Payload Too Large and rethrew every other error, so Bun's default handler answered with a 500. In development that was an HTML page, in production plain text. The response had no JSON envelope and no CORS headers, the afterRequest hooks were skipped, and the request's Connection was never destroyed, so api.connections grew by one per request. A malformed multipart body (req.formData() throwing) escaped the same way.

Changes

  • servers/web.ts: any failure from parseRequestParams now destroys the connection and returns buildError, which gives the JSON envelope and CORS headers. The status is 413 for Payload Too Large and 400 for everything else. Non-TypedError failures are wrapped as a fallback. The response now reaches the afterRequest hooks.
  • util/webRouting.ts:
    • JSON parse failures are reported as CONNECTION_ACTION_PARAM_VALIDATION instead of CONNECTION_ACTION_RUN.
    • A JSON body that isn't an object (null, 5, [1, 2]) is rejected the same way. Before, null gave an unhelpful TypeError message, and an array was spread into numeric-key params.
    • req.formData() failures are wrapped as the same TypedError.
    • Oversize errors from the streaming reader are no longer caught and rethrown by the JSON try.
  • Docs: docs/reference/servers.md now describes the 400/413 behavior.
  • Bumps keryx to 0.48.2.

Tests

  • __tests__/util/webRouting.test.ts: covers empty, array, null, scalar and malformed JSON bodies, plus a malformed multipart body. Each throws a CONNECTION_ACTION_PARAM_VALIDATION TypedError.
  • __tests__/servers/web.test.ts: real HTTP requests with empty JSON, malformed JSON, a non-object JSON body and malformed multipart. Each gets a 400 JSON envelope with Access-Control-Allow-Origin, the connection count is unchanged, and the next request still succeeds.

All 10 new tests fail on main and pass with this change. bun lint and tsc --noEmit are clean. With in-memory Redis and no DB, __tests__/util, __tests__/servers and standalone.test.ts give 595/596 passing. The one failure is the OAuth "unreachable document" test, and it fails identically on main in this sandbox because of the network proxy.


Generated by Claude Code


Note

Medium Risk
Changes HTTP error handling and JSON body acceptance (non-object bodies now fail), which may affect API clients that previously sent arrays or relied on 500 responses.

Overview
Unparseable HTTP request bodies now get a proper API response instead of falling through to Bun’s default 500 (HTML/text, no CORS, leaked Connection).

handleWebAction catches all parseRequestParams failures, destroys the connection, and returns the standard JSON { error: { message, type } } envelope via buildError — 400 for parse/validation issues and 413 when the message is “Payload Too Large”. Oversized bodies were already handled for 413; malformed cases are new.

Body parsing in parseRequestParams is stricter: JSON must be a plain object (empty, array, null, or scalar bodies are rejected); parse and formData() errors use CONNECTION_ACTION_PARAM_VALIDATION instead of CONNECTION_ACTION_RUN.

Docs in servers.md describe this behavior. keryx is bumped to 0.48.2, with unit and HTTP integration tests covering the new responses and connection cleanup.

Reviewed by Cursor Bugbot for commit 38446db. Bugbot is set up for automated code reviews on this repo. Configure here.

An empty or malformed JSON body (or a malformed multipart body) failed
inside parseRequestParams and was rethrown out of handleWebAction, so
Bun's default handler answered with a 500 HTML/text page: no JSON error
envelope, no CORS headers, afterRequest hooks skipped, and the request's
Connection was never destroyed.

handleWebAction now destroys the connection for any body-parsing
failure and answers with buildError: 413 for oversized bodies, 400
otherwise. parseRequestParams reports parse failures (including a JSON
body that is not an object) as CONNECTION_ACTION_PARAM_VALIDATION.

closes #552

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KoctY8sHYTqGjxSJxngGNC
@evantahler
evantahler marked this pull request as ready for review October 20, 2026 21:44
@evantahler
evantahler enabled auto-merge (squash) October 20, 2026 21:45
@evantahler
evantahler merged commit d119595 into main Oct 20, 2026
20 checks passed
@evantahler
evantahler deleted the claude-evan/elegant-cori-houxov branch October 20, 2026 21:45
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.

A malformed or empty JSON body escapes the web server: Bun's 500 page, no JSON envelope or CORS headers, and a leaked Connection

2 participants