diff --git a/backend/handler/sync/retroarch/psp.py b/backend/handler/sync/retroarch/psp.py index a6870b5e1..9b7a2aca6 100644 --- a/backend/handler/sync/retroarch/psp.py +++ b/backend/handler/sync/retroarch/psp.py @@ -52,6 +52,11 @@ _PENDING_CATEGORY = "psp_pending" _BUNDLE_MAX_MEMBERS = 64 BUNDLE_MAX_UNCOMPRESSED_BYTES = 64 * 1024 * 1024 +# A PSP save's PARAM.SFO is under 5 KiB and its TITLE field holds 128 bytes, so +# anything larger is crafted, and a longer title would become a huge ROM search. +_SFO_MAX_BYTES = 64 * 1024 +_SFO_TITLE_MAX_LENGTH = 128 + _BUNDLE_FOLDER_PATTERN = re.compile(r"^PSP-(.+?)(?: \[.*])?\.zip$") @@ -166,6 +171,8 @@ def parse_sfo(data: bytes) -> dict[str, str | int]: """ if len(data) < 20 or data[0:4] != b"\x00PSF": raise ValueError("Not a PARAM.SFO file (bad magic)") + if len(data) > _SFO_MAX_BYTES: + raise ValueError(f"PARAM.SFO file too large ({len(data)} bytes)") try: return _parse_sfo_tables(data) @@ -379,6 +386,9 @@ async def _resolve_folder_rom( log.warning(f"Failed to parse PARAM.SFO: {exc}") else: sfo_title = title if isinstance(title, str) else None + if sfo_title and len(sfo_title) > _SFO_TITLE_MAX_LENGTH: + log.warning(f"Ignoring PARAM.SFO TITLE of {len(sfo_title)} characters") + sfo_title = None rom = await asyncio.to_thread(_resolve_rom, info.save_folder, sfo_title, can_see) if rom: diff --git a/backend/tests/handler/sync/retroarch/test_psp.py b/backend/tests/handler/sync/retroarch/test_psp.py index 747dfe700..27fcbb6e9 100644 --- a/backend/tests/handler/sync/retroarch/test_psp.py +++ b/backend/tests/handler/sync/retroarch/test_psp.py @@ -4,8 +4,11 @@ from pathlib import Path import pytest +from handler.sync.retroarch import psp from handler.sync.retroarch.psp import ( _BUNDLE_MAX_MEMBERS, + _SFO_MAX_BYTES, + _SFO_TITLE_MAX_LENGTH, PspFilePath, _bundle_folder, _load_bundle_entries, @@ -158,3 +161,36 @@ class TestParseSfo: header = b"\x00PSF" + struct.pack("