Commit bdbf01e
fix(oauth): prevent authorization-code replay race at /oauth/token (#2612)
# fix(oauth): prevent authorization-code replay race at `/oauth/token`
## What
Fixes an authorization-code replay race (TOCTOU) in the
`authorization_code` grant at `POST /oauth/token`.
## Why
`handleAuthorizationCodeGrant` read the code
(`FindOAuthServerAuthorizationByCode`) and validated
expiry/client/redirect_uri/PKCE **with no transaction or row lock**,
opening a transaction only later to issue tokens and `Destroy` the code.
Two requests with the same code can both clear validation before either
commits; `tx.Destroy`'s `DELETE` matching 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
- Reuse `FindOAuthServerAuthorizationByIDForUpdate` (#2512's `FOR UPDATE
SKIP LOCKED` helper) to lock the row at the top of the issuing
transaction. Concurrent redemptions serialize — the loser skips the
locked row and gets `invalid_grant`. No new model code.
- Propagate `*apierrors.OAuthError` out of the transaction so
`invalid_grant` reaches the client instead of a 500.
## Impact
No schema/API changes. Behavior differs only under concurrent redemption
of the same code.
## Testing
`go build ./...` and `go vet` pass. Reproduction: N concurrent exchanges
of one fresh code behind a start barrier — before, ≥2 responses carried
`access_token`; after, exactly one succeeds and the rest return `400
invalid_grant` (`success <= 1`).
Co-authored-by: Chris Stockton <180184+cstockton@users.noreply.github.com>1 parent 8748000 commit bdbf01e
2 files changed
Lines changed: 75 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
394 | 394 | | |
395 | 395 | | |
396 | 396 | | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
397 | 404 | | |
398 | 405 | | |
399 | 406 | | |
| |||
424 | 431 | | |
425 | 432 | | |
426 | 433 | | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
427 | 437 | | |
428 | 438 | | |
429 | 439 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
0 commit comments