fix(oauthserver): consume authorization code under a row lock so it stays single-use#2605
Open
kanywst wants to merge 1 commit into
Open
fix(oauthserver): consume authorization code under a row lock so it stays single-use#2605kanywst wants to merge 1 commit into
kanywst wants to merge 1 commit into
Conversation
…tays single-use The authorization_code grant looks up the code outside the transaction and without a lock, then issues the token and deletes the row inside the transaction, so concurrent requests with the same code each mint a token. Re-acquire and consume the code under FOR UPDATE SKIP LOCKED before issuing, matching the consent path.
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.
What kind of change does this PR introduce?
Bug fix
summary
The
authorization_codegrant can return more than one token for a single code when requests race, so the code is not single-use (RFC 6749 §4.1.2).handleAuthorizationCodeGrantlooks up the code outside the transaction and without a lock (handlers.go:336), then issues the token and deletes the row inside the transaction (:409,:416), so concurrent requests with the same code all pass the lookup and PKCE check before any of them deletes it. The consent path already locks withFindOAuthServerAuthorizationByIDForUpdate(FOR UPDATE SKIP LOCKED); the token path does not.Fix: add
FindOAuthServerAuthorizationByCodeForUpdateand consume the code under that lock inside the transaction, matching the consent path. The loser sees no row and getsinvalid_grant.Severity is low: PKCE is required here, so a stolen code is not redeemable without the verifier; the effect is the lost single-use guarantee under concurrency.
Test plan:
TestAuthorizationCodeSingleUseUnderConcurrency(needs the dev postgres,make docker-test) fires 8 concurrentPOST /oauth/tokenwith the same code and asserts one token comes back. Before the fix all 8 succeed (got 8); after, one succeeds and the rest getinvalid_grant.