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): key every per-IP limit on the shared clientIp() reader

nirholasc86b03cc5543
diff --git a/api/_lib/zauth.js b/api/_lib/zauth.js
index 8207fd675..50f287be7 100644
--- a/api/_lib/zauth.js
+++ b/api/_lib/zauth.js
@@ -26,6 +26,7 @@
// bundling, so the main entry is the reliable one.
import { env } from './env.js';
+import { clientIp } from './rate-limit.js';
let cached;
let _bootLogged = false;
@@ -348,11 +349,7 @@ function shimRequest(req) {
const path = qIdx >= 0 ? url.slice(0, qIdx) : url;
const xfProto = req.headers['x-forwarded-proto'];
const protocol = (Array.isArray(xfProto) ? xfProto[0] : xfProto) || 'https';
- const xfFor = req.headers['x-forwarded-for'];
- const ip =
- (typeof xfFor === 'string' ? xfFor.split(',')[0].trim() : null) ||
- req.socket?.remoteAddress ||
- '';
+ const ip = clientIp(req);
if (!('path' in req)) Object.defineProperty(req, 'path', { value: path });
if (!('originalUrl' in req)) Object.defineProperty(req, 'originalUrl', { value: url });
diff --git a/api/agent/guard.js b/api/agent/guard.js
index 8a055250c..91f56e627 100644
--- a/api/agent/guard.js
+++ b/api/agent/guard.js
@@ -18,7 +18,7 @@
// compute endpoints.
import { cors, error, json, method, rateLimited, readJson, wrap } from '../_lib/http.js';
-import { limits } from '../_lib/rate-limit.js';
+import { limits, clientIp } from '../_lib/rate-limit.js';
import { solUsdPrice } from '../_lib/avatar-wallet.js';
import { GuardChain, SpendGuard, TradeGuard, createX402Hook } from '@three-ws/agent-runtime';
@@ -81,11 +81,7 @@ export default wrap(async function handler(req, res) {
if (cors(req, res, { methods: 'POST, OPTIONS' })) return;
if (!method(req, res, ['POST'])) return;
- const ip =
- String(req.headers['x-forwarded-for'] || '').split(',')[0].trim() ||
- req.socket?.remoteAddress ||
- 'unknown';
- const rl = await limits.agentGuardIp(ip);
+ const rl = await limits.agentGuardIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
let body;
diff --git a/api/avatar/capabilities.js b/api/avatar/capabilities.js
index ae7eb367a..a7edc9807 100644
--- a/api/avatar/capabilities.js
+++ b/api/avatar/capabilities.js
@@ -28,6 +28,7 @@
import { cors, error, json, wrap, rateLimited } from '../_lib/http.js';
import { getAvatar } from '../_lib/avatars.js';
+import { clientIp } from '../_lib/rate-limit.js';
import { inspectGlbCapabilities } from '../_lib/avatar-capabilities.js';
import { ARKIT_52, ARKIT_VISEMES } from '../../src/runtime/arkit52.js';
import { CANONICAL_BONES } from '../../src/glb-canonicalize.js';
@@ -117,8 +118,7 @@ export default wrap(async function handler(req, res) {
return error(res, 405, 'method_not_allowed', 'Use GET');
}
- const ip = (req.headers['x-forwarded-for'] || '').split(',')[0].trim() || req.socket?.remoteAddress;
- const rl = rateCheck(ip);
+ const rl = rateCheck(clientIp(req));
if (!rl.success) {
return rateLimited(res, rl, `Limit: ${RATE_LIMIT_MAX} inspections per ${RATE_LIMIT_WINDOW_MS / 60000}m`);
}
diff --git a/api/avatar/render.js b/api/avatar/render.js
index 015120ba9..84e93e655 100644
--- a/api/avatar/render.js
+++ b/api/avatar/render.js
@@ -33,6 +33,7 @@
import { cors, error, json, wrap, rateLimited } from '../_lib/http.js';
import { getAvatar } from '../_lib/avatars.js';
+import { clientIp } from '../_lib/rate-limit.js';
import {
SCENE_PRESETS,
FORMAT_TYPES,
@@ -77,8 +78,7 @@ export default wrap(async function handler(req, res) {
return error(res, 405, 'method_not_allowed', 'Use GET');
}
- const ip = (req.headers['x-forwarded-for'] || '').split(',')[0].trim() || req.socket?.remoteAddress;
- const rl = rateCheck(ip);
+ const rl = rateCheck(clientIp(req));
if (!rl.success) {
return rateLimited(res, rl, `Limit: ${RATE_LIMIT_MAX} renders per ${RATE_LIMIT_WINDOW_MS / 60000}m`);
}
diff --git a/api/x402-checkout-record.js b/api/x402-checkout-record.js
index bdcb3060f..ee2729c2a 100644
--- a/api/x402-checkout-record.js
+++ b/api/x402-checkout-record.js
@@ -59,7 +59,7 @@ export default wrap(async (req, res) => {
if (existing) return json(res, 200, { ok: true, id: existing.id, paid_at: existing.paid_at, deduped: true });
}
- const ip = (req.headers['x-forwarded-for'] || req.socket?.remoteAddress || '').toString().split(',')[0].trim();
+ const ip = clientIp(req);
const ipHash = ip ? simpleHash(ip) : null;
const ua = (req.headers['user-agent'] || '').toString().slice(0, 240);