Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions bin/agy-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -856,17 +857,19 @@ 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();
}
});

req.on('end', async () => {
if (bodyCollector.tooLarge) return;
const bodyText = bodyCollector.text();
let body;
try {
body = JSON.parse(bodyText);
Expand Down
9 changes: 6 additions & 3 deletions bin/antigravity-ls-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -747,17 +748,19 @@ 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();
}
});

req.on('end', async () => {
if (bodyCollector.tooLarge) return;
const bodyText = bodyCollector.text();
let body;
try {
body = JSON.parse(bodyText);
Expand Down
31 changes: 31 additions & 0 deletions bin/antigravity-ls-proxy.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});
9 changes: 6 additions & 3 deletions bin/claude-code-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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' });
Expand Down
9 changes: 6 additions & 3 deletions bin/clef-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -201,17 +202,19 @@ 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();
}
});

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).
Expand Down
9 changes: 6 additions & 3 deletions bin/codex-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -1070,17 +1071,19 @@ 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();
}
});

req.on('end', async () => {
if (bodyCollector.tooLarge) return;
const bodyText = bodyCollector.text();
let body;
try {
body = JSON.parse(bodyText);
Expand Down
9 changes: 6 additions & 3 deletions bin/commandcode-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -896,17 +897,19 @@ 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();
}
});

req.on('end', () => {
if (bodyCollector.tooLarge) return;
const bodyText = bodyCollector.text();
let body;
try {
body = JSON.parse(bodyText);
Expand Down
9 changes: 6 additions & 3 deletions bin/design-code-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -189,17 +190,19 @@ 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();
}
});

req.on('end', () => {
if (bodyCollector.tooLarge) return;
const bodyText = bodyCollector.text();
let body;
try {
body = JSON.parse(bodyText);
Expand Down
17 changes: 11 additions & 6 deletions bin/muse-proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' });
Expand Down Expand Up @@ -1581,17 +1584,19 @@ 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();
}
});

req.on('end', async () => {
if (bodyCollector.tooLarge) return;
const bodyText = bodyCollector.text();
let body;
try {
body = JSON.parse(bodyText);
Expand Down
26 changes: 26 additions & 0 deletions bin/request-body.mjs
Original file line number Diff line number Diff line change
@@ -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');
},
};
}
24 changes: 24 additions & 0 deletions bin/request-body.test.mjs
Original file line number Diff line number Diff line change
@@ -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);
});