Merge pull request #4376 from rommapp/fix/export-endpoints-require-platforms-write

fix(auth): require platforms.write and enforce platform visibility on the export endpoints
This commit is contained in:
Georges-Antoine Assi
2026-09-06 12:02:02 -04:00
committed by GitHub
3 changed files with 127 additions and 2 deletions

View File

@@ -4,7 +4,10 @@ from fastapi import HTTPException, Query, Request, status
from fastapi.responses import Response
from decorators.auth import protected_route
from exceptions.endpoint_exceptions import PlatformNotFoundInDatabaseException
from handler.auth.constants import Scope
from handler.auth.dependencies import assert_platform_visible
from handler.database import db_platform_handler
from logger.formatter import BLUE
from logger.formatter import highlight as hl
from logger.logger import log
@@ -18,7 +21,20 @@ router = APIRouter(
)
@protected_route(router.post, "/gamelist-xml", [Scope.ROMS_READ])
def _assert_platforms_exportable(request: Request, platform_ids: List[int]) -> None:
"""Reject the whole request before anything is written to disk.
A platform hidden from the caller 404s exactly like a missing one, so the
response never reveals that the platform exists.
"""
for platform_id in platform_ids:
platform = db_platform_handler.get_platform(platform_id)
if not platform:
raise PlatformNotFoundInDatabaseException(platform_id)
assert_platform_visible(request, platform)
@protected_route(router.post, "/gamelist-xml", [Scope.PLATFORMS_WRITE])
async def export_gamelist_xml(
request: Request,
platform_ids: Annotated[
@@ -35,6 +51,8 @@ async def export_gamelist_xml(
detail="At least one platform ID must be provided",
)
_assert_platforms_exportable(request, platform_ids)
try:
exporter = GamelistExporter(local_export=local_export)
files_written = []
@@ -71,7 +89,7 @@ async def export_gamelist_xml(
) from e
@protected_route(router.post, "/pegasus", [Scope.ROMS_READ])
@protected_route(router.post, "/pegasus", [Scope.PLATFORMS_WRITE])
async def export_pegasus(
request: Request,
platform_ids: Annotated[
@@ -88,6 +106,8 @@ async def export_pegasus(
detail="At least one platform ID must be provided",
)
_assert_platforms_exportable(request, platform_ids)
try:
exporter = PegasusExporter(local_export=local_export)
files_written = []

View File

@@ -0,0 +1,80 @@
from unittest.mock import AsyncMock, patch
import pytest
from fastapi import status
from utils.gamelist_exporter import GamelistExporter
from utils.pegasus_exporter import PegasusExporter
EXPORTERS = {
"/api/export/gamelist-xml": GamelistExporter,
"/api/export/pegasus": PegasusExporter,
}
with_endpoint = pytest.mark.parametrize("path", list(EXPORTERS))
@with_endpoint
def test_export_rejects_viewer(client, viewer_access_token: str, platform, path: str):
# Both endpoints write into the library, so reading ROMs is not enough.
response = client.post(
f"{path}?platform_ids={platform.id}",
headers={"Authorization": f"Bearer {viewer_access_token}"},
)
assert response.status_code == status.HTTP_403_FORBIDDEN
@with_endpoint
def test_export_rejects_anonymous(client, platform, path: str):
response = client.post(f"{path}?platform_ids={platform.id}")
assert response.status_code == status.HTTP_401_UNAUTHORIZED
@with_endpoint
def test_export_allows_editor(client, editor_access_token: str, platform, path: str):
with patch.object(
EXPORTERS[path],
"export_platform_to_file",
new_callable=AsyncMock,
return_value=True,
) as export_mock:
response = client.post(
f"{path}?platform_ids={platform.id}",
headers={"Authorization": f"Bearer {editor_access_token}"},
)
assert response.status_code == status.HTTP_200_OK
export_mock.assert_awaited_once()
@with_endpoint
def test_export_unknown_platform_is_404(client, editor_access_token: str, path: str):
# Matches the 404 a hidden platform gets, so the response is not an oracle
# for whether the platform exists (see test_permissions_visibility).
with patch.object(
EXPORTERS[path], "export_platform_to_file", new_callable=AsyncMock
) as export_mock:
response = client.post(
f"{path}?platform_ids=99999",
headers={"Authorization": f"Bearer {editor_access_token}"},
)
assert response.status_code == status.HTTP_404_NOT_FOUND
export_mock.assert_not_awaited()
@with_endpoint
def test_export_rejects_batch_with_one_unknown_platform(
client, editor_access_token: str, platform, path: str
):
# The whole request is refused up front, so a partial batch never writes.
with patch.object(
EXPORTERS[path], "export_platform_to_file", new_callable=AsyncMock
) as export_mock:
response = client.post(
f"{path}?platform_ids={platform.id}&platform_ids=99999",
headers={"Authorization": f"Bearer {editor_access_token}"},
)
assert response.status_code == status.HTTP_404_NOT_FOUND
export_mock.assert_not_awaited()

View File

@@ -26,6 +26,8 @@ from models.permission import (
PermissionGroupGrant,
)
from models.rom import Rom, RomFile, RomFileCategory
from utils.gamelist_exporter import GamelistExporter
from utils.pegasus_exporter import PegasusExporter
def _auth(user):
@@ -155,6 +157,29 @@ def test_physical_game_create_on_hidden_platform_is_404_masked(
assert db_rom_handler.get_roms_scalar(platform_ids=[platform.id]) == []
@pytest.mark.parametrize(
("path", "exporter"),
[
("/api/export/gamelist-xml", GamelistExporter),
("/api/export/pegasus", PegasusExporter),
],
)
def test_export_of_hidden_platform_is_404_masked(
client, editor_user, platform, monkeypatch, path, exporter
):
# Editor holds platforms write, so the coarse gate passes. The hide must
# mask the export before it reads or rewrites the platform's metadata file.
_hide(PermEntity.PLATFORMS, platform.id, editor_user.id)
async def _unreachable(*args, **kwargs):
raise AssertionError("exported a hidden platform")
monkeypatch.setattr(exporter, "export_platform_to_file", _unreachable)
resp = client.post(f"{path}?platform_ids={platform.id}", headers=_auth(editor_user))
assert resp.status_code == status.HTTP_404_NOT_FOUND
def test_hidden_rom_props_update_is_404_masked(client, viewer_user, rom):
# ROMS_USER_WRITE is a self-service scope every user holds, so the coarse
# gate passes; the hidden rom must still be masked, not confirmed.