Skip to content

fix(auth): end a sign-in waiting at the code step with the password, and refuse a password step a change superseded (#923) - #925

Merged
NonoHM merged 4 commits into
mainfrom
fix/923-pending-mfa-survives-password-change
Oct 7, 2026
Merged

NonoHM merged 4 commits into
mainfrom
fix/923-pending-mfa-survives-password-change

Conversation

@NonoHM

@NonoHM NonoHM commented Oct 7, 2026

Copy link
Copy Markdown
Owner

Closes #923

A sign-in that has passed its password step and waits at the code step now ends with the password. The same holds one step earlier: a password step still comparing when the password changes is refused.

What changes, before and after

Moment Before After
The owner changes the password (Settings, the forced change, an admin reset) while someone waits at the code step with the old password That sign-in completes with a valid code and gets a session nobody revoked The challenge is deleted in the change's transaction; the code is sent back to the password step
« Log out other sessions » while a sign-in waits at the code step Same Same as above
The change commits while the code is being checked The session is written anyway The challenge is claimed in the session's own transaction; a challenge ended meanwhile matches nothing and the sign-in is refused
The change commits while /login is still comparing the OLD password (a bcrypt comparison, hundreds of ms) The challenge, or without two-factor the session, is written afterwards and nothing ends it Written only after a compare-and-set on the User row: a change committed first refuses the step with the ordinary wrong-password sentence; one committing after waits on that row and ends what the step wrote

In CIA terms: confidentiality and integrity of the account after the owner reacts to a suspected compromise. The control a user reaches for (change the password) no longer leaves a sign-in in flight that survives it. Availability is unchanged: a refused step reads exactly like a wrong password or an expired challenge.

The last row was not in the issue. The contradiction pass on the design note found it before any code was written, and it is the one an attacker can reach without luck: submitting the password in a loop keeps a comparison in flight, so one of them straddles the owner's change. It applies without two-factor too.

Mechanism, no schema change:

  • endPendingSignIns(tx, userId) deletes the account's challenges. revokeSessionsOtherThan runs it first (Settings changePassword, revokeOtherSessions, /force-password-change), and /admin resetPassword runs it before deleting the sessions.
  • claimMfaChallenge(tx, id) deletes the challenge only if it is still there and unexpired, and throws SignInSuperseded otherwise. createSession(userId, cookies, stillProven) runs it in the session's transaction, retried whole on a transient write conflict.
  • passwordStillCurrent(tx, userId, verifiedHash) is an UPDATE "User" ... WHERE id = ? AND passwordHash = ? writing the value already there. It is checked by its count, and it locks the row a password change writes. The statement was read from MariaDB's general log; the driver counts matched rows (foundRows, its default).

What this does NOT do

  • « Log out other sessions » does not end a password step still comparing when it commits. It changes nothing that step reads, so on PostgreSQL a challenge written while it commits outlives it. Whoever holds that challenge holds the current password and the second factor, and could sign in again anyway; only a password change removes them. Recorded in endPendingSignIns's docstring.
  • Disabling or re-enrolling two-factor ends no pending sign-in, as it ends no session today. Disabling it already sends a waiting sign-in back to /login without a session.
  • The sign-in limiter is unchanged. A refused password step is counted as a failed attempt, as a wrong password is.

The measurement

src/lib/server/auth/pendingSignIn.db-smoke.ts drives the real /login, /login/verify-totp, /settings, /force-password-change and /admin actions. Nothing in it writes or deletes a challenge itself. Three gates are used, and each test says which: after the bcrypt comparison, between the accepted code and the session, and inside the code step's transaction after the claim.

The figure is whether a valid code submitted after the change signs in. The calibration is the first test: with nothing in between, the same two steps sign in.

On origin/main (d1a8bc1), the calibration passed everywhere. The 6 tests of the first commit were red on all three engines. The 4 password-step tests of the second commit were run red on SQLite only. These are the unforced races (8 accounts each, staggered across one reset's measured duration, with the resets started at once), live sessions afterwards:

Race SQLite PostgreSQL MariaDB
code step against a reset 5 6 8
password step, two-factor on 2 not run not run
password step, two-factor off 2 not run not run

With the fix, every race prints 0 live on all three engines. The races also print how many sign-ins had completed before their reset landed, so that "whatever the order" is a measurement: 1 to 3 of 8 per race and engine. A first MariaDB run had 0 in the code-step race, which proves nothing about the other order. The resets now start one reset-duration late; the reruns had 2, 2 and 1.

Re-run: DATABASE_URL=<empty throwaway db> npx vitest run --config vitest.db.config.ts src/lib/server/auth/pendingSignIn.db-smoke.ts --reporter=verbose, after npx prisma migrate deploy.

Breaks

Each break ran in the foreground, was restored in a finally and was checked by sha256. The runner was calibrated first, reading 12 of 12 green on the unbroken tree on SQLite and PostgreSQL.

Break Engine Red The two states it separates
B1 endPendingSignIns dropped from revokeSessionsOtherThan SQLite changePassword, revokeOtherSessions, forcePasswordChange the challenge outlives the change / it ends with it
B2 dropped from the admin reset SQLite resetPassword, the forced code-step gate, the code-step race same, for the reset
B3 the claim ignores its count SQLite the forced gate between the accepted code and the session challenge read at the start and trusted / claimed when the session is written
B4 the compare-and-set compares nothing SQLite both forced password-step gates, both password-step races the step writes what it verified / only while it still holds
B5 the compare-and-set dropped from the challenge write SQLite forced password-step gate and race, two-factor on same, the two-factor half alone
B6 dropped from the session write without two-factor SQLite forced password-step gate and race, two-factor off same, the other half alone
B7 the claim committed outside the session's transaction PostgreSQL the forced gate after the claim claim and session commit together / a change slips between them
B8 sessions revoked before the challenges end PostgreSQL the forced gate after the claim the change waits for a claimed sign-in, then ends its session / it finishes first and misses it

B7 and B8 were green at first, because no test opened the window they concern. The test « is ended by a change starting after the challenge is claimed » was added for them, and each then turned exactly that one test red. Its gate holds the code step's transaction open while a forced password change starts. So it proves the order and the atomicity are checked; it does not prove that window is reachable without a gate. The unforced races are the reachability figure.

Gates, as read

Each figure was read from its own log in one sequential run after the last code change (commit 191ee01; the later commit is docs only).

Gate Read
npm run check 0 errors, 0 warnings over 4115 files
npm run lint:tracked prettier read 1340 tracked files, all formatted; eslint read 932, exit 0
npm run test:unit -- --run 397 files, 6208 passed, 0 failed
npm run build exit 0
npm run test:db, SQLite, empty database 58 files, 470 passed, 1 skipped (crossProvider, not touched here)
npm run test:db, PostgreSQL 17, empty database 58 files, 470 passed, 1 skipped
npm run test:db, MariaDB 11, empty database 58 files, 470 passed, 1 skipped
npm run test:e2e, full suite 282 passed, 0 failed, 0 flaky

The 58 files are main's 57 plus pendingSignIn.db-smoke.ts. The new suite also ran three times on MariaDB with the final test set: 12 of 12 each time. CI's docker-smoke is the image check and has not run yet.

Walk

The build was served on its own port with a fresh database, and driven by a headless Playwright script, one browser context per journey. The account was registered through /register and enrolled in two-factor through /settings.

Width Journey Oracle, read outside the page
390 password, code, signed in lands on / (26165 bytes); live sessions 1 to 2 in the database
390 password, then the password changed from another session, then a valid code lands on /login (9924 bytes), no session cookie; live sessions 1 to 1
1280 the same two the same answers

« Sign in » and « Verify » were each inside the viewport and topmost at their centre, at both widths. Screenshots: scr/923/pr/ (ordinary and superseded, steps 1 to 3, at 390 and 1280). No screen changed, so axe was not run.

On being sent back, the reader sees the plain « Sign in » form, as for an expired challenge. That is deliberate. Whoever holds a challenge that a password change ended is most likely the person the change was meant to remove, and a sentence saying the password changed would tell them the owner reacted (#854: describe the input, never another user).

Standards

  • ASVS v5.0.0-7.4.3 (L2): « Verify that the application gives the option to terminate all other active sessions after a successful change or removal of any authentication factor (including password change via reset or recovery and, if present, an MFA settings update). »
  • ASVS v5.0.0-7.4.1 (L1): « Verify that when session termination is triggered (such as logout or expiration), the application disallows any further use of the session. ... »
  • No ASVS 5.0.0 row names a sign-in in progress at the second factor. This PR claims none for it: ending it is the same promise as 7.4.3, kept for a sign-in that has not finished yet. Resolved with the cite skill from the local ASVS 5.0.0 copy.

What comparable products do

Read from their sources on 2026-10-07:

  • Actual Budget has no two-factor, and changePassword ends no session.
  • Firefly III ends no other web session on a password change. Its « log out other devices » relies on Laravel middleware it does not register, by reading; not reproduced.
  • Django binds every session to a value derived from the password hash and re-checks it on each request.
  • ASP.NET Core Identity fixed this exact race in Harden race condition when submitting 2FA during password reset dotnet/aspnetcore#68350 (« Harden race condition when submitting 2FA during password reset », merged 2026-09-04). It embeds the security stamp in the two-factor cookie and re-validates it before the second factor completes.
  • Laravel Fortify and django-two-factor-auth keep only the user id for a pending second factor, so by reading they carry the same defect.

The common mechanism is a credential version bound into the pending state and re-checked at use. This PR reaches the same outcome without storing one. It deletes at the change, claims atomically at use, and runs a compare-and-set on the password hash at the password step. A stamp column remains the alternative if a fourth kind of change ever needs to end sign-ins without touching the User row.

Filed

None.

🤖 Generated with Claude Code

NonoHM and others added 4 commits October 7, 2026 18:56
…word (#923)

Red on main: after changePassword, revokeOtherSessions, the forced password
change and an administrator's reset, a valid code submitted for a challenge
opened before the change still signs in. A forced interleaving (the change
landing between the accepted code and the session) and an unforced race
(8 accounts, the code staggered across a reset: 5 signed in afterwards)
are red too. The first test is the calibration: with nothing in between
the same steps sign in.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…erseded (#923)

The contradiction pass on the design found the window one step earlier:
/login compares the old password, a reset commits meanwhile, and the
challenge (or, without two-factor, the session) written afterwards has
nothing left to end it. Red on main: forced (a gate after the comparison)
with and without two-factor, and unforced (8 password posts staggered
across a reset: 2 live afterwards with two-factor on, 2 with it off).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…and refuse one proven by a password changed meanwhile (#923)

A password change, an administrator's reset and « log out other sessions »
now end the account's sign-ins waiting at their code step, in the same
transaction and before the sessions (endPendingSignIns). The code step
claims its challenge in the session's own transaction and is sent back to
the password step when the claim matches nothing, so a change landing
during the code check is not answered with a session.

The contradiction pass on the design found the window one step earlier:
/login compares the old password while the change commits, then writes a
challenge (or, without two-factor, a session) that nothing ends. What the
comparison proved is now written only after a compare-and-set on the User
row (passwordStillCurrent): a change committed first makes the step refuse
as any wrong password is; one committing after waits on that row and ends
what the step wrote. No schema change.

« Log out other sessions » changes nothing the password step reads, so on
PostgreSQL a challenge written while it commits outlives it. Whoever holds
that challenge holds the current password and could sign in again anyway;
recorded in endPendingSignIns.

Breaks, each red on the tests naming its clause: the ending dropped from
the revocation (3) and from the reset (3), the claim's count ignored (1),
the compare-and-set comparing nothing (4), dropped from the challenge (2)
and from the session without two-factor (2), the claim committed on its
own (1, PostgreSQL), sessions revoked before challenges (1, PostgreSQL).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…ting for its code (#923)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

A two-factor sign-in waiting at the code step survives a password change, and completes with the old password

1 participant