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(home): stop a spent connect budget from blocking a disconnect
nirholas3f97e2078dd5
diff --git a/api/_lib/rate-limit.js b/api/_lib/rate-limit.jsindex 94951fc90..a78c79e45 100644--- a/api/_lib/rate-limit.js+++ b/api/_lib/rate-limit.js@@ -2110,6 +2110,19 @@ export const limits = {// the same reason as homeAct.homeConnect: (userId) =>getLimiter('home:connect', { limit: 10, window: '10 m' }).limit(userId),+ // revoke: disconnecting a home (DELETE /api/home/:id). Its own bucket, and+ // NOT a share of homeConnect, because sharing one inverts this lane's whole+ // rule that a move toward safety always goes through. Revoking carries no+ // token, is not a credential-stuffing surface, and is how a person cuts our+ // access to their building. Sharing the connect budget meant the moment that+ // budget was spent, which is exactly the moment someone had been fumbling a+ // token or connecting several houses, they were told "too many connection+ // changes, wait a moment" when they tried to disconnect one. Being unable to+ // revoke for ten minutes is the wrong failure to have chosen. Still bounded,+ // because a revoke writes to the database and drops sockets: 30 in 10 minutes+ // is far more disconnecting than anyone does on purpose.+ homeRevoke: (userId) =>+ getLimiter('home:revoke', { limit: 30, window: '10 m' }).limit(userId),// The live stream (api/home/[id]/stream.js). Its own bucket rather than a// share of homeRead: an EventSource that reconnects in a tight loop (a dead// network, a client bug) would otherwise eat a page-load budget it should notdiff --git a/api/home/[id].js b/api/home/[id].jsindex b9569e5a8..c56126639 100644--- a/api/home/[id].js+++ b/api/home/[id].js@@ -87,8 +87,8 @@ async function handleRead(req, res, { caller, home, scope }) {async function handleRevoke(req, res, { caller, home }) {if (!(await requireCsrf(req, res, caller.userId))) return;- const rl = await limits.homeConnect(caller.userId);- if (!rl.success) return rateLimited(res, rl, 'too many connection changes, wait a moment');+ const rl = await limits.homeRevoke(caller.userId);+ if (!rl.success) return rateLimited(res, rl, 'too many disconnects, wait a moment');const result = await revokeConnection(home.id, caller.userId);// The row is gone and the ciphertext is scrubbed, but a socket this instancediff --git a/tests/api-home-contract.test.js b/tests/api-home-contract.test.jsindex 82ce84c16..c1c36ad2d 100644--- a/tests/api-home-contract.test.js+++ b/tests/api-home-contract.test.js@@ -88,7 +88,20 @@ vi.mock('../api/_lib/csrf.js', () => ({ requireCsrf: (...a) => csrfOk(...a) }));const allow = () => ({ success: true, limit: 100, remaining: 99, reset: Date.now() + 60_000 });const deny = () => ({ success: false, limit: 40, remaining: 0, reset: Date.now() + 300_000 });-const buckets = { homeRead: vi.fn(allow), homeAct: vi.fn(allow), homeConnect: vi.fn(allow), homeStream: vi.fn(allow) };+// A Proxy rather than a hand-listed map. This mock going stale is a known way+// to lose a day: the day api/_lib/home/access.js started calling a new export,+// two tests failed with "no such export on the mock" and read as handler bugs.+// Any bucket a route reaches for now materialises as an allowing spy, so adding+// a limiter to a home route can never again fail here as a phantom.+const buckets = new Proxy(+ { homeRead: vi.fn(allow), homeAct: vi.fn(allow), homeConnect: vi.fn(allow), homeRevoke: vi.fn(allow), homeStream: vi.fn(allow) },+ {+ get(target, name) {+ if (typeof name === 'string' && !(name in target)) target[name] = vi.fn(allow);+ return target[name];+ },+ },+);vi.mock('../api/_lib/rate-limit.js', () => ({ limits: buckets, clientIp: () => '127.0.0.1' }));vi.mock('../api/_lib/audit.js', () => ({ logAudit: vi.fn(), logAuditNow: vi.fn() }));diff --git a/tests/home-revoke-limit.test.js b/tests/home-revoke-limit.test.jsnew file mode 100644index 000000000..7a2438c4f--- /dev/null+++ b/tests/home-revoke-limit.test.js@@ -0,0 +1,69 @@+// Disconnecting a home must not be rationed out of the budget that connecting+// one spends (api/home/[id].js + api/_lib/rate-limit.js).+//+// The lane's governing rule is asymmetric on purpose: a move toward safety+// always goes through, a move that opens the house stops and asks. Revoking a+// connection is the safety direction at its strongest, because it is how a+// person cuts our access to their building entirely.+//+// It used to spend `homeConnect`, ten in ten minutes, shared with POST /api/home+// and the relay pairing call. That bucket exists because the connect body+// carries a Home Assistant token and is therefore the credential-stuffing+// surface. Revoking carries no token and can stuff nothing. Sharing meant the+// budget was spent exactly when someone had been fumbling a token or connecting+// several houses, and the answer to "disconnect this house" became "too many+// connection changes, wait a moment" for up to ten minutes.+//+// Found by tests/e2e/home-scene.spec.js, whose disconnect journey returned a+// real 429 from a real server after the suite's earlier connects.+import { test, expect } from 'vitest';++const { limits } = await import('../api/_lib/rate-limit.js');+const revokeSource = (await import('node:fs')).readFileSync('api/home/[id].js', 'utf8');++test('revoking has a bucket of its own, not a share of the connect budget', () => {+ expect(typeof limits.homeRevoke, 'a dedicated revoke limiter must exist').toBe('function');+ expect(limits.homeRevoke).not.toBe(limits.homeConnect);+});++test('the revoke handler spends the revoke bucket and never the connect one', () => {+ const revoke = revokeSource.slice(revokeSource.indexOf('async function handleRevoke'));+ expect(revoke).toContain('limits.homeRevoke(');+ // The connect bucket must not be reachable from the revoke path at all: a+ // second call to it here would re-create the coupling with extra steps.+ expect(revoke).not.toContain('limits.homeConnect(');+});++test('connecting still spends the credential-stuffing bucket', async () => {+ // The fix must not have loosened the surface it was protecting. Both writers+ // that carry a Home Assistant token still meter on homeConnect.+ const fs = await import('node:fs');+ for (const file of ['api/home/index.js', 'api/home/pair.js']) {+ expect(fs.readFileSync(file, 'utf8'), `${file} must still meter on homeConnect`).toContain('limits.homeConnect(');+ }+});++test('the refusal names disconnecting, so the message matches what was refused', () => {+ const revoke = revokeSource.slice(revokeSource.indexOf('async function handleRevoke'));+ expect(revoke).toContain('too many disconnects');+ expect(revoke).not.toContain('too many connection changes');+});++test('a revoke is still bounded, and more generous than a connect', async () => {+ // Not unlimited: a revoke writes to the database and drops sockets. The+ // numbers are read from the module rather than restated, so this pins the+ // relationship (revoke is roomier) and not a literal that would rot.+ const src = (await import('node:fs')).readFileSync('api/_lib/rate-limit.js', 'utf8');+ const read = (bucket) => {+ const m = src.match(new RegExp(`getLimiter\\('${bucket}', \\{ limit: (\\d+), window: '([^']+)'`));+ expect(m, `${bucket} must be a real configured bucket`).toBeTruthy();+ return { limit: Number(m[1]), window: m[2] };+ };+ const revoke = read('home:revoke');+ const connect = read('home:connect');+ expect(revoke.limit).toBeGreaterThan(connect.limit);+ expect(revoke.limit).toBeLessThan(1000);+ // Not `local`: a per-instance counter multiplied by the instance count is the+ // bypass that makes a ceiling a decoration, which is why neither of these is.+ expect(src).toMatch(/getLimiter\('home:revoke', \{ limit: \d+, window: '[^']+' \}\)/);+});