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(swarms): send the CSRF token from the /swarms client and document the header
nirholas06b187a3a0b8
diff --git a/docs/swarms.md b/docs/swarms.mdindex 63ce1a598..a591b8c23 100644--- a/docs/swarms.md+++ b/docs/swarms.md@@ -120,16 +120,23 @@ curl -sN https://three.ws/api/swarms/<SWARM_ID>/stream # SSE: votes, payouts,All mutations are `POST /api/swarms` with an `action` field.+Every mutation moves or governs real funds, so two guards apply to all of them.+A cookie-session caller must present a single-use CSRF token+(`GET /api/csrf-token`, echoed back as `X-CSRF-Token`); a bearer/API-key caller+is exempt, since the token is itself the proof of intent. Both are metered at+30 mutations per minute per user.+```bash# Create. The policy is optional; anything you omit takes the default above.+CSRF=$(curl -s https://three.ws/api/csrf-token -H 'Cookie: <session>' | jq -r .token)curl -s -X POST https://three.ws/api/swarms \- -H 'Content-Type: application/json' -H 'Cookie: <session>' \+ -H 'Content-Type: application/json' -H 'Cookie: <session>' -H "X-CSRF-Token: $CSRF" \-d '{"action":"create","owner_agent_id":"<AGENT_UUID>","name":"Momentum pod","policy":{"min_consensus":0.7,"creator_fee_bps":500}}'# Join with one of your agentscurl -s -X POST https://three.ws/api/swarms \- -H 'Content-Type: application/json' -H 'Cookie: <session>' \+ -H 'Content-Type: application/json' -H 'Cookie: <session>' -H "X-CSRF-Token: $(curl -s https://three.ws/api/csrf-token -H 'Cookie: <session>' | jq -r .token)" \-d '{"action":"join","swarm_id":"<SWARM_UUID>","agent_id":"<AGENT_UUID>"}'```@@ -138,12 +145,12 @@ curl -s -X POST https://three.ws/api/swarms \```bash# Contribute 0.05 SOL. This is a real on-chain transfer.curl -s -X POST https://three.ws/api/swarms \- -H 'Content-Type: application/json' -H 'Cookie: <session>' \+ -H 'Content-Type: application/json' -H 'Cookie: <session>' -H "X-CSRF-Token: $(curl -s https://three.ws/api/csrf-token -H 'Cookie: <session>' | jq -r .token)" \-d '{"action":"contribute","swarm_id":"<SWARM_UUID>","agent_id":"<AGENT_UUID>","sol":0.05}'# Exit: paid out per the swarm's exit_policycurl -s -X POST https://three.ws/api/swarms \- -H 'Content-Type: application/json' -H 'Cookie: <session>' \+ -H 'Content-Type: application/json' -H 'Cookie: <session>' -H "X-CSRF-Token: $(curl -s https://three.ws/api/csrf-token -H 'Cookie: <session>' | jq -r .token)" \-d '{"action":"exit","swarm_id":"<SWARM_UUID>","agent_id":"<AGENT_UUID>"}'```@@ -162,6 +169,8 @@ Every failure returns `{ error, error_description }` with a specific code rather| 400 | `bad_action` | Unrecognized `action` || 400 | `too_small` | Contribution under the 0.005 SOL floor || 401 | `unauthorized` | Mutation or `?mine=1` without a session |+| 403 | `csrf_missing`, `csrf_invalid` | Session mutation with no (or a spent) `X-CSRF-Token` |+| 429 | `rate_limited` | Over 30 mutations per minute for this user || 404 | `not_found` | No such swarm || 409 | `killed` | The swarm's kill switch is already pulled |diff --git a/src/swarms.js b/src/swarms.jsindex 82f6a854f..86fcb3cdf 100644--- a/src/swarms.js+++ b/src/swarms.js@@ -16,11 +16,32 @@ const short = (a) => (a ? `${a.slice(0, 4)}…${a.slice(-4)}` : '');const state = { network: 'mainnet', agents: null, authed: false };+// Single-use CSRF token for a state-changing call. Never cached: the server+// consumes the token in the same statement that validates it, so a reused one is+// a guaranteed 403 on the next write.+async function csrfToken() {+ try {+ const r = await fetch('/api/csrf-token', { credentials: 'include' });+ if (!r.ok) return null;+ const j = await r.json().catch(() => null);+ return j?.token || j?.data?.token || null;+ } catch {+ return null;+ }+}+async function api(path, opts = {}) {const ctrl = new AbortController();const to = setTimeout(() => ctrl.abort(), opts.timeout || 20000);try {- const res = await fetch(path, { credentials: 'include', signal: ctrl.signal, ...opts });+ const headers = { ...(opts.headers || {}) };+ // Every swarm mutation moves or governs real funds, so the server requires a+ // CSRF token on cookie-session writes.+ if (opts.method && opts.method.toUpperCase() !== 'GET') {+ const token = await csrfToken();+ if (token) headers['x-csrf-token'] = token;+ }+ const res = await fetch(path, { credentials: 'include', signal: ctrl.signal, ...opts, headers });const data = await res.json().catch(() => null);return { ok: res.ok, status: res.status, data: data?.data ?? data, error: res.ok ? null : data?.message || data?.error || `HTTP ${res.status}` };} catch (e) {