Fix OAuth2 authorization loop when grant scope changes#36900
Open
mango766 wants to merge 1 commit intogo-gitea:mainfrom
Open
Fix OAuth2 authorization loop when grant scope changes#36900mango766 wants to merge 1 commit intogo-gitea:mainfrom
mango766 wants to merge 1 commit intogo-gitea:mainfrom
Conversation
When an OAuth2 client requests a different scope than what was previously granted, the GrantApplicationOAuth handler returned a server_error to the redirect URI. Since the client typically retries the authorization, this caused an infinite redirect loop with no way for the user to recover except by manually deleting the grant from the database. Update the existing grant's scope instead of returning an error, since the user has already explicitly consented to the new scope by clicking "Authorize" on the authorization page. Also update the scope for confidential/trusted clients in the AuthorizeOAuth auto-redirect path so they receive tokens with the correct (current) scope. Fixes go-gitea#36762 Co-Authored-By: Claude (claude-opus-4-6) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
OAuth2Grant.SetScope()method to update the scope of an existing grant in the databaseGrantApplicationOAuth()to update the grant scope instead of returning aserver_errorwhen scopes differAuthorizeOAuth()to sync grant scope for confidential/trusted clients on auto-redirectFixes #36762
Details
When an OAuth2 client changes its requested scope (e.g., from
repositorytoread:repository,write:repository,read:user), and a user has a pre-existing grant with the old scope, theGrantApplicationOAuthhandler previously returned aserver_errorto theredirect_uri. Since the OAuth2 client typically retries the authorization flow upon receiving an error, this created an infinite redirect loop with no way for the user to recover through the UI.This PR updates the grant's scope in the database when the user re-authorizes with a different scope. Since the user has already explicitly consented to the new permissions by clicking "Authorize" on the authorization page, this is safe and expected behavior.
The fix also addresses the related issue for confidential clients and applications with
skip_secondary_authorizationenabled, whereAuthorizeOAuth()auto-redirects using the old grant's scope without updating it.Test plan
oauth2_granttable is updated with the new scope