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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(security): give framed embeds a script policy and stop junk bearer headers skipping CSRF
nirholasdeb82bf584ea
diff --git a/api/_lib/csrf.js b/api/_lib/csrf.jsindex 2a8b207e5..69363f423 100644--- a/api/_lib/csrf.js+++ b/api/_lib/csrf.js@@ -7,6 +7,7 @@import crypto from 'node:crypto';import { sql } from './db.js';import { error } from './http.js';+import { authenticateBearer, extractBearer, hasSessionCookie } from './auth.js';const TTL_SECONDS = 3600;@@ -23,6 +24,21 @@ export async function issueCsrf(userId) {// and returns false. The CSRF_DISABLED=1 escape hatch is honored ONLY outside// production — a misconfigured env must not silently disable CSRF platform-wide// on the live site. Machine-to-machine bearer auth is exempted below regardless.+// A bearer credential cannot ride along on a cross-site request the way a cookie+// does, so a request carrying one needs no CSRF token. The exemption has to be+// tied to the credential that actually authenticated, though: getRequestUser+// prefers the session cookie, so a request with the victim's cookie plus any+// junk "Bearer x" header would otherwise run as the victim with the token check+// skipped. Exempt only when there is no session cookie to ride on, or when the+// bearer itself authenticates as the same account the session does.+async function bearerVouchesFor(req, userId) {+ const token = extractBearer(req);+ if (!token) return false;+ if (!hasSessionCookie(req)) return true;+ const bearer = await authenticateBearer(token).catch(() => null);+ return Boolean(bearer && userId && String(bearer.userId) === String(userId));+}+const IS_PROD = process.env.VERCEL_ENV === 'production' || process.env.NODE_ENV === 'production';/**@@ -36,8 +52,7 @@ export async function checkCsrf(req, userId) {// Bearer-token requests are exempt: the token itself is the proof of intent// and bearer tokens aren't auto-attached by browsers like cookies are.- const authHeader = req.headers?.authorization || '';- if (authHeader.startsWith('Bearer ')) return { ok: true };+ if (await bearerVouchesFor(req, userId)) return { ok: true };const sent =req.headers['x-csrf-token'] ||diff --git a/tests/csrf-bearer-exemption.test.js b/tests/csrf-bearer-exemption.test.jsnew file mode 100644index 000000000..4884f9b0c--- /dev/null+++ b/tests/csrf-bearer-exemption.test.js@@ -0,0 +1,91 @@+// The CSRF guard skips the token check for bearer-authenticated requests, because+// a bearer credential cannot ride along on a cross-site request the way a cookie+// does. getRequestUser prefers the session cookie, though, so the exemption must+// follow the credential that actually authenticated: a request carrying the+// victim's cookie plus a junk "Bearer x" header used to run as the victim with+// the token check skipped.++import { describe, it, expect, vi, beforeEach } from 'vitest';++const db = vi.hoisted(() => ({ consumed: null }));+const bearers = vi.hoisted(() => new Map());++vi.mock('../api/_lib/db.js', () => ({+ sql: async (strings, ...values) => {+ const text = strings.join('?');+ if (/DELETE FROM csrf_tokens/.test(text)) {+ const [token, userId] = values;+ if (db.consumed && db.consumed.token === token && db.consumed.userId === userId) {+ db.consumed = null;+ return [{ user_id: userId }];+ }+ return [];+ }+ return [];+ },+}));++vi.mock('../api/_lib/auth.js', () => ({+ extractBearer: (req) => {+ const h = req.headers.authorization || '';+ return h.toLowerCase().startsWith('bearer ') ? h.slice(7).trim() : null;+ },+ hasSessionCookie: (req) => /(?:^|;\s*)__Host-sid=/.test(req.headers.cookie || ''),+ authenticateBearer: async (token) => bearers.get(token) || null,+}));++const { checkCsrf } = await import('../api/_lib/csrf.js');++const VICTIM = 'user-victim';+const ATTACKER = 'user-attacker';++function req(headers) {+ return { headers };+}++describe('checkCsrf bearer exemption', () => {+ beforeEach(() => {+ db.consumed = null;+ bearers.clear();+ bearers.set('victim-key', { userId: VICTIM, source: 'apikey' });+ bearers.set('attacker-key', { userId: ATTACKER, source: 'apikey' });+ });++ it('exempts a bearer request that carries no session cookie', async () => {+ const verdict = await checkCsrf(req({ authorization: 'Bearer victim-key' }), VICTIM);+ expect(verdict.ok).toBe(true);+ });++ it('rejects a cookie-authenticated request that adds a junk bearer header', async () => {+ const verdict = await checkCsrf(+ req({ authorization: 'Bearer x', cookie: '__Host-sid=victim-session' }),+ VICTIM,+ );+ expect(verdict).toMatchObject({ ok: false, code: 'csrf_missing' });+ });++ it("rejects a cookie session paired with another account's valid key", async () => {+ const verdict = await checkCsrf(+ req({ authorization: 'Bearer attacker-key', cookie: '__Host-sid=victim-session' }),+ VICTIM,+ );+ expect(verdict.ok).toBe(false);+ });++ it("exempts a signed-in user calling with their own key (the API playground)", async () => {+ const verdict = await checkCsrf(+ req({ authorization: 'Bearer victim-key', cookie: '__Host-sid=victim-session' }),+ VICTIM,+ );+ expect(verdict.ok).toBe(true);+ });++ it('still accepts a valid CSRF token on a cookie request', async () => {+ db.consumed = { token: 'tok123', userId: VICTIM };+ const verdict = await checkCsrf(+ req({ cookie: '__Host-sid=victim-session', 'x-csrf-token': 'tok123' }),+ VICTIM,+ );+ expect(verdict.ok).toBe(true);+ });+});diff --git a/vercel.json b/vercel.jsonindex 41477e24e..0d9760b97 100644--- a/vercel.json+++ b/vercel.json@@ -1821,7 +1821,7 @@{"src": "/walk-embed/?","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","cross-origin-resource-policy": "cross-origin"@@ -1851,7 +1851,7 @@{"src": "/viewer/?","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","cross-origin-resource-policy": "cross-origin"@@ -2079,7 +2079,7 @@{"src": "/assistant-frame/?","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "autoplay=*",@@ -2102,7 +2102,7 @@{"src": "/embed/v1/gated/?","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","cross-origin-resource-policy": "cross-origin"@@ -2112,7 +2112,7 @@{"src": "/embed/v1/gated.html","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","cross-origin-resource-policy": "cross-origin"@@ -2150,7 +2150,7 @@{"src": "/embodiment/embed/?","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "microphone=(self), camera=(self), autoplay=(self), xr-spatial-tracking=*",@@ -2161,7 +2161,7 @@{"src": "/embed/dextools-boost/?","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","cross-origin-resource-policy": "cross-origin"@@ -2171,7 +2171,7 @@{"src": "/embed/avatar","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "microphone=(self), camera=(self), xr-spatial-tracking=*",@@ -2182,7 +2182,7 @@{"src": "/embed/avatar/([a-z0-9_-]{3,30})","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "microphone=(self), camera=(self), xr-spatial-tracking=*",@@ -2217,7 +2217,7 @@{"src": "/widget","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "microphone=(self), camera=(self), xr-spatial-tracking=*, autoplay=*",@@ -2228,7 +2228,7 @@{"src": "/widget/","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "microphone=(self), camera=(self), xr-spatial-tracking=*, autoplay=*",@@ -2239,7 +2239,7 @@{"src": "/widget\\.html$","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "microphone=(self), camera=(self), xr-spatial-tracking=*, autoplay=*",@@ -4669,7 +4669,7 @@{"src": "/agent/([^/]+)/embed","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "microphone=(self), camera=(self), xr-spatial-tracking=*",@@ -5294,7 +5294,7 @@{"src": "/agents/([^/.]+)/embed","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "microphone=(self), camera=(self), xr-spatial-tracking=*",@@ -6138,7 +6138,7 @@{"src": "/a-embed","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "microphone=(self), camera=(self), xr-spatial-tracking=*"@@ -6148,7 +6148,7 @@{"src": "/a/(\\d+)/(\\d+)/embed","headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "microphone=(self), camera=(self), xr-spatial-tracking=*"@@ -6169,7 +6169,7 @@}],"headers": {- "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; report-uri /api/client-errors",+ "content-security-policy": "frame-ancestors *; base-uri 'self'; object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.jsdelivr.net https://cdnjs.cloudflare.com https://unpkg.com https://esm.sh https://ajax.googleapis.com https://s3.tradingview.com https://platform.twitter.com https://3d-agent.vercel.app https://three.ws; worker-src 'self' blob:; report-uri /api/client-errors","x-content-type-options": "nosniff","referrer-policy": "strict-origin-when-cross-origin","permissions-policy": "microphone=(self), camera=(self), autoplay=(self), xr-spatial-tracking=*"