Files
romm/backend/endpoints/netplay.py
Sam Dornan 79391961ea fix(security): close netplay authorization gaps found in review
The connect-time identity was cached for the life of the socket, so an
account disabled afterwards kept its access until it disconnected. The
signal relay trusted the room id stored on the session, which a departed
socket still carried, so it could keep injecting offers into the room it
had left, and the game data relay had the same stale room. The room
listing answered for asset-read tokens, though what it discloses is
ROM-scoped.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-22 11:03:28 -05:00

88 lines
2.3 KiB
Python

from typing import Dict, TypedDict
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
router = APIRouter(
prefix="/netplay",
tags=["netplay"],
)
DEFAULT_MAX_PLAYERS = 4
def _get_owner_player_name(room: NetplayRoom) -> str:
owner_sid = room.get("owner")
if not owner_sid:
return "Unknown"
return next(
(
p["player_name"]
for p in room["players"].values()
if p["socketId"] == owner_sid
),
"Unknown",
)
def _is_room_open(room: NetplayRoom, game_id: str) -> bool:
if len(room["players"]) >= room["max_players"]:
return False
return str(room["game_id"]) == str(game_id)
class RoomsResponse(TypedDict):
room_name: str
current: int
max: int
player_name: str
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.ROMS_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] = {
session_id: RoomsResponse(
room_name=room["room_name"],
current=len(room["players"]),
max=room["max_players"],
player_name=_get_owner_player_name(room),
hasPassword=bool(room["password"]),
)
for session_id, room in netplay_rooms.items()
if _is_room_open(room, game_id)
}
return open_rooms