From 00e40bdeb1ea39da55a22a2afa862c8ddc0d9680 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi <3247106+gantoine@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:48:05 -0500 Subject: [PATCH] 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 --- backend/handler/sync/retroarch/psp.py | 10 ++++++ .../tests/handler/sync/retroarch/test_psp.py | 36 +++++++++++++++++++ 2 files changed, 46 insertions(+) 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("