mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 10:33:22 +01:00
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NH5s4hcHN99cvX93yKGsES
This commit is contained in:
@@ -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(
|
||||
'<!doctype html><meta charset="utf-8">'
|
||||
f'<meta http-equiv="refresh" content="0;url={href}">'
|
||||
f'<a href="{href}">Continue</a>',
|
||||
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={
|
||||
|
||||
@@ -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": '"><script>alert(1)</script>'},
|
||||
follow_redirects=False,
|
||||
)
|
||||
|
||||
assert "<script>" not in response.text
|
||||
|
||||
|
||||
def test_the_callback_links_the_account_steam_vouches_for(
|
||||
client: TestClient,
|
||||
access_token: str,
|
||||
@@ -118,7 +145,7 @@ def test_the_callback_links_the_account_steam_vouches_for(
|
||||
state = _start_login(client, admin_user, access_token)
|
||||
|
||||
response = client.get(
|
||||
f"/api/users/{admin_user.id}/steam/callback",
|
||||
f"/api/users/{admin_user.id}/steam/complete",
|
||||
params={"state": state, "openid.claimed_id": "x", "openid.mode": "id_res"},
|
||||
headers=_bearer(access_token),
|
||||
follow_redirects=False,
|
||||
@@ -139,7 +166,7 @@ def test_a_sign_in_steam_will_not_confirm_links_nothing(
|
||||
state = _start_login(client, admin_user, access_token)
|
||||
|
||||
response = client.get(
|
||||
f"/api/users/{admin_user.id}/steam/callback",
|
||||
f"/api/users/{admin_user.id}/steam/complete",
|
||||
params={"state": state, "openid.claimed_id": "forged"},
|
||||
headers=_bearer(access_token),
|
||||
follow_redirects=False,
|
||||
@@ -161,7 +188,7 @@ def test_a_callback_that_did_not_start_here_links_nothing(
|
||||
_start_login(client, admin_user, access_token)
|
||||
|
||||
response = client.get(
|
||||
f"/api/users/{admin_user.id}/steam/callback",
|
||||
f"/api/users/{admin_user.id}/steam/complete",
|
||||
params={"state": "someone-elses-nonce", "openid.claimed_id": "x"},
|
||||
headers=_bearer(access_token),
|
||||
follow_redirects=False,
|
||||
@@ -182,7 +209,7 @@ def test_a_replayed_callback_links_nothing(
|
||||
state = _start_login(client, admin_user, access_token)
|
||||
params = {"state": state, "openid.claimed_id": "x"}
|
||||
first = client.get(
|
||||
f"/api/users/{admin_user.id}/steam/callback",
|
||||
f"/api/users/{admin_user.id}/steam/complete",
|
||||
params=params,
|
||||
headers=_bearer(access_token),
|
||||
follow_redirects=False,
|
||||
@@ -191,7 +218,7 @@ def test_a_replayed_callback_links_nothing(
|
||||
|
||||
db_user_handler.update_user(admin_user.id, {"steam_id": None})
|
||||
replay = client.get(
|
||||
f"/api/users/{admin_user.id}/steam/callback",
|
||||
f"/api/users/{admin_user.id}/steam/complete",
|
||||
params=params,
|
||||
headers=_bearer(access_token),
|
||||
follow_redirects=False,
|
||||
@@ -212,7 +239,7 @@ def test_an_account_steam_cannot_describe_is_reported(
|
||||
state = _start_login(client, admin_user, access_token)
|
||||
|
||||
response = client.get(
|
||||
f"/api/users/{admin_user.id}/steam/callback",
|
||||
f"/api/users/{admin_user.id}/steam/complete",
|
||||
params={"state": state, "openid.claimed_id": "x"},
|
||||
headers=_bearer(access_token),
|
||||
follow_redirects=False,
|
||||
|
||||
Reference in New Issue
Block a user