Skip to content

Commit 8a82a46

Browse files
luvs01lidge-jun
andauthored
fix(server): account for normalized inbound JSON (#8026)
* fix(server): account for normalized inbound JSON * fix(server): measure parsed JSON size without recursion Normalized JSON byte accounting now walks the parsed value iteratively and returns the UTF-8 length JSON.stringify would produce, so deeply nested valid bodies are measured instead of throwing. * fix(server): keep JSON size measurement memory bounded by depth Walk containers with one cursor frame each instead of queuing every element, so wide arrays add no allocation proportional to their length. * docs(server): describe JSON measurement memory precisely * ci: run the full Ubuntu shards for this head --------- Co-authored-by: JUN <[email protected]>
1 parent 9c25b49 commit 8a82a46

5 files changed

Lines changed: 110 additions & 7 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Buffer } from "node:buffer";
2+
3+
type Frame =
4+
| { items: readonly unknown[]; index: number }
5+
| { record: Record<string, unknown>; keys: readonly string[]; index: number };
6+
7+
/**
8+
* UTF-8 byte length of JSON.stringify(value) for a JSON.parse result, computed without
9+
* recursion: a valid body nested deeper than the call stack must be measured, not crash.
10+
* Each open container keeps one cursor frame, so arrays add no per-element queue; an object
11+
* frame retains its key list, which stays bounded by the input size.
12+
*/
13+
export function serializedJsonByteLength(value: unknown): number {
14+
let bytes = 0;
15+
const stack: Frame[] = [];
16+
const visit = (item: unknown): void => {
17+
if (item === null || typeof item !== "object") {
18+
bytes += Buffer.byteLength(JSON.stringify(item) ?? "null", "utf8");
19+
} else if (Array.isArray(item)) {
20+
bytes += 2 + Math.max(0, item.length - 1);
21+
stack.push({ items: item, index: 0 });
22+
} else {
23+
const record = item as Record<string, unknown>;
24+
const keys = Object.keys(record);
25+
bytes += 2 + Math.max(0, keys.length - 1);
26+
stack.push({ record, keys, index: 0 });
27+
}
28+
};
29+
visit(value);
30+
while (stack.length > 0) {
31+
const frame = stack[stack.length - 1]!;
32+
if ("items" in frame) {
33+
if (frame.index < frame.items.length) visit(frame.items[frame.index++]);
34+
else stack.pop();
35+
} else if (frame.index < frame.keys.length) {
36+
const key = frame.keys[frame.index++]!;
37+
bytes += Buffer.byteLength(JSON.stringify(key), "utf8") + 1;
38+
visit(frame.record[key]);
39+
} else {
40+
stack.pop();
41+
}
42+
}
43+
return bytes;
44+
}

‎src/server/request-decompress.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Buffer } from "node:buffer";
22
import { gunzipSync, inflateRawSync, inflateSync, zstdDecompressSync } from "node:zlib";
33
import type { TranslatorBudget } from "../lib/translator-budget";
44
import { updateInboundBodyReservation } from "./inbound-body-budget";
5+
import { serializedJsonByteLength } from "./json-serialized-bytes";
56

67
/**
78
* Request-body decompression for the /v1/responses data plane.
@@ -333,8 +334,9 @@ export async function readBoundedJsonRequestBody(
333334
return options.emptyBodyFallback;
334335
}
335336
const parsed = JSON.parse(text);
336-
// Keep the serialized-size contract: normalization can expand numeric literals.
337-
const parsedBytes = budget ? Buffer.byteLength(JSON.stringify(parsed), "utf8") : decoded.byteLength;
337+
// Keep the serialized-size contract: normalization can expand numeric literals. The
338+
// measurement walks iteratively, so a deeply nested valid body cannot overflow the stack.
339+
const parsedBytes = serializedJsonByteLength(parsed);
338340
budget?.observeAcceptedRequestCopy(parsedBytes);
339341
updateInboundBodyReservation(req, Math.max(raw.byteLength, decoded.byteLength, parsedBytes));
340342
return parsed;

‎structure/transports/byte-accounting.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ only. It copies no request content and adds no bytes to the upstream wire; see t
7575

7676
`src/server/request-decompress.ts` observes the UTF-8 sizes of decoded text and reserialized JSON
7777
without allocating encoded byte arrays solely to count them. Parsed-body accounting still uses
78-
`JSON.stringify(parsed)`: numeric normalization can make it larger than the input text. These
78+
the byte length of `JSON.stringify(parsed)`: numeric normalization can make it larger than the input
79+
text. `src/server/json-serialized-bytes.ts` computes that length with an iterative walk, so a valid
80+
body nested deeper than the call stack is measured instead of throwing during accounting. These
7981
observations retain the existing ownership and release lifecycle and do not consume the translator's
8082
hard byte cap. Per-body limits, parsing, compression, and reader error envelopes are unchanged.
8183
`tests/usage/request-decompress.test.ts` covers exact accounting across codecs and Unicode/numeric
82-
normalization, UTF-8 counting without encoded copies, and release after malformed or optional empty input.
84+
normalization, UTF-8 counting without encoded copies, release after malformed or optional empty input,
85+
and serialized-length parity with `JSON.stringify` including nesting it cannot walk.
8386

8487
The final native ChatGPT Responses HTTP send in `src/server/responses/fetch-helpers.ts` encodes JSON
8588
strings of at least 1 MiB as a UTF-8 buffer for Bun upload compatibility. This is a transport copy,
@@ -105,7 +108,7 @@ discarding active leases or FIFO waiters; lowering capacity cannot erase outstan
105108
checks, before any request body is read. Default and smaller allowances do not consume this budget.
106109
Missing or small Content-Length and compressed wire bodies do not reduce the upload reservation.
107110
After successful bounded JSON parsing, the reader reduces it to the largest observed wire/decoded
108-
size, including normalized JSON bytes when the existing translator measurement is enabled.
111+
size, including normalized JSON bytes whether or not the route uses a translator budget.
109112
This reduction never exceeds the original allowance. The measured charge remains live;
110113
parsing never frees the whole lease.
111114
An already-aborted request or explicitly oversized declaration keeps the existing abort/413 path.

‎tests/server/inbound-body-admission.test.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ describe("raised inbound body admission lifetime", () => {
591591
}
592592

593593
for (const normalized of [false, true]) {
594-
test(`retains measured JSON bytes with translator normalization ${normalized}`, async () => {
594+
test(`retains normalized JSON bytes with translator budget ${normalized}`, async () => {
595595
const text = "1e20";
596596
const req = admissionRequest("/v1/responses", text);
597597
const translator = normalized ? createTranslatorBudget() : undefined;
@@ -602,7 +602,7 @@ describe("raised inbound body admission lifetime", () => {
602602
assert.equal(await readJsonRequestBody(req, translator, RAISED_BODY_LIMIT), 1e20);
603603
return new Response("held");
604604
}, req);
605-
const expected = normalized ? Buffer.byteLength(JSON.stringify(1e20)) : Buffer.byteLength(text);
605+
const expected = Buffer.byteLength(JSON.stringify(1e20));
606606
assert.deepEqual(testBudget.snapshot(), { reservedBytes: expected, activeLeases: 1, queuedRequests: 0 });
607607
await response.text();
608608
} finally {
@@ -612,6 +612,29 @@ describe("raised inbound body admission lifetime", () => {
612612
});
613613
}
614614

615+
for (const normalized of [false, true]) {
616+
test(`measures a deeply nested valid JSON body with translator budget ${normalized}`, async () => {
617+
// Deeper than the call stack allows a recursive serializer to walk, and well under the limit.
618+
const depth = 500_000;
619+
const text = "[".repeat(depth) + "]".repeat(depth);
620+
const req = admissionRequest("/v1/responses", text);
621+
const translator = normalized ? createTranslatorBudget() : undefined;
622+
let response: Response | undefined;
623+
try {
624+
response = await admittedBodyWork(async () => {
625+
assert.equal(Array.isArray(await readJsonRequestBody(req, translator, RAISED_BODY_LIMIT)), true);
626+
return new Response("held");
627+
}, req);
628+
assert.equal(response.status, 200);
629+
assert.deepEqual(testBudget.snapshot(), { reservedBytes: text.length, activeLeases: 1, queuedRequests: 0 });
630+
await response.text();
631+
} finally {
632+
await response?.body?.cancel().catch(() => undefined);
633+
translator?.dispose();
634+
}
635+
});
636+
}
637+
615638
for (const end of ["eof", "error", "cancel"] as const) {
616639
test(`retains decoded compressed bytes until response ${end} settles exactly once`, async () => {
617640
const text = JSON.stringify({ input: "x".repeat(10_000) });

‎tests/usage/request-decompress.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { MANAGEMENT_JSON_BODY_MAX_BYTES } from "../../src/server/management/body";
1717
import { handleManagementAPI } from "../../src/server/management-api";
1818
import { decodeRequestErrorResponse } from "../../src/server/responses/core";
19+
import { serializedJsonByteLength } from "../../src/server/json-serialized-bytes";
1920
import type { OcxConfig } from "../../src/types";
2021

2122
const PAYLOAD = { model: "gpt-5.5", input: "hello", stream: true };
@@ -624,3 +625,33 @@ describe("readJsonRequestBody", () => {
624625
await expect(readJsonRequestBody(req)).rejects.toBeInstanceOf(SyntaxError);
625626
});
626627
});
628+
629+
describe("serializedJsonByteLength", () => {
630+
test("matches the UTF-8 length of JSON.stringify for parsed JSON", () => {
631+
const sources = [
632+
"null", "true", "false", "0", "-0", "1e20", "1e400", "-1e400", "0.1", "1.5e-7", "123456789012345678901234567890",
633+
"\"\"", "\"plain\"", "\"\\ud800\"", "\"\\udc00x\\ud83d\\ude00\"", "\"\\u0000\\u001f\\u007f\"", "\"é漢字\\\"\\\\\\n\"",
634+
"[]", "{}", "[[]]", "[{}]", "{\"a\":{}}", "[[],[[]],{}]", "[1,\"x\",null,true,[2,3],{\"k\":[]}]",
635+
"{\"__proto__\":{\"x\":1},\"constructor\":[1]}", "{\"a\":1,\"a\":2}", "{\"2\":1,\"1\":2,\"b\":3,\"a\":4}",
636+
"{\"\\ud800\":\"\\udfff\",\"é\":\"漢\"}", JSON.stringify({ input: [{ role: "user", content: "x".repeat(1000) }] }),
637+
];
638+
for (const source of sources) {
639+
const parsed: unknown = JSON.parse(source);
640+
expect([source, serializedJsonByteLength(parsed)]).toEqual([source, Buffer.byteLength(JSON.stringify(parsed), "utf8")]);
641+
}
642+
});
643+
644+
test("measures nesting deeper than a recursive serializer can walk", () => {
645+
const depth = 500_000;
646+
const parsed: unknown = JSON.parse("[".repeat(depth) + "]".repeat(depth));
647+
expect(() => JSON.stringify(parsed)).toThrow(RangeError);
648+
expect(serializedJsonByteLength(parsed)).toBe(2 * depth);
649+
});
650+
651+
test("measures wide and mixed containers exactly", () => {
652+
const wide = JSON.parse("[" + Array(1_000_000).fill("0").join(",") + "]");
653+
expect(serializedJsonByteLength(wide)).toBe(Buffer.byteLength(JSON.stringify(wide), "utf8"));
654+
const mixed = JSON.parse(JSON.stringify({ a: Array.from({ length: 50_000 }, (_, i) => ({ [`k${i}`]: [i, "é", null] })) }));
655+
expect(serializedJsonByteLength(mixed)).toBe(Buffer.byteLength(JSON.stringify(mixed), "utf8"));
656+
});
657+
});

0 commit comments

Comments
 (0)