Repository navigation
Description
Summary
packages/supabase/migrations/336_fix_validate_oauth_scope.sql (landed in #410, "fix(ci): restore REST API Tests + Lint jobs; repair OAuth flows") rewrites core.validate_oauth_scope to fix a real bug — the original (migration 220_oauth_helper_functions.sql) joined core.role_permissions/core.permissions, tables that were never created (team management created core.team_role_permissions instead), so every call errored and all OAuth /authorize requests failed.
The fix removes the broken join and authorizes any requested scope whose rbac_permissions is empty:
IF v_scope_permissions IS NULL OR array_length(v_scope_permissions, 1) IS NULL THEN
v_authorized_scopes := v_authorized_scopes || v_scope_record.scope;
END IF;Per the seed data (core.oauth_scopes, originally 295/224_seed_oauth_scopes.sql), every scope currently in the catalog has an empty rbac_permissions array — including scopes marked is_sensitive = true / requires_consent = true such as write:jobs, write:organizations, delete:applications, delete:documents, write:profile. That means, as shipped, validate_oauth_scope authorizes any catalog scope for any authenticated user — it no longer performs any real per-user RBAC check. The migration's own comment acknowledges this: "a non-empty requirement yields no authorization until a real permission source is wired up," but doesn't flag that the current behavior for every existing scope is now unconditional authorization rather than fail-closed.
Risk: Medium
- No RLS/table-level authorization is bypassed directly by this — actual resource access still depends on downstream RLS/route checks — so this is not a direct data-access bypass by itself.
- However, the OAuth consent/scope system exists specifically to let a user grant a third-party app a limited subset of permissions. With this change, the scope check provides no limiting signal at all: any third-party OAuth client can request (and, once the user clicks through consent) receive a token scoped to any sensitive action in the catalog, regardless of the user's actual role/permissions. If any downstream code trusts "token has scope X" as authorization for action X (rather than re-checking RLS/role independently), that would be a full privilege-escalation path.
- This is a regression in intent (the original design clearly meant to gate sensitive scopes by RBAC) even though the original code never worked in practice.
Recommendation
- Audit downstream consumers of OAuth-scoped tokens (all routes using
authMiddleware/c.get("user")after an OAuth-issued token) to confirm none treat scope membership as a substitute for a real authorization/RLS check. - Populate
core.oauth_scopes.rbac_permissionsfor sensitive scopes (write:*,delete:*, anythingis_sensitive = true) and wirevalidate_oauth_scopeto a real permission source (e.g.core.team_role_permissions) so those scopes fail closed until a user's role actually grants them, rather than being open to every authenticated user. - Until (2) lands, consider treating this as a known/accepted gap only if a compensating control (independent RLS/role check on every OAuth-token-authenticated route) is confirmed to exist.
Where
packages/supabase/migrations/336_fix_validate_oauth_scope.sql- Introduced in PR fix(ci): restore REST API Tests + Lint jobs; repair OAuth flows (#399, #400) #410 (commit
0a2fc68bb) - Related:
packages/supabase/functions/api/routes/oauth.ts(/authorizecallsvalidate_oauth_scope)
Activity
fix(security): enforce OAuth scopes and stop PostgREST filter injection
thinkclay commentedon Jul 30, 2026
Fixed in
ab91e98e7(migration 338, applied to production).An important correction to the report
Production was not rubber-stamping — it was failing closed. The deployed function still carried migration 220's join against
core.role_permissions, so every/authorizecall raised and the endpoint 403'd:Migration 336 was never applied to production. It is recorded nowhere and the function body proves it. So the bypass was real in the repo and would have gone live the moment 336 was applied — but it was never exploitable, which matches 0 tokens ever issued.
Also worth recording: OAuth access tokens cannot authenticate to any resource endpoint.
api/middleware/auth.tsaccepts JWTs and API keys only, and no route checks token scopes. So the grant flow exists without a resource-server half.The fix
Both 220 and 336 failed the same way: treating absence of a declared requirement as an answer. 338 makes absence mean "unclassified, therefore denied" via an explicit
is_self_scopedflag. A newly added scope is denied until someone classifies it.Name enumeration would not have held — there are two live, incompatible vocabularies:
resource:actionaction:resource+admin:*and 224 opens with
DELETE FROM core.oauth_scopes, so a replay swaps the catalog wholesale. Both are classified.documents:*needed gating: every document table is keyed onorganization_id(core.organization_documentsand friends). There is no user-owned document table, so documents are org data, not personal data.Verified
Production and local, across every RBAC state:
memberteam.viewscopesrecruiterjob.manageteam_admindocument.manage,team.settings,team.managehas_team_permission)Migration 336 now carries a header warning that applying it alone reintroduces the bypass.
fix(migrations): make the ledger reflect reality and stop it drifting (…
fix(admin): make the API keys portal and five sibling routes reachable (