Skip to content

fix(otp): store one-time codes per attempt and verify under a row lock - #645

Open
Bccorb wants to merge 2 commits into
mainfrom
fix/per-attempt-one-time-codes
Open

Bccorb wants to merge 2 commits into
mainfrom
fix/per-attempt-one-time-codes

Conversation

@Bccorb

@Bccorb Bccorb commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Closes #600 (L-API-4, L-API-5, L-API-6).

Every email and phone code for an account lived in one pair of columns on users. Starting a flow for an address replaced the code the owner was waiting on. A recovery resume wrote its code over the owner's open request. And a verification and an account claim, each working from a copy of the user loaded earlier in the request, could act on values the other had just changed.

What changes

  • Codes per attempt. The new one_time_codes table holds each code hashed, keyed by user, channel and the attempt that asked for it: the ephemeral token's attempt, or the access session. A resend replaces only that attempt's code. A code verifies only on its own attempt, and only while the account still holds the address it was sent to. A caller with no attempt or session scope is refused rather than given a shared slot.
  • Verification under a row lock. Deleting the matching code row is the claim on it. The user row is then reloaded FOR UPDATE in the same transaction before the verification is applied (withLockedUser). Magic link and OAuth claims take the same lock. Claiming an unverified account drops the codes other attempts requested before the address was proven.
  • Recovery. startRecovery always opens its own awaiting_code request. A resume records the request it resumes (resumesRequestId) and no longer writes onto the owner's. verifyRecovery reads only awaiting_code requests.
  • A code is spent even when the address check refuses it, so a refused verification fails closed and needs a resend.

Migration

20261204120000-add-one-time-codes adds the table and account_recovery_requests."resumesRequestId". It was run up, down and up against a scratch database. The old code columns on users stay in place and are no longer read, so an instance on the previous release keeps working during a rolling deploy. Two cases cross releases:

  • A code sent by the previous release does not verify on this one. It expires within five minutes, and a new one can be sent.
  • A recovery resume verified on an instance still on the previous release opens a new request instead of resuming, which only restarts the delay.

Contract (minor changeset, breaking)

No route, schema or status code changes. A code now verifies only on the attempt that requested it. The server adapters keep a single ephemeral cookie per browser, so a second sign-in there already replaced the first code, and the adapters see no change. A direct client that starts a new attempt must send a new code on it.

Residual

  • A real account now pays an indexed lookup on a wrong code before answering, which decoy responders do not. A timing gap of this kind already existed, and this widens it slightly.
  • Expired rows are removed on the user's next send, not by a periodic purge.

Tests

  • Postgres (tests/postgres/oneTimeCodes.spec.ts):
    • Codes held per attempt, each verifying only on its own attempt.
    • A resend replaces only its own attempt's code.
    • A claim drops other attempts' codes.
    • The recovery and claim suites were updated.
  • Unit and integration:
    • The scope key, including the refusal without one.
    • The resend race fallback.
    • Destination checks and the locked claim through email code, magic link and OAuth.
    • Recovery resume rows.

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.

fix: low severity session and credential flow hardening (review bundle 1)

1 participant