Skip to content

fix(auth): preserve verified passkey when webauthn register() enrollment fails - #2639

Closed
pouyashahrdami wants to merge 1 commit into
supabase:masterfrom
pouyashahrdami:fix/webauthn-register-preserve-verified-factor
Closed

pouyashahrdami wants to merge 1 commit into
supabase:masterfrom
pouyashahrdami:fix/webauthn-register-preserve-verified-factor

Conversation

@pouyashahrdami

@pouyashahrdami pouyashahrdami commented Aug 31, 2026 •

Copy link
Copy Markdown

Fixes #2640

Description

mfa.webauthn.register() runs a cleanup step when its internal enrollment call fails. That cleanup looked up a webauthn factor matching the requested friendly name with status !== 'unverified' — i.e. a verified factor — and unenrolled it:

// packages/core/auth-js/src/lib/webauthn.ts
if (!factor) {
  await this.client.mfa
    .listFactors()
    .then((factors) =>
      factors.data?.all.find(
        (v) =>
          v.factor_type === 'webauthn' &&
          v.friendly_name === friendlyName &&
          v.status !== 'unverified' // ← targets a VERIFIED factor
      )
    )
    .then((factor) => (factor ? this.client.mfa.unenroll({ factorId: factor?.id }) : void 0))
  return { data: null, error: enrollError }
}

Enrollment commonly fails on a friendly-name conflict — precisely the case where a verified factor with that name already exists. So a user who retries registration with an existing name (or whose UI naively retries after a transient error) has their existing, working passkey silently deleted, while the call just returns the original enrollment error.

The sensible cleanup semantics here are the opposite: remove a stale unverified factor left behind by a previous incomplete attempt so a retry can succeed, and never touch a verified factor. This PR flips the condition to status === 'unverified'.

Type of Change

  • Bug fix (fix)
  • New feature (feat)
  • Breaking change
  • Documentation update (docs)
  • Other

Testing

Adds packages/core/auth-js/test/webauthn.register.test.ts covering the failed-enrollment cleanup path:

  • a verified factor is preserved when enrollment fails (the regression),
  • a stale unverified factor is cleaned up so a retry can succeed,
  • a verified factor sharing a name with a stale unverified one is left intact.

All three tests fail against the previous !== 'unverified' behavior and pass with the fix. nx lint auth-js, nx build auth-js, and prettier --check all pass.

Checklist

  • Code formatted (nx format)
  • Tests added and passing
  • Builds passing (nx build auth-js)
  • Used conventional commits

…ent fails

The failed-enrollment cleanup in WebAuthnApi.register() searched for a
webauthn factor with a matching friendly name and status !== 'unverified'
— i.e. a verified factor — and unenrolled it. A registration that failed
on a friendly-name conflict would therefore delete the user's existing,
working passkey.

Target only a stale unverified factor (a leftover from a previous
incomplete attempt) so a retry can succeed, and never touch a verified
factor.

Add regression tests covering: a verified factor is preserved on failure,
a stale unverified factor is cleaned up, and a verified factor sharing a
name with a stale unverified one is left intact.
@pouyashahrdami
pouyashahrdami requested review from a team as code owners August 31, 2026 00:24
@coderabbitai

coderabbitai Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3468ad5e-6e13-4582-8b2a-e4cc84a7df87

📥 Commits

Reviewing files that changed from the base of the PR and between b3b939a and f869c5f.

📒 Files selected for processing (2)
  • packages/core/auth-js/src/lib/webauthn.ts
  • packages/core/auth-js/test/webauthn.register.test.ts

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


📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Fixed failed passkey enrollment cleanup to remove only stale, unverified factors.
    • Prevented verified passkeys from being deleted when enrollment fails.
    • Improved retry behavior after an unsuccessful enrollment attempt.
  • Tests

    • Added coverage for preserving verified factors and removing the correct stale factor.

Walkthrough

The failed WebAuthn enrollment cleanup now targets factors with status === 'unverified' instead of verified factors. New tests mock browser APIs and verify that verified factors remain intact, stale unverified factors are unenrolled by factorId, and same-name verified factors are preserved.

Merge Risk: 🔵 Low · up to f869c

The change preserves verified passkeys and removes only stale unverified factors after failed enrollment. A bounded race remains because factor selection and deletion are separate operations, so concurrent enrollment or verification could remove the wrong factor belonging to the same account; this should remain an explicit owner follow-up.


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

Comment @coderabbitai help to get the list of available commands.

@mandarini

Copy link
Copy Markdown
Contributor

Hi @pouyashahrdami, thank you for contributing to Supabase! 💚

Closing this in favour of #2641, which makes the same one character fix but puts its tests in the existing passkey.methods.test.ts, reusing the browser and client helpers already there instead of standing up a second setup.

Nothing wrong with your diff, it came down to test placement. The report in #2640 was yours and the trace you wrote made this quick to confirm, so credit for the fix is yours either way. Thanks again!

@mandarini mandarini closed this Sep 1, 2026
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: mfa.webauthn.register() deletes an existing verified passkey when a re-registration fails

2 participants