mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 18:18:47 +01:00
Enforce hidden-rom visibility on rom child routes
The screenshot, soundtrack, manual and note routes looked up the rom by
path id but never ran the visibility gate that the core rom routes and
roms/files.py already apply, so a hidden rom stayed reachable through its
child resources.
Add assert_rom_visible() after each lookup. update_rom_note and
delete_rom_note took {id} in the path without resolving it at all, so
they now load the rom first.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,7 @@ from decorators.auth import protected_route
|
||||
from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException
|
||||
from exceptions.fs_exceptions import RomAlreadyExistsException
|
||||
from handler.auth.constants import Scope
|
||||
from handler.auth.dependencies import assert_rom_visible
|
||||
from handler.database import db_rom_handler
|
||||
from handler.filesystem import fs_resource_handler, fs_rom_handler
|
||||
from handler.filesystem.resources_handler import ALLOWED_MANUAL_EXTENSIONS
|
||||
@@ -56,6 +57,7 @@ async def add_rom_manuals(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
if not _is_allowed_manual_file(filename):
|
||||
raise HTTPException(
|
||||
@@ -145,6 +147,7 @@ async def redownload_rom_manual(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
if not rom.url_manual:
|
||||
raise HTTPException(
|
||||
@@ -199,6 +202,7 @@ async def add_rom_manual_file(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
if rom.has_simple_single_file:
|
||||
try:
|
||||
@@ -306,6 +310,7 @@ async def delete_rom_manual_file(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
rom_file = db_rom_handler.get_rom_file_by_id(file_id)
|
||||
if (
|
||||
@@ -359,6 +364,7 @@ async def delete_rom_manuals(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
if not fs_resource_handler.manual_exists(rom):
|
||||
raise HTTPException(
|
||||
|
||||
@@ -8,6 +8,7 @@ from decorators.auth import protected_route
|
||||
from endpoints.responses.rom import UserNoteSchema
|
||||
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
|
||||
from models.rom import RomNote
|
||||
from utils.router import APIRouter
|
||||
@@ -36,6 +37,7 @@ async def get_rom_notes(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
if tags is None:
|
||||
tags = []
|
||||
@@ -65,6 +67,7 @@ async def get_rom_note_identifiers(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
notes = db_rom_handler.get_rom_notes(
|
||||
rom_id=id,
|
||||
@@ -90,6 +93,7 @@ async def create_rom_note(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
note = db_rom_handler.create_rom_note(
|
||||
rom_id=id,
|
||||
@@ -120,6 +124,11 @@ async def update_rom_note(
|
||||
note_data: Annotated[dict, Body()],
|
||||
) -> UserNoteSchema:
|
||||
"""Update a ROM note."""
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
note = db_rom_handler.update_rom_note(
|
||||
note_id=note_id,
|
||||
user_id=request.user.id,
|
||||
@@ -154,6 +163,11 @@ async def delete_rom_note(
|
||||
note_id: Annotated[int, PathVar(description="Note id.", ge=1)],
|
||||
) -> dict:
|
||||
"""Delete a ROM note."""
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
success = db_rom_handler.delete_rom_note(note_id=note_id, user_id=request.user.id)
|
||||
|
||||
if not success:
|
||||
|
||||
@@ -13,6 +13,7 @@ from decorators.auth import protected_route
|
||||
from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException
|
||||
from exceptions.fs_exceptions import RomAlreadyExistsException
|
||||
from handler.auth.constants import Scope
|
||||
from handler.auth.dependencies import assert_rom_visible
|
||||
from handler.database import db_rom_handler
|
||||
from handler.filesystem import fs_rom_handler
|
||||
from handler.rom_conversion import promote_single_file_to_folder
|
||||
@@ -54,6 +55,7 @@ async def add_rom_screenshots(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
if rom.has_simple_single_file:
|
||||
try:
|
||||
@@ -163,6 +165,7 @@ async def delete_rom_screenshot(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
rom_file = db_rom_handler.get_rom_file_by_id(file_id)
|
||||
if (
|
||||
|
||||
@@ -14,6 +14,7 @@ from endpoints.responses.rom import SoundtrackTrackMetaSchema, TrackMetaSchema
|
||||
from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException
|
||||
from exceptions.fs_exceptions import RomAlreadyExistsException
|
||||
from handler.auth.constants import Scope
|
||||
from handler.auth.dependencies import assert_rom_visible
|
||||
from handler.database import db_rom_handler
|
||||
from handler.filesystem import fs_rom_handler
|
||||
from handler.rom_conversion import promote_single_file_to_folder
|
||||
@@ -51,6 +52,7 @@ async def get_rom_soundtrack_metadata(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
tracks = db_rom_handler.get_rom_files_by_category(
|
||||
rom_id=rom.id, category=RomFileCategory.SOUNDTRACK
|
||||
@@ -92,6 +94,7 @@ async def add_rom_soundtracks(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
if rom.has_simple_single_file:
|
||||
try:
|
||||
@@ -228,6 +231,7 @@ async def delete_rom_soundtrack(
|
||||
rom = db_rom_handler.get_rom(id)
|
||||
if not rom:
|
||||
raise RomNotFoundInDatabaseException(id)
|
||||
assert_rom_visible(request, rom)
|
||||
|
||||
rom_file = db_rom_handler.get_rom_file_by_id(file_id)
|
||||
if (
|
||||
|
||||
@@ -13,8 +13,9 @@ from fastapi import status
|
||||
|
||||
from config import OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS
|
||||
from handler.auth import oauth_handler
|
||||
from handler.database import db_user_handler
|
||||
from handler.database import db_rom_handler, db_user_handler
|
||||
from handler.database.base_handler import sync_session
|
||||
from handler.filesystem import fs_resource_handler
|
||||
from models.permission import (
|
||||
HiddenEntity,
|
||||
PermAction,
|
||||
@@ -22,6 +23,7 @@ from models.permission import (
|
||||
PermissionGroup,
|
||||
PermissionGroupGrant,
|
||||
)
|
||||
from models.rom import RomFile, RomFileCategory
|
||||
|
||||
|
||||
def _auth(user):
|
||||
@@ -151,6 +153,105 @@ def test_hidden_rom_patch_is_404_masked(client, viewer_user, rom, rom_file):
|
||||
assert resp.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("path", "headers"),
|
||||
[
|
||||
("/screenshots", {"x-upload-filename": "shot.png"}),
|
||||
("/soundtracks", {"x-upload-filename": "track.mp3"}),
|
||||
("/manuals", {"x-upload-filename": "manual.pdf"}),
|
||||
("/manuals/files", {"x-upload-filename": "manual.pdf"}),
|
||||
("/manuals/redownload", {}),
|
||||
],
|
||||
)
|
||||
def test_hidden_rom_child_upload_routes_are_404_masked(
|
||||
client, editor_user, rom, path, headers
|
||||
):
|
||||
# Editor holds library-wide roms write, so the coarse scope gate passes; the
|
||||
# child media routes must still mask the hidden rom instead of writing to it.
|
||||
_hide(PermEntity.ROMS, rom.id, editor_user.id)
|
||||
resp = client.post(
|
||||
f"/api/roms/{rom.id}{path}", headers={**_auth(editor_user), **headers}
|
||||
)
|
||||
assert resp.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("path", "category"),
|
||||
[
|
||||
("/screenshots/{file_id}", RomFileCategory.SCREENSHOT),
|
||||
("/soundtracks/{file_id}", RomFileCategory.SOUNDTRACK),
|
||||
("/manuals/files/{file_id}", RomFileCategory.MANUAL),
|
||||
],
|
||||
)
|
||||
def test_hidden_rom_child_delete_routes_are_404_masked(
|
||||
client, editor_user, rom, path, category
|
||||
):
|
||||
# The child file must match the route's category, otherwise the route 404s
|
||||
# on the category check and the visibility gate is never exercised.
|
||||
child = db_rom_handler.add_rom_file(
|
||||
RomFile(
|
||||
rom_id=rom.id,
|
||||
file_name=f"child.{category}",
|
||||
file_path=f"{rom.fs_path}/{category}",
|
||||
file_size_bytes=10,
|
||||
category=category,
|
||||
)
|
||||
)
|
||||
_hide(PermEntity.ROMS, rom.id, editor_user.id)
|
||||
|
||||
resp = client.delete(
|
||||
f"/api/roms/{rom.id}{path.format(file_id=child.id)}",
|
||||
headers=_auth(editor_user),
|
||||
)
|
||||
assert resp.status_code == status.HTTP_404_NOT_FOUND
|
||||
# The row must survive: masking is worthless if the sink already ran.
|
||||
assert db_rom_handler.get_rom_file_by_id(child.id) is not None
|
||||
|
||||
|
||||
def test_hidden_rom_manual_delete_is_404_masked(client, editor_user, rom, monkeypatch):
|
||||
# Without a manual on disk the route 404s before the visibility gate, so
|
||||
# force the existence check to pass.
|
||||
monkeypatch.setattr(fs_resource_handler, "manual_exists", lambda _rom: True)
|
||||
_hide(PermEntity.ROMS, rom.id, editor_user.id)
|
||||
|
||||
resp = client.delete(f"/api/roms/{rom.id}/manuals", headers=_auth(editor_user))
|
||||
assert resp.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"path", ["/soundtracks/metadata", "/notes", "/notes/identifiers"]
|
||||
)
|
||||
def test_hidden_rom_child_read_routes_are_404_masked(client, viewer_user, rom, path):
|
||||
_hide(PermEntity.ROMS, rom.id, viewer_user.id)
|
||||
resp = client.get(f"/api/roms/{rom.id}{path}", headers=_auth(viewer_user))
|
||||
assert resp.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
|
||||
def test_hidden_rom_note_write_routes_are_404_masked(client, viewer_user, rom):
|
||||
# ROMS_USER_WRITE is self-service, so only the entity check can mask these.
|
||||
_hide(PermEntity.ROMS, rom.id, viewer_user.id)
|
||||
headers = _auth(viewer_user)
|
||||
|
||||
create = client.post(
|
||||
f"/api/roms/{rom.id}/notes", headers=headers, json={"title": "x"}
|
||||
)
|
||||
assert create.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
update = client.put(
|
||||
f"/api/roms/{rom.id}/notes/1", headers=headers, json={"title": "x"}
|
||||
)
|
||||
assert update.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
delete = client.delete(f"/api/roms/{rom.id}/notes/1", headers=headers)
|
||||
assert delete.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
|
||||
def test_visible_rom_child_route_is_not_masked(client, viewer_user, rom):
|
||||
# Control: the same route reaches its handler when the rom isn't hidden.
|
||||
resp = client.get(f"/api/roms/{rom.id}/notes", headers=_auth(viewer_user))
|
||||
assert resp.status_code == status.HTTP_200_OK
|
||||
|
||||
|
||||
def test_delete_requires_delete_grant_even_with_write_scope(
|
||||
client, viewer_user, platform
|
||||
):
|
||||
|
||||
Reference in New Issue
Block a user