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): scope-check bearer tokens on account-level home routes and room-scoped logs

nirholas72e4286ae8fd
diff --git a/api/_lib/home/access.js b/api/_lib/home/access.js
index 5f12bf21f..5b1ef30fe 100644
--- a/api/_lib/home/access.js
+++ b/api/_lib/home/access.js
@@ -64,6 +64,21 @@ const CAPABILITY_SCOPE = Object.freeze({
disconnect: 'home:act',
});
+/**
+ * The scope refusal for an account-level home route (list, connect, pair, plan)
+ * that has no single home to resolve through `resolveHomeAccess`, or null when
+ * the caller may proceed. Same table, same rule: a session is unscoped, a
+ * bearer needs the scope its capability maps to.
+ *
+ * @param {{ via?: string, scope?: string|null }|null} caller
+ * @param {string} capability
+ */
+export function homeScopeRefusal(caller, capability) {
+ const required = CAPABILITY_SCOPE[capability] || CAPABILITY_SCOPE.act;
+ if (caller?.via !== 'bearer' || hasScope(caller.scope, required)) return null;
+ return { status: 403, code: 'insufficient_scope', message: `${required} required. This token was not granted it.` };
+}
+
/**
* Whether this caller may assert `confirmed: true`, the flag that stands for a
* human saying yes to opening a building.
diff --git a/api/home/[id]/log.js b/api/home/[id]/log.js
index 68688d708..36bbb87ae 100644
--- a/api/home/[id]/log.js
+++ b/api/home/[id]/log.js
@@ -14,7 +14,7 @@
// this whole surface holds; an agent token that can act does not thereby get to
// read the history of everything that ever acted.
-import { resolveHomeAccess } from '../../_lib/home/access.js';
+import { outOfScopeEntities, resolveHomeAccess } from '../../_lib/home/access.js';
import { listHomeActions } from '../../_lib/home/store.js';
import { cors, error, json, method, rateLimited, wrap } from '../../_lib/http.js';
import { limits } from '../../_lib/rate-limit.js';
@@ -28,7 +28,7 @@ export default wrap(async (req, res) => {
const access = await resolveHomeAccess(req, res, req.query?.id, 'read');
if (!access.ok) return error(res, access.status, access.code, access.message);
- const { caller, home } = access;
+ const { caller, home, scope, scoped } = access;
if (caller.via !== 'session') {
return error(res, 403, 'forbidden', 'The action log is readable from a signed-in session only.');
@@ -56,12 +56,27 @@ export default wrap(async (req, res) => {
const hasMore = rows.length > limit;
const page = hasMore ? rows.slice(0, limit) : rows;
+ // A member scoped to some rooms (a guest given the kitchen) sees only the
+ // actions on devices in that scope, and never who confirmed them or why.
+ // The cursor still advances over the unfiltered page so paging terminates.
+ const visible = scoped ? page.filter((row) => inScope(scope, row)).map(redactForScoped) : page;
+
return json(res, 200, {
- actions: page.map(shape),
+ actions: visible.map(shape),
next_before: hasMore ? new Date(page[page.length - 1].created_at).toISOString() : null,
});
});
+/** A row is visible to a scoped member only when it names devices, all of them theirs. */
+function inScope(scope, row) {
+ const ids = row.entity_ids || [];
+ return ids.length > 0 && outOfScopeEntities(scope, ids).length === 0;
+}
+
+function redactForScoped(row) {
+ return { ...row, confirmed_by: null, detail: null };
+}
+
function shape(row) {
return {
id: String(row.id),
diff --git a/api/home/[id]/members.js b/api/home/[id]/members.js
index 019447d77..028726431 100644
--- a/api/home/[id]/members.js
+++ b/api/home/[id]/members.js
@@ -13,6 +13,7 @@
// only one of them is safe to disclose.
import { logAudit } from '../../_lib/audit.js';
+import { env } from '../../_lib/env.js';
import { sendHouseholdInviteEmail } from '../../_lib/email.js';
import { getConnection } from '../../_lib/home/store.js';
import { getSessionUser } from '../../_lib/auth.js';
@@ -46,10 +47,11 @@ import { isUuid } from '../../_lib/validate.js';
* before it asks them to sign in, and sends them through the existing register
* and login flow and back to the same link if they need an account.
*/
-function inviteUrl(req, token) {
- const host = req.headers['x-forwarded-host'] || req.headers.host || 'three.ws';
- const proto = /^localhost|^127\./.test(String(host)) ? 'http' : 'https';
- return `${proto}://${host}/smart-home/join?invite=${encodeURIComponent(token)}`;
+function inviteUrl(token) {
+ // The configured origin, never a request header: `x-forwarded-host` and
+ // `host` are caller-controlled, and an emailed link built from them is a
+ // platform-branded phishing link to whatever host the inviter named.
+ return `${env.APP_ORIGIN}/smart-home/join?invite=${encodeURIComponent(token)}`;
}
/** "an admin" / "a member": role names are user-facing copy, not identifiers. */
@@ -161,7 +163,7 @@ export default wrap(async (req, res) => {
invitedBy: user.id,
});
- const url = inviteUrl(req, invite.token);
+ const url = inviteUrl(invite.token);
// Mail it, and say honestly whether that worked.
//
diff --git a/api/home/index.js b/api/home/index.js
index 0978f18d4..c7b188af7 100644
--- a/api/home/index.js
+++ b/api/home/index.js
@@ -28,7 +28,7 @@ import { normalizeBaseUrl } from '@three-ws/home-bridge';
import { logAudit } from '../_lib/audit.js';
import { requireCsrf } from '../_lib/csrf.js';
-import { publicHome, resolveCaller } from '../_lib/home/access.js';
+import { homeScopeRefusal, publicHome, resolveCaller } from '../_lib/home/access.js';
import { assertWithinLimit, HomeQuotaError, resolveHomeEntitlementsForUser } from '../_lib/home/entitlements.js';
import { homeError, homeFailure, HOME_ERR } from '../_lib/home/errors.js';
import { assertDialableHomeUrl, HomeUrlError } from '../_lib/home-url-guard.js';
@@ -48,6 +48,10 @@ export default wrap(async (req, res) => {
const caller = await resolveCaller(req, res);
if (!caller) return error(res, 401, 'unauthorized', 'Sign in to see your homes.');
+ // No single home to resolve here, so the OAuth scope is checked directly:
+ // listing is home:read, connecting, pairing and pausing are home:act.
+ const refused = homeScopeRefusal(caller, req.method === 'GET' ? 'read' : 'act');
+ if (refused) return error(res, refused.status, refused.code, refused.message);
if (req.method === 'GET') return handleList(req, res, caller);
return handleConnect(req, res, caller);
diff --git a/api/home/pair.js b/api/home/pair.js
index ca9dedfc2..1b338c14a 100644
--- a/api/home/pair.js
+++ b/api/home/pair.js
@@ -15,7 +15,7 @@
// no URL we could ever dial. Those houses dial us. See
// docs/home-relay-threat-model.md for what that connection can and cannot do.
-import { publicHome, resolveCaller } from '../_lib/home/access.js';
+import { homeScopeRefusal, publicHome, resolveCaller } from '../_lib/home/access.js';
import { can } from '../_lib/home/members.js';
import { homeError, homeFailure, HOME_ERR } from '../_lib/home/errors.js';
import {
@@ -40,6 +40,10 @@ export default wrap(async (req, res) => {
const caller = await resolveCaller(req, res);
if (!caller) return error(res, 401, 'unauthorized', 'Sign in to connect a home.');
+ // No single home to resolve here, so the OAuth scope is checked directly:
+ // listing is home:read, connecting, pairing and pausing are home:act.
+ const refused = homeScopeRefusal(caller, req.method === 'GET' ? 'read' : 'act');
+ if (refused) return error(res, refused.status, refused.code, refused.message);
if (!isRelayConfigured()) {
return homeError(
diff --git a/api/home/plan.js b/api/home/plan.js
index 9250aec77..d3c9f5fd9 100644
--- a/api/home/plan.js
+++ b/api/home/plan.js
@@ -23,7 +23,7 @@
import { requireAdmin } from '../_lib/admin.js';
import { requireCsrf } from '../_lib/csrf.js';
-import { resolveCaller } from '../_lib/home/access.js';
+import { homeScopeRefusal, resolveCaller } from '../_lib/home/access.js';
import {
applyDowngrade,
clearAccountOverride,
@@ -46,6 +46,10 @@ export default wrap(async (req, res) => {
const caller = await resolveCaller(req, res);
if (!caller) return error(res, 401, 'unauthorized', 'Sign in to see your home plan.');
+ // No single home to resolve here, so the OAuth scope is checked directly:
+ // listing is home:read, connecting, pairing and pausing are home:act.
+ const refused = homeScopeRefusal(caller, req.method === 'GET' ? 'read' : 'act');
+ if (refused) return error(res, refused.status, refused.code, refused.message);
if (req.method === 'GET') return handleGet(req, res, caller);
return handlePost(req, res, caller);