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
..
2026-08-09 22:54:25 -04:00

mysql_testing

Test-only MySQL harness: guarded connections to a test database, schema setup from the checked-in migrations, and shared fixtures (accounts, sessions, wallets, media files). Use it as a dev-dependency only.

Safety model

Every connection goes through guard::guard_test_database_url, which PANICS unless:

  • the database name contains "test",
  • the database name is not storyteller or artcraft (the real databases),
  • the host does not look like a managed/cloud database (digitalocean, aws, amazonaws, rds, gcp, googleapis, cloudsql, azure, ...).

Test code reads its URL from exactly one env var — ARTCRAFT_TEST_DATABASE_URL — never MYSQL_URL or DATABASE_URL, so test runs can't inherit a real database by accident. The default is mysql://root:@localhost:3306/artcraft_test.

One-time local setup

mysql -u root -e "CREATE DATABASE IF NOT EXISTS artcraft_test;"

The harness creates the database itself when the connecting user has the privilege, applies all _database/sql/migrations/*/up.sql in order (tracked in mysql_testing_applied_migrations, so repeat runs are fast), and seeds user_roles. If the schema ever wedges: DROP DATABASE artcraft_test; and rerun.

Writing a database test

Database tests run by default; mark them with the skip_database_tests off switch so database-less machines/CI can exclude them. Tests run in PARALLEL — create your own users/wallets/rows and never touch another test's data (schema setup single-flights via a MySQL named lock). Only a test that must mutate shared/global state takes mysql_testing::serial::acquire_serial_test_lock().

#[tokio::test]
#[cfg_attr(feature = "skip_database_tests", ignore)]
async fn my_database_test() {
  let pool = mysql_testing::pool::create_test_pool().await;

  let user = mysql_testing::fixtures::users::create_test_user(&pool).await.unwrap();
  mysql_testing::fixtures::wallets::fund_wallet_banked(&pool, &user.user_token, 10_000)
    .await
    .unwrap();
  // ...
}

Skip them (machines/CI without MySQL) with:

SQLX_OFFLINE=true cargo test -p storyteller-web --features skip_database_tests

The first end-to-end consumers are the omni_gen video pricing tests in crates/service/web/storyteller_web/src/http_server/endpoints/omni_gen/generate/video/tests/, which drive the real generate handler with dummy Actix requests, stub the provider with an in-process HTTP server, and assert the exact credits debited from the wallet.