Skip to content

fix(oauth-server): serialize concurrent authorize/consent with row-level lock - #2512

Merged
fadymak merged 1 commit into
masterfrom
fm/prodsec-103
Apr 28, 2026
Merged

fadymak merged 1 commit into
masterfrom
fm/prodsec-103

Conversation

@fadymak

@fadymak fadymak commented Apr 27, 2026

Copy link
Copy Markdown
Contributor
  • Refactors OAuthServerGetAuthorization and OAuthServerConsent to use a single transaction to serialize claiming pending authorizations via FOR UPDATE SKIP LOCKED
  • Ensures MarkExpired is committed even if the transaction is rolled back
  • Add tests for the OAuthServerGetAuthorization and OAuthServerConsent handlers

@fadymak
fadymak requested a review from a team as a code owner April 27, 2026 07:48
@fadymak
fadymak merged commit c816cfe into master Apr 28, 2026
8 checks passed
@fadymak
fadymak deleted the fm/prodsec-103 branch April 28, 2026 14:33
@cemalkilic cemalkilic mentioned this pull request Apr 30, 2026
cstockton pushed a commit that referenced this pull request Jun 15, 2026
🤖 I have created a release *beep* *boop*
---


##
[2.190.0](v2.189.0...v2.190.0)
(2026-06-11)


### Features

* add -failfast to make test command
([#2553](#2553))
([bec1dd0](bec1dd0))
* **conf:** add JSON config file support
([#2540](#2540))
([e63fef9](e63fef9))
* create tools directory for deterministic builds
([#2522](#2522))
([52cf3d9](52cf3d9))
* **custom-oidc:** support non-standard discovery URLs
([#2573](#2573))
([3dacc64](3dacc64))
* fix the vulncheck-filter to parse the text format instead
([#2525](#2525))
([4136d49](4136d49))
* fork github.com/joho/godotenv into internal/conf/envparse
([#2521](#2521))
([cda62a9](cda62a9))


### Bug Fixes

* Azure issuer validation
([#2560](#2560))
([a39858b](a39858b))
* catch cancelation errors in bg workers & servers
([#2530](#2530))
([77f5918](77f5918))
* check session state for admin tokens
([#2555](#2555))
([c5969ed](c5969ed))
* **config:** warn on invalid WebAuthn config instead of erroring
([#2545](#2545))
([ca0b154](ca0b154))
* **custom-oidc:** strip trailing slashes from issuer
([#2570](#2570))
([169ad67](169ad67))
* **Dockerfile:** ensure forks exists on fs before make deps
([#2567](#2567))
([01f136d](01f136d))
* **mailer:** include SiteURL in notification template data
([#2532](#2532))
([dc015da](dc015da))
* **oauth-server:** serialize concurrent authorize/consent with
row-level lock ([#2512](#2512))
([c816cfe](c816cfe))
* **passkeys:** delete webauthn creds when user is soft-deleted
([#2564](#2564))
([7e1c060](7e1c060))
* **passkeys:** enforce AAL checks on passkey registration and deletion
([#2565](#2565))
([7e6f2e4](7e6f2e4))
* source WebAuthn RP config from env vars
([#2490](#2490))
([63949ca](63949ca))
* when version is empty set to 0.0.0
([#2531](#2531))
([a3b7c8c](a3b7c8c))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: supabase-releaser[bot] <223506987+supabase-releaser[bot]@users.noreply.github.com>
cstockton added a commit that referenced this pull request Jul 14, 2026
…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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants