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.
This commit is contained in:
Spinnich
2026-08-02 20:04:30 +00:00
parent 1ee6cea070
commit 7bbf94c08c
2 changed files with 41 additions and 0 deletions

View File

@@ -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)

View File

@@ -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,