From c9a8ceedd7286e7c0bd6ebe697ae5a9155cce563 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Mon, 20 Jul 2026 11:04:49 -0400 Subject: [PATCH] fix: enforce hidden-ROM/platform visibility on asset download and streaming endpoints Several endpoints authorized cross-user access by the is_public flag (or by scope alone for streaming) without re-checking whether the parent ROM or platform is hidden from the caller. A user blocked from a ROM/platform could still download a public save/state/screenshot attached to it, or claim a streaming session for it, by supplying the asset/ROM id directly. Route all four through the existing assert_rom_visible() 404-masking helper, right after loading the record and before any file access or broker launch, matching the ROM detail/content endpoints. - saves.py / states.py / screenshots.py download_* : public sharing no longer overrides the hidden-resource boundary. - streaming.py claim_session : hidden ROM/platform can no longer be launched via its id; broker is never contacted. Regression tests cover hidden-ROM and hidden-platform cascade for each endpoint, plus a positive control that public sharing still works when the ROM is visible. Co-Authored-By: Claude Opus 4.8 (1M context) --- backend/endpoints/saves.py | 8 +++ backend/endpoints/screenshots.py | 5 ++ backend/endpoints/states.py | 7 +++ backend/endpoints/streaming.py | 6 ++ backend/tests/endpoints/test_saves.py | 69 +++++++++++++++++++++ backend/tests/endpoints/test_screenshots.py | 50 +++++++++++++++ backend/tests/endpoints/test_states.py | 38 ++++++++++++ backend/tests/endpoints/test_streaming.py | 33 ++++++++++ 8 files changed, 216 insertions(+) diff --git a/backend/endpoints/saves.py b/backend/endpoints/saves.py index 989c91f0f..2dea75276 100644 --- a/backend/endpoints/saves.py +++ b/backend/endpoints/saves.py @@ -12,6 +12,7 @@ from endpoints.responses.assets import SaveSchema, SaveSummarySchema, SlotSummar from endpoints.responses.device import DeviceSyncSchema from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException from handler.auth.constants import Scope +from handler.auth.dependencies import assert_rom_visible from handler.database import ( db_device_handler, db_device_save_sync_handler, @@ -482,6 +483,13 @@ def download_save( status_code=status.HTTP_404_NOT_FOUND, detail=f"Save with ID {id} not found", ) + + # Sharing must not override the hidden-ROM/platform policy: a save on a ROM + # hidden from the caller stays 404-masked, just like the ROM itself. + assert_rom_visible( + request, save.rom, not_found_detail=f"Save with ID {id} not found" + ) + is_owner = save.user_id == request.user.id try: diff --git a/backend/endpoints/screenshots.py b/backend/endpoints/screenshots.py index 83e9a2225..b9366a3ee 100644 --- a/backend/endpoints/screenshots.py +++ b/backend/endpoints/screenshots.py @@ -9,6 +9,7 @@ from decorators.auth import protected_route from endpoints.responses.assets import ScreenshotSchema from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException from handler.auth.constants import Scope +from handler.auth.dependencies import assert_rom_visible from handler.database import db_rom_handler, db_screenshot_handler from handler.filesystem import fs_asset_handler from handler.filesystem.assets_handler import build_asset_file_response @@ -137,6 +138,10 @@ def download_screenshot( detail="Screenshot not found", ) + # Sharing must not override the hidden-ROM/platform policy: a screenshot on a + # ROM hidden from the caller stays 404-masked, just like the ROM itself. + assert_rom_visible(request, screenshot.rom, not_found_detail="Screenshot not found") + try: file_path = fs_asset_handler.validate_path(screenshot.full_path) except ValueError: diff --git a/backend/endpoints/states.py b/backend/endpoints/states.py index edf1f3087..3480ef87f 100644 --- a/backend/endpoints/states.py +++ b/backend/endpoints/states.py @@ -8,6 +8,7 @@ from decorators.auth import protected_route from endpoints.responses.assets import StateSchema from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException from handler.auth.constants import Scope +from handler.auth.dependencies import assert_rom_visible from handler.database import db_rom_handler, db_screenshot_handler, db_state_handler from handler.filesystem import fs_asset_handler from handler.filesystem.assets_handler import build_asset_file_response @@ -221,6 +222,12 @@ def download_state(request: Request, id: int) -> FileResponse: detail=f"State with ID {id} not found", ) + # Sharing must not override the hidden-ROM/platform policy: a state on a ROM + # hidden from the caller stays 404-masked, just like the ROM itself. + assert_rom_visible( + request, state.rom, not_found_detail=f"State with ID {id} not found" + ) + try: file_path = fs_asset_handler.validate_path(state.full_path) except ValueError: diff --git a/backend/endpoints/streaming.py b/backend/endpoints/streaming.py index 2fee36aff..e9e4bb404 100644 --- a/backend/endpoints/streaming.py +++ b/backend/endpoints/streaming.py @@ -15,6 +15,7 @@ from config import LIBRARY_BASE_PATH, STREAMING_BROKER_SECRET, STREAMING_SAVE_TI from config.config_manager import config_manager as cm from decorators.auth import protected_route from handler.auth.constants import Scope +from handler.auth.dependencies import assert_rom_visible from handler.database import db_rom_handler from handler.redis_handler import async_cache from models.user import Role @@ -507,6 +508,11 @@ async def claim_session( if rom is None: raise HTTPException(status_code=404, detail="ROM not found") + # A hidden ROM/platform must not be launchable via its id: enforce the same + # visibility policy as the ROM detail/content endpoints before any broker + # launch. Raises a 404 that masks the hidden ROM's existence. + assert_rom_visible(request, rom, not_found_detail="ROM not found") + container = _container_for_platform(rom.platform_slug) if container is None: raise HTTPException( diff --git a/backend/tests/endpoints/test_saves.py b/backend/tests/endpoints/test_saves.py index d46270a1b..bd6025ae0 100644 --- a/backend/tests/endpoints/test_saves.py +++ b/backend/tests/endpoints/test_saves.py @@ -13,13 +13,20 @@ from handler.database import ( db_device_save_sync_handler, db_save_handler, ) +from handler.database.base_handler import sync_session from models.assets import Save from models.device import Device +from models.permission import HiddenEntity, PermEntity from models.platform import Platform from models.rom import Rom from models.user import User +def _hide(entity: PermEntity, entity_id: int, user_id: int) -> None: + with sync_session.begin() as s: + s.add(HiddenEntity(entity=entity, entity_id=entity_id, user_id=user_id)) + + @pytest.fixture def device(admin_user: User): return db_device_handler.add_device( @@ -1933,6 +1940,68 @@ class TestSaveDownload: assert response.status_code == status.HTTP_404_NOT_FOUND assert "99999" in response.json()["detail"] + @mock.patch("endpoints.saves.fs_asset_handler.validate_path") + def test_other_user_downloads_public_save_on_visible_rom( + self, + mock_validate_path, + client, + viewer_access_token: str, + save: Save, + tmp_path, + ): + # Sanity: public sharing still works when the ROM is visible. + db_save_handler.update_save(save.id, {"is_public": True}) + test_file = tmp_path / "test.sav" + test_file.write_bytes(b"SHARED_SAVE") + mock_validate_path.return_value = test_file + + response = client.get( + f"/api/saves/{save.id}/content", + headers={"Authorization": f"Bearer {viewer_access_token}"}, + ) + + assert response.status_code == status.HTTP_200_OK + assert response.content == b"SHARED_SAVE" + + def test_hidden_rom_masks_public_save_download( + self, + client, + viewer_access_token: str, + viewer_user: User, + save: Save, + rom: Rom, + ): + # A public save on a ROM hidden from the caller must stay 404-masked; + # sharing cannot override the hidden-resource boundary. + db_save_handler.update_save(save.id, {"is_public": True}) + _hide(PermEntity.ROMS, rom.id, viewer_user.id) + + response = client.get( + f"/api/saves/{save.id}/content", + headers={"Authorization": f"Bearer {viewer_access_token}"}, + ) + + assert response.status_code == status.HTTP_404_NOT_FOUND + + def test_hidden_platform_masks_public_save_download( + self, + client, + viewer_access_token: str, + viewer_user: User, + save: Save, + platform: Platform, + ): + # Hiding the parent platform cascades to its saves as well. + db_save_handler.update_save(save.id, {"is_public": True}) + _hide(PermEntity.PLATFORMS, platform.id, viewer_user.id) + + response = client.get( + f"/api/saves/{save.id}/content", + headers={"Authorization": f"Bearer {viewer_access_token}"}, + ) + + assert response.status_code == status.HTTP_404_NOT_FOUND + @mock.patch("endpoints.saves.fs_asset_handler.validate_path") def test_download_save_file_missing_on_disk( self, diff --git a/backend/tests/endpoints/test_screenshots.py b/backend/tests/endpoints/test_screenshots.py index b5a0a1c01..a68ea7223 100644 --- a/backend/tests/endpoints/test_screenshots.py +++ b/backend/tests/endpoints/test_screenshots.py @@ -4,7 +4,9 @@ from unittest import mock from fastapi import status from handler.database import db_screenshot_handler +from handler.database.base_handler import sync_session from models.assets import Screenshot +from models.permission import HiddenEntity, PermEntity from models.platform import Platform from models.rom import Rom from models.user import User @@ -14,6 +16,11 @@ def _auth(token: str) -> dict[str, str]: return {"Authorization": f"Bearer {token}"} +def _hide(entity: PermEntity, entity_id: int, user_id: int) -> None: + with sync_session.begin() as s: + s.add(HiddenEntity(entity=entity, entity_id=entity_id, user_id=user_id)) + + # ---------- POST /api/screenshots ---------- @@ -296,6 +303,49 @@ def test_other_user_downloads_public_screenshot( assert response.content == b"SHARED_SHOT" +def test_hidden_rom_masks_public_screenshot_download( + client, + viewer_access_token: str, + viewer_user: User, + admin_user: User, + rom: Rom, + platform: Platform, +): + # A public screenshot on a ROM hidden from the caller must stay 404-masked; + # sharing cannot override the hidden-resource boundary. + screenshot = _add_screenshot( + rom, platform, admin_user.id, "shot", is_gallery=True, is_public=True + ) + _hide(PermEntity.ROMS, rom.id, viewer_user.id) + + response = client.get( + f"/api/screenshots/{screenshot.id}/content", + headers=_auth(viewer_access_token), + ) + assert response.status_code == status.HTTP_404_NOT_FOUND + + +def test_hidden_platform_masks_public_screenshot_download( + client, + viewer_access_token: str, + viewer_user: User, + admin_user: User, + rom: Rom, + platform: Platform, +): + # Hiding the parent platform cascades to its screenshots as well. + screenshot = _add_screenshot( + rom, platform, admin_user.id, "shot", is_gallery=True, is_public=True + ) + _hide(PermEntity.PLATFORMS, platform.id, viewer_user.id) + + response = client.get( + f"/api/screenshots/{screenshot.id}/content", + headers=_auth(viewer_access_token), + ) + assert response.status_code == status.HTTP_404_NOT_FOUND + + def test_download_screenshot_not_found(client, access_token: str): response = client.get("/api/screenshots/99999/content", headers=_auth(access_token)) assert response.status_code == status.HTTP_404_NOT_FOUND diff --git a/backend/tests/endpoints/test_states.py b/backend/tests/endpoints/test_states.py index 2018aa08c..e92eb96d2 100644 --- a/backend/tests/endpoints/test_states.py +++ b/backend/tests/endpoints/test_states.py @@ -3,7 +3,9 @@ from unittest import mock from fastapi import status from handler.database import db_screenshot_handler, db_state_handler +from handler.database.base_handler import sync_session from models.assets import Screenshot, State +from models.permission import HiddenEntity, PermEntity from models.platform import Platform from models.rom import Rom from models.user import User @@ -13,6 +15,11 @@ def _auth(token: str) -> dict[str, str]: return {"Authorization": f"Bearer {token}"} +def _hide(entity: PermEntity, entity_id: int, user_id: int) -> None: + with sync_session.begin() as s: + s.add(HiddenEntity(entity=entity, entity_id=entity_id, user_id=user_id)) + + @mock.patch("endpoints.states.fs_asset_handler.validate_path") def test_owner_downloads_own_state( mock_validate_path, client, access_token: str, state: State, tmp_path @@ -53,6 +60,37 @@ def test_other_user_downloads_public_state( assert response.content == b"SHARED_STATE" +def test_hidden_rom_masks_public_state_download( + client, viewer_access_token: str, viewer_user: User, state: State, rom: Rom +): + # A public state on a ROM hidden from the caller must stay 404-masked; + # sharing cannot override the hidden-resource boundary. + db_state_handler.update_state(state.id, {"is_public": True}) + _hide(PermEntity.ROMS, rom.id, viewer_user.id) + + response = client.get( + f"/api/states/{state.id}/content", headers=_auth(viewer_access_token) + ) + assert response.status_code == status.HTTP_404_NOT_FOUND + + +def test_hidden_platform_masks_public_state_download( + client, + viewer_access_token: str, + viewer_user: User, + state: State, + platform: Platform, +): + # Hiding the parent platform cascades to its states as well. + db_state_handler.update_state(state.id, {"is_public": True}) + _hide(PermEntity.PLATFORMS, platform.id, viewer_user.id) + + response = client.get( + f"/api/states/{state.id}/content", headers=_auth(viewer_access_token) + ) + assert response.status_code == status.HTTP_404_NOT_FOUND + + def test_download_state_not_found(client, access_token: str): response = client.get("/api/states/99999/content", headers=_auth(access_token)) assert response.status_code == status.HTTP_404_NOT_FOUND diff --git a/backend/tests/endpoints/test_streaming.py b/backend/tests/endpoints/test_streaming.py index a0111d77e..6babcf8df 100644 --- a/backend/tests/endpoints/test_streaming.py +++ b/backend/tests/endpoints/test_streaming.py @@ -12,12 +12,19 @@ from main import app from config import LIBRARY_BASE_PATH, OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS from handler.auth import oauth_handler from handler.database import db_platform_handler, db_rom_handler +from handler.database.base_handler import sync_session from handler.redis_handler import async_cache +from models.permission import HiddenEntity, PermEntity from models.platform import Platform from models.rom import Rom from models.user import User +def _hide(entity: PermEntity, entity_id: int, user_id: int) -> None: + with sync_session.begin() as s: + s.add(HiddenEntity(entity=entity, entity_id=entity_id, user_id=user_id)) + + @pytest.fixture def client(): with TestClient(app) as client: @@ -173,6 +180,32 @@ def test_claim_unknown_rom_returns_404(client, access_token): assert r.status_code == 404 +def test_claim_hidden_rom_is_404_masked( + client, viewer_access_token, viewer_user: User, rom: Rom +): + """A user with roms.read cannot claim a session for a ROM hidden from them; + the launch must be 404-masked before any broker call.""" + _hide(PermEntity.ROMS, rom.id, viewer_user.id) + with _streaming(_container_for(rom)): + # If the visibility check were missing this would 200 and launch. + with patch("endpoints.streaming._call_broker") as call_broker: + r = _claim(client, viewer_access_token, rom.id) + assert r.status_code == 404 + call_broker.assert_not_called() + + +def test_claim_rom_on_hidden_platform_is_404_masked( + client, viewer_access_token, viewer_user: User, rom: Rom, platform: Platform +): + """Hiding the parent platform cascades: its ROMs cannot be streamed either.""" + _hide(PermEntity.PLATFORMS, platform.id, viewer_user.id) + with _streaming(_container_for(rom)): + with patch("endpoints.streaming._call_broker") as call_broker: + r = _claim(client, viewer_access_token, rom.id) + assert r.status_code == 404 + call_broker.assert_not_called() + + def test_claim_skips_container_with_schemeless_host(client, access_token, rom: Rom): """A container whose host has no scheme would produce a broken broker URL and a colliding session key; it must be skipped (404), not a 500 KeyError."""