From c97bfc181658d1cf2d521cf2ec1b1396f951853d Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 06:02:41 -0600 Subject: [PATCH] fix(pm-dispatch): measure the body cap in UTF-8 bytes (#3010) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit readJsonBody enforced MAX_BODY_BYTES (256 KiB) as text.length, which counts UTF-16 code units — a 300 KB CJK body passed the too-large check. Measure new TextEncoder().encode(text).length against the same cap, same ValidationError. Boundary tests in server.test.ts: CJK over-in-bytes rejected, CJK just-under-in-bytes passes the size gate, ASCII over/under unchanged. Full dispatch suite: 90 pass. --- pm-stack/dispatch/src/server.ts | 5 +++- pm-stack/dispatch/test/server.test.ts | 35 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/pm-stack/dispatch/src/server.ts b/pm-stack/dispatch/src/server.ts index 0786855b99..6f8026e557 100644 --- a/pm-stack/dispatch/src/server.ts +++ b/pm-stack/dispatch/src/server.ts @@ -45,7 +45,10 @@ function json(status: number, body: unknown): Response { async function readJsonBody(req: Request): Promise { const text = await req.text(); - if (text.length > MAX_BODY_BYTES) throw new ValidationError("body: too large"); + // The cap is in bytes (MAX_BODY_BYTES): measure the UTF-8 byte length, + // not String.length (UTF-16 code units) — a multibyte body can be far + // larger in bytes than in code units (#3010). + if (new TextEncoder().encode(text).length > MAX_BODY_BYTES) throw new ValidationError("body: too large"); if (!text.trim()) throw new ValidationError("body: required JSON object"); try { return JSON.parse(text); diff --git a/pm-stack/dispatch/test/server.test.ts b/pm-stack/dispatch/test/server.test.ts index 97a6d31071..194353692c 100644 --- a/pm-stack/dispatch/test/server.test.ts +++ b/pm-stack/dispatch/test/server.test.ts @@ -82,3 +82,38 @@ describe("POST /v1/sessions directory scoping (issue #2903)", () => { expect(res.status).toBe(502); }); }); + +describe("POST /v1/sessions body cap is measured in UTF-8 bytes (issue #3010)", () => { + test("multibyte body under the cap in code units but over in bytes is rejected as too large", async () => { + const token = signOidc("developing-today/code"); + // 100,000 CJK chars = 100,000 UTF-16 code units (under the 262,144 cap) + // but 300,000 UTF-8 bytes (over it). + const res = await handler(sessionsRequest(token, { ...base, prompt: "界".repeat(100_000) })); + expect(res.status).toBe(400); + const body = (await res.json()) as { error: string }; + expect(body.error).toMatch(/too large/); + }); + + test("multibyte body just under the cap in bytes passes the size gate", async () => { + const token = signOidc("developing-today/code"); + // 87,000 CJK chars = 261,000 UTF-8 bytes plus JSON overhead: under the + // 262,144-byte cap, so the request clears the size gate and validation + // and reaches the unconfigured queue (502), like the small-body tests. + const res = await handler(sessionsRequest(token, { ...base, prompt: "界".repeat(87_000) })); + expect(res.status).toBe(502); + }); + + test("ASCII body over the cap is rejected as too large, as before", async () => { + const token = signOidc("developing-today/code"); + const res = await handler(sessionsRequest(token, { ...base, prompt: "a".repeat(262_144) })); + expect(res.status).toBe(400); + const body = (await res.json()) as { error: string }; + expect(body.error).toMatch(/too large/); + }); + + test("ASCII body under the cap passes the size gate, as before", async () => { + const token = signOidc("developing-today/code"); + const res = await handler(sessionsRequest(token, { ...base, prompt: "a".repeat(260_000) })); + expect(res.status).toBe(502); + }); +});