diff --git a/.serena/memories/backend/session-expiration.md b/.serena/memories/backend/session-expiration.md index 99c0d6acac..7efbf49089 100644 --- a/.serena/memories/backend/session-expiration.md +++ b/.serena/memories/backend/session-expiration.md @@ -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`. diff --git a/backend/src/app/http/session.clj b/backend/src/app/http/session.clj index 4e9ce3bd77..12fd7353b1 100644 --- a/backend/src/app/http/session.clj +++ b/backend/src/app/http/session.clj @@ -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]) diff --git a/backend/src/app/rpc/commands/auth.clj b/backend/src/app/rpc/commands/auth.clj index 50361fd9dc..11d32a636f 100644 --- a/backend/src/app/rpc/commands/auth.clj +++ b/backend/src/app/rpc/commands/auth.clj @@ -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" diff --git a/backend/test/backend_tests/rpc_auth_test.clj b/backend/test/backend_tests/rpc_auth_test.clj index e2a4a54f8c..32eda259d2 100644 --- a/backend/test/backend_tests/rpc_auth_test.clj +++ b/backend/test/backend_tests/rpc_auth_test.clj @@ -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*)