fix(auth): a tab acts only as the account it was built for, and the review's other findings

A tab names the account it was claimed for on every request, beacon and upload
(X-Siftlode-Expect-Account); current_user and /auth/logout refuse a mismatch with
401 account_changed and keep the session, so a tab left showing the account
signed in before reloads instead of writing to the new one.

/auth/login clears a connect marker left by an abandoned connect, and a link
completes only on a session that still holds. The prefs merge reads the row
under a lock. A tab's sessionStorage records its own owner. A page the account
loses is given up, address bar and remembered page included. The panel layouts
are preferences; the storage contract also refuses a key handed to the Storage
API or to usePref outside its kind. A refused sign-in as someone else and a
failed sign-out say so. The chromium-only e2e specs are in the gate register,
the member walk waits for its requests, and devices.spec checks the save itself.
This commit is contained in:
2026-10-21 01:05:12 +02:00
parent 28b77fed1e
commit 94418a087e
39 changed files with 576 additions and 78 deletions
+42 -1
View File
@@ -140,7 +140,7 @@ def test_a_google_link_lands_only_on_the_account_still_signed_in(db, make_user,
from app.routes.auth import _complete_link
starter, other = make_user(), make_user()
session = {"user_id": starter.id if signed_in_as == "starter" else other.id}
session = {"user_id": starter.id if signed_in_as == "starter" else other.id, "epoch": 0}
request = Request({"type": "http", "session": session, "headers": []})
resp = _complete_link(request, db, starter.id, True, "sub-123", {"email": ""}, {"scope": ""})
db.refresh(starter)
@@ -165,6 +165,47 @@ def test_no_app_code_reads_a_second_account():
assert hits == []
def test_a_tab_built_for_the_account_signed_in_before_is_refused(client, make_user):
# Two tabs of one browser: one signed in as someone else, so the cookie is Bob's under both. The
# tab still showing Alice says so with every request; acting as Bob there would put her player
# positions, pref saves and Sign out on his account. Refused, and the session stays Bob's.
alice = make_user(email="alice5@example.com", password=_PW)
bob = make_user(email="bob5@example.com", password=_PW)
assert _login(client, "alice5@example.com", _PW).status_code == 200
assert _login(client, "bob5@example.com", _PW).status_code == 200
stale = client.get("/api/feed", headers={"X-Siftlode-Expect-Account": str(alice.id)})
assert stale.status_code == 401
assert stale.json()["detail"]["reason"] == "account_changed"
assert (
client.get("/api/feed", headers={"X-Siftlode-Expect-Account": str(bob.id)}).status_code
== 200
)
assert client.get("/api/feed").status_code == 200
# Nor may its Sign out end Bob's session.
assert (
client.post(
"/auth/logout", headers={"X-Siftlode-Expect-Account": str(alice.id)}
).status_code
== 401
)
# The probe answers who is signed in now, so the stale tab can reload as Bob.
probe = client.get("/api/me", headers={"X-Siftlode-Expect-Account": str(alice.id)})
assert probe.json()["email"] == "bob5@example.com"
def test_setting_a_password_keeps_the_caller_signed_in(client, make_user):
# The change ends every OTHER session (the epoch moves); the caller is stamped with the new one.
make_user(email="dave@example.com", password=_PW)
assert _login(client, "dave@example.com", _PW).status_code == 200
r = client.post(
"/auth/set-password",
json={"password": "a brand new passphrase", "current_password": _PW},
)
assert r.status_code == 200
assert client.get("/api/feed").status_code == 200
def test_logout_others_revokes_the_other_session_but_keeps_the_caller(client, make_user):
# Two independent cookie jars (browsers) signed into the same account. `logout-others` from one
# bumps the account epoch so the OTHER's cookie is rejected on its next request, while the caller