Repository navigation
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
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
/loginis still comparing the OLD password (a bcrypt comparison, hundreds of ms)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.revokeSessionsOtherThanruns it first (SettingschangePassword,revokeOtherSessions,/force-password-change), and/adminresetPasswordruns it before deleting the sessions.claimMfaChallenge(tx, id)deletes the challenge only if it is still there and unexpired, and throwsSignInSupersededotherwise.createSession(userId, cookies, stillProven)runs it in the session's transaction, retried whole on a transient write conflict.passwordStillCurrent(tx, userId, verifiedHash)is anUPDATE "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
endPendingSignIns's docstring./loginwithout a session.The measurement
src/lib/server/auth/pendingSignIn.db-smoke.tsdrives the real/login,/login/verify-totp,/settings,/force-password-changeand/adminactions. 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: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, afternpx prisma migrate deploy.Breaks
Each break ran in the foreground, was restored in a
finallyand was checked by sha256. The runner was calibrated first, reading 12 of 12 green on the unbroken tree on SQLite and PostgreSQL.endPendingSignInsdropped fromrevokeSessionsOtherThanB7 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).
npm run checknpm run lint:trackednpm run test:unit -- --runnpm run buildnpm run test:db, SQLite, empty databasecrossProvider, not touched here)npm run test:db, PostgreSQL 17, empty databasenpm run test:db, MariaDB 11, empty databasenpm run test:e2e, full suiteThe 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'sdocker-smokeis 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
/registerand enrolled in two-factor through/settings./(26165 bytes); live sessions 1 to 2 in the database/login(9924 bytes), no session cookie; live sessions 1 to 1« 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. ... »citeskill from the local ASVS 5.0.0 copy.What comparable products do
Read from their sources on 2026-10-07:
changePasswordends no session.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