Skip to content

fix(auth): keep the pending PKCE verifier when a later flow start fails - #2864

Open
hsusul wants to merge 1 commit into
supabase:masterfrom
hsusul:fix/pkce-keep-pending-verifier
Open

hsusul wants to merge 1 commit into
supabase:masterfrom
hsusul:fix/pkce-keep-pending-verifier

Conversation

@hsusul

@hsusul hsusul commented Nov 15, 2026

Copy link
Copy Markdown

🔍 Description

What changed?

removePKCEVerifier() in packages/core/auth-js/src/lib/helpers.ts used to delete the legacy <storageKey>-code-verifier key whenever it held the removed flow's verifier. Now it points that key at the newest flow still in the slot index, and deletes it only when no other flow is left. Nothing else changes. Slots are never cleared for other flows, and the no-flow-id path still touches only the legacy key.

Tests:

  • test/helpers.test.ts: removing the newest of three flows leaves the legacy key holding the previous flow's verifier.
  • test/GoTrueClient.pkce.test.ts: end-to-end with the existing synthetic fetch. The first signInWithOtp succeeds and the second gets a 429. exchangeCodeForSession(code) must still send the first flow's verifier. The harness gains a rateLimitedRequests option.

Why was this change needed?

Every PKCE flow start (signInWithOtp, resend, resetPasswordForEmail, signUp, updateUser, signInWithSSO) writes the legacy key and calls removePKCEVerifier(flowId) when its request fails. So a failed start, such as a rate-limited second magic-link request, deleted the legacy key. The earlier link that is still valid then failed with AuthPKCECodeVerifierMissingError, because callbacks without a flow id only read the legacy key. The earlier verifier was still in its slot (#2569) and just wasn't reachable.

Closes #2863

📸 Screenshots/Examples

await supabase.auth.signInWithOtp({ email })   // link #1 sent
await supabase.auth.signInWithOtp({ email })   // 429 over_email_send_rate_limit, no link sent
// open link #1 in the same browser
// before: AuthPKCECodeVerifierMissingError
// after:  session created

🔄 Breaking changes

  • This PR contains no breaking changes

📋 Checklist

  • I have read the Contributing Guidelines
  • My PR title follows the conventional commit format: <type>(<scope>): <description>
  • I have run pnpm nx format to ensure consistent code formatting
  • I have added tests for new functionality (if applicable)
  • I have updated documentation (if applicable)

📝 Additional notes

Verification (Node v24.9.0, no Docker):

  • cd packages/core/auth-js && npx jest test/GoTrueClient.pkce.test.ts test/helpers.test.ts --coverage=false
    • Without the fix: 2 failed, 61 passed. The new end-to-end test made no token request, and the helper test got null instead of "verifier-b".
    • With the fix: 63 passed.
  • npx jest --coverage=false (all auth-js suites): 299 passed, 1 failed. On master it's 297 passed, 1 failed. The failure, plus the suites that don't run, need the Docker auth server (fetch failed) and are the same on both.
  • pnpm nx build auth-js: passes.
  • npx prettier --check on the changed files: passes. npx commitlint --from HEAD~1: passes.
  • pnpm nx lint auth-js reports errors that already exist on master (no-undef in jest.config*.js, unused catch vars and a missing no-extra-semi rule definition in helpers.ts). None are on the changed lines.

Behaviour notes for review:

  • In experimental appendPkceFlowIdToRedirects mode, a successful exchange of the newest flow now leaves the legacy key on the previous pending flow instead of clearing it. That matches what the key is meant to do: mirror the most recent pending flow.
  • In the default mode, a successful exchange still removes only the legacy key, and its slot stays until ring eviction or sign-out (existing behaviour). So in rare cases the restored legacy value can belong to a flow that already finished. That's the same state as an abandoned flow today. Clearing the matching slot on legacy-key exchanges could be a follow-up; I left it out to keep this change minimal and the existing "no flow id only touches the legacy key" contract intact.

When a PKCE flow start request fails (for example a magic link request
that is rate limited), its cleanup deleted the legacy code verifier key
because that key mirrored the failed flow. The verifier of the earlier,
still valid flow stayed in its slot, but callbacks without a flow id only
read the legacy key, so the earlier link failed with "PKCE code verifier
not found in storage".

Point the legacy key back at the newest flow still in the slot index
instead of deleting it.
@hsusul
hsusul requested review from a team as code owners November 15, 2026 22:43
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where a rate-limited magic-link request could prevent an earlier pending magic link from completing sign-in.
    • When multiple magic-link requests are pending, sign-in now correctly falls back to an earlier valid request if a newer request is rate limited. This helps ensure that a link already sent can still be used to finish signing in, even if a subsequent request cannot be completed.

When a PKCE flow is removed and its verifier matches the legacy verifier, the helper now restores the legacy key from the newest remaining indexed flow with a string verifier. Tests cover removing the newest flow and handling a rate-limited second magic-link request before exchanging the first link's callback without a flow ID.

Assessment against linked issues

Objective Addressed Explanation
Preserve the earlier PKCE verifier when a later flow start fails [#2863] ✅

Suggested labels: auth-js

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 237d4

This change keeps an earlier email link usable when a later PKCE flow start fails, such as after a rate limit. The change is narrow and has tests for the reported scenario, so merge risk is minimal.

- [ ] 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: 88004e6d-5464-465d-b1c5-f988856d3619
Commits

Reviewing files that changed from the base of the PR and between 13211d9 and 237d474.

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