From 78a634e025aa75ae86ccbac9edc5dabbed0e563b Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Sat, 31 Oct 2026 12:52:24 -0500 Subject: [PATCH] fix(ra): extract zips RAHasher can't unzip before hashing them (#5416) --- backend/adapters/services/rahasher.py | 30 +++++- .../tests/adapters/services/test_rahasher.py | 96 +++++++++++++++++-- 2 files changed, 113 insertions(+), 13 deletions(-) diff --git a/backend/adapters/services/rahasher.py b/backend/adapters/services/rahasher.py index fee4c1414..7c3915b96 100644 --- a/backend/adapters/services/rahasher.py +++ b/backend/adapters/services/rahasher.py @@ -1,6 +1,7 @@ import asyncio import re import tempfile +import zipfile from pathlib import Path from handler.metadata.ra_handler import RAGamesPlatform @@ -26,6 +27,26 @@ RAHASHER_VALID_HASH_REGEX = re.compile(r"[0-9a-f]{32}") # matches nothing on RetroAchievements (issue #3808). RAHASHER_NATIVE_ARCHIVE_EXTENSIONS: tuple[str, ...] = (".zip",) +# The zip methods miniz can inflate; RAHasher fails outright on any other. +RAHASHER_ZIP_METHODS: frozenset[int] = frozenset( + {zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED} +) + + +def _rahasher_reads_zip(file_path: Path) -> bool: + """Whether RAHasher unzips this itself: one entry (directories count), stored or deflated.""" + try: + with zipfile.ZipFile(file_path) as z: + entries = z.infolist() + except zipfile.BadZipFile, OSError: + return False + return ( + len(entries) == 1 + and not entries[0].is_dir() + and entries[0].compress_type in RAHASHER_ZIP_METHODS + ) + + # Disc descriptor / playlist files that point RAHasher at the real data # tracks. Handing RAHasher one of these reproduces the hash a shell-expanded # glob would, in priority order: a per-disc descriptor (.cue/.gdi) wins over @@ -249,8 +270,8 @@ class RAHasherService: if is_native_entry: file_path = entry_str - # RAHasher only decompresses .zip archives itself; any other container - # gets hashed as its raw archive bytes, silently storing a hash that + # RAHasher only decompresses single-entry .zip archives itself; any + # other container gets hashed as its raw archive bytes, storing a hash that # matches nothing on RetroAchievements (issue #3808). Extract the ROM # to a temp file and hash that instead. Arcade is exempt: its RA hash # is the file's own basename, so the container format never matters @@ -258,8 +279,11 @@ class RAHasherService: lower_path = file_path.lower() if ( lower_path.endswith(tuple(COMPRESSED_FILE_EXTENSIONS)) - and not lower_path.endswith(RAHASHER_NATIVE_ARCHIVE_EXTENSIONS) and platform["ra_id"] != ARCADE_RA_ID + and not ( + lower_path.endswith(RAHASHER_NATIVE_ARCHIVE_EXTENSIONS) + and await asyncio.to_thread(_rahasher_reads_zip, Path(file_path)) + ) ): with tempfile.TemporaryDirectory(prefix="romm_rahasher_") as tmp_dir: extracted = await asyncio.to_thread( diff --git a/backend/tests/adapters/services/test_rahasher.py b/backend/tests/adapters/services/test_rahasher.py index f119ba635..26af4899e 100644 --- a/backend/tests/adapters/services/test_rahasher.py +++ b/backend/tests/adapters/services/test_rahasher.py @@ -1,7 +1,10 @@ import asyncio +import zipfile +from pathlib import Path from unittest.mock import AsyncMock, MagicMock, patch import pytest +from tests._zipfile_shim import reload_zipfile from adapters.services.rahasher import ( PLATFORM_SLUG_TO_RETROACHIEVEMENTS_ID, @@ -14,6 +17,23 @@ from adapters.services.rahasher import ( from utils.platform_slugs import UniversalPlatformSlug as UPS +def _write_zip( + path: Path, + entries: list[str], + compression: int = zipfile.ZIP_STORED, + payload: bytes = b"rom-bytes", +) -> Path: + """Write a zip of `entries`, where a trailing slash makes a directory.""" + reload_zipfile() + with zipfile.ZipFile(path, "w", compression=compression) as z: + for name in entries: + if name.endswith("/"): + z.mkdir(name) + else: + z.writestr(name, payload) + return path + + class TestRAHasherValidHashRegex: """Test the hash validation regex.""" @@ -281,11 +301,12 @@ class TestRAHasherArchiveSkip: @pytest.mark.asyncio async def test_does_not_skip_archive_for_cartridge_platform( - self, service: RAHasherService + self, service: RAHasherService, tmp_path: Path ): """Cartridge platforms (e.g. NES) support buffer hash; don't skip.""" assert UPS.NES not in RA_BUFFER_HASH_UNSUPPORTED nes_id = PLATFORM_SLUG_TO_RETROACHIEVEMENTS_ID[UPS.NES] + archive = _write_zip(tmp_path / "game.zip", ["game.nes"]) mock_proc = AsyncMock() mock_proc.wait.return_value = 0 @@ -296,7 +317,7 @@ class TestRAHasherArchiveSkip: "asyncio.create_subprocess_exec", return_value=mock_proc ) as mock_subprocess: result = await service.calculate_hash( - {"ra_id": nes_id, "slug": "nes"}, "/path/to/game.zip" + {"ra_id": nes_id, "slug": "nes"}, str(archive) ) assert result == "a1b2c3d4e5f6789012345678901234ab" @@ -359,10 +380,8 @@ class TestRAHasherArchiveSkip: """Cartridge platforms: folder with archives should hash the largest archive directly.""" assert UPS.GBA not in RA_BUFFER_HASH_UNSUPPORTED platform_id = PLATFORM_SLUG_TO_RETROACHIEVEMENTS_ID[UPS.GBA] - small = tmp_path / "small.zip" - large = tmp_path / "large.zip" - small.write_bytes(b"s" * 100) - large.write_bytes(b"l" * 500) + _write_zip(tmp_path / "small.zip", ["small.gba"], payload=b"s" * 100) + large = _write_zip(tmp_path / "large.zip", ["large.gba"], payload=b"l" * 500) mock_proc = AsyncMock() mock_proc.wait.return_value = 0 @@ -1154,10 +1173,11 @@ class TestRAHasherArchiveExtraction: @pytest.mark.asyncio async def test_zip_archive_still_handed_to_rahasher_directly( - self, service: RAHasherService + self, service: RAHasherService, tmp_path: Path ): - """RAHasher decompresses .zip natively; no extraction round-trip.""" + """RAHasher decompresses a single-entry .zip natively; no extraction round-trip.""" gba_id = PLATFORM_SLUG_TO_RETROACHIEVEMENTS_ID[UPS.GBA] + archive = _write_zip(tmp_path / "game.zip", ["game.gba"]) mock_proc = AsyncMock() mock_proc.wait.return_value = 0 @@ -1173,13 +1193,69 @@ class TestRAHasherArchiveExtraction: ) as mock_subprocess, ): result = await service.calculate_hash( - {"ra_id": gba_id, "slug": "gba"}, "/roms/gba/game.zip" + {"ra_id": gba_id, "slug": "gba"}, str(archive) ) assert result == "a1b2c3d4e5f6789012345678901234ab" extractor.assert_not_called() call_args = mock_subprocess.call_args[0] - assert "/roms/gba/game.zip" in call_args + assert str(archive) in call_args + + @pytest.mark.asyncio + @pytest.mark.parametrize( + "entries, compression", + [ + pytest.param( + ["Fatal Fury (USA).md", "readme.txt"], + zipfile.ZIP_DEFLATED, + id="extra-file", + ), + pytest.param( + ["Fatal Fury (USA)/", "Fatal Fury (USA)/Fatal Fury (USA).md"], + zipfile.ZIP_DEFLATED, + id="directory-entry", + ), + pytest.param( + ["Fatal Fury (USA).md"], zipfile.ZIP_BZIP2, id="unsupported-method" + ), + ], + ) + async def test_zip_rahasher_cant_read_is_extracted( + self, + service: RAHasherService, + tmp_path: Path, + entries: list[str], + compression: int, + ): + """RAHasher hashes a multi-entry zip whole and fails on non-deflate + methods, so those zips go through extraction like any other archive.""" + genesis_id = PLATFORM_SLUG_TO_RETROACHIEVEMENTS_ID[UPS.GENESIS] + archive = _write_zip(tmp_path / "Fatal Fury (USA).zip", entries, compression) + extractor = self._fake_extractor("Fatal Fury (USA).md") + + mock_proc = AsyncMock() + mock_proc.wait.return_value = 0 + mock_proc.stdout.read.return_value = b"a1b2c3d4e5f6789012345678901234ab\n" + mock_proc.stderr = None + + with ( + patch( + "adapters.services.rahasher.extract_largest_archive_member", + extractor, + ), + patch( + "asyncio.create_subprocess_exec", return_value=mock_proc + ) as mock_subprocess, + ): + result = await service.calculate_hash( + {"ra_id": genesis_id, "slug": "genesis"}, str(archive) + ) + + assert result == "a1b2c3d4e5f6789012345678901234ab" + extractor.assert_called_once() + call_args = mock_subprocess.call_args[0] + assert any(str(a).endswith("Fatal Fury (USA).md") for a in call_args) + assert str(archive) not in call_args @pytest.mark.asyncio async def test_extraction_failure_returns_empty_hash_without_rahasher(