Files
sentio/migrations/005_platform_admin_scope.sql
T
Jens Nylander 7fd794ac75 tenant isolation: name the cross-tenant scope platform:admin
Wildcard scope was doing two jobs. A new tenant could never get its
first API key: ensure_tenant_match is strict equality and nothing
bypassed it, so only SQL could provision one. And every handler in
routes/tenants.rs was scope-gated only, so any `*` key could read,
rename, suspend or delete another tenant, including a `*` key a tenant
minted for itself.

Cross-tenant reach is now platform:admin. `*` does not cover it, and a
session cannot grant it unless it holds it, so a tenant has no way to
mint itself out of its tenant. Migration 005 gives it to the system
tenant's wildcard keys, which are the ones that already had the reach.
2026-10-17 09:43:50 +02:00

32 lines
1.2 KiB
SQL

-- 005: the `platform:admin` scope
--
-- Cross-tenant reach used to be implied by wildcard scope: a key with `*`
-- could address any tenant. But `*` is also what a tenant hands its own
-- keys, so `admin:api_keys:write` inside one tenant was enough to mint a
-- key that left it. Reach across tenants is now the named scope
-- `platform:admin`, which `*` does not cover.
--
-- Credentials that already had that reach keep it: the wildcard ones
-- belonging to the system tenant, which is the platform operator's own
-- (`config->>'system'`, seeded by 002). Nothing else gains anything.
--
-- Idempotent: the scope is only appended where it is absent.
UPDATE api_keys k
SET scopes = array_append(k.scopes, 'platform:admin')
FROM tenants t
WHERE t.id = k.tenant_id
AND t.config ->> 'system' = 'true'
AND '*' = ANY (k.scopes)
AND NOT ('platform:admin' = ANY (k.scopes));
-- Same for OAuth bearer tokens, which resolve to the same AuthContext.
-- The API has no token endpoint, so any such row was seeded by hand.
UPDATE oauth_tokens o
SET scopes = array_append(o.scopes, 'platform:admin')
FROM tenants t
WHERE t.id = o.tenant_id
AND t.config ->> 'system' = 'true'
AND '*' = ANY (o.scopes)
AND NOT ('platform:admin' = ANY (o.scopes));