feat(auth): store PKCE verifiers per flow to survive overlapping flows - #1622
Open
itsybitsci wants to merge 1 commit into
Open
itsybitsci wants to merge 1 commit into
itsybitsci wants to merge 1 commit 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 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.