Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(chat): send only an allowlist of body fields upstream from the free chat proxy

nirholas38b8ccfe87c4
diff --git a/api/chat/proxy.js b/api/chat/proxy.js
index 62ee3133d..40ee2b4c0 100644
--- a/api/chat/proxy.js
+++ b/api/chat/proxy.js
@@ -46,6 +46,40 @@ function isModelUnavailable(status, body) {
);
}
+// Standard chat-completion fields the built-in chat client may send. Anything
+// else, such as model fallback lists, plugins, provider routing or transforms,
+// would let an anonymous caller steer the platform key to paid models or paid
+// features despite the :free gate on `model`.
+const UPSTREAM_BODY_FIELDS = [
+ 'messages',
+ 'stream',
+ 'stream_options',
+ 'temperature',
+ 'top_p',
+ 'top_k',
+ 'frequency_penalty',
+ 'presence_penalty',
+ 'repetition_penalty',
+ 'seed',
+ 'stop',
+ 'tools',
+ 'tool_choice',
+ 'parallel_tool_calls',
+ 'response_format',
+ 'reasoning',
+];
+const MAX_OUTPUT_TOKENS = 16_384;
+
+function buildUpstreamBody(body, model) {
+ const out = {};
+ for (const k of UPSTREAM_BODY_FIELDS) if (body[k] !== undefined) out[k] = body[k];
+ for (const k of ['max_tokens', 'max_completion_tokens']) {
+ const n = Number(body[k]);
+ if (Number.isFinite(n) && n > 0) out[k] = Math.min(Math.floor(n), MAX_OUTPUT_TOKENS);
+ }
+ return { ...out, model };
+}
+
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'POST,OPTIONS' })) return;
if (!method(req, res, ['POST'])) return;
@@ -125,7 +159,7 @@ export default wrap(async (req, res) => {
'Content-Type': 'application/json',
...OPENROUTER_ATTRIBUTION_HEADERS,
},
- body: JSON.stringify({ ...body, model: activeModel }),
+ body: JSON.stringify(buildUpstreamBody(body, activeModel)),
signal: headersCtrl.signal,
});
} catch (err) {
diff --git a/tests/api/chat-proxy-body-allowlist.test.js b/tests/api/chat-proxy-body-allowlist.test.js
new file mode 100644
index 000000000..cbbb053cb
--- /dev/null
+++ b/tests/api/chat-proxy-body-allowlist.test.js
@@ -0,0 +1,135 @@
+// OpenRouter answers a free-tier quota cap with 429 and NO Retry-After header:
+// the reset instant lives inside the error body, at
+// error.metadata.headers['X-RateLimit-Reset'] (epoch milliseconds). The proxy
+// used to look only at headers, so a quota-exhausted response reached the
+// browser with no backoff hint at all and clients could only guess or spin.
+
+import { describe, it, expect, beforeEach, vi } from 'vitest';
+import { Readable } from 'node:stream';
+
+vi.mock('../../api/_lib/env.js', () => ({
+ env: { APP_ORIGIN: 'http://localhost:3000', OPENROUTER_API_KEY: 'sk-or-test', OPENROUTER_FALLBACK_KEYS: [] },
+}));
+
+vi.mock('../../api/_lib/rate-limit.js', () => ({
+ limits: { chatIp: vi.fn(async () => ({ success: true })) },
+ clientIp: vi.fn(() => '203.0.113.7'),
+}));
+
+// The model gate and liveness check are covered by chat-proxy-ratelimit.test.js;
+// here they must simply let a valid :free id through to the upstream call.
+vi.mock('../../api/_lib/openrouter-free.js', () => ({
+ isFreeModelId: (id) => typeof id === 'string' && id.endsWith(':free'),
+ isLiveFreeModel: async () => true,
+ pickDefaultFreeModel: async () => null,
+}));
+
+vi.mock('../../api/agent/run.js', () => ({
+ AGENT_MODEL_ID: 'three-ws/agent',
+ runAgentCompletion: vi.fn(),
+}));
+
+vi.mock('../../api/_lib/sentry.js', () => ({ captureException: vi.fn() }));
+vi.mock('../../api/_lib/zauth.js', () => ({
+ instrument: vi.fn(() => null),
+ drain: vi.fn(async () => {}),
+}));
+
+const fetchMock = vi.fn();
+globalThis.fetch = fetchMock;
+
+const { default: handler } = await import('../../api/chat/proxy.js');
+
+function makeReq(body) {
+ const stream = Readable.from([Buffer.from(JSON.stringify(body))]);
+ stream.method = 'POST';
+ stream.url = '/api/chat/proxy';
+ stream.headers = { host: 'localhost', 'content-type': 'application/json' };
+ return stream;
+}
+
+function makeRes() {
+ return {
+ statusCode: 200,
+ headers: {},
+ body: '',
+ writableEnded: false,
+ headersSent: false,
+ setHeader(k, v) {
+ this.headers[k.toLowerCase()] = v;
+ },
+ getHeader(k) {
+ return this.headers[k.toLowerCase()];
+ },
+ end(chunk) {
+ if (chunk !== undefined) this.body += String(chunk);
+ this.writableEnded = true;
+ },
+ write(chunk) {
+ if (chunk !== undefined) this.body += String(chunk);
+ },
+ };
+}
+
+/** A 429 shaped exactly like OpenRouter's real quota-cap response. */
+function quotaCapped(resetEpochMs, { retryAfterHeader = null } = {}) {
+ return {
+ status: 429,
+ body: null,
+ headers: { get: (k) => (k.toLowerCase() === 'retry-after' ? retryAfterHeader : null) },
+ text: async () =>
+ JSON.stringify({
+ error: {
+ message: 'Rate limit exceeded: free-models-per-day-high-balance. ',
+ code: 429,
+ metadata: {
+ headers: {
+ 'X-RateLimit-Limit': '1000',
+ 'X-RateLimit-Remaining': '0',
+ 'X-RateLimit-Reset': String(resetEpochMs),
+ },
+ },
+ },
+ }),
+ };
+}
+
+beforeEach(() => {
+ fetchMock.mockReset();
+});
+
+describe('POST /api/chat/proxy: upstream body allowlist', () => {
+ it('drops routing, plugin and fallback-model fields and clamps max_tokens', async () => {
+ fetchMock.mockResolvedValue({
+ status: 200,
+ body: null,
+ headers: { get: () => 'application/json' },
+ text: async () => '{}',
+ });
+ const res = makeRes();
+ await handler(
+ makeReq({
+ model: 'x/y:free',
+ messages: [{ role: 'user', content: 'hi' }],
+ stream: true,
+ temperature: 0.5,
+ models: ['openai/gpt-4o'],
+ plugins: [{ id: 'web' }],
+ provider: { order: ['openai'] },
+ route: 'fallback',
+ transforms: ['middle-out'],
+ max_tokens: 10_000_000,
+ }),
+ res,
+ );
+ const sent = JSON.parse(fetchMock.mock.calls[0][1].body);
+ expect(sent.model).toBe('x/y:free');
+ expect(sent.stream).toBe(true);
+ expect(sent.temperature).toBe(0.5);
+ expect(sent.messages).toHaveLength(1);
+ for (const k of ['models', 'plugins', 'provider', 'route', 'transforms']) {
+ expect(sent).not.toHaveProperty(k);
+ }
+ expect(sent.max_tokens).toBeLessThanOrEqual(16384);
+ });
+});