diff --git a/backend/endpoints/roms/notes.py b/backend/endpoints/roms/notes.py index f75af7607..7457e1edf 100644 --- a/backend/endpoints/roms/notes.py +++ b/backend/endpoints/roms/notes.py @@ -132,6 +132,7 @@ async def update_rom_note( note = db_rom_handler.update_rom_note( note_id=note_id, user_id=request.user.id, + rom_id=rom.id, **{ k: v for k, v in note_data.items() @@ -168,7 +169,9 @@ async def delete_rom_note( raise RomNotFoundInDatabaseException(id) assert_rom_visible(request, rom) - success = db_rom_handler.delete_rom_note(note_id=note_id, user_id=request.user.id) + success = db_rom_handler.delete_rom_note( + note_id=note_id, user_id=request.user.id, rom_id=rom.id + ) if not success: raise HTTPException( diff --git a/backend/handler/database/roms_handler.py b/backend/handler/database/roms_handler.py index 51a319d26..0a64984a5 100644 --- a/backend/handler/database/roms_handler.py +++ b/backend/handler/database/roms_handler.py @@ -2377,12 +2377,17 @@ class DBRomsHandler(DBBaseHandler): self, note_id: int, user_id: int, + rom_id: int, session: Session = None, # type: ignore **fields, ) -> dict | None: note = ( session.query(RomNote) - .filter(RomNote.id == note_id, RomNote.user_id == user_id) + .filter( + RomNote.id == note_id, + RomNote.user_id == user_id, + RomNote.rom_id == rom_id, + ) .first() ) @@ -2413,11 +2418,16 @@ class DBRomsHandler(DBBaseHandler): self, note_id: int, user_id: int, + rom_id: int, session: Session = None, # type: ignore ) -> bool: result = session.execute( delete(RomNote).where( - and_(RomNote.id == note_id, RomNote.user_id == user_id) + and_( + RomNote.id == note_id, + RomNote.user_id == user_id, + RomNote.rom_id == rom_id, + ) ) ) return result.rowcount > 0 diff --git a/backend/tests/endpoints/test_permissions_visibility.py b/backend/tests/endpoints/test_permissions_visibility.py index a35cb1540..9137826f5 100644 --- a/backend/tests/endpoints/test_permissions_visibility.py +++ b/backend/tests/endpoints/test_permissions_visibility.py @@ -23,7 +23,7 @@ from models.permission import ( PermissionGroup, PermissionGroupGrant, ) -from models.rom import RomFile, RomFileCategory +from models.rom import Rom, RomFile, RomFileCategory def _auth(user): @@ -246,6 +246,41 @@ def test_hidden_rom_note_write_routes_are_404_masked(client, viewer_user, rom): assert delete.status_code == status.HTTP_404_NOT_FOUND +def test_note_on_hidden_rom_not_reachable_via_visible_rom_path( + client, viewer_user, rom, platform +): + # The path rom only authorizes itself: a note belonging to a hidden rom must + # not be reachable by pairing its id with a visible rom in the path. + hidden = db_rom_handler.add_rom( + Rom( + platform_id=platform.id, + name="hidden_rom", + slug="hidden_rom_slug", + fs_name="hidden_rom.zip", + fs_name_no_tags="hidden_rom", + fs_name_no_ext="hidden_rom", + fs_extension="zip", + fs_path=f"{platform.slug}/roms", + ) + ) + note = db_rom_handler.create_rom_note( + rom_id=hidden.id, user_id=viewer_user.id, title="secret" + ) + _hide(PermEntity.ROMS, hidden.id, viewer_user.id) + headers = _auth(viewer_user) + + update = client.put( + f"/api/roms/{rom.id}/notes/{note['id']}", headers=headers, json={"title": "x"} + ) + assert update.status_code == status.HTTP_404_NOT_FOUND + + delete = client.delete(f"/api/roms/{rom.id}/notes/{note['id']}", headers=headers) + assert delete.status_code == status.HTTP_404_NOT_FOUND + + surviving = db_rom_handler.get_rom_notes(rom_id=hidden.id, user_id=viewer_user.id) + assert [n.title for n in surviving] == ["secret"] + + 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))