diff --git a/bin/agy-proxy.mjs b/bin/agy-proxy.mjs index b22d98e02c..24779b8247 100755 --- a/bin/agy-proxy.mjs +++ b/bin/agy-proxy.mjs @@ -45,6 +45,7 @@ import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os'; import { parseProxyPort } from './proxy-port.mjs'; +import { createRequestBodyCollector } from './request-body.mjs'; const PORT = parseProxyPort('AGY_PROXY_PORT', 18743); const HOST = process.env.AGY_PROXY_HOST || '127.0.0.1'; @@ -856,10 +857,10 @@ const server = http.createServer((req, res) => { // Chat completions if (req.method === 'POST' && (pathname === '/v1/chat/completions' || pathname === '/chat/completions')) { - let bodyText = ''; + const bodyCollector = createRequestBodyCollector(20 * 1024 * 1024); req.on('data', chunk => { - bodyText += chunk; - if (bodyText.length > 20 * 1024 * 1024) { + if (bodyCollector.tooLarge) return; + if (!bodyCollector.push(chunk)) { res.writeHead(413, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Request payload exceeds 20MB limit' })); req.destroy(); @@ -867,6 +868,8 @@ const server = http.createServer((req, res) => { }); req.on('end', async () => { + if (bodyCollector.tooLarge) return; + const bodyText = bodyCollector.text(); let body; try { body = JSON.parse(bodyText); diff --git a/bin/antigravity-ls-proxy.mjs b/bin/antigravity-ls-proxy.mjs index 76f5fed6b8..c4491357d0 100755 --- a/bin/antigravity-ls-proxy.mjs +++ b/bin/antigravity-ls-proxy.mjs @@ -6,6 +6,7 @@ import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os'; import { parseProxyPort, parseAddressPortValue } from './proxy-port.mjs'; +import { createRequestBodyCollector } from './request-body.mjs'; const PORT = parseProxyPort('ANTIGRAVITY_LS_PROXY_PORT', 18745); const HOST = process.env.ANTIGRAVITY_LS_PROXY_HOST || '127.0.0.1'; @@ -747,10 +748,10 @@ const server = http.createServer(async (req, res) => { // Chat completions if (req.method === 'POST' && (pathname === '/v1/chat/completions' || pathname === '/chat/completions')) { - let bodyText = ''; + const bodyCollector = createRequestBodyCollector(20 * 1024 * 1024); req.on('data', chunk => { - bodyText += chunk; - if (bodyText.length > 20 * 1024 * 1024) { + if (bodyCollector.tooLarge) return; + if (!bodyCollector.push(chunk)) { res.writeHead(413, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Request payload exceeds 20MB limit' })); req.destroy(); @@ -758,6 +759,8 @@ const server = http.createServer(async (req, res) => { }); req.on('end', async () => { + if (bodyCollector.tooLarge) return; + const bodyText = bodyCollector.text(); let body; try { body = JSON.parse(bodyText); diff --git a/bin/antigravity-ls-proxy.test.mjs b/bin/antigravity-ls-proxy.test.mjs index 68d07a27c7..dd27371f54 100644 --- a/bin/antigravity-ls-proxy.test.mjs +++ b/bin/antigravity-ls-proxy.test.mjs @@ -251,3 +251,34 @@ test('non-object JSON body is a 400 and the proxy stays alive (#2816)', async () assert.equal(alive.status, 400, 'proxy must still serve after non-object bodies'); await alive.text(); }); + +test('preserves UTF-8 split across HTTP request-body chunks (#3186)', async () => { + const content = 'before-😀-after'; + const body = Buffer.from(JSON.stringify({ + model: 'gemini-3.8-flash', + messages: [{ role: 'user', content }] + }), 'utf8'); + const emojiStart = body.indexOf(Buffer.from('😀', 'utf8')); + const chunks = [body.subarray(0, emojiStart + 2), body.subarray(emojiStart + 2)]; + capturedPrompt = undefined; + + const response = await new Promise((resolve, reject) => { + const request = http.request({ + hostname: '127.0.0.1', + port: proxyPort, + path: '/v1/chat/completions', + method: 'POST', + headers: { 'Content-Type': 'application/json' } + }, (res) => { + res.resume(); + res.on('end', () => resolve(res.statusCode)); + }); + request.on('error', reject); + request.write(chunks[0]); + request.write(chunks[1]); + request.end(); + }); + + assert.equal(response, 200); + assert.match(capturedPrompt, /before-😀-after/); +}); diff --git a/bin/claude-code-proxy.mjs b/bin/claude-code-proxy.mjs index 33932772db..880596f3f9 100644 --- a/bin/claude-code-proxy.mjs +++ b/bin/claude-code-proxy.mjs @@ -36,6 +36,7 @@ import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { parseProxyPort } from './proxy-port.mjs'; +import { createRequestBodyCollector } from './request-body.mjs'; // Default port 18749: the next free port in the fleet family (clef 18742, // agy 18743, codex 18744, antigravity-ls 18745, design-code 18746, @@ -720,16 +721,18 @@ const server = http.createServer((req, res) => { } if (req.method === 'POST' && (pathname === '/v1/chat/completions' || pathname === '/chat/completions')) { - let bodyText = ''; + const bodyCollector = createRequestBodyCollector(50 * 1024 * 1024); req.on('data', chunk => { - bodyText += chunk; - if (bodyText.length > 50 * 1024 * 1024) { + if (bodyCollector.tooLarge) return; + if (!bodyCollector.push(chunk)) { res.writeHead(413, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: 'Request payload too large' } })); req.destroy(); } }); req.on('end', async () => { + if (bodyCollector.tooLarge) return; + const bodyText = bodyCollector.text(); let body; try { body = JSON.parse(bodyText); } catch (err) { res.writeHead(400, { 'Content-Type': 'application/json' }); diff --git a/bin/clef-proxy.mjs b/bin/clef-proxy.mjs index 18b288cd45..ea8d913ae4 100755 --- a/bin/clef-proxy.mjs +++ b/bin/clef-proxy.mjs @@ -4,6 +4,7 @@ import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os'; import { parseProxyPort } from './proxy-port.mjs'; +import { createRequestBodyCollector } from './request-body.mjs'; const PORT = parseProxyPort('CLEF_PROXY_PORT', 18742); // Test seam (#692): point the gateway base at a stub upstream via @@ -201,10 +202,10 @@ const server = http.createServer(async (req, res) => { } if (req.method === 'POST' && (pathname === '/v1/systemone' || pathname === '/systemone' || pathname === '/')) { - let bodyText = ''; + const bodyCollector = createRequestBodyCollector(5 * 1024 * 1024); req.on('data', chunk => { - bodyText += chunk; - if (bodyText.length > 5 * 1024 * 1024) { + if (bodyCollector.tooLarge) return; + if (!bodyCollector.push(chunk)) { res.writeHead(413, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Request body too large' })); req.destroy(); @@ -212,6 +213,8 @@ const server = http.createServer(async (req, res) => { }); req.on('end', async () => { + if (bodyCollector.tooLarge) return; + const bodyText = bodyCollector.text(); // Parse the client body on its own: a malformed request is a 400, // not a 500 from the shared upstream catch below (sibling proxies // all parse separately — #690). diff --git a/bin/codex-proxy.mjs b/bin/codex-proxy.mjs index f80c6a8da5..54c35297fe 100755 --- a/bin/codex-proxy.mjs +++ b/bin/codex-proxy.mjs @@ -8,6 +8,7 @@ import fs from 'node:fs'; import path from 'node:path'; import os from 'node:os'; import { parseProxyPort } from './proxy-port.mjs'; +import { createRequestBodyCollector } from './request-body.mjs'; const PORT = parseProxyPort('CODEX_PROXY_PORT', 18744); const HOST = process.env.CODEX_PROXY_HOST || '127.0.0.1'; @@ -1070,10 +1071,10 @@ const server = http.createServer(async (req, res) => { // Chat completions if (req.method === 'POST' && (pathname === '/v1/chat/completions' || pathname === '/chat/completions')) { - let bodyText = ''; + const bodyCollector = createRequestBodyCollector(50 * 1024 * 1024); req.on('data', chunk => { - bodyText += chunk; - if (bodyText.length > 50 * 1024 * 1024) { + if (bodyCollector.tooLarge) return; + if (!bodyCollector.push(chunk)) { res.writeHead(413, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: 'Request payload too large' } })); req.destroy(); @@ -1081,6 +1082,8 @@ const server = http.createServer(async (req, res) => { }); req.on('end', async () => { + if (bodyCollector.tooLarge) return; + const bodyText = bodyCollector.text(); let body; try { body = JSON.parse(bodyText); diff --git a/bin/commandcode-proxy.mjs b/bin/commandcode-proxy.mjs index 2f74fe2fdd..481b2e7d94 100644 --- a/bin/commandcode-proxy.mjs +++ b/bin/commandcode-proxy.mjs @@ -54,6 +54,7 @@ import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { parseProxyPort } from './proxy-port.mjs'; +import { createRequestBodyCollector } from './request-body.mjs'; const PORT = parseProxyPort('COMMANDCODE_PROXY_PORT', 18748); const HOST = process.env.COMMANDCODE_PROXY_HOST || '127.0.0.1'; @@ -896,10 +897,10 @@ const server = http.createServer((req, res) => { // Chat completions if (req.method === 'POST' && (pathname === '/v1/chat/completions' || pathname === '/chat/completions')) { - let bodyText = ''; + const bodyCollector = createRequestBodyCollector(50 * 1024 * 1024); req.on('data', chunk => { - bodyText += chunk; - if (bodyText.length > 50 * 1024 * 1024) { + if (bodyCollector.tooLarge) return; + if (!bodyCollector.push(chunk)) { res.writeHead(413, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: 'Request payload too large' } })); req.destroy(); @@ -907,6 +908,8 @@ const server = http.createServer((req, res) => { }); req.on('end', () => { + if (bodyCollector.tooLarge) return; + const bodyText = bodyCollector.text(); let body; try { body = JSON.parse(bodyText); diff --git a/bin/design-code-proxy.mjs b/bin/design-code-proxy.mjs index 08005365b0..4756f5e499 100755 --- a/bin/design-code-proxy.mjs +++ b/bin/design-code-proxy.mjs @@ -2,6 +2,7 @@ import http from 'node:http'; import https from 'node:https'; import { parseProxyPort } from './proxy-port.mjs'; +import { createRequestBodyCollector } from './request-body.mjs'; const PORT = parseProxyPort('DESIGN_CODE_PROXY_PORT', 18746); const HOST = process.env.DESIGN_CODE_PROXY_HOST || '127.0.0.1'; @@ -189,10 +190,10 @@ const server = http.createServer((req, res) => { // Chat completions if (req.method === 'POST' && (pathname === '/v1/chat/completions' || pathname === '/chat/completions')) { - let bodyText = ''; + const bodyCollector = createRequestBodyCollector(50 * 1024 * 1024); req.on('data', chunk => { - bodyText += chunk; - if (bodyText.length > 50 * 1024 * 1024) { + if (bodyCollector.tooLarge) return; + if (!bodyCollector.push(chunk)) { res.writeHead(413, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: 'Payload too large' } })); req.destroy(); @@ -200,6 +201,8 @@ const server = http.createServer((req, res) => { }); req.on('end', () => { + if (bodyCollector.tooLarge) return; + const bodyText = bodyCollector.text(); let body; try { body = JSON.parse(bodyText); diff --git a/bin/muse-proxy.mjs b/bin/muse-proxy.mjs index f87a796cf9..ba5405f647 100644 --- a/bin/muse-proxy.mjs +++ b/bin/muse-proxy.mjs @@ -31,6 +31,7 @@ import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { parseProxyPort } from './proxy-port.mjs'; +import { createRequestBodyCollector } from './request-body.mjs'; // Default port 18747: 18746 is claimed by design-code-proxy // (DESIGN_CODE_PROXY_PORT) in this repo's config, so muse-proxy takes the @@ -1483,16 +1484,18 @@ const server = http.createServer((req, res) => { // HTTP response is piped through as-is. Without direct credentials the // route is not offered (404, as before direct mode existed). if (req.method === 'POST' && (pathname === '/v1/responses' || pathname === '/responses')) { - let bodyText = ''; + const bodyCollector = createRequestBodyCollector(50 * 1024 * 1024); req.on('data', chunk => { - bodyText += chunk; - if (bodyText.length > 50 * 1024 * 1024) { + if (bodyCollector.tooLarge) return; + if (!bodyCollector.push(chunk)) { res.writeHead(413, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: 'Request payload too large' } })); req.destroy(); } }); req.on('end', async () => { + if (bodyCollector.tooLarge) return; + const bodyText = bodyCollector.text(); const keyResult = await getDirectKey(); if (keyResult.status === 'disabled' || keyResult.status === 'none') { res.writeHead(404, { 'Content-Type': 'application/json' }); @@ -1581,10 +1584,10 @@ const server = http.createServer((req, res) => { // Chat completions if (req.method === 'POST' && (pathname === '/v1/chat/completions' || pathname === '/chat/completions')) { - let bodyText = ''; + const bodyCollector = createRequestBodyCollector(50 * 1024 * 1024); req.on('data', chunk => { - bodyText += chunk; - if (bodyText.length > 50 * 1024 * 1024) { + if (bodyCollector.tooLarge) return; + if (!bodyCollector.push(chunk)) { res.writeHead(413, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: 'Request payload too large' } })); req.destroy(); @@ -1592,6 +1595,8 @@ const server = http.createServer((req, res) => { }); req.on('end', async () => { + if (bodyCollector.tooLarge) return; + const bodyText = bodyCollector.text(); let body; try { body = JSON.parse(bodyText); diff --git a/bin/request-body.mjs b/bin/request-body.mjs new file mode 100644 index 0000000000..30bc6ff22e --- /dev/null +++ b/bin/request-body.mjs @@ -0,0 +1,26 @@ +/** Collect a complete HTTP request body and decode UTF-8 only after all bytes arrive. */ +export function createRequestBodyCollector(maxBytes) { + const chunks = []; + let byteLength = 0; + let tooLarge = false; + + return { + get tooLarge() { return tooLarge; }, + get byteLength() { return byteLength; }, + push(chunk) { + if (tooLarge) return false; + const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); + if (byteLength + bytes.length > maxBytes) { + tooLarge = true; + return false; + } + chunks.push(bytes); + byteLength += bytes.length; + return true; + }, + text() { + if (tooLarge) throw new Error('Request body exceeds configured byte limit'); + return Buffer.concat(chunks, byteLength).toString('utf8'); + }, + }; +} diff --git a/bin/request-body.test.mjs b/bin/request-body.test.mjs new file mode 100644 index 0000000000..b56c9aa4d4 --- /dev/null +++ b/bin/request-body.test.mjs @@ -0,0 +1,24 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { createRequestBodyCollector } from './request-body.mjs'; + +test('decodes UTF-8 only after joining request body chunks', () => { + const value = 'before-😀-after'; + const bytes = Buffer.from(JSON.stringify({ value }), 'utf8'); + const emojiStart = bytes.indexOf(Buffer.from('😀', 'utf8')); + const collector = createRequestBodyCollector(bytes.length); + + assert.equal(collector.push(bytes.subarray(0, emojiStart + 2)), true); + assert.equal(collector.push(bytes.subarray(emojiStart + 2)), true); + assert.equal(collector.byteLength, bytes.length); + assert.equal(JSON.parse(collector.text()).value, value); +}); + +test('enforces the request body limit in received bytes', () => { + const collector = createRequestBodyCollector(3); + + assert.equal(collector.push(Buffer.from('a')), true); + assert.equal(collector.push(Buffer.from('😀', 'utf8')), false); + assert.equal(collector.tooLarge, true); + assert.equal(collector.push(Buffer.from('b')), false); +});