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(agents): refuse to link an avatar the caller does not own on agent update

nirholaseb0cdbbd7604
diff --git a/api/agents.js b/api/agents.js
index 2cfea72d5..e908ebc8a 100644
--- a/api/agents.js
+++ b/api/agents.js
@@ -444,7 +444,7 @@ export async function handleGetOne(req, res, id) {
// ── Update ────────────────────────────────────────────────────────────────
-async function handleUpdate(req, res, id, auth) {
+export async function handleUpdate(req, res, id, auth) {
const scopeErr = requireScopeForMutation(auth, res, 'avatars:write');
if (scopeErr) return scopeErr;
const [existing] = await sql`
@@ -462,6 +462,21 @@ async function handleUpdate(req, res, id, auth) {
const homeUrl = normalizeHomeUrl(body.home_url);
if (homeUrl.error) return error(res, 400, 'validation_error', homeUrl.error);
+ // An agent may only point at an avatar its owner owns; the public manifest
+ // resolves the linked avatar's storage key without re-checking ownership.
+ let avatarId = null;
+ if (body.avatar_id) {
+ const raw = String(body.avatar_id);
+ if (!isUuid(raw)) return error(res, 400, 'validation_error', 'avatar_id must be a UUID');
+ const [av] = await sql`
+ SELECT id FROM avatars
+ WHERE id = ${raw} AND owner_id = ${auth.userId} AND deleted_at IS NULL
+ LIMIT 1
+ `;
+ if (!av) return error(res, 404, 'not_found', 'avatar not found');
+ avatarId = av.id;
+ }
+
// Server-side meta merge. GET strips encrypted_wallet_key /
// encrypted_solana_secret before returning meta, so a client read-modify-write
// must never be able to wipe (or set) them: merge the client's meta over the
@@ -530,7 +545,7 @@ async function handleUpdate(req, res, id, auth) {
UPDATE agent_identities SET
name = COALESCE(${body.name || null}, name),
description = COALESCE(${body.description || null}, description),
- avatar_id = COALESCE(${body.avatar_id || null}, avatar_id),
+ avatar_id = COALESCE(${avatarId}, avatar_id),
skills = COALESCE(${body.skills || null}, skills),
meta = COALESCE(${mergedMeta ? JSON.stringify(mergedMeta) : null}::jsonb, meta),
persona_prompt = COALESCE(${personaPrompt}, persona_prompt),
diff --git a/tests/agents-update-avatar-ownership.test.js b/tests/agents-update-avatar-ownership.test.js
new file mode 100644
index 000000000..989a254d5
--- /dev/null
+++ b/tests/agents-update-avatar-ownership.test.js
@@ -0,0 +1,60 @@
+// PATCH/PUT /api/agents/:id must not let a caller attach an avatar they do not
+// own. handleCreate already scopes avatar_id to the caller; handleUpdate used to
+// COALESCE the raw body value straight into the row, and the public manifest
+// then resolved that avatar's storage key as if it were public.
+
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const calls = [];
+let ownedAvatar = null;
+
+vi.mock('../api/_lib/db.js', () => ({
+ sql: vi.fn(async (strings, ...vals) => {
+ const q = strings.join('?').toLowerCase();
+ calls.push({ q, vals });
+ if (/from agent_identities/.test(q)) return [{ id: 'a1', user_id: 'u1', meta: {} }];
+ if (/from avatars/.test(q)) return ownedAvatar ? [ownedAvatar] : [];
+ if (/update agent_identities/.test(q)) return [{ id: 'a1', user_id: 'u1', meta: {} }];
+ return [];
+ }),
+ isDbUnavailableError: () => false,
+ isDbCapacityError: () => false,
+}));
+
+vi.mock('../api/_lib/http.js', async (orig) => ({
+ ...(await orig()),
+ readJson: vi.fn(async () => ({ avatar_id: '11111111-1111-4111-8111-111111111111' })),
+}));
+
+const { handleUpdate } = await import('../api/agents.js');
+
+function res() {
+ return {
+ statusCode: 0,
+ headers: {},
+ setHeader(k, v) { this.headers[k] = v; },
+ end(b) { this.body = b; },
+ };
+}
+
+describe('agents: handleUpdate avatar ownership', () => {
+ beforeEach(() => {
+ calls.length = 0;
+ ownedAvatar = null;
+ });
+
+ it('rejects an avatar_id the caller does not own and never writes it', async () => {
+ const r = res();
+ await handleUpdate({ headers: {} }, r, 'a1', { userId: 'u1', source: 'session' });
+ expect(r.statusCode).toBe(404);
+ expect(calls.some((c) => /update agent_identities/.test(c.q))).toBe(false);
+ });
+
+ it('accepts an avatar the caller owns', async () => {
+ ownedAvatar = { id: '11111111-1111-4111-8111-111111111111' };
+ const r = res();
+ await handleUpdate({ headers: {} }, r, 'a1', { userId: 'u1', source: 'session' });
+ expect(r.statusCode).toBe(200);
+ expect(calls.some((c) => /update agent_identities/.test(c.q))).toBe(true);
+ });
+});