🐛 Invalidate all sessions on password recovery (#12967)

* 🐛 Invalidate all sessions on password recovery

recover-profile validated the recovery token and updated the
password, but never deleted the existing server-side sessions in
http_session_v2. A previously captured auth-token kept working
after the victim "recovered" the account (GHSA-3j2p-qr45-q365,
CWE-613).

The command now calls session/invalidate-all for the recovered
profile. The command already runs with ::db/transaction true, so
the delete joins the same transaction as the password UPDATE: both
commit or roll back together. There is no current session to
preserve — the RPC is unauthenticated — which is the same contract
the profile deletion path uses.

Tests in rpc_auth_test.clj pin the behavior: the victim's sessions
die, other profiles' sessions survive, and a failed recovery is a
no-op. Both invalidation tests failed before the fix.

AI-assisted-by: inkling-2-small-free

* 🐛 Pin recovery-transaction atomicity after review

Review of 92a65f544b found the tests pinned the end state but not
the rollback guarantee the fix relies on: the session DELETE joins
the password UPDATE in one transaction.

Add recover-profile-rollback-keeps-password-and-sessions: send-template
throws after the update, so the command must fail with password and
sessions both untouched. recover-profile-failure-keeps-sessions now
also asserts the password, matching its docstring. The invalidate-all
docstring mentions the recovery caller.

Addresses review findings M1, M2 and M3.

AI-assisted-by: inkling-2-small-free

* 🔥 Drop recovery test that never reached the fix

The failure test used a token with the :authentication issuer, so
tokens/verify rejected it before any write. It passed with or without
the invalidation fix and exercised no new code. The rollback test
already covers that a failed recovery leaves no partial effects.

AI-assisted-by: hy4-flash-free
This commit is contained in:
Andrey Antukh 2026-11-16 15:40:36 +01:00 committed by GitHub
parent c0f746ee34
commit 0fda857426
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 89 additions and 1 deletions

View File

@ -19,6 +19,12 @@
- Renewal fires when `modified-at` is older than 6h (`default-renewal-max-age`, not configurable). It `UPDATE`s the same row (`modified-at` only) and issues a new token that keeps the original `:exp`.
- `read-session` does not check age. Idle expiration is enforced on every request by `wrap-authz` (sessions past the idle window authenticate like missing ones and are never renewed); the GC only deletes the rows afterwards.
## Revocation paths
- Profile deletion → `session/invalidate-all` (all sessions of the profile die).
- Authenticated password change → `session/invalidate-others` (every session except the caller's dies).
- Password recovery (`recover-profile`, GHSA-3j2p-qr45-q365) → `session/invalidate-all` inside the same transaction as the password `UPDATE`; there is no current session to preserve, and a captured session must not survive recovery. Sibling of GHSA-mj9f-5cwq-7p3q (logout deletes the current session row).
## GC (`::tasks/gc`, cron `session-gc`, daily)
- Deletes from `http_session_v2` where `modified_at < now - :auth-token-cookie-max-age` **or** `created_at < now - :auth-token-cookie-max-age-absolute`.

View File

@ -222,7 +222,8 @@
(defn invalidate-all
"Delete all sessions for a given profile. Used when a profile is deleted
to ensure immediate access revocation across all devices."
or a password is recovered, to ensure immediate access revocation across
all devices."
[cfg profile-id]
(let [sql "delete from http_session_v2 where profile_id = ?"]
(-> (db/exec-one! cfg [sql profile-id])

View File

@ -205,6 +205,11 @@
(let [profile (some->> (validate-token token)
(update-password conn))]
(when profile
;; Recovery must revoke every existing session: a previously
;; captured cookie otherwise keeps working after the password
;; change (GHSA-3j2p-qr45-q365). Runs inside the same transaction
;; as the password update (::db/transaction true).
(session/invalidate-all cfg (:id profile))
(login-lockout/clear-attempts! cfg (:id profile))
(eml/send-template cfg {::eml/reuse-conn true
::eml/template "password-changed"

View File

@ -10,6 +10,7 @@
[app.auth.login-lockout :as lol]
[app.common.flags :as flags]
[app.common.generic-pool :as gpool]
[app.common.time :as ct]
[app.common.uuid :as uuid]
[app.http.session :as session]
[app.loggers.audit :as-alias audit]
@ -17,6 +18,7 @@
[app.rpc.commands.auth :as auth]
[app.rpc.commands.ldap :as ldap-cmd]
[app.storage :as sto]
[app.tokens :as tokens]
[backend-tests.helpers :as th]
[clojure.test :as t]
[datoteka.io :as io]
@ -28,6 +30,80 @@
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
(t/deftest recover-profile-invalidates-existing-sessions
"Regression: password recovery must delete the victim's server-side
sessions, otherwise a previously captured cookie keeps working
(GHSA-3j2p-qr45-q365)."
(let [prof (th/create-profile* 1)
manager (::session/manager th/*system*)
sid1 (uuid/random)
sid2 (uuid/random)
_ (th/db-exec-one! ["INSERT INTO http_session_v2 (id, profile_id, user_agent) VALUES (?, ?, ?)"
sid1 (:id prof) "agent-1"])
_ (th/db-exec-one! ["INSERT INTO http_session_v2 (id, profile_id, user_agent) VALUES (?, ?, ?)"
sid2 (:id prof) "agent-2"])
token (tokens/generate th/*system*
{:iss :password-recovery
:exp (ct/in-future "15m")
:profile-id (:id prof)})]
(with-mocks [_ {:target 'app.email/send-template :return nil}]
(let [out (th/command! {::th/type :recover-profile
:token token
:password "Foobar12!"})]
(t/is (nil? (:error out)))))
(t/is (nil? (session/read-session manager sid1))
"recover-profile must delete the victim's sessions (GHSA-3j2p-qr45-q365)")
(t/is (nil? (session/read-session manager sid2))
"recover-profile must delete the victim's sessions (GHSA-3j2p-qr45-q365)")))
(t/deftest recover-profile-keeps-other-profiles-sessions
(let [prof-a (th/create-profile* 1)
prof-b (th/create-profile* 2)
manager (::session/manager th/*system*)
sid-a (uuid/random)
sid-b (uuid/random)
_ (th/db-exec-one! ["INSERT INTO http_session_v2 (id, profile_id, user_agent) VALUES (?, ?, ?)"
sid-a (:id prof-a) "agent-a"])
_ (th/db-exec-one! ["INSERT INTO http_session_v2 (id, profile_id, user_agent) VALUES (?, ?, ?)"
sid-b (:id prof-b) "agent-b"])
token (tokens/generate th/*system*
{:iss :password-recovery
:exp (ct/in-future "15m")
:profile-id (:id prof-a)})]
(with-mocks [_ {:target 'app.email/send-template :return nil}]
(let [out (th/command! {::th/type :recover-profile
:token token
:password "Foobar12!"})]
(t/is (nil? (:error out)))))
(t/is (nil? (session/read-session manager sid-a)))
(t/is (some? (session/read-session manager sid-b))
"sessions of other profiles must survive the recovery")))
(t/deftest recover-profile-rollback-keeps-password-and-sessions
"A failure after the password UPDATE rolls the transaction back, so the
captured session and the old password both survive
(GHSA-3j2p-qr45-q365)."
(let [prof (th/create-profile* 1)
manager (::session/manager th/*system*)
sid (uuid/random)
_ (th/db-exec-one! ["INSERT INTO http_session_v2 (id, profile_id, user_agent) VALUES (?, ?, ?)"
sid (:id prof) "agent-1"])
before (:password (th/db-exec-one! ["SELECT password FROM profile WHERE id = ?" (:id prof)]))
token (tokens/generate th/*system*
{:iss :password-recovery
:exp (ct/in-future "15m")
:profile-id (:id prof)})]
(with-mocks [_ {:target 'app.email/send-template
:throw (ex-info "smtp down" {})}]
(let [out (th/command! {::th/type :recover-profile
:token token
:password "Foobar12!"})]
(t/is (some? (:error out)))))
(t/is (some? (session/read-session manager sid))
"a rolled-back recovery must not delete sessions")
(t/is (= before (:password (th/db-exec-one! ["SELECT password FROM profile WHERE id = ?" (:id prof)])))
"a rolled-back recovery must not change the password")))
(t/deftest logout-invalidates-current-session
(let [prof (th/create-profile* 1)
manager (::session/manager th/*system*)