Skip to content

fix(auth): restore prior PKCE verifier after failed flow start - #2865

Open
umeraamir69 wants to merge 1 commit into
supabase:masterfrom
umeraamir69:fix/auth-pkce-failed-start-preserves-prior-verifier
Open

umeraamir69 wants to merge 1 commit into
supabase:masterfrom
umeraamir69:fix/auth-pkce-failed-start-preserves-prior-verifier

Conversation

@umeraamir69

Copy link
Copy Markdown

Summary

  • Failed PKCE starts (e.g. rate-limited signInWithOtp) dual-write a new verifier then call removePKCEVerifier, which previously deleted the legacy <storageKey>-code-verifier key even when an earlier live flow still had a usable verifier in its slot.
  • Default callbacks without a flow id only read that legacy key, so the earlier magic link then failed with AuthPKCECodeVerifierMissingError.
  • On remove, when the legacy key belonged to the removed flow, restore it from the most recently started remaining flow instead of deleting it blindly. Only clear the legacy key when no other pending flow remains.

Fixes #2863

Test plan

  • pnpm nx test auth-js -- test/helpers.test.ts test/GoTrueClient.pkce.test.ts --runInBand
  • New unit test: removePKCEVerifier restores legacy key from prior live flow
  • New integration-style synthetic-fetch test: rate-limited second OTP keeps first verifier and exchangeCodeForSession succeeds without a flow id
  • Manual: flowType: 'pkce', send magic link, immediately resend within rate limit, open first email link in same browser

When a PKCE start fails after dual-writing the legacy key, clear only
the failed flow slot and restore the previous live verifier so default
callbacks without a flow id can still exchange the earlier valid link.

Fixes supabase#2863
@umeraamir69
umeraamir69 requested review from a team as code owners November 16, 2026 06:37
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of email sign-in when a new magic-link request is rate-limited. Existing sign-in attempts can still complete successfully.
    • Preserved the correct verification state when the most recent sign-in attempt is removed, so an earlier active attempt remains usable.

When a flow is removed, removePKCEVerifier now restores the legacy verifier key from the most recently started remaining flow if the removed flow held that key. It deletes the legacy key if no remaining flow has a string verifier. Tests cover removing the newer of two flows and rate-limiting a second magic-link request before exchanging the first link’s code.

Assessment against linked issues

Objective Addressed Explanation
Preserve the earlier PKCE verifier when a later flow start fails, so the earlier link can be exchanged. [#2863] ✅

Possibly related PRs

  • supabase/supabase-js#2864: Addresses the same verifier cleanup issue with an overlapping approach to restoring the legacy key.

Suggested labels: auth-js

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 5407c

This fix keeps an earlier email link usable after a failed or rate-limited PKCE flow start. A rare race remains with asynchronous storage adapters when two flows start at nearly the same moment. It already existed before this change and a retry resolves it. No merge-blocking risk remains.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8422e73b-7f09-49f8-accc-c6ef66d9387d
Commits

Reviewing files that changed from the base of the PR and between 13211d9 and 5407c30.

Files selected for processing: 3

  • packages/core/auth-js/src/lib/helpers.ts
  • packages/core/auth-js/test/GoTrueClient.pkce.test.ts
  • packages/core/auth-js/test/helpers.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

This branch has not been deployed

No deployments
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.

auth-js: a failed PKCE flow start (e.g. rate-limited magic link) breaks the previous, still-valid link

1 participant