Files
Dylan M. Taylor 147f9a7fa7 fix: revoke existing sessions when a password reset is redeemed (#310)
## What was wrong

Redeeming a password reset changed the password (and bumped
`password_version`) but left every existing session valid, and session
checks don't look at `password_version`. Someone who already had a
session on the account (the reason for recovering it, often) kept access
after recovery and could even change the password again.

## What changed

`change_password_from_password_reset` now also soft-deletes all of the
user's live `user_sessions` rows inside the same transaction as the
password update, rolling back if that fails. The redeem handler already
creates a fresh session for the person who completed the reset, after
commit.

It's a runtime `sqlx::query` (no new `.sqlx` offline entry needed).
Sessions already cached in Redis expire within the cache TTL
(`REDIS_CACHE_TTL_SECONDS`, 60s by default).
- A password login now creates its session with one conditional insert:
it succeeds only if `password_version` still matches the version the
password was verified against, under a row lock on the user. A login
that races a reset is answered as invalid credentials instead of leaving
a live session behind.


## Checks

- Run: `SQLX_OFFLINE=true cargo check -p storyteller-web` (exit 0).
- Not run: the reset flow tests (need a live MySQL).
- Not changed: ordinary password changes (`change_password_handler`)
still keep other sessions.


## Review round (ed360d5dc)

- Session listing, revoke and the conditional insert are one query per
file, each with an Args struct (`list_live_session_tokens`,
`revoke_all_user_sessions`, `create_user_session_if_password_version`).
No behavior change.
- Every session insert that verifies no password is now ordered against
a reset under the same `users` row lock: Google SSO uses the conditional
insert with the version read inside the statement; impersonation login
and the login-challenge bridge session inserts became `INSERT ... SELECT
FROM users ... FOR UPDATE` (runtime queries; their stale `.sqlx` entries
removed). `create_account_handler`, billing `user_creation_case` and the
redeem handler's post-commit session are unchanged.
- Redis cache-fill race: after the first purge, the redeem handler
purges the revoked sessions' cache keys again after 5 s. A fill that
started before the commit and lands later than that is still bounded by
the cache TTL (60 s default); the session checker was not redesigned.
- Checks run: `cargo check -p storyteller-web`, `cargo check --tests -p
storyteller-web -p mysql_testing` (both pass); unit test
`purges_both_cache_entries_for_every_revoked_session` passes. Not run:
DB tests (no MySQL), clippy/full fmt (repo has pre-existing fmt diffs).

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Security**
* Active sessions are revoked after a password is changed through a
password reset. If revocation fails, the change is rolled back.
* Revoked sessions are also removed from the session cache when
possible.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-11 11:21:13 -05:00
..