Skip to content

fix(oauth): restrict OAuth client access tokens on account-management… - #2878

Closed
Denx00 wants to merge 1 commit into
supabase:masterfrom
Denx00:fix/oauth-scope-limit
Closed

Denx00 wants to merge 1 commit into
supabase:masterfrom
Denx00:fix/oauth-scope-limit

Conversation

@Denx00

@Denx00 Denx00 commented Oct 18, 2026 •

Copy link
Copy Markdown

Fixes #2877

Summary of Fix

Delegated third-party OAuth 2.1 client tokens (such as MCP connectors) should only have authorization according to their consented scopes and should not possess account-management privileges. I have addressed Issue #2877 by restricting OAuth client access tokens from modifying credentials, factors, or account state, while scope-limiting user profile reads.

Changes Made

  1. OAuth Client Detection (Session.IsOAuth()):

    • Added s.IsOAuth() on *models.Session to check if session.OAuthClientID is set.
    • Added isOAuthClientRequest(ctx) in internal/api/auth.go checking both JWT claims (client_id) and session data.
  2. Route Middleware (requireNotOAuthClient):

    • Rejects requests originating from third-party OAuth client sessions with 403 Forbidden (no_authorization).
    • Mounted across sensitive routes in internal/api/api.go:
      • PUT /user
      • /factors/* (enrollment, challenge, verify, unenroll, recovery codes)
      • /passkeys/* (registration options & verify, delete, patch)
      • /user/identities/* (link, unlink)
      • /user/oauth/grants (grant management)
      • /oauth/authorizations/* (authorization view and consent)
      • /reauthenticate
      • /logout
  3. Defense-in-Depth Handler Checks:

    • In addition to router middleware, added direct safeguards inside handlers (UserUpdate, EnrollFactor, requirePasskeyManagementAAL, DeleteIdentity, LinkIdentity, Reauthenticate, Logout, OAuthServerConsent, UserListOAuthGrants, UserRevokeOAuthGrant).
  4. Scope-Limiting on GET /auth/v1/user:

    • For applications using @supabase/supabase-js with OAuth tokens (getUser()), GET /auth/v1/user now runs through sanitizeUserForOAuth():
      • Internal security fields (app_metadata, identities, factors) are always stripped.
      • email, phone, and user_metadata are only included if the respective scopes (email, phone, profile) were explicitly consented.
  5. Unit Tests:

    • Added unit test suites in internal/api/oauth_client_restriction_test.go and internal/api/oauthserver/oauth_client_restriction_test.go verifying that OAuth client tokens are blocked from account modification and correctly scope-limited on GET /user.

@Denx00

Denx00 commented Oct 25, 2026

Copy link
Copy Markdown
Author

Hi maintainers, thanks for the work on this project. This is my first contribution here, so the CI workflows on this PR are sitting at action_required. Could someone approve the workflow run when you have a moment? I'm happy to address any feedback, and to split the change into smaller PRs if that's easier to review (it's about 650 lines, mostly tests).

@cemalkilic

Copy link
Copy Markdown
Contributor

Hey @Denx00, thanks for reporting #2877 and for the PR, really appreciate it!

We talked this through internally and ended up going a slightly different direction in #2898. It denies OAuth client tokens by default and only allows them on /oauth/userinfo, so GET /user gets blocked entirely instead of sanitized. We also want to get this fix into the next release, so I’m closing this one in favor of that to move faster. It’s mostly a timing call.

Thanks again 🙏

@cemalkilic cemalkilic closed this Nov 3, 2026
@Denx00

Denx00 commented Nov 4, 2026 •

Copy link
Copy Markdown
Author

Thanks @cemalkilic, that makes sense, and the deny-by-default approach in #2898 is cleaner. Glad it's fixed.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OAuth 2.1 server: OAuth client access tokens can call /user and /factors (modify user, enroll MFA)

2 participants