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(marketplace): send the CSRF token on bookmark and fork writes

nirholas030b5beca66b
diff --git a/src/agent-detail-market.js b/src/agent-detail-market.js
index 5161b7e06..a09722568 100644
--- a/src/agent-detail-market.js
+++ b/src/agent-detail-market.js
@@ -34,6 +34,7 @@ import {
openAssetPurchaseFlow,
formatAssetPrice,
apiPostWithCsrf,
+ apiWriteWithCsrf,
USDC_MAINNET_MINT,
} from './shared/skill-purchase.js';
import { log } from './shared/log.js';
@@ -298,9 +299,8 @@ function setBookmark(btn, on) {
async function toggleBookmark(agentId, btn) {
const cur = btn.dataset.on === '1';
try {
- const r = await fetch(`${API}/marketplace/agents/${agentId}/bookmark`, {
+ const r = await apiWriteWithCsrf(`${API}/marketplace/agents/${agentId}/bookmark`, {
method: cur ? 'DELETE' : 'POST',
- credentials: 'include',
});
if (r.status === 401) {
location.href = `/login?next=${encodeURIComponent(location.pathname)}`;
@@ -322,10 +322,7 @@ async function forkAgent(agentId) {
const btn = $('ad-fork');
if (btn) { btn.disabled = true; btn.textContent = 'Forking…'; }
try {
- const r = await fetch(`${API}/marketplace/agents/${agentId}/fork`, {
- method: 'POST',
- credentials: 'include',
- });
+ const r = await apiWriteWithCsrf(`${API}/marketplace/agents/${agentId}/fork`);
if (r.status === 401) {
location.href = `/login?next=${encodeURIComponent(location.pathname)}`;
return;
diff --git a/src/marketplace.js b/src/marketplace.js
index f677fa6f9..a27409060 100644
--- a/src/marketplace.js
+++ b/src/marketplace.js
@@ -78,9 +78,8 @@ async function toggleAgentBookmarkFromCard(agentId, btn) {
btn.setAttribute('aria-pressed', String(!wasOn));
btn.textContent = !wasOn ? '★' : '☆';
try {
- const r = await fetch(`${API}/marketplace/agents/${agentId}/bookmark`, {
+ const r = await apiWriteWithCsrf(`${API}/marketplace/agents/${agentId}/bookmark`, {
method: wasOn ? 'DELETE' : 'POST',
- credentials: 'include',
});
if (!r.ok) throw new Error(`bookmark failed ${r.status}`);
const j = await r.json().catch(() => ({}));
@@ -5844,10 +5843,7 @@ async function fork() {
const id = detailState.agent.id;
if (btn) { btn.disabled = true; btn.textContent = 'Forking…'; }
try {
- const r = await fetch(`${API}/marketplace/agents/${id}/fork`, {
- method: 'POST',
- credentials: 'include',
- });
+ const r = await apiWriteWithCsrf(`${API}/marketplace/agents/${id}/fork`);
if (r.status === 401) {
location.href = `/login?next=${encodeURIComponent(location.pathname)}`;
return;
@@ -5875,9 +5871,8 @@ async function toggleBookmark() {
const cur = detailState.bookmarked;
if (btn) btn.disabled = true;
try {
- const r = await fetch(`${API}/marketplace/agents/${id}/bookmark`, {
+ const r = await apiWriteWithCsrf(`${API}/marketplace/agents/${id}/bookmark`, {
method: cur ? 'DELETE' : 'POST',
- credentials: 'include',
});
if (r.status === 401) {
location.href = `/login?next=${encodeURIComponent(location.pathname)}`;
@@ -6232,12 +6227,7 @@ async function publishExistingAgent(id, btn) {
row?.querySelector('.msm-error')?.remove();
try {
const body = catSel ? { category: catSel.value } : {};
- const r = await fetch(`${API}/marketplace/agents/${id}/publish`, {
- method: 'POST',
- headers: { 'content-type': 'application/json' },
- credentials: 'include',
- body: JSON.stringify(body),
- });
+ const r = await apiWriteWithCsrf(`${API}/marketplace/agents/${id}/publish`, { body });
const j = await r.json().catch(() => ({}));
if (!r.ok) throw new Error(j.error_description || 'Publish failed');
const item = (submitMine.items || []).find((a) => a.id === id);
@@ -6408,12 +6398,7 @@ function bindSubmit() {
btn.textContent = publish ? 'Publishing…' : 'Saving…';
try {
errorEl.hidden = true;
- const r = await fetch(`${API}/marketplace/agents`, {
- method: 'POST',
- headers: { 'content-type': 'application/json' },
- credentials: 'include',
- body: JSON.stringify(body),
- });
+ const r = await apiWriteWithCsrf(`${API}/marketplace/agents`, { body });
const j = await r.json();
if (!r.ok) throw new Error(j.error_description || 'Submission failed');
@@ -7262,16 +7247,25 @@ async function getCsrfToken() {
const j = await r.json();
return j.data.token;
}
-async function apiPostWithCsrf(url, body) {
+// Every state-changing call goes through here so the single-use token is always
+// attached. A bodyless write (bookmark on/off) still needs one: the server
+// rejects the request, not the payload.
+async function apiWriteWithCsrf(url, { method = 'POST', body = null } = {}) {
const token = await getCsrfToken();
+ const headers = { 'X-CSRF-Token': token };
+ if (body != null) headers['Content-Type'] = 'application/json';
return fetch(url, {
- method: 'POST',
- headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': token },
+ method,
+ headers,
credentials: 'include',
body: body == null ? undefined : JSON.stringify(body),
});
}
+async function apiPostWithCsrf(url, body) {
+ return apiWriteWithCsrf(url, { method: 'POST', body });
+}
+
async function createPendingPurchase(agentId, skill, durationHours = null) {
const body = { agent_id: agentId, skill };
if (durationHours) body.duration_hours = durationHours;
diff --git a/src/shared/skill-purchase.js b/src/shared/skill-purchase.js
index 4bb68eeb2..4c57f7007 100644
--- a/src/shared/skill-purchase.js
+++ b/src/shared/skill-purchase.js
@@ -577,17 +577,26 @@ async function getCsrfToken() {
_csrf = { token: j.data.token, expiresAt: Date.now() + (j.data.expires_in - 30) * 1000 };
return _csrf.token;
}
-export async function apiPostWithCsrf(url, body) {
+// Any state-changing call, not just POST. A bodyless write (removing a bookmark
+// with DELETE) still carries the token: the server gates the request, not the
+// payload, and Content-Type is only sent when there is something to type.
+export async function apiWriteWithCsrf(url, { method = 'POST', body = null } = {}) {
const token = await getCsrfToken();
_csrf = null;
+ const headers = { 'X-CSRF-Token': token };
+ if (body != null) headers['Content-Type'] = 'application/json';
return fetch(url, {
- method: 'POST',
- headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': token },
+ method,
+ headers,
credentials: 'include',
body: body == null ? undefined : JSON.stringify(body),
});
}
+export async function apiPostWithCsrf(url, body) {
+ return apiWriteWithCsrf(url, { method: 'POST', body });
+}
+
async function buildSplTransferWithReference({ payer, recipient, mint, amount, reference }) {
const { web3, spl } = await loadSolanaModules();
const { PublicKey, Transaction } = web3;