fix(oauth): prevent authorization-code replay race at /oauth/token#2612
Open
reactima wants to merge 1 commit into
Open
fix(oauth): prevent authorization-code replay race at /oauth/token#2612reactima wants to merge 1 commit into
reactima wants to merge 1 commit into
Conversation
cf86132 to
adb8c4f
Compare
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.
fix(oauth): prevent authorization-code replay race at
/oauth/tokenWhat
Fixes an authorization-code replay race (TOCTOU) in the
authorization_codegrant atPOST /oauth/token.Why
handleAuthorizationCodeGrantread the code (FindOAuthServerAuthorizationByCode) and validated expiry/client/redirect_uri/PKCE with no transaction or row lock, opening a transaction only later to issue tokens andDestroythe code. Two requests with the same code can both clear validation before either commits;tx.Destroy'sDELETEmatching 0 rows still commits under READ COMMITTED — so both mint a full token set for one single-use code, violating RFC 6749 §4.1.2 / PKCE.The authorize/consent path was hardened in #2512; token-exchange never got the equivalent locked read-modify-write.
Change
FindOAuthServerAuthorizationByIDForUpdate(fix(oauth-server): serialize concurrent authorize/consent with row-level lock #2512'sFOR UPDATE SKIP LOCKEDhelper) to lock the row at the top of the issuing transaction. Concurrent redemptions serialize — the loser skips the locked row and getsinvalid_grant. No new model code.*apierrors.OAuthErrorout of the transaction soinvalid_grantreaches the client instead of a 500.Impact
No schema/API changes. Behavior differs only under concurrent redemption of the same code.
Testing
go build ./...andgo vetpass. Reproduction: N concurrent exchanges of one fresh code behind a start barrier — before, ≥2 responses carriedaccess_token; after, exactly one succeeds and the rest return400 invalid_grant(success <= 1).