fix(sync): reject RetroArch names over 255 bytes with 409

Covers the client's own name and a state screenshot named after a longer
owning state, both of which failed the write with a 500.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-10-01 16:31:55 -04:00
parent 9bb8efac25
commit 405183b98a
2 changed files with 48 additions and 1 deletions

View File

@@ -36,7 +36,7 @@ from logger.logger import log
from models.assets import Save, Screenshot, State
from models.rom import Rom
from models.user import User
from utils.filesystem import sanitize_filename
from utils.filesystem import check_filename_length, sanitize_filename
router = APIRouter(prefix="/retroarch")
@@ -496,6 +496,7 @@ async def retroarch_sync_put(request: Request, file_path: str) -> Response:
try:
if sanitize_filename(file_name) != file_name:
return _empty(status.HTTP_409_CONFLICT)
check_filename_length(file_name)
except ValueError:
return _empty(status.HTTP_409_CONFLICT)
@@ -518,6 +519,10 @@ async def retroarch_sync_put(request: Request, file_path: str) -> Response:
screenshot_file_name = (
f"{owning_state.file_name}.png" if owning_state else file_name
)
try:
check_filename_length(screenshot_file_name)
except ValueError:
return _empty(status.HTTP_409_CONFLICT)
screenshot_path = sync_handler.state_screenshot_dir(
request.user, rom, parsed.emulator

View File

@@ -13,6 +13,7 @@ from tests.factories import make_rom, make_save, make_screenshot
from handler.database import (
db_deleted_asset_handler,
db_device_handler,
db_rom_handler,
db_save_handler,
db_screenshot_handler,
db_state_handler,
@@ -553,6 +554,25 @@ class TestRetroArchSyncStateScreenshots:
)
assert screenshots is not None
@mock.patch(
"endpoints.sync.retroarch.fs_asset_handler.write_file",
new_callable=mock.AsyncMock,
)
def test_rejects_a_screenshot_the_owning_state_name_pushes_over_255_bytes(
self, mock_write_file: mock.AsyncMock, client, make_state
):
# The client's name fits, but the screenshot takes the slot's longer state name.
make_state(f"test_rom [{'x' * 236}].state")
response = client.put(
"/api/sync/retroarch/states/Snes9x/test_rom.state.png",
content=b"pngdata",
auth=ADMIN_AUTH,
)
assert response.status_code == status.HTTP_409_CONFLICT
mock_write_file.assert_not_awaited()
@mock.patch(
"endpoints.sync.retroarch.fs_asset_handler.write_file",
new_callable=mock.AsyncMock,
@@ -808,6 +828,28 @@ class TestRetroArchSyncUpload:
assert response.status_code == status.HTTP_409_CONFLICT
@mock.patch(
"endpoints.sync.retroarch.fs_asset_handler.write_file",
new_callable=mock.AsyncMock,
)
def test_rejects_a_name_over_255_bytes(
self, mock_write_file: mock.AsyncMock, client, admin_user: User, rom: Rom
):
# The ROM's own 254-byte name fits; its save's longer extension does not.
stem = "a" * 252
db_rom_handler.update_rom(
rom.id, {"fs_name": f"{stem}.z", "fs_name_no_ext": stem}
)
response = client.put(
f"/api/sync/retroarch/saves/Snes9x/{stem}.srm",
content=b"data",
auth=ADMIN_AUTH,
)
assert response.status_code == status.HTTP_409_CONFLICT
mock_write_file.assert_not_awaited()
@mock.patch(
"endpoints.sync.retroarch.fs_asset_handler.write_file",
new_callable=mock.AsyncMock,