Repository navigation
feat(auth): store PKCE verifiers per flow to survive overlapping flows - #1622
Closed
itsybitsci wants to merge 2 commits into
Closed
itsybitsci wants to merge 2 commits into
itsybitsci wants to merge 2 commits into
Conversation
Store each PKCE code verifier in its own slot keyed by a generated flow id, bounded to five pending flows through an ordered index, instead of one shared key that a second flow overwrites. sign_in_with_oauth and link_identity return the flow_id, and exchange_code_for_session accepts it to select the matching verifier; a missing slot fails fast rather than spending the single-use code on another flow's verifier. The legacy key is still written and used when no flow id is given, and sign_out clears every pending slot. Closes supabase#1560
This was referenced Nov 2, 2026
spydon
pushed a commit
that referenced
this pull request
Nov 3, 2026
## Why Closes #1905. Apps that wait for `PASSWORD_RECOVERY` miss successful PKCE password-reset callbacks. Code exchange strips the stored recovery marker and always emits `SIGNED_IN`. ## What changed - Keep the recovery context until a successful exchange selects its auth event. - Preserve `SIGNED_IN` for ordinary exchanges and explicit caller-provided verifiers. - Add public API regression tests for recovery, sign-in, explicit verifier precedence, and failed exchanges. Regenerate sync code and tests from async. ## Scope This covers PKCE completion in both Auth clients. Concurrent verifier storage in #1622 remains separate. ## Blast radius Event selection changes only when the exchange uses a stored recovery verifier. Tests check the token request, returned and persisted session, verifier cleanup, and failed-exchange behavior. ## Verification - `uv run --frozen --package supabase_auth pytest src/auth/tests/_async/test_pkce_exchange.py src/auth/tests/_sync/test_pkce_exchange.py -q` produced `2 failed, 6 passed` before the fix and `8 passed` after it. Reverting each implementation separately produces `1 failed, 7 passed`. - The broader run under `src/auth/tests` passed 103 tests, excluding the service-dependent `test_gotrue.py`, `test_gotrue_admin_api.py`, and `test_utils.py` modules in both client directories. - `make auth.build-sync`, `make ruff`, `make auth.mypy`, and `git diff --check` passed. The dedicated Auth integration services on ports 9996 through 9999 were unavailable locally, so `make auth.tests` was not run. The regression uses HTTPX MockTransport and does not test live email delivery. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit - **Bug Fixes** - Password recovery exchanges now notify listeners with the password recovery event, while other exchanges continue to signal sign-in. - Failed recovery exchanges preserve the verification data so the attempt can be retried. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Contributor
|
We will handle this in v3, but we won't merge this into v2 since it's a pretty big change and we're swapping main and the v3 branch very soon (and putting v2 into maintenance mode). |
Contributor
|
Thanks for your contribution though! |
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?
Feature (SDK parity). Closes #1560.
What is the current behavior?
supabase_authkeeps a single PKCE code verifier at{storage_key}-code-verifier. Starting a second PKCE flow while one is pending (two OAuth sign-ins, or an OAuth sign-in while a link-identity flow is pending) overwrites the first verifier, so whichever flow completes second fails atexchange_code_for_session.What is the new behavior?
Ports the per-flow verifier storage that already shipped in supabase-js (supabase-js#2569), Dart (supabase-flutter#1662) and Swift (supabase-swift#1245), using the same key shapes and semantics:
flow_id(32 hex chars) and its verifier is stored at{storage_key}-flow-{flow_id}-code-verifier.{storage_key}-flows-code-verifier(oldest first) bounds the ring toPKCE_MAX_CONCURRENT_FLOWS = 5; the oldest pending flow is evicted when a sixth starts. Storage backends cannot enumerate keys, hence the explicit index.OAuthResponse.flow_idis returned bysign_in_with_oauthandlink_identity(Noneon the implicit flow).exchange_code_for_sessionacceptsflow_idinCodeExchangeParams. With a flow id only that flow's verifier is used; a miss raises the newAuthPKCECodeVerifierMissingErrorbefore any request is made, rather than spending the single-use auth code on another flow's verifier. Without a flow id the legacy key is used, so existing callers keep working.{storage_key}-code-verifierkey is still written with the newest verifier and is cleared alongside a slot only when it holds that slot's value, so finishing one flow never destroys a newer flow's fallback.sign_outclears every pending slot, the index and the legacy key.^[a-zA-Z0-9_-]{8,64}$) both when supplied by callers and when read back from storage, since they are embedded in storage keys.Scope deliberately matches the Dart and Swift ports rather than the seven methods listed in the auto-generated issue text:
sign_in_with_otp,sign_in_with_sso,resend,update_userandreset_password_for_emaildo not send a PKCE challenge in this SDK today, so there is no verifier for them to keep. The opt-insb_flow_idredirect parameter from supabase-js is also left out; a server-side caller holds theflow_idand passes it explicitly. Both can be follow-ups if wanted.Usage
Additional context
supabase_auth/_async/pkce_verifier_store.py(sync twin generated byrun-unasync.py, which gains anasynciotothreadingreplacement for the lock).CodeExchangeParams.code_verifierandredirect_tobecomeNotRequired, matching how the code already reads them with.get().sdk-compliance.yamldeclaresauth.sign_in.concurrent_pkce_flows.flow_idwith the right verifier and slot-only cleanup, fail-fast on unknown or malformed ids with no request sent, legacy-key fallback, andsign_outclearing all slots. The two existing tests that hardcoded the old two-tuple return of_get_url_for_providerare updated.make auth.tests(mypy + pytest against the docker infra) andmake ruffare clean.