From be7d3c5da25ef906ae865c068b4b76a5094633b2 Mon Sep 17 00:00:00 2001 From: Henry Su Date: Fri, 20 Nov 2026 23:59:00 -0600 Subject: [PATCH 1/2] test(oauth): show a revoked grant can still exchange its code DELETE /user/oauth/grants removes the consent and its sessions. An authorization code issued before that call still returns tokens. --- internal/api/oauthserver/handlers_test.go | 77 +++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/internal/api/oauthserver/handlers_test.go b/internal/api/oauthserver/handlers_test.go index 3aef32f543..853dd2d7e0 100644 --- a/internal/api/oauthserver/handlers_test.go +++ b/internal/api/oauthserver/handlers_test.go @@ -7,11 +7,13 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/gofrs/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "github.com/supabase/auth/internal/api/apierrors" "github.com/supabase/auth/internal/api/shared" "github.com/supabase/auth/internal/conf" "github.com/supabase/auth/internal/conf/confload" @@ -711,6 +713,81 @@ func (ts *OAuthClientTestSuite) TestUserRevokeOAuthGrant() { assert.Nil(ts.T(), deletedSession) } +func (ts *OAuthClientTestSuite) exchangeAuthorizationCode(client *models.OAuthServerClient, code string) *httptest.ResponseRecorder { + req := httptest.NewRequest(http.MethodPost, "/oauth/token", nil) + ctx := shared.WithOAuthServerClient(req.Context(), client) + req = req.WithContext(ctx) + w := httptest.NewRecorder() + err := ts.Server.handleAuthorizationCodeGrant(ctx, w, req, &OAuthTokenParams{ + GrantType: GrantTypeAuthorizationCode, + Code: code, + }) + if err != nil { + ts.T().Fatalf("exchange: %v", err) + } + return w +} + +func (ts *OAuthClientTestSuite) TestRevokeOAuthGrantRejectsUnexchangedCode() { + owner := ts.createTestUser("revoke-code-owner@example.com") + otherUser := ts.createTestUser("revoke-code-other@example.com") + client, _ := ts.createTestOAuthClient() + otherClient, _ := ts.createTestOAuthClient() + + ts.createTestConsent(owner.ID.String(), client.ID.String(), []string{"profile"}) + ts.createTestConsent(owner.ID.String(), otherClient.ID.String(), []string{"profile"}) + ts.createTestConsent(otherUser.ID.String(), client.ID.String(), []string{"profile"}) + + // Same client, same user, exchanged before the revoke. Proves this grant can mint tokens. + issued := ts.exchangeAuthorizationCode(client, ts.mintApprovedCode(client.ID, owner.ID)) + assert.Equal(ts.T(), http.StatusOK, issued.Code) + var issuedBody map[string]any + require.NoError(ts.T(), json.Unmarshal(issued.Body.Bytes(), &issuedBody)) + assert.NotEmpty(ts.T(), issuedBody["access_token"]) + assert.NotEmpty(ts.T(), issuedBody["refresh_token"]) + + unused := ts.mintApprovedCode(client.ID, owner.ID) + otherClientCode := ts.mintApprovedCode(otherClient.ID, owner.ID) + otherUserCode := ts.mintApprovedCode(client.ID, otherUser.ID) + + pending := models.NewOAuthServerAuthorization(models.NewOAuthServerAuthorizationParams{ + ClientID: client.ID, + RedirectURI: "https://example.com/callback", + Scope: "profile", + TTL: time.Hour, + }) + require.NoError(ts.T(), models.CreateOAuthServerAuthorization(ts.DB, pending)) + require.NoError(ts.T(), pending.SetUser(ts.DB, owner.ID)) + + req := httptest.NewRequest(http.MethodDelete, "/user/oauth/grants?client_id="+client.ID.String(), nil) + req = req.WithContext(shared.WithUser(req.Context(), owner)) + w := httptest.NewRecorder() + require.NoError(ts.T(), ts.Server.UserRevokeOAuthGrant(w, req)) + assert.Equal(ts.T(), http.StatusNoContent, w.Code) + + exchangeReq := httptest.NewRequest(http.MethodPost, "/oauth/token", nil) + exchangeCtx := shared.WithOAuthServerClient(exchangeReq.Context(), client) + exchangeReq = exchangeReq.WithContext(exchangeCtx) + exchangeW := httptest.NewRecorder() + err := ts.Server.handleAuthorizationCodeGrant(exchangeCtx, exchangeW, exchangeReq, &OAuthTokenParams{ + GrantType: GrantTypeAuthorizationCode, + Code: unused, + }) + var oauthErr *apierrors.OAuthError + require.ErrorAs(ts.T(), err, &oauthErr) + assert.Equal(ts.T(), "invalid_grant", oauthErr.Err) + + stillOtherClient := ts.exchangeAuthorizationCode(otherClient, otherClientCode) + assert.Equal(ts.T(), http.StatusOK, stillOtherClient.Code) + stillOtherUser := ts.exchangeAuthorizationCode(client, otherUserCode) + assert.Equal(ts.T(), http.StatusOK, stillOtherUser.Code) + + kept, err := models.FindOAuthServerAuthorizationByID(ts.DB, pending.AuthorizationID) + require.NoError(ts.T(), err) + assert.Equal(ts.T(), models.OAuthServerAuthorizationPending, kept.Status) + assert.Nil(ts.T(), kept.AuthorizationCode) +} + func (ts *OAuthClientTestSuite) TestUserRevokeOAuthGrantNotFound() { // Create test user user := ts.createTestUser("test4@example.com") From 7ccdf6778561b5dd5a7204259810a34850654ce4 Mon Sep 17 00:00:00 2001 From: Henry Su Date: Fri, 20 Nov 2026 23:59:00 -0600 Subject: [PATCH 2/2] fix(oauth): invalidate unused authorization codes when a grant is revoked The token endpoint redeems the approved authorization row. Delete those rows for the user and client in the revoke transaction before deleting sessions, so a code issued before the revoke cannot mint a new session. --- internal/api/oauthserver/handlers.go | 7 +++++++ internal/models/oauth_authorization.go | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/internal/api/oauthserver/handlers.go b/internal/api/oauthserver/handlers.go index bd93f7801f..e4c4358204 100644 --- a/internal/api/oauthserver/handlers.go +++ b/internal/api/oauthserver/handlers.go @@ -622,6 +622,13 @@ func (s *Server) UserRevokeOAuthGrant(w http.ResponseWriter, r *http.Request) er return terr } + // Drop unexchanged codes before sessions. An exchange that already + // locked one of these rows finishes and inserts its session first; + // the session delete below then removes that session too. + if terr := models.DeleteApprovedOAuthServerAuthorizations(tx, user.ID, clientID); terr != nil { + return terr + } + // Delete all sessions associated with this OAuth client for this user // This will invalidate all refresh tokens for those sessions if terr := models.RevokeOAuthSessions(tx, user.ID, clientID); terr != nil { diff --git a/internal/models/oauth_authorization.go b/internal/models/oauth_authorization.go index 2b7fab2579..0df621e12d 100644 --- a/internal/models/oauth_authorization.go +++ b/internal/models/oauth_authorization.go @@ -313,6 +313,13 @@ func CleanupExpiredOAuthServerAuthorizations(tx *storage.Connection) error { return tx.RawQuery(query, OAuthServerAuthorizationExpired, OAuthServerAuthorizationPending).Exec() } +// DeleteApprovedOAuthServerAuthorizations removes unexchanged authorization +// codes issued to one user for one client. +func DeleteApprovedOAuthServerAuthorizations(tx *storage.Connection, userID, clientID uuid.UUID) error { + query := "DELETE FROM " + (&OAuthServerAuthorization{}).TableName() + " WHERE user_id = ? AND client_id = ? AND status = ?" + return tx.RawQuery(query, userID, clientID, OAuthServerAuthorizationApproved).Exec() +} + // Error types for OAuth authorization operations type OAuthServerAuthorizationNotFoundError struct{}