fix(auth): a Google connect names its tab's account too, and a refused tab says why

- /auth/google/connect is a navigation and carries no headers: the tab names the account it
  was built for in `expect`, checked by the same comparison as the request header
  (built_for_another_account); a mismatch lands on /?account=changed
- the link marker names the OAuth state of its own flow: an older flow coming back no longer
  spends the marker of the connect still open (which then returned as a plain sign-in)
- a 401 account_changed reloads the tab with a one-time flag, and the reloaded tab says it
  now shows the account signed in
- a refused switch to a suspended or not-yet-approved account says so in the signed-in
  app's words, naming the address a request was recorded for
- the prefs merge locks FOR NO KEY UPDATE, so a transaction that only added a row referring
  to the account does not hold the save up; a test pins the lock
- giving up a lost page closes a channel page open over it
- e2e README names the chromium-only specs
This commit is contained in:
2026-10-21 02:05:33 +02:00
parent 94418a087e
commit ef99f7b5c7
19 changed files with 487 additions and 121 deletions
+56 -4
View File
@@ -7,6 +7,7 @@ the refused sign-in that doubles as an access request, and the link flow's confl
"""
import pytest
from authlib.integrations.starlette_client import OAuthError
from sqlalchemy import select
from starlette.responses import RedirectResponse
@@ -19,7 +20,8 @@ _PW = "correct horse battery"
class _FakeGoogle:
"""authlib's client, reduced to the two calls the routes make: the redirect out to Google and
the token exchange when Google redirects back."""
the token exchange when Google redirects back. Like authlib, it honours only the newest flow's
state: starting a flow retires every earlier one."""
def __init__(self, userinfo: dict):
self.userinfo = userinfo
@@ -29,7 +31,9 @@ class _FakeGoogle:
self.asked = kw
return RedirectResponse(url="/fake-google")
async def authorize_access_token(self, _request):
async def authorize_access_token(self, request):
if request.query_params.get("state") != self.asked.get("state"):
raise OAuthError(error="mismatching_state")
return {"userinfo": self.userinfo, "access_token": "at", "scope": "openid email profile"}
@@ -46,9 +50,13 @@ def google(monkeypatch):
return fake
def _sign_in(client, google, **userinfo):
def _sign_in(client, google, state: str | None = None, **userinfo):
"""Google redirecting back: by default with the state of the flow started last."""
google.userinfo = {"email_verified": True, **userinfo}
return client.get("/auth/callback", follow_redirects=False)
state = state or google.asked.get("state")
return client.get(
"/auth/callback", params={"state": state} if state else {}, follow_redirects=False
)
def _rows(db, model, column, value):
@@ -305,6 +313,50 @@ def test_a_link_whose_session_was_revoked_meanwhile_does_not_finish(client, db,
assert me.google_sub is None
def test_an_older_flow_coming_back_leaves_the_connect_in_flight_a_link(
client, db, google, make_user
):
# Two round-trips from one browser: an older one (another tab's, or Back to Google's screen)
# returns after a connect started. Its state is refused, and it must not spend the connect's
# marker: the connect itself, coming back, would then land as a plain sign-in.
me = make_user(email="me6@example.com")
_start_link(client, "me6@example.com")
older = _sign_in(client, google, state="an-older-flow", sub="sub-6", email="g6@example.com")
assert older.headers["location"] == "/?oauth=cancelled"
r = _sign_in(client, google, sub="sub-6", email="g6@example.com")
assert r.headers["location"] == "/?link=ok"
db.refresh(me)
assert me.google_sub == "sub-6"
def test_a_connect_from_a_tab_showing_another_account_starts_nothing(client, google, make_user):
# A navigation carries no headers, so the tab names the account it was built for in the query.
# One still showing the account signed in before must not start a link onto the one signed in
# now: it lands on the app, which boots as that one and says why.
me = make_user(email="me7@example.com")
_password_login(client, "me7@example.com")
r = client.get(
f"/auth/google/connect?access=identity&expect={me.id + 1}", follow_redirects=False
)
assert r.headers["location"] == auth_routes.ACCOUNT_CHANGED_LANDING == "/?account=changed"
assert google.asked == {}
assert client.get("/api/me").json()["email"] == "me7@example.com"
def test_a_connect_naming_the_signed_in_account_goes_to_google(client, google, make_user):
me = make_user(email="me8@example.com")
_password_login(client, "me8@example.com")
r = client.get(f"/auth/google/connect?access=identity&expect={me.id}", follow_redirects=False)
assert r.headers["location"] == "/fake-google"
assert google.asked["state"]
@pytest.mark.parametrize(
("access", "scope", "prompt"),
[