mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
Bind note update/delete to the rom in the path
update_rom_note and delete_rom_note selected on note_id and user_id only, so the rom resolved from the path never constrained which note was touched. Pairing a note id with any other rom id in the path reached that note regardless. Both handlers now filter on rom_id as well, and the endpoints pass the rom they resolved. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user