Skip to content

End an account's other sessions when its password changes - #288

Merged
danielplohmann merged 3 commits into
masterfrom
fix/password-change-ends-sessions
Oct 16, 2026
Merged

danielplohmann merged 3 commits into
masterfrom
fix/password-change-ends-sessions

Conversation

@r0ny123

@r0ny123 r0ny123 commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

The third finding in #284. The session is Flask's signed cookie, holding user_id and the CSRF token, and nothing on the server tied it to the password it was issued for. A copy of the cookie (from a shared machine, a proxy log, a browser profile) kept working after its owner changed the password, for as long as Flask accepts the signature: PERMANENT_SESSION_LIFETIME, 31 days, which MCRITweb does not set. Reproduced in the test client on master (1cdc195): a cookie copied after login still opened /settings as that user after a password change and a logout.

This PR makes a password change end every other session of the account, and replace the account's API token. Logout is not covered: it clears the browser's own copy (mcritweb/views/authentication.py:439), and without a server-side session there is nothing for it to end. That would take a per-user counter in the database, the second option in #284.

Change.

flowchart LR
    L[login] -->|"session gets mark = HMAC(SECRET_KEY, password hash)"| R[next request]
    R --> C{"mark matches the account's hash?"}
    C -- yes --> OK[logged in]
    C -- "no: password changed since" --> X[session cleared, logged out]
    C -- "no mark: issued before this change" --> M[logged in, marked at the end of the request]
Loading
  • At login the session gets password_mark() (authentication.py:290, :303): the account's id and an HMAC-SHA256 of its stored password hash, keyed with SECRET_KEY. Keyed, because the cookie is signed but readable, and a plain digest of the hash would be something to test password guesses against. It is set after the rehash at login (:288), which replaces the hash.
  • load_logged_in_user() (:322) clears a session whose mark for this account no longer matches (:332, hmac.compare_digest), so that request and every later one with that cookie is logged out. A mark naming another account counts as no mark. The app never sets user_id without a login that clears the session first, but tests do, by calling as_role twice or writing session["user_id"] directly; without the id, six tests in five files looked like sessions whose password had changed and failed.
  • An after-request hook, mark_the_session_with_the_current_password() (:340), gives a logged-in session the current mark when it lacks it or carries another one. The request has already passed the check above, so this covers two cases only:
  • The password change also replaces the account's API token (administration.py:60), and the flash says so. /settings shows the token, and /api takes it in place of a session (authentication.py:414), so whoever read that page with a stolen session could have copied it, and it would have kept working after the owner changed the password. A failed change keeps the token. Anything that uses the old token has to be given the new one, which is the cost of this.

What stays open: a cookie copied before the upgrade, and first used again only after a password change, is accepted and marked with the new password. That window closes when such cookies expire, 31 days after they were issued.

Side effects, each ending a session that is otherwise fine, once:

  • when the login rehash moves a password to werkzeug's current method (_rehash_if_stale, authentication.py:110), the hash changes, so that account's other sessions end;
  • an operator who rotates SECRET_KEY with Flask's SECRET_KEY_FALLBACKS keeps old cookies valid, but their marks were made with the old key, so everyone logs in again. Nothing in this repository sets fallbacks;
  • a request of the owner's that was already running when the password changed, and writes to its session (a flash, the sort memory), sends back the old mark, and the owner has to log in again.

Verification. In the test client only, with real logins through /login and real password changes through /admin/change_password.

Tests. New tests/testPasswordChangeEndsSessions.py:

  • test_a_copied_session_ends_when_the_password_changes: a copy of the owner's cookie is logged in, then is not after the owner changes the password.
  • test_another_login_ends_when_the_password_changes: a second login of the same account is logged out, and logging in with the new password works.
  • test_a_session_from_before_the_mark_is_marked_and_then_ends_too: a session holding only user_id works, and ends after a password change made elsewhere.
  • test_the_cookie_carries_nothing_to_test_password_guesses_against: the mark is neither the hash nor its plain SHA-256.
  • test_the_api_token_is_replaced_with_the_password: after the change the old token gets 403 from /api/version, and the stored token is a new one that maps to the account.
  • test_the_session_that_changed_the_password_stays_logged_in and test_a_failed_password_change_keeps_the_api_token are guards and pass on master too.

tests/testSettingsPages.py::test_changing_the_password now expects the longer flash.

The first five fail on master. Removing the check on load fails three; not marking unmarked sessions, not re-marking after the account's own change, an unkeyed digest and not replacing the token each fail one.

The second commit is the test fix from #285 for mcrit 1.15.0, which CI now installs: its McritClient opens a requests.Session(), which the stub in tests/testSubmitMetadata.py did not have. It is a no-op once #285 is merged. The third commit replaces the API token, after review.

Relation to open PRs.

The session is Flask's signed cookie, and nothing on the server tied it
to the password it was issued for. A copy of the cookie kept working
after its owner changed the password, for as long as Flask accepts the
signature (31 days by default). Reproduced in the test client: a cookie
copied after login still opened /settings after a password change.

At login the session now carries a mark: an HMAC of the account's stored
password hash, keyed with SECRET_KEY, so the readable cookie holds
nothing to test password guesses against. load_logged_in_user() drops a
session whose mark no longer matches the account's hash. An after-request
hook re-marks the session that changed its own password, so it stays
logged in, and marks sessions issued before this change on their first
request, so a later password change ends them too instead of logging
every user out on upgrade.

Logout still clears only the browser's own copy: without a server-side
session there is nothing for it to end.
mcrit 1.15.0, which CI now installs, makes McritClient send through a
requests.Session() it opens in its constructor. The test swaps the
client's requests module for a stub with get and post only, so creating
the client raised AttributeError and all eight tests in the file
errored. The stub now hands itself back as the session. Same change as
in #285; it works with 1.13 and 1.15.
@danielplohmann

Copy link
Copy Markdown
Collaborator

Reviewed for the security batch. The mechanism works:

  • The session carries an HMAC of the stored password hash, keyed with SECRET_KEY (authentication.py:303).
  • load_logged_in_user compares it with compare_digest and clears the session on a mismatch.
  • The session that changed the password survives: change_password updates g.user.password, and the after-request hook re-marks it.

No DB column is needed, and there's no admin-initiated reset path that would bypass it.

One thing to add before it goes in. The API token survives the password change. /api authenticates by the apitoken header (authentication.py:414), and the token is shown on /settings. Someone holding a stolen session cookie could have copied it from there, and it keeps working after the owner changes the password, which is exactly the situation this PR is for. Please regenerate the token in change_password. If you think that's the wrong trade-off, at least say clearly on the page and in the PR that the token is not rotated.

Known limits, fine as stated in the PR:

/settings shows the API token, so whoever could read that page with a
stolen session could have copied it, and /api takes it in place of a
session. A password change now issues a new token along with the new
password hash, and the flash says so; a failed change keeps the old one.
@r0ny123

r0ny123 commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator Author

@danielplohmann agreed, done in eecaf0a. change_password now issues a new API token along with the new hash (mcritweb/views/administration.py:60), and the flash says the old one stops working. A failed change keeps the token. Two tests in tests/testPasswordChangeEndsSessions.py: after the change the old token gets 403 from /api/version and the stored one is new; a wrong old password leaves it as it was. The description now has it, and also notes the conflict with #278 (which I had missed in the list before) and how it resolves.

@danielplohmann
danielplohmann merged commit da3c8df into master Oct 16, 2026
8 checks passed
danielplohmann added a commit that referenced this pull request Oct 16, 2026
r0ny123 pushed a commit to r0ny123/mcritweb that referenced this pull request Oct 19, 2026
…test

With familiary#288 a password change replaces the API token itself, so a token
rotated while the password is being changed is expected to be replaced,
not to survive. The username and role cases still hold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HERmVhHo4ibp7HSTXr3Ya8
@r0ny123
r0ny123 deleted the fix/password-change-ends-sessions branch October 30, 2026 06:08
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.

2 participants