fix(sync): bound PSP PARAM.SFO size and TITLE length before ROM search (#5631)

A PARAM.SFO up to the upload limit was parsed in full, and its TITLE,
of any length, became the ROM search term. The search builds one SQL
condition per word, so a title of millions of words exhausted the
worker's memory before any query ran.

Reject PARAM.SFO files over 64 KiB (real PSP saves are under 5 KiB) and
ignore a TITLE longer than the 128-byte field the format defines.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-11-17 08:48:05 -05:00
committed by GitHub
parent 494b4b4291
commit 00e40bdeb1
2 changed files with 46 additions and 0 deletions

View File

@@ -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:

View File

@@ -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("<IIII", 0x0101, 20, 20, 1000)
with pytest.raises(ValueError):
parse_sfo(header)
def test_rejects_an_oversized_file(self):
sfo = _build_fake_sfo({"TITLE": "x " * _SFO_MAX_BYTES})
with pytest.raises(ValueError):
parse_sfo(sfo)
class TestResolveFolderRom:
INFO = PspFilePath(
emulator="ppsspp", save_folder="ULUS10336DATA0", file_name="PARAM.SFO"
)
@pytest.mark.parametrize(
("title", "expected"),
[
("CRISIS CORE", "CRISIS CORE"),
("x" * _SFO_TITLE_MAX_LENGTH, "x" * _SFO_TITLE_MAX_LENGTH),
("x" * (_SFO_TITLE_MAX_LENGTH + 1), None),
],
)
async def test_searches_only_by_a_title_that_fits_the_field(
self, mocker, title: str, expected: str | None
):
rom = object()
resolve = mocker.patch.object(psp, "_resolve_rom", return_value=rom)
resolved = await psp._resolve_folder_rom(
self.INFO, _build_fake_sfo({"TITLE": title}), "pending", lambda _: True
)
assert resolved is rom
resolve.assert_called_once()
assert resolve.call_args.args[1] == expected