fix(security): hide netplay rooms for roms the caller cannot see

GET /api/netplay/list answered for any game_id a caller passed, including roms
hidden from them, which let the listing confirm that someone was playing a rom
the caller is not allowed to know about.

The room list is now scoped to a rom the caller can see. A hidden rom and a
rom that does not exist both answer with no rooms, so the listing cannot be
used to probe for hidden roms.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Sam Dornan
2026-09-22 10:42:53 -05:00
parent 34a0fbfa75
commit dfd7ca9f6a
2 changed files with 107 additions and 0 deletions

View File

@@ -4,6 +4,8 @@ from fastapi import Request
from decorators.auth import protected_route
from handler.auth.constants import Scope
from handler.auth.dependencies import get_permissions
from handler.database import db_rom_handler
from handler.netplay_handler import NetplayRoom, netplay_handler
from utils.router import APIRouter
@@ -45,8 +47,29 @@ class RoomsResponse(TypedDict):
hasPassword: bool
def _caller_may_see_rom(request: Request, game_id: str) -> bool:
"""Whether the caller may know that ``game_id`` has open rooms.
A rom the caller cannot see, like one that does not exist, reads as having
no rooms rather than as forbidden, so the listing cannot be used to probe
for hidden roms.
"""
try:
rom = db_rom_handler.get_rom_visibility(int(game_id))
except (TypeError, ValueError):
return False
if rom is None:
return False
return get_permissions(request).can_see_rom(rom.id, rom.platform_id)
@protected_route(router.get, "/list", [Scope.ASSETS_READ])
async def get_rooms(request: Request, game_id: str) -> Dict[str, RoomsResponse]:
if not _caller_may_see_rom(request, game_id):
return {}
netplay_rooms = await netplay_handler.get_all()
open_rooms: Dict[str, RoomsResponse] = {

View File

@@ -0,0 +1,84 @@
"""The netplay room listing must not reveal rooms for a hidden rom."""
from datetime import timedelta
from unittest.mock import AsyncMock
import pytest
from config import OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS
from endpoints import netplay as netplay_endpoints
from handler.auth import oauth_handler
from handler.database.base_handler import sync_session
from models.permission import HiddenEntity, PermEntity
def _auth(user):
# Re-reads the user's current (projected) scopes each call.
token = oauth_handler.create_access_token(
data={
"sub": user.username,
"iss": "romm:oauth",
"scopes": " ".join(user.oauth_scopes),
},
expires_delta=timedelta(seconds=OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS),
)
return {"Authorization": f"Bearer {token}"}
def _hide(entity, entity_id, user_id):
with sync_session.begin() as s:
s.add(HiddenEntity(entity=entity, entity_id=entity_id, user_id=user_id))
def _room(rom_id: int) -> dict:
return {
"owner": "sid",
"players": {
"p": {
"socketId": "sid",
"player_name": "Player p",
"userid": None,
"playerId": "p",
}
},
"peers": [],
"room_name": "Room",
"game_id": str(rom_id),
"domain": None,
"password": None,
"max_players": 4,
}
@pytest.fixture
def rooms(mocker):
return mocker.patch.object(
netplay_endpoints.netplay_handler, "get_all", AsyncMock(return_value={})
)
def test_listing_shows_rooms_for_a_visible_rom(client, viewer_user, rom, rooms):
rooms.return_value = {"room-1": _room(rom.id)}
resp = client.get(f"/api/netplay/list?game_id={rom.id}", headers=_auth(viewer_user))
assert resp.status_code == 200
assert "room-1" in resp.json()
def test_listing_hides_rooms_for_a_hidden_rom(client, viewer_user, rom, rooms):
_hide(PermEntity.ROMS, rom.id, viewer_user.id)
rooms.return_value = {"room-1": _room(rom.id)}
resp = client.get(f"/api/netplay/list?game_id={rom.id}", headers=_auth(viewer_user))
assert resp.status_code == 200
assert resp.json() == {}
rooms.assert_not_awaited()
def test_listing_of_a_missing_rom_is_empty(client, viewer_user, rooms):
resp = client.get("/api/netplay/list?game_id=999999", headers=_auth(viewer_user))
assert resp.status_code == 200
assert resp.json() == {}