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(ssrf): resolve self-call origins from config in agent hire, embed diagnose and play OG

nirholas183776fec975
diff --git a/api/agent-collab.js b/api/agent-collab.js
index b0b9e7e2e..72d98cdca 100644
--- a/api/agent-collab.js
+++ b/api/agent-collab.js
@@ -23,6 +23,7 @@ import { isUuid } from './_lib/validate.js';
import { limits } from './_lib/rate-limit.js';
import { sql } from './_lib/db.js';
import { env } from './_lib/env.js';
+import { selfOrigin } from './_lib/self-origin.js';
import { runAgentDelegation, AgentNotFoundError } from './_lib/agent-delegate.js';
import { listOffersWithStats } from './_lib/agent-economy.js';
import { writeScreenFrame } from './_lib/agent-screen-frame.js';
@@ -188,13 +189,9 @@ export default wrap(async (req, res) => {
return json(res, 200, { ok: true, taskId: tree.taskId, max_usd: maxUsd, hard_cap_usd: HARD_MAX_USD, tree });
});
-// Build the same-origin base URL for the internal hire call from the inbound
-// request, so a server-to-server call hits THIS running instance (not a stale
-// PUBLIC_APP_ORIGIN). Falls back to the configured app origin when no host header.
+// Base URL for the internal hire call. Never a request header: the call
+// carries a freshly minted bearer token, and a caller-chosen
+// `x-forwarded-host` would POST it (and the hire body) to any host named.
function internalBaseUrl(req) {
- const host = req.headers['x-forwarded-host'] || req.headers.host;
- if (!host) return env.APP_ORIGIN;
- const proto = String(req.headers['x-forwarded-proto'] || '').split(',')[0].trim()
- || (host.startsWith('localhost') || host.startsWith('127.') ? 'http' : 'https');
- return `${proto}://${host}`;
+ return selfOrigin(req);
}
diff --git a/api/embed/diagnose.js b/api/embed/diagnose.js
index 07b9931b8..e77c81c97 100644
--- a/api/embed/diagnose.js
+++ b/api/embed/diagnose.js
@@ -36,6 +36,7 @@
import { cors, error, json, method, readJson, rateLimited, wrap } from '../_lib/http.js';
import { clientIp } from '../_lib/rate-limit.js';
import { diagnose, SsrfBlockedError } from '../_lib/embed-doctor.js';
+import { selfOrigin } from '../_lib/self-origin.js';
export const maxDuration = 60;
@@ -69,13 +70,14 @@ function rateCheck(ip) {
return { success: true, limit: RATE_MAX, remaining: RATE_MAX - hits.length, reset: now + RATE_WINDOW_MS };
}
+// The origin the headless browser treats as "the platform": it hosts the
+// snippet sandbox page and answers the agent lookup. Configured, never taken
+// from `host` / `x-forwarded-host`, which the caller controls; a spoofed value
+// would run the caller's snippet on an origin of their choosing.
function platformOrigin(req) {
const envOrigin = process.env.PUBLIC_ORIGIN || process.env.SITE_ORIGIN;
if (envOrigin) return String(envOrigin).replace(/\/$/, '');
- const host = req.headers['x-forwarded-host'] || req.headers.host;
- if (!host) return 'https://three.ws';
- const proto = String(req.headers['x-forwarded-proto'] || 'https').split(',')[0];
- return `${proto}://${host}`;
+ return selfOrigin(req);
}
export default wrap(async function handler(req, res) {
diff --git a/api/play-og.js b/api/play-og.js
index da40e49c4..4c68d0163 100644
--- a/api/play-og.js
+++ b/api/play-og.js
@@ -13,6 +13,7 @@
// ServerResponse, the same pattern proven in api/demo/coin/og.js.
import { ImageResponse } from '@vercel/og';
import { method } from './_lib/http.js';
+import { selfOrigin } from './_lib/self-origin.js';
const WIDTH = 1200;
const HEIGHT = 630;
@@ -49,16 +50,12 @@ function formatMarketCap(mc) {
return `$${n.toFixed(0)}`;
}
+// The coin lookup is a server-side self-call, so its origin is the configured
+// one (api/_lib/self-origin.js), never the client-supplied `host` or
+// `x-forwarded-host`: trusting those let any caller point this fetch, and the
+// image URL it feeds into the renderer, at a host of their choosing.
function originOf(req) {
- const rawHost = req.headers['x-forwarded-host'] || req.headers.host || 'three.ws';
- // Host headers are client-supplied. A malformed value (spaces, control chars,
- // multiple comma-joined hosts) makes the downstream `${origin}/api/pump/coin`
- // an invalid URL and throws "Invalid URL". Keep only the first host and the
- // characters legal in an authority; fall back to the canonical host otherwise.
- const host = String(rawHost).split(',')[0].trim();
- const safeHost = /^[a-zA-Z0-9.\-:]+$/.test(host) ? host : 'three.ws';
- const proto = req.headers['x-forwarded-proto'] === 'http' ? 'http' : 'https';
- return `${proto}://${safeHost}`;
+ return selfOrigin(req);
}
async function fetchCoin(origin, mint) {