From 7bbf94c08c8e5a7d0a8a84315809d8dfc8e7f506 Mon Sep 17 00:00:00 2001 From: Spinnich Date: Sun, 2 Aug 2026 20:04:30 +0000 Subject: [PATCH] fix(roms): re-check visibility on the random pick after fetching The id comes from a permission-filtered query but the row is fetched by raw id, so a rom that moved to a hidden platform in between was picked under its old one and would serialize under its new one. Re-check the loaded row against the caller's permissions, as every other raw-id rom lookup does. Returns null rather than raising 404: it keeps a hidden rom indistinguishable from an empty scope, and keeps an error path out of a contract that otherwise has none. --- backend/endpoints/roms/__init__.py | 7 +++++ backend/tests/endpoints/roms/test_random.py | 34 +++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index ab4c0e1fb..59a49d476 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -951,6 +951,13 @@ def get_random_rom( if not rom: return None + # The fetch is by raw id, so it re-checks the row it actually loaded rather + # than trusting the filter that chose the id: a rom that moved to a hidden + # platform in between was picked under its old one. Reads no database, and + # null keeps a hidden rom indistinguishable from an empty scope. + if not perms.can_see_rom(rom.id, rom.platform_id): + return None + return SimpleRomSchema.from_orm_with_request(rom, request) diff --git a/backend/tests/endpoints/roms/test_random.py b/backend/tests/endpoints/roms/test_random.py index 52a905fda..b92974042 100644 --- a/backend/tests/endpoints/roms/test_random.py +++ b/backend/tests/endpoints/roms/test_random.py @@ -215,6 +215,40 @@ def test_get_random_rom_skips_hidden_roms( assert response.json()["id"] == visible.id +def test_get_random_rom_rechecks_visibility_after_fetching( + client: TestClient, + viewer_user: User, + viewer_access_token: str, + rom: Rom, + platform: Platform, +) -> None: + """The pick is re-checked against the row that came back, not the filter. + + The id is chosen by a filtered query but fetched by raw id, so a rom that + moved to a hidden platform in between would arrive hidden. Standing in for + that race by handing the endpoint a rom the filter would have excluded. + """ + # A visible rom so the pick resolves an id, otherwise the empty scope + # returns null on its own and the fetch is never reached. + _add_rom(platform, "visible_rom") + with sync_session.begin() as session: + session.add( + HiddenEntity( + entity=PermEntity.ROMS, entity_id=rom.id, user_id=viewer_user.id + ) + ) + + with patch.object(db_rom_handler, "get_rom_simple", return_value=rom): + response = client.get( + "/api/roms/random", + headers={"Authorization": f"Bearer {viewer_access_token}"}, + ) + + assert response.status_code == status.HTTP_200_OK + # Null, not 404: a hidden rom stays indistinguishable from an empty scope. + assert response.json() is None + + def test_get_random_rom_does_not_page_or_count( client: TestClient, access_token: str,