From 20354aea31ee24db563bffbb790f7acf4fc2bf80 Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Wed, 4 Nov 2026 03:39:42 +0000 Subject: [PATCH] fix(steam): finish the sign-in from a same-site page so the session cookie is sent Strict session cookies are withheld on the navigation Steam starts, so the callback answered "Not authenticated" for every default (non-OIDC) install. The callback is now a public page that navigates to a protected /steam/complete route with the same query. That second navigation starts from RomM's own origin, so it carries the session and the nonce check still binds the sign-in to the browser that began it. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01NH5s4hcHN99cvX93yKGsES --- backend/endpoints/user.py | 29 +++++++++++++-- backend/tests/endpoints/test_user_steam.py | 41 ++++++++++++++++++---- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/backend/endpoints/user.py b/backend/endpoints/user.py index 49fc3cbd1..d77ec9b58 100644 --- a/backend/endpoints/user.py +++ b/backend/endpoints/user.py @@ -1,4 +1,5 @@ import asyncio +import html import json import secrets from typing import Annotated, Any, Final, cast @@ -7,7 +8,7 @@ from urllib.parse import quote from fastapi import Body, Form, HTTPException from fastapi import Path as PathVar from fastapi import Query, Request, status -from fastapi.responses import FileResponse, RedirectResponse +from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse from adapters.services.steam import SteamOpenIDService from config import STEAM_API_KEY @@ -838,9 +839,33 @@ def start_steam_login( ) +@router.get( + "/{id}/steam/callback", + include_in_schema=False, + response_class=HTMLResponse, +) +def steam_login_return( + request: Request, + id: Annotated[int, PathVar(description="User internal id.", ge=1)], +) -> HTMLResponse: + """Hop from Steam's cross-site redirect to the authenticated completion route.""" + # Strict session cookies are withheld on a navigation Steam started, so the + # page navigates again itself: that request is same-site and carries them. + target = f"/api/users/{id}/steam/complete" + if request.url.query: + target = f"{target}?{request.url.query}" + href = html.escape(target, quote=True) + return HTMLResponse( + '' + f'' + f'Continue', + headers={"Referrer-Policy": "no-referrer", "Cache-Control": "no-store"}, + ) + + @protected_route( router.get, - "/{id}/steam/callback", + "/{id}/steam/complete", [Scope.ME_WRITE], summary="Finish a Steam sign-in", responses={ diff --git a/backend/tests/endpoints/test_user_steam.py b/backend/tests/endpoints/test_user_steam.py index 616eac6a9..362313e42 100644 --- a/backend/tests/endpoints/test_user_steam.py +++ b/backend/tests/endpoints/test_user_steam.py @@ -84,7 +84,7 @@ def _linked_steam_id(user_id: int) -> str | None: def _sign_in(client: TestClient, user: User, token: str) -> None: state = _start_login(client, user, token) response = client.get( - f"/api/users/{user.id}/steam/callback", + f"/api/users/{user.id}/steam/complete", params={"state": state, "openid.claimed_id": "x"}, headers=_bearer(token), follow_redirects=False, @@ -108,6 +108,33 @@ def test_the_login_route_points_at_steam( ).replace("%2F", "/") +def test_the_callback_hops_to_the_authenticated_route_without_a_session( + client: TestClient, admin_user: User +): + response = client.get( + f"/api/users/{admin_user.id}/steam/callback", + params={"state": "abc", "openid.mode": "id_res"}, + follow_redirects=False, + ) + + assert response.status_code == status.HTTP_200_OK + assert response.headers["referrer-policy"] == "no-referrer" + target = ( + f"/api/users/{admin_user.id}/steam/complete?state=abc&openid.mode=id_res" + ) + assert f'content="0;url={target}"' in response.text + + +def test_the_callback_hop_escapes_the_query(client: TestClient, admin_user: User): + response = client.get( + f"/api/users/{admin_user.id}/steam/callback", + params={"state": '">'}, + follow_redirects=False, + ) + + assert "