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(home): redeem an invite in one statement so concurrent claims cannot pass the seat cap

nirholas75788ec4856a
diff --git a/api/_lib/home/members.js b/api/_lib/home/members.js
index 8470a6613..cacd2e969 100644
--- a/api/_lib/home/members.js
+++ b/api/_lib/home/members.js
@@ -526,18 +526,29 @@ export async function acceptInvite({ token, userId }) {
return { ok: true, alreadyMember: true, membership: existing, homeLabel: inspected.homeLabel };
}
- const [claimed, member] = await sql.transaction([
- sql`UPDATE home_invites SET accepted_at = now(), accepted_by = ${userId} WHERE id = ${invite.id} AND accepted_at IS NULL AND revoked_at IS NULL AND expires_at > now() RETURNING id`,
- sql`
+ // One statement, and the membership row is selected FROM the claim. A
+ // two-statement batch ran the INSERT even when the UPDATE matched nothing,
+ // so N concurrent redemptions of one link (or a just-revoked one) all became
+ // members, past the seat cap, while being told the invite was spent.
+ const [row] = await sql`
+ WITH claimed AS (
+ UPDATE home_invites SET accepted_at = now(), accepted_by = ${userId}
+ WHERE id = ${invite.id} AND accepted_at IS NULL AND revoked_at IS NULL AND expires_at > now()
+ RETURNING id
+ ), added AS (
INSERT INTO home_members (home_id, user_id, role, entity_scope, invited_by)
- VALUES (${invite.homeId}, ${userId}, ${invite.role}, ${JSON.stringify(invite.scope)}::jsonb, ${invite.invitedBy})
+ SELECT ${invite.homeId}::uuid, ${userId}::uuid, ${invite.role}::text, ${JSON.stringify(invite.scope)}::jsonb, ${invite.invitedBy}::uuid
+ FROM claimed
ON CONFLICT (home_id, user_id) DO NOTHING
RETURNING home_id, user_id, role, entity_scope, invited_by, created_at, updated_at
- `,
- ]);
-
- if (!claimed?.[0]) return { ok: false, code: 'invite_spent', reason: 'this invitation has already been used' };
- return { ok: true, alreadyMember: false, membership: shapeMembership(member?.[0]), homeLabel: inspected.homeLabel };
+ )
+ SELECT (SELECT id FROM claimed) AS claimed_id, added.*
+ FROM (SELECT 1) AS one LEFT JOIN added ON true
+ `;
+
+ if (!row?.claimed_id) return { ok: false, code: 'invite_spent', reason: 'this invitation has already been used' };
+ const { claimed_id: _claimed, ...member } = row;
+ return { ok: true, alreadyMember: false, membership: shapeMembership(member.home_id ? member : null), homeLabel: inspected.homeLabel };
}
/**
diff --git a/tests/home-security.test.js b/tests/home-security.test.js
index 4f72d6fa1..3d66d09b7 100644
--- a/tests/home-security.test.js
+++ b/tests/home-security.test.js
@@ -386,6 +386,36 @@ describe('home security 3: only a session with CSRF can redeem a confirmation',
expect(writes).toBeGreaterThan(-1);
expect(asked, 'the session check must run before any grant is written').toBeLessThan(writes);
});
+
+ it('an invite admits a member only through the row its own claim matched', () => {
+ // A batch of UPDATE-then-INSERT runs the INSERT even when the UPDATE
+ // matched nothing, so concurrent redemptions of one link all got in.
+ const src = stripComments(read(join(REPO, 'api', '_lib', 'home', 'members.js')));
+ const body = src.slice(src.indexOf('export async function acceptInvite'));
+ const fn = body.slice(0, body.indexOf('\n}\n'));
+ expect(fn).toMatch(/WITH claimed AS \(\s*UPDATE home_invites/);
+ expect(fn).toMatch(/INSERT INTO home_members[\s\S]*FROM claimed/);
+ expect(fn).not.toMatch(/sql\.transaction\(/);
+ });
+
+ it('scoped members read only in-scope rows of the action log, redacted', () => {
+ const code = stripComments(read(join(REPO, 'api', 'home', '[id]', 'log.js')));
+ expect(code).toMatch(/scoped\s*\?\s*page\.filter/);
+ expect(code).toMatch(/confirmed_by:\s*null,\s*detail:\s*null/);
+ });
+
+ it('account-level home routes check the home scope for a bearer', () => {
+ for (const name of ['index.js', 'pair.js', 'plan.js']) {
+ const code = stripComments(read(join(REPO, 'api', 'home', name)));
+ expect(code, `api/home/${name} must gate a bearer on its home scope`).toMatch(/homeScopeRefusal\s*\(/);
+ }
+ });
+
+ it('the household invite link is built from the configured origin, never a request header', () => {
+ const code = stripComments(read(join(REPO, 'api', 'home', '[id]', 'members.js')));
+ expect(code).not.toMatch(/x-forwarded-host/);
+ expect(code).toMatch(/env\.APP_ORIGIN\}\/smart-home\/join/);
+ });
});
// ---------------------------------------------------------------------------