diff --git a/bin/opencode-route-proxy.mjs b/bin/opencode-route-proxy.mjs index c8bc2c435c..cc1d08a35c 100755 --- a/bin/opencode-route-proxy.mjs +++ b/bin/opencode-route-proxy.mjs @@ -56,6 +56,7 @@ import { fileURLToPath } from "node:url"; import { DatabaseSync } from "node:sqlite"; import { randomBytes } from "node:crypto"; import { parseProxyMillis, parseProxyPort } from "./proxy-port.mjs"; +import { createRequestBodyCollector } from "./request-body.mjs"; const PORT = parseProxyPort("OPENCODE_ROUTE_PROXY_PORT", 18750); const HOST = process.env.OPENCODE_ROUTE_PROXY_HOST || "127.0.0.1"; @@ -1055,15 +1056,29 @@ const server = createServer(async (req, res) => { }); } if (req.method === "POST" && url.pathname === "/v1/chat/completions") { - let raw = ""; - for await (const part of req) raw += part; - let body = {}; - try { - body = JSON.parse(raw || "{}"); - } catch { - return sendError(res, 400, "invalid JSON body"); - } - return await handleChatCompletions(req, res, body); + // Collect the body as bytes and decode UTF-8 once, after the full + // body arrives (#4335): per-chunk string coercion corrupts multibyte + // characters split across TCP chunks. Cap 50 MB like the sibling + // proxies (bin/request-body.mjs collector) with a 413 past the cap. + const bodyCollector = createRequestBodyCollector(50 * 1024 * 1024); + req.on("data", (chunk) => { + if (bodyCollector.tooLarge) return; + if (!bodyCollector.push(chunk)) { + sendError(res, 413, "request body too large"); + req.destroy(); + } + }); + req.on("end", async () => { + if (bodyCollector.tooLarge) return; + let body = {}; + try { + body = JSON.parse(bodyCollector.text() || "{}"); + } catch { + return sendError(res, 400, "invalid JSON body"); + } + await handleChatCompletions(req, res, body); + }); + return; } return sendError(res, 404, `no route for ${req.method} ${url.pathname}`); } catch (err) { diff --git a/bin/opencode-route-proxy.test.mjs b/bin/opencode-route-proxy.test.mjs index eef6918672..abb5aa4384 100644 --- a/bin/opencode-route-proxy.test.mjs +++ b/bin/opencode-route-proxy.test.mjs @@ -430,6 +430,47 @@ test("unknown route and malformed bodies are rejected", async () => { } }); +test("chunk boundary inside a multibyte character arrives intact (#4335)", async () => { + const p = await startProxy(); + try { + const text = "café ☃ fin"; + const payload = JSON.stringify({ model: "opencode-route/t-ordered", messages: [{ role: "user", content: text }] }); + const bytes = Buffer.from(payload, "utf8"); + // Split inside the é bytes (0xC3 0xA9) and inside the ☃ bytes + // (0xE2 0x98 0x83): per-chunk string coercion would emit U+FFFD here. + const eAcute = bytes.indexOf(Buffer.from("é", "utf8")); + const snowman = bytes.indexOf(Buffer.from("☃", "utf8")); + assert.ok(eAcute > 0 && snowman > eAcute); + const parts = [bytes.subarray(0, eAcute + 1), bytes.subarray(eAcute + 1, snowman + 2), bytes.subarray(snowman + 2)]; + const res = await fetch(`${p.base}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: new ReadableStream({ + start(c) { + for (const part of parts) c.enqueue(part); + c.close(); + }, + }), + duplex: "half", + }); + assert.equal(res.status, 200); + assert.equal(p.leafA.last.body.messages[0].content, text); + } finally { + await p.cleanup(); + } +}); + +test("oversize body is rejected with 413 (#4335)", async () => { + const p = await startProxy(); + try { + const big = "x".repeat(51 * 1024 * 1024); + const res = await p.post({ model: "opencode-route/t-ordered", messages: [{ role: "user", content: big }] }); + assert.equal(res.status, 413); + } finally { + await p.cleanup(); + } +}); + // Window gates (#3225): the fixture "peak" window is Mon-Fri // 01:00-04:00 UTC. 2026-11-04 is a Wednesday. const IN_PEAK = Date.UTC(2026, 10, 4, 2, 30);