Repository navigation
fix: reject non-object JSON bodies with 400; keep 502 detail out of responses - #6
Merged
Merged
Conversation
…502 detail
Review-reproduced on 2026-10-05: JSON.parse('null') parses legally, but
handleChatCompletions treated a null parsed body as 'error response already
written' and returned without writing anything — the client hung until its own
timeout. Bodies of 123/'str' hit the typeof check inside
stripReasoningTriggers ('in' on a primitive), throwing a TypeError that the
upstream catch block classified as a connect failure: a misleading 502 that
counted toward the errors dashboard and leaked the internal exception text.
Root fix: readJsonBody — the single place every route parses a body — now
rejects non-object JSON (null / number / string / array) with 400, covering
chat completions, messages, and count_tokens with the correct error envelope
per route. The 502 message is fixed to 'upstream connect failed' with details
kept in logs only.
Tests: malformed-body matrix 400 regression (OpenAI + Anthropic envelopes),
upstream connection reset → 502 with the fixed message, and a streaming-path
variant for the reasoning-toggle strip assertions. 67/67 green.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to the production code review of PR #5. Two findings, one root cause, fixed at the single parse point.
Findings (both reproduced before the fix)
nullrequest body → response never written, client hangs.JSON.parse('null')returnsnulllegally, butreadJsonBodyusednullas its "error already sent" sentinel andhandleChatCompletionstrusted it — a literalnullbody fell through the guard and the handler returned without ever writing a response. Client waits until its own timeout.123/"str"bodies → misleading 502 with leaked internals. These reachforward()and throwTypeError: Cannot use 'in' operator to search for 'reasoning_effort'...inside the strip helper; the upstreamcatchclassifies it as a connect failure (502, counted on the errors dashboard) and embeds the internal exception text in the client-facing message.Root fix
readJsonBodyis the only place any route parses a request body — it now rejects valid-JSON-but-non-object payloads (null/number/string/array) with400, emitting the correct envelope per route (OpenAIinvalid_request_errorvs Anthropicerror.type). Covers all three routes (/v1/chat/completions,/v1/messages,/v1/messages/count_tokens) with no per-route changes. The 502 message is now a constant'upstream connect failed'; exception details stay in structured logs.Tests
null,123,"str",[1,2]) → 400 with parseable response on both chat (OpenAI envelope) and messages (Anthropic envelope) routes — the parseable-response assertion is the hang regression tripwiremock-resetupstream (socket destroy) → 502 with the exact fixed message, proving no internal detail leaks on the non-timeout fetch-failure path