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.

security: same-site origin gate on cookie-authed mutations (csrf, medium)

nirholas237be134ca14
diff --git a/api/agents/_id/mood.js b/api/agents/_id/mood.js
index 2a2d5001c..ff115bfbc 100644
--- a/api/agents/_id/mood.js
+++ b/api/agents/_id/mood.js
@@ -15,7 +15,7 @@
// inspector's signal feed and the "mood over time" sparkline.
import { sql } from '../../_lib/db.js';
-import { getSessionUser, authenticateBearer, extractBearer } from '../../_lib/auth.js';
+import { getSessionUser, authenticateBearer, extractBearer, isSameSiteOrigin } from '../../_lib/auth.js';
import { cors, json, method, readJson, wrap, error } from '../../_lib/http.js';
import { parse } from '../../_lib/validate.js';
import { z } from 'zod';
@@ -38,7 +38,16 @@ const sensitivityBody = z.object({ sensitivity: z.number().min(0).max(1) });
async function resolveAuth(req) {
const session = await getSessionUser(req);
- if (session) return { userId: session.id };
+ if (session) {
+ // CSRF defense-in-depth for the cookie path: mood writes persist state
+ // onto the agent, so a cross-site POST riding the session cookie must
+ // never reach them. Reads stay open; bearer callers are exempt.
+ const isRead = ['GET', 'HEAD', 'OPTIONS'].includes(String(req.method || '').toUpperCase());
+ if (!isRead && !isSameSiteOrigin(req)) {
+ throw Object.assign(new Error('cross-site request blocked'), { status: 403, code: 'forbidden' });
+ }
+ return { userId: session.id };
+ }
const bearer = await authenticateBearer(extractBearer(req));
if (bearer) return { userId: bearer.userId };
return null;
diff --git a/api/agents/_id/persona.js b/api/agents/_id/persona.js
index faf27a1a0..92f5e6e08 100644
--- a/api/agents/_id/persona.js
+++ b/api/agents/_id/persona.js
@@ -13,7 +13,7 @@
import { createHash, createHmac } from 'node:crypto';
import { sql } from '../../_lib/db.js';
-import { getSessionUser, authenticateBearer, extractBearer } from '../../_lib/auth.js';
+import { getSessionUser, authenticateBearer, extractBearer, isSameSiteOrigin } from '../../_lib/auth.js';
import { cors, json, method, readJson, wrap, error, rateLimited } from '../../_lib/http.js';
import { limits } from '../../_lib/rate-limit.js';
import { env } from '../../_lib/env.js';
@@ -48,7 +48,17 @@ const restoreBody = z.object({ version: z.number().int().positive() });
async function resolveAuth(req) {
const session = await getSessionUser(req);
- if (session) return { userId: session.id };
+ if (session) {
+ // CSRF defense-in-depth for the cookie path: persona writes rewrite the
+ // agent's compiled prompt (prompt-injection persistence), so a
+ // cross-site POST riding the session cookie must never reach them.
+ // Reads stay open; bearer callers are exempt.
+ const isRead = ['GET', 'HEAD', 'OPTIONS'].includes(String(req.method || '').toUpperCase());
+ if (!isRead && !isSameSiteOrigin(req)) {
+ throw Object.assign(new Error('cross-site request blocked'), { status: 403, code: 'forbidden' });
+ }
+ return { userId: session.id };
+ }
const bearer = await authenticateBearer(extractBearer(req));
if (bearer) return { userId: bearer.userId };
return null;
diff --git a/api/agents/pumpfun/[action].js b/api/agents/pumpfun/[action].js
index 31a5b2b60..7c512670e 100644
--- a/api/agents/pumpfun/[action].js
+++ b/api/agents/pumpfun/[action].js
@@ -12,7 +12,7 @@
* One bundle so Vercel doesn't re-bundle @solana/web3.js + @pump-fun/* per file.
*/
-import { getSessionUser, authenticateBearer, extractBearer } from '../../_lib/auth.js';
+import { getSessionUser, authenticateBearer, extractBearer, isSameSiteOrigin } from '../../_lib/auth.js';
import {
cors,
json,
@@ -37,7 +37,18 @@ const NATIVE_SOL_MINT = 'So11111111111111111111111111111111111111112';
async function resolveAuth(req) {
const session = await getSessionUser(req);
- if (session) return { userId: session.id };
+ if (session) {
+ // CSRF defense-in-depth for the cookie path: these handlers sign real
+ // spends with platform-held custodial keys, so a cross-site POST riding
+ // the session cookie must never reach them. Reads stay open; bearer
+ // callers (agent keys, workers) are exempt since the token itself is
+ // the proof of intent.
+ const isRead = ['GET', 'HEAD', 'OPTIONS'].includes(String(req.method || '').toUpperCase());
+ if (!isRead && !isSameSiteOrigin(req)) {
+ throw Object.assign(new Error('cross-site request blocked'), { status: 403, code: 'forbidden' });
+ }
+ return { userId: session.id };
+ }
const bearer = await authenticateBearer(extractBearer(req));
if (bearer) return { userId: bearer.userId };
return null;
diff --git a/api/pump/[action].js b/api/pump/[action].js
index 88252ae57..49067229f 100644
--- a/api/pump/[action].js
+++ b/api/pump/[action].js
@@ -56,7 +56,7 @@ import { Keypair, PublicKey, LAMPORTS_PER_SOL } from '@solana/web3.js';
import { sql } from '../_lib/db.js';
import { resolveSocialReward } from '../_lib/github-reward.js';
import { submitProtected } from '../_lib/execution-engine.js';
-import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';
+import { getSessionUser, authenticateBearer, extractBearer, isSameSiteOrigin } from '../_lib/auth.js';
import { cors, json, method, readJson, wrap, error, rateLimited, respondError } from '../_lib/http.js';
import { putObject, publicUrl as r2PublicUrl } from '../_lib/r2.js';
import { env } from '../_lib/env.js';
@@ -121,7 +121,18 @@ const RPC = {
async function resolveAuth(req) {
const session = await getSessionUser(req);
- if (session) return { userId: session.id };
+ if (session) {
+ // CSRF defense-in-depth for the cookie path: these handlers sign real
+ // spends with platform-held custodial keys, so a cross-site POST riding
+ // the session cookie must never reach them. Reads stay open; bearer
+ // callers (agent keys, workers) are exempt since the token itself is
+ // the proof of intent.
+ const isRead = ['GET', 'HEAD', 'OPTIONS'].includes(String(req.method || '').toUpperCase());
+ if (!isRead && !isSameSiteOrigin(req)) {
+ throw Object.assign(new Error('cross-site request blocked'), { status: 403, code: 'forbidden' });
+ }
+ return { userId: session.id };
+ }
const bearer = await authenticateBearer(extractBearer(req));
if (bearer) return { userId: bearer.userId };
return null;
diff --git a/tests/api/pump.test.js b/tests/api/pump.test.js
index 6d72ba5c1..2c9de3ba8 100644
--- a/tests/api/pump.test.js
+++ b/tests/api/pump.test.js
@@ -7,11 +7,15 @@ import { Readable } from 'node:stream';
// ── Auth state ────────────────────────────────────────────────────────────
const authState = { session: null, bearer: null };
-vi.mock('../../api/_lib/auth.js', () => ({
- getSessionUser: vi.fn(async () => authState.session),
- authenticateBearer: vi.fn(async () => authState.bearer),
- extractBearer: vi.fn(() => null),
-}));
+vi.mock('../../api/_lib/auth.js', async () => {
+ const actual = await vi.importActual('../../api/_lib/auth.js');
+ return {
+ ...actual,
+ getSessionUser: vi.fn(async () => authState.session),
+ authenticateBearer: vi.fn(async () => authState.bearer),
+ extractBearer: vi.fn(() => null),
+ };
+});
// ── SQL mock ──────────────────────────────────────────────────────────────
const sqlState = { queue: [], calls: [] };
@@ -103,6 +107,9 @@ function makeReq({ method = 'GET', url = '/', headers = {}, body = null, query =
if (query) base.query = query;
base.headers = {
host: 'localhost',
+ // Same-site Origin: the pump dispatchers gate cookie-authed mutations on
+ // isSameSiteOrigin (browsers always send Origin on POST).
+ origin: 'https://three.ws',
...(body ? { 'content-type': 'application/json' } : {}),
...headers,
};
@@ -563,3 +570,51 @@ describe('GET /.well-known/x402', () => {
expect(json.pump_agent_payments.prep).toBe('/api/pump/accept-payment-prep');
});
});
+
+describe('cookie-CSRF gate on pump mutations (2026-07-23 audit)', () => {
+ beforeEach(resetAll);
+
+ it('rejects a cross-site POST riding the session cookie before any handler work', async () => {
+ authState.session = { id: 'user-1' };
+ const { default: handler } = await import('../../api/pump/launch-prep.js');
+ const { res, json } = await invoke(handler, {
+ method: 'POST', url: '/api/pump/launch-prep',
+ headers: { origin: 'https://evil.example' },
+ // A schema-valid body: launch-prep validates input before auth, so
+ // the CSRF gate inside resolveAuth is only reached with this shape.
+ body: { agent_id: '00000000-0000-0000-0000-000000000001',
+ wallet_address: walletB58, name: 'X', symbol: 'X', uri: 'https://x/m.json' },
+ });
+ expect(res.statusCode).toBe(403);
+ expect(json.error).toBe('forbidden');
+ // Nothing mutating ran: the gate fires inside resolveAuth, ahead of the
+ // spend/signing path. (A read-only agent lookup may precede auth.)
+ expect(
+ sqlState.calls.every((c) => !/\b(insert|update|delete)\b/i.test(String(c.query))),
+ ).toBe(true);
+ });
+
+ it('rejects a cookie-authed POST with no Origin and no Referer', async () => {
+ authState.session = { id: 'user-1' };
+ const { default: handler } = await import('../../api/pump/launch-prep.js');
+ const { res } = await invoke(handler, {
+ method: 'POST', url: '/api/pump/launch-prep',
+ headers: { origin: undefined },
+ body: { agent_id: '00000000-0000-0000-0000-000000000001',
+ wallet_address: walletB58, name: 'X', symbol: 'X', uri: 'https://x/m.json' },
+ });
+ expect(res.statusCode).toBe(403);
+ });
+
+ it('does not gate bearer-authed mutations (agent keys, workers)', async () => {
+ authState.bearer = { userId: 'user-1' };
+ const { default: handler } = await import('../../api/pump/launch-prep.js');
+ const { res } = await invoke(handler, {
+ method: 'POST', url: '/api/pump/launch-prep',
+ headers: { origin: 'https://evil.example' },
+ body: {},
+ });
+ // Past the gate; may fail later on input validation, never on the CSRF gate.
+ expect(res.statusCode).not.toBe(403);
+ });
+});