Repository navigation
End an account's other sessions when its password changes - #288
Conversation
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.
|
Reviewed for the security batch. The mechanism works:
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. 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.
|
@danielplohmann agreed, done in eecaf0a. |
…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
The third finding in #284. The session is Flask's signed cookie, holding
user_idand 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/settingsas 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]password_mark()(authentication.py:290,:303): the account's id and an HMAC-SHA256 of its stored password hash, keyed withSECRET_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 setsuser_idwithout a login that clears the session first, but tests do, by callingas_roletwice or writingsession["user_id"]directly; without the id, six tests in five files looked like sessions whose password had changed and failed.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:/admin/change_password,mcritweb/views/administration.py:57) keeps its session;user_idinto the test session, here and in open PRs such as Surface, rotate and optionally retire the pre-1.5.0 API tokens (#250) #254 and Bound the result cache and write cached reports as compact JSON #229.administration.py:60), and the flash says so./settingsshows the token, and/apitakes 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:
_rehash_if_stale,authentication.py:110), the hash changes, so that account's other sessions end;SECRET_KEYwith Flask'sSECRET_KEY_FALLBACKSkeeps old cookies valid, but their marks were made with the old key, so everyone logs in again. Nothing in this repository sets fallbacks;Verification. In the test client only, with real logins through
/loginand 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 onlyuser_idworks, 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_inandtest_a_failed_password_change_keeps_the_api_tokenare guards and pass on master too.tests/testSettingsPages.py::test_changing_the_passwordnow 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
McritClientopens arequests.Session(), which the stub intests/testSubmitMetadata.pydid not have. It is a no-op once #285 is merged. The third commit replaces the API token, after review.Relation to open PRs.
clear_login_failuresline next to the login change; the mark is set above it, beforesaveToDb(), so they do not touch.git merge-tree) of this branch with each of Surface, rotate and optionally retire the pre-1.5.0 API tokens (#250) #254, Run the shared SQLite database in WAL mode (#258) #262, Forget only the failed logins of the name that just logged in #276, Refuse a rename to a name taken after the lookup #277, Log at most 32 characters of a throttled login's username #281, Answer input the UI never sends with 400s instead of 500s #283, Let only one of two simultaneous first registrations become admin #286 and Refuse lookalike usernames on registration and rename #287 on its own is clean; Bound the result cache and write cached reports as compact JSON #229 and Add a dark theme, chosen per account in Settings (#70) #160 already conflict with master.authentication.py, where both change the login save (this branch sets the mark beforesaveToDb(withPassword=rehashed), which Write back only the user columns a request changed #278 replaces withsaveLogin()), and inchange_password, where Write back only the user columns a request changed #278 dropswithPassword=True. The merge keeps both sides: Write back only the user columns a request changed #278 writes a column whose value differs from the one read, so the new token is written.