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(rate-limit): trust no forwarded hops on the direct run.app host so spoofed headers cannot rotate buckets

nirholas93f4a514e04b
diff --git a/api/_lib/rate-limit.js b/api/_lib/rate-limit.js
index 500ebdb1c..39db57141 100644
--- a/api/_lib/rate-limit.js
+++ b/api/_lib/rate-limit.js
@@ -2552,7 +2552,13 @@ export function clientIp(req) {
if (hops.length) {
// Clamp: a shorter-than-expected chain (direct hit, or a stripped header)
// must degrade to the leftmost real entry, never to a negative index.
- const ip = hops[Math.max(0, hops.length - 1 - TRUSTED_PROXY_HOPS)];
+ // The *.run.app URL bypasses the load balancer: Cloud Run appends only the
+ // real client, so no trailing hop may be skipped there. The Host header on
+ // that URL is fixed by Google's routing; a caller who sends it through the
+ // LB only collapses their own key onto the LB address.
+ const host = String(req.headers?.host ?? '').split(':')[0].toLowerCase();
+ const skip = host.endsWith('.run.app') ? 0 : TRUSTED_PROXY_HOPS;
+ const ip = hops[Math.max(0, hops.length - 1 - skip)];
if (ip) return ip;
}
}
diff --git a/tests/client-ip-proxy.test.js b/tests/client-ip-proxy.test.js
index b63e5c57e..45776b4bb 100644
--- a/tests/client-ip-proxy.test.js
+++ b/tests/client-ip-proxy.test.js
@@ -114,3 +114,25 @@ describe('clientIp without any proxy header', () => {
expect(clientIp(req({ 'x-vercel-forwarded-for': SPOOF, 'x-forwarded-for': `${REAL_CLIENT}, ${LB}` }))).toBe(REAL_CLIENT);
});
});
+
+describe('clientIp on the direct Cloud Run URL (no load balancer)', () => {
+ const RUN_HOST = 'three-ws-api-lp642k3kpa-uc.a.run.app';
+
+ it('uses the entry Cloud Run appended, not a caller-forged one', async () => {
+ const clientIp = await loadWithHops(undefined);
+ const forged = `${SPOOF}, ${REAL_CLIENT}`;
+ expect(clientIp(req({ host: RUN_HOST, 'x-forwarded-for': forged }))).toBe(REAL_CLIENT);
+ });
+
+ it('a rotating forged header cannot mint fresh buckets on the run.app host', async () => {
+ const clientIp = await loadWithHops(undefined);
+ const a = clientIp(req({ host: RUN_HOST, 'x-forwarded-for': `1.1.1.1, ${REAL_CLIENT}` }));
+ const b = clientIp(req({ host: RUN_HOST, 'x-forwarded-for': `2.2.2.2, ${REAL_CLIENT}` }));
+ expect(a).toBe(b);
+ });
+
+ it('the load-balanced host still skips the balancer hop', async () => {
+ const clientIp = await loadWithHops(undefined);
+ expect(clientIp(req({ host: 'three.ws', 'x-forwarded-for': `${SPOOF}, ${REAL_CLIENT}, ${LB}` }))).toBe(REAL_CLIENT);
+ });
+});