Skip to content

Commit 5d35684

Browse files
committed
fix(adapters): validate buffered response and ndjson frame shapes
`JSON.parse("null")` returns null without throwing, so a try/catch around a body parse cannot see it. #1240 closed that at the SSE frame root for the four SSE parsers; the buffered bodies and the NDJSON transport were never swept. - google/anthropic parseResponse: a valid-JSON non-record body reached `raw.error` and `json.content` and threw out of the adapter. A buffered body has no next frame to recover into, so both now fail closed with a structured error, matching the unparseable-body branch beside them. - anthropic parseResponse: `content` was consumed unchecked. A present non-array was silently accepted, and a string is iterable, so a claimed answer was walked one character at a time and reported as a successful empty turn. Absence (omitted or null) stays legal. - openai-chat parseResponse: `if (!choice.message)` split this input class on truthiness, not shape - null and 0 failed closed while "text", true and [{...}] passed and completed as a successful empty turn, stranding any tool call the choice claimed. Empty array stays legal, as in the google adapter. - command-code ndjson: a non-record line crashed on `event.type`. A stream frame does have a next frame, so it is skipped as padding, per #1240.
1 parent 98ed186 commit 5d35684

5 files changed

Lines changed: 489 additions & 6 deletions

File tree

‎src/adapters/anthropic.ts‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,39 @@ export function formatAnthropicErrorBody(status: number, _headers: Headers, payl
265265
return redactSecretString(detail).slice(0, 400);
266266
}
267267

268+
function isAnthropicRecord(value: unknown): value is Record<string, unknown> {
269+
return value !== null && typeof value === "object" && !Array.isArray(value);
270+
}
271+
272+
function anthropicStructuralValueType(value: unknown): string {
273+
if (value === null) return "null";
274+
return Array.isArray(value) ? "array" : typeof value;
275+
}
276+
277+
interface InvalidAnthropicShapeDiagnostic {
278+
reason: "content_not_array" | "content_block_not_object";
279+
blockIndex?: number;
280+
valueType: string;
281+
}
282+
283+
/**
284+
* Structured refusal for a malformed buffered response body, shaped like the google adapter's
285+
* `invalidGoogleShapeEvent` so an operator reading a log can tell which rung failed: the content
286+
* container, or one block inside an otherwise well-formed container.
287+
*/
288+
function invalidAnthropicShapeEvent(
289+
diagnostic: InvalidAnthropicShapeDiagnostic,
290+
): Extract<AdapterEvent, { type: "error" }> {
291+
const at = diagnostic.blockIndex !== undefined ? `; blockIndex=${diagnostic.blockIndex}` : "";
292+
const subject = diagnostic.reason === "content_not_array" ? "content" : "content block";
293+
return {
294+
type: "error",
295+
message: `anthropic response contained invalid ${subject} (${diagnostic.reason}${at}; valueType=${diagnostic.valueType})`,
296+
status: 502,
297+
errorType: "upstream_error",
298+
};
299+
}
300+
268301
function extractAnthropicErrorDetail(parsed: unknown): string | undefined {
269302
if (typeof parsed === "string") return parsed.trim() || undefined;
270303
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return undefined;
@@ -1279,12 +1312,55 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
12791312
},
12801313

12811314
async parseResponse(response: Response, budget: TranslatorBudget): Promise<AdapterEvent[]> {
1282-
const json = await response.json() as Record<string, unknown>;
1315+
const parsed: unknown = await response.json();
1316+
// `response.json()` resolves a body of `null` to `null` without throwing, so the cast below
1317+
// used to reach `json.content` on it — the #1219 defect at the buffered body root. The
1318+
// streaming parser has skipped a non-record frame since #1240, but a buffered body has no
1319+
// next frame to recover into, so this fails closed instead.
1320+
if (!isAnthropicRecord(parsed)) {
1321+
return [{
1322+
type: "error",
1323+
message: `anthropic response was not a JSON object (${anthropicStructuralValueType(parsed)})`,
1324+
status: 502,
1325+
errorType: "upstream_error",
1326+
}];
1327+
}
1328+
const json = parsed;
12831329
const responseBytes = new TextEncoder().encode(JSON.stringify(json)).byteLength;
12841330
budget.chargeRetained(responseBytes, { kind: "retained_collectors" });
12851331
try {
12861332
const events: AdapterEvent[] = [];
1287-
const content = json.content as { type: string; text?: string; id?: string; name?: string; input?: unknown; thinking?: string; reasoning?: string; signature?: string; data?: string }[] | undefined;
1333+
// Same retain-then-return tail every other terminal branch in this function uses; naming it
1334+
// keeps the two shape guards below from drifting out of step with the accounting.
1335+
const finishWithEvents = (batch: AdapterEvent[]): AdapterEvent[] => {
1336+
retainTranslatedEventBatch(batch, budget);
1337+
return batch;
1338+
};
1339+
const rawContent: unknown = json.content;
1340+
// `content` is claimed model output inside a well-formed response, so it is governed by the
1341+
// #1332 nested-shape rule (fail closed) rather than #1240's root-frame padding rule (skip).
1342+
// Absence stays legal in both encodings. A present non-array was silently accepted and is
1343+
// the dangerous case: a string is iterable, so `for (const block of "text")` walked it one
1344+
// CHARACTER at a time, read `undefined` from every `block.type`, and completed the turn as a
1345+
// successful empty response — the #2231 failure mode on a different adapter.
1346+
if (rawContent !== undefined && rawContent !== null && !Array.isArray(rawContent)) {
1347+
return finishWithEvents([invalidAnthropicShapeEvent({
1348+
reason: "content_not_array",
1349+
valueType: anthropicStructuralValueType(rawContent),
1350+
})]);
1351+
}
1352+
if (Array.isArray(rawContent)) {
1353+
for (let blockIndex = 0; blockIndex < rawContent.length; blockIndex++) {
1354+
if (!isAnthropicRecord(rawContent[blockIndex])) {
1355+
return finishWithEvents([invalidAnthropicShapeEvent({
1356+
reason: "content_block_not_object",
1357+
blockIndex,
1358+
valueType: anthropicStructuralValueType(rawContent[blockIndex]),
1359+
})]);
1360+
}
1361+
}
1362+
}
1363+
const content = rawContent as { type: string; text?: string; id?: string; name?: string; input?: unknown; thinking?: string; reasoning?: string; signature?: string; data?: string }[] | undefined;
12881364
if (content) {
12891365
for (const block of content) {
12901366
if (block.type === "text" && block.text) {

‎src/adapters/command-code.ts‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { isAllowedToolChoice, namespacedToolName, resolveToolChoiceWireName, too
77
import type { AdapterFetchContext, AdapterRequest, ProviderAdapter } from "./base";
88
import type { TranslatorBudget } from "../lib/translator-budget";
99
import { readBoundedResponseBody } from "../lib/bounded-body";
10+
import { debugDroppedFrame } from "../lib/debug";
1011
import { configuredReasoningEfforts } from "../reasoning-effort";
1112
import { commandCodeReasoningEfforts, refreshCommandCodeReasoningEfforts } from "../providers/command-code-efforts";
1213
import { identifyRoutedModel } from "./identity";
@@ -365,7 +366,7 @@ async function*ndjson(response: Response, budget: TranslatorBudget): AsyncGenera
365366
let newline = buffer.indexOf("\n");
366367
while (newline >= 0) {
367368
const line = buffer.slice(0, newline).trim(); buffer = buffer.slice(newline + 1);
368-
if (line) { try { yield JSON.parse(stripEventFrame(line)) as Record<string, unknown>; } catch { /* ignore non-events */ } }
369+
if (line) yield* decodeEventLine(line);
369370
newline = buffer.indexOf("\n");
370371
}
371372
const residualBytes = encoder.encode(buffer).byteLength;
@@ -376,14 +377,49 @@ async function*ndjson(response: Response, budget: TranslatorBudget): AsyncGenera
376377
if (done) break;
377378
}
378379
const final = buffer.trim();
379-
if (final) { try { yield JSON.parse(stripEventFrame(final)) as Record<string, unknown>; } catch { /* ignore */ } }
380+
if (final) yield* decodeEventLine(final);
380381
} finally {
381382
budget.releaseRetained(bufferBytes, { kind: "live_transient" });
382383
try { await reader.cancel(); } catch { /* already closed */ }
383384
reader.releaseLock();
384385
}
385386
}
386387

388+
/**
389+
* Yield one NDJSON line as an event record, or nothing.
390+
*
391+
* `JSON.parse("null")` returns `null` instead of throwing, so the `try/catch` around the parse
392+
* cannot see it and the `event.type` read in parseStream crashed the turn — the #1219 defect, on
393+
* the one streaming transport the #1240 audit did not cover because it is NDJSON rather than SSE.
394+
*
395+
* A frame that does not parse to a record is padding, not an event: drop it and continue exactly
396+
* as an unparseable line is already dropped, so a stream whose only frames are junk ends in the
397+
* same single terminal `done` as an empty body. Skipping is what preserves an answer whose deltas
398+
* have already arrived — the observed #1219 case is `null` padding BETWEEN content deltas, where
399+
* terminating would discard a complete response (#1240).
400+
*
401+
* Note this deliberately makes a junk-only stream a quiet `[done]` where it previously threw. That
402+
* throw was an unguarded type assumption, not a designed failure signal, and `[done]` is already
403+
* what an empty body, a blank-line-only body and an unparseable-only body all produce here. The
404+
* broader question — whether this adapter should report *any* no-valid-event stream as a failure
405+
* rather than an empty success — is pre-existing, applies to all four of those inputs equally, and
406+
* is deliberately not decided by this change.
407+
*/
408+
function* decodeEventLine(line: string): Generator<Record<string, unknown>> {
409+
let parsed: unknown;
410+
try {
411+
parsed = JSON.parse(stripEventFrame(line));
412+
} catch {
413+
debugDroppedFrame("command-code", line);
414+
return;
415+
}
416+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
417+
debugDroppedFrame("command-code", line);
418+
return;
419+
}
420+
yield parsed as Record<string, unknown>;
421+
}
422+
387423
/** The endpoint is newline-delimited JSON; defensively strip an SSE `data:` frame if the gateway ever switches shapes. */
388424
function stripEventFrame(line: string): string {
389425
return line.startsWith("data:") ? line.slice("data:".length).trim() : line;

‎src/adapters/google.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,18 @@ export function createGoogleAdapter(provider: OcxProviderConfig): ProviderAdapte
11581158
let raw: Record<string, unknown>;
11591159
let rawBytes = 0;
11601160
try {
1161-
raw = JSON.parse(rawText) as Record<string, unknown>;
1161+
const parsedRaw: unknown = JSON.parse(rawText);
1162+
// `JSON.parse("null")` returns null instead of throwing, so the catch below cannot see it
1163+
// and the `raw.error` read crashed the turn — #1219 at the buffered body root, which #1240
1164+
// never reached because that audit swept SSE frame parsers only. There is no next frame to
1165+
// recover into here, so unlike a stream frame this fails closed, matching the
1166+
// unparseable-body branch just below and the buffered candidate guards added in #2232.
1167+
if (!isGoogleRecord(parsedRaw)) {
1168+
budget.releaseRetained(rawTextBytes, { kind: "retained_collectors" });
1169+
const valueType = googleStructuralValueType(parsedRaw);
1170+
return [{ type: "error", message: `google response was not a JSON object (${valueType})` }];
1171+
}
1172+
raw = parsedRaw;
11621173
rawBytes = new TextEncoder().encode(JSON.stringify(raw)).byteLength;
11631174
const rawReservation = budget.reserveTransient(rawBytes, { kind: "retained_collectors" });
11641175
rawReservation.commitRetained();

‎src/adapters/openai-chat.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1932,8 +1932,28 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
19321932
const choice = rawChoice;
19331933
if (choice.finish_reason === "error") return [upstreamErrorEvent(choice.error, usage)];
19341934
if (!choice.message) return [{ type: "error", message: "upstream response contained no choices", ...(usage ? { usage } : {}) }];
1935+
// `!choice.message` splits this input class on TRUTHINESS, not on shape: `null` and `0` fail
1936+
// closed here, while `"text"`, `true` and `[{...}]` pass and every property read below yields
1937+
// `undefined` — so a choice claiming an assistant message completed as a SUCCESSFUL EMPTY
1938+
// turn, stranding any tool call it claimed. The one line above already validates the choice
1939+
// container this way; its message was left on a truthiness test.
1940+
//
1941+
// Every non-record is rejected, arrays included. The google adapter does carve out `[]` for
1942+
// `content`, but that carve-out is specific to a protobuf-derived wire where a repeated
1943+
// field can spell an empty message — and `content` is genuinely an ARRAY of blocks there.
1944+
// `message` is a record on a plain-JSON wire that already has `{}`, so importing the
1945+
// exception would be an analogy rather than evidence. `[{"content":"…"}]` is the case that
1946+
// matters: it discards a complete answer, #2232's `content: [{ parts: [...] }]` one adapter over.
1947+
//
1948+
// Read through `unknown` rather than the declared type: `choices` is a cast over wire data,
1949+
// so its `message?: Record<string, unknown>` is an assertion the upstream never made, and
1950+
// narrowing against it is what let the missing check look type-safe.
1951+
const rawMessage: unknown = choice.message;
1952+
if (!isRecord(rawMessage)) {
1953+
return [invalidChoicesEvent(usage)];
1954+
}
19351955

1936-
const msg = choice.message;
1956+
const msg = rawMessage as Record<string, unknown>;
19371957
const reasoningText = reasoningTextFrom(msg);
19381958
if (reasoningText !== undefined) events.push({ type: "reasoning_raw_delta", text: reasoningText });
19391959
if (typeof msg.content === "string") events.push({ type: "text_delta", text: msg.content });

0 commit comments

Comments
 (0)