Skip to content

security: OAuth scope authorization is a rubber stamp after migration 336 (core.validate_oauth_scope) #428

Closed

Description

@thinkclay

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

  1. 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.
  2. Populate core.oauth_scopes.rbac_permissions for sensitive scopes (write:*, delete:*, anything is_sensitive = true) and wire validate_oauth_scope to 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.
  3. 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

Activity

  1. added
    security
    on Jul 29, 2026
  2. thinkclay commented on Jul 30, 2026

    @thinkclay
    ContributorAuthor

    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 /authorize call raised and the endpoint 403'd:

    ERROR: 42P01: relation "core.role_permissions" does not exist
    CONTEXT: PL/pgSQL function validate_oauth_scope(uuid,text[]) line 10
    

    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.ts accepts 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_scoped flag. A newly added scope is denied until someone classifies it.

    Name enumeration would not have held — there are two live, incompatible vocabularies:

    Seed Style Count Where
    138 resource:action 7 production
    224 action:resource + admin:* 31 full migration replay, local

    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 on organization_id (core.organization_documents and friends). There is no user-owned document table, so documents are org data, not personal data.

    Verified

    Production and local, across every RBAC state:

    Principal Authorized
    no membership, no ownership self-scoped only
    member + team.view scopes
    recruiter + job.manage
    team_admin + document.manage, team.settings, team.manage
    membership revoked back to self-scoped only
    org owner all (owner bypass, mirrors has_team_permission)
    null user / unknown scope names denied

    Migration 336 now carries a header warning that applying it alone reintroduces the bypass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    security

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions