Skip to content

MFA challenge returns 500 when two factors are challenged at the same instant (unique constraint on mfa_factors.last_challenged_at) #2854

Open

Description

@peytonhobson

Bug

mfa_factors.last_challenged_at has a unique constraint (mfa_factors_last_challenged_at_key, from migration 20240802193726_add_mfa_factors_column_last_challenged_at). It holds the time of a factor's latest challenge, so two different users' factors challenged in the same instant collide. The second POST /factors/{id}/challenge fails with a 500:

Unhandled server error: ERROR: duplicate key value violates unique constraint "mfa_factors_last_challenged_at_key" (SQLSTATE 23505)

The client gets 500 Unexpected failure, please check server logs for more information.

Timestamps aren't unique across users, so a unique constraint on this column doesn't seem intended. A plain (non-unique) column or index would fix it.

Reproduce

Version: gotrue:v2.197.0 (local Supabase CLI stack). supabase-js 2.x.

  1. Create 20 users, sign each in, and enroll a TOTP factor for each.
  2. Challenge all 20 factors at once (Promise.all(users.map(u => u.client.auth.mfa.challenge({ factorId: u.factorId })))), repeated 30 times.

Result: 15 of 600 challenges returned the 500 above. Each user challenges only their own factor.

We first hit it in a parallel end-to-end test suite, where two tests reached the authenticator step at the same moment. In production it would hit any two people who start an MFA challenge at the same time.

Where

  • migrations/20240802193726_add_mfa_factors_column_last_challenged_at.up.sql: add column if not exists last_challenged_at timestamptz unique default null
  • internal/models/factor.go: CreateChallenge sets LastChallengedAt = time.Now() and calls tx.UpdateOnly(f, "last_challenged_at")

Activity

  1. mtburgess9 commented on Nov 14, 2026

    @mtburgess9

    Second independent reproduction, on gotrue:v2.197.0 (local Supabase CLI stack). Twelve users, each challenging only their own TOTP factor, all twelve at once, repeated 1,000 times: 52 of 12,000 challenges returned the 500, and the auth log shows 52 mfa_factors_last_challenged_at_key violations and no other error. A second run gave 65 of 12,000. Retrying the challenge after 15 to 60 ms succeeded every time. Worth noting for anyone handling this client-side: challengeAndVerify surfaces it as a failed verification, so an app that maps any failure to "wrong code" tells a user with a correct code that it was wrong. #2860 would fix it.

  2. lars-roe commented on Nov 20, 2026

    @lars-roe

    We see the same constraint on a hosted project, not only on the local CLI stack.

    • Hosted: pg_indexes on a hosted project (eu-central-1) still carries CREATE UNIQUE INDEX mfa_factors_last_challenged_at_key ON auth.mfa_factors USING btree (last_challenged_at) (read on 11 and again on 20 November 2026). So any two users of one project whose TOTP challenges land on the same microsecond collide, and the second sign-in gets a 500. A project cannot fix this itself: the index belongs to the auth schema.
    • Hosted traffic is too low for a rate: since 11 November 2026 our client retries a challenge that Auth answers with a 500, once, and records each retry. From then to 20 November 2026 the hosted project had 10 MFA challenges (auth.mfa_challenges), all verified, and recorded no retry and no failure. That says nothing about the rate either way; we make no claim from it.
    • Local reproduction (gotrue:v2.195.0): a parallel Playwright suite (6 workers) hit it in its set-up 2 times in 36 tests on 16 October 2026, and twice more on 21 October. Auth's log: POST /factors/<id>/challenge -> 500, Unhandled server error: ERROR: duplicate key value violates unique constraint "mfa_factors_last_challenged_at_key" (SQLSTATE 23505). Postgres named the colliding keys (16:27:28.721721, 16:41:30.252527), and both times another account's challenge was created 34-36 µs later.
    • Not reproduced on demand here: 20 users, 30 rounds of concurrent challenges from one Node process, gave 0 collisions in 2 x 600 challenges on 11 November 2026, so the rate depends on timing we could not control. The reproduction above on v2.197.0 (52 and 65 in 12,000) is the stronger one.
    • How it reaches the client: @supabase/auth-js 2.112.4 reports it as AuthRetryableFetchError with status 500 and no code (500 is in NETWORK_ERROR_CODES), so an app cannot tell it from any other 500, and challengeAndVerify cannot say whether the challenge or the verify failed. We now challenge and verify separately and retry the challenge once on a 500.

    +1 for #2860 - nothing in Auth seems to rely on the uniqueness (last_challenged_at is only read per factor for the SMS send-frequency check), and dropping the constraint removes the 500 entirely. Could it be reviewed, and would the migration also reach hosted projects?

  3. holm commented on Nov 24, 2026

    @holm

    We had this error occur to us also. Would appreciate this unique index getting dropped. It doesn't make much sense to me to have it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions