diff --git a/Dockerfile b/Dockerfile index d4115ad7d..69ff807b8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,6 +21,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ ca-certificates \ libmagic-dev \ 7zip \ + libarchive-tools \ tzdata \ libbz2-dev \ libssl-dev \ diff --git a/backend/tests/utils/test_archives.py b/backend/tests/utils/test_archives.py index 16dda569b..776f3f487 100644 --- a/backend/tests/utils/test_archives.py +++ b/backend/tests/utils/test_archives.py @@ -27,6 +27,12 @@ def _fake_7z_listing_sized( return "\n".join(blocks) +def _fake_mtree_listing(lines: list[str]) -> bytes: + """Build a `bsdtar --format=mtree` style listing (always bytes, ASCII-only: + libarchive octal-escapes every other byte).""" + return ("#mtree\n" + "\n".join(lines) + "\n").encode() + + def _mock_popen_streaming( chunk_streams: list[list[bytes]], returncodes: list[int], @@ -258,9 +264,9 @@ class TestExtractLargestArchiveMember: assert result is None def test_returns_none_and_cleans_up_on_extract_failure(self, tmp_path): - """A codec the 7zz build can't decompress (e.g. RAR) streams nothing - and exits non-zero; no partial file may be left behind, and the 7zz - reason must reach the error log so scans explain the missing hash.""" + """A codec the extractor can't decompress streams nothing and exits + non-zero; no partial file may be left behind, and the reason must reach + the error log so scans explain the missing hash.""" listing = MagicMock(stdout=_fake_7z_listing_sized([("game.gba", 500)])) popen = _mock_popen_streaming( [[]], [2], stderr=b"ERROR: Unsupported Method : game.gba" @@ -272,7 +278,7 @@ class TestExtractLargestArchiveMember: patch.object(archives.log, "error") as log_error, ): result = archives.extract_largest_archive_member( - Path("/fake/game.rar"), tmp_path + Path("/fake/game.7z"), tmp_path ) assert result is None @@ -294,3 +300,149 @@ class TestExtractLargestArchiveMember: assert result is None assert list(tmp_path.iterdir()) == [] + + +class TestRarArchives: + """RAR reading through bsdtar/libarchive: the bundled 7zz is built without + the RAR codec, so it can neither list nor extract RAR members (GitHub issue + #3884).""" + + def test_lists_file_members_with_sizes(self): + listing = MagicMock( + stdout=_fake_mtree_listing( + [ + "./game.gba type=file size=500", + "./readme.txt type=file size=12", + ] + ) + ) + + with patch.object(archives.subprocess, "run", return_value=listing) as run: + members = archives._list_rar_file_members(Path("/fake/game.rar")) + + # The archive-root "./" prefix is not part of the stored member name. + assert members == [("game.gba", 500), ("readme.txt", 12)] + assert run.call_args[0][0][0] == archives.BSDTAR_PATH + assert "@/fake/game.rar" in run.call_args[0][0] + + def test_directories_and_links_are_ignored(self): + listing = MagicMock( + stdout=_fake_mtree_listing( + [ + "./subdir type=dir", + "./subdir/link type=link", + "./subdir/game.gba type=file size=500", + ] + ) + ) + + with patch.object(archives.subprocess, "run", return_value=listing): + members = archives._list_rar_file_members(Path("/fake/game.rar")) + + assert members == [("subdir/game.gba", 500)] + + def test_escaped_member_names_are_decoded(self): + """libarchive octal-escapes the backslash and every byte outside + printable ASCII, so a UTF-8 name comes back byte by byte.""" + listing = MagicMock( + stdout=_fake_mtree_listing( + [r"./caf\303\251\040\0431\134x.gba type=file size=500"] + ) + ) + + with patch.object(archives.subprocess, "run", return_value=listing): + members = archives._list_rar_file_members(Path("/fake/game.rar")) + + assert members == [("café #1\\x.gba", 500)] + + def test_returns_no_members_when_listing_fails(self): + """Encrypted headers and corrupt archives make bsdtar exit non-zero.""" + with ( + patch.object( + archives.subprocess, + "run", + side_effect=archives.subprocess.CalledProcessError(1, "bsdtar"), + ), + patch.object(archives.log, "error") as log_error, + ): + members = archives._list_rar_file_members(Path("/fake/game.rar")) + + assert members == [] + log_error.assert_called_once() + + def test_member_pattern_escapes_glob_metacharacters(self): + """bsdtar matches member arguments as globs, so a stored name holding + "*" or "?" would otherwise select (and concatenate) other members.""" + assert archives._bsdtar_member_pattern("g*me?[1].gba") == r"g\*me\?\[1\].gba" + assert archives._bsdtar_member_pattern("back\\slash.gba") == r"back\\slash.gba" + + def test_extraction_command_is_chosen_by_extension(self): + rar_command = archives._archive_member_command(Path("/fake/GAME.RAR"), "a.gba") + assert rar_command == [archives.BSDTAR_PATH, "-xOf", "/fake/GAME.RAR", "a.gba"] + + seven_zip_command = archives._archive_member_command( + Path("/fake/game.7z"), "a.gba" + ) + assert seven_zip_command[0] == archives.SEVEN_ZIP_PATH + + def test_read_rar_archive_files_streams_members_in_ascii_order(self): + listing = MagicMock( + stdout=_fake_mtree_listing( + [ + "./b.gba type=file size=3", + "./a.gba type=file size=3", + "./skip.nfo type=file size=3", + "./cover.jpg type=file size=3", + ] + ) + ) + popen = _mock_popen_streaming([[b"aaa"], [b"bbb"]], [0, 0]) + + with ( + patch.object(archives.subprocess, "run", return_value=listing), + patch.object(archives.subprocess, "Popen", popen), + ): + results = [ + (name, size, b"".join(chunks)) + for name, size, chunks in archives.read_rar_archive_files( + Path("/fake/game.rar"), ["cover.jpg"], ["nfo"] + ) + ] + + assert results == [("a.gba", 3, b"aaa"), ("b.gba", 3, b"bbb")] + assert popen.call_args_list[0][0][0][:3] == [ + archives.BSDTAR_PATH, + "-xOf", + "/fake/game.rar", + ] + + def test_largest_member_is_extracted_through_bsdtar(self, tmp_path): + """RAHasher is fed a real ROM extracted from the RAR (GitHub issue + #3808 left this broken for RAR only).""" + listing = MagicMock( + stdout=_fake_mtree_listing( + [ + "./readme.txt type=file size=12", + "./game.gba type=file size=500", + ] + ) + ) + popen = _mock_popen_streaming([[b"abc", b"def"]], [0]) + + with ( + patch.object(archives.subprocess, "run", return_value=listing), + patch.object(archives.subprocess, "Popen", popen), + ): + result = archives.extract_largest_archive_member( + Path("/fake/game.rar"), tmp_path + ) + + assert result is not None + assert result == tmp_path / "game.gba" + assert result.read_bytes() == b"abcdef" + assert popen.call_args[0][0] == [ + archives.BSDTAR_PATH, + "-xOf", + "/fake/game.rar", + "game.gba", + ] diff --git a/backend/utils/archives.py b/backend/utils/archives.py index d7451490c..26fd86e10 100644 --- a/backend/utils/archives.py +++ b/backend/utils/archives.py @@ -3,6 +3,7 @@ import bz2 import fnmatch import os +import re import subprocess import tarfile import tempfile @@ -21,6 +22,19 @@ from logger.logger import log from utils.filesystem import COMPRESSED_FILE_EXTENSIONS SEVEN_ZIP_PATH = "/usr/bin/7zz" +# The bundled 7zz is built without the RAR codec (it is under the restrictive +# unRAR license), so RAR archives are read through libarchive's bsdtar instead. +BSDTAR_PATH = "/usr/bin/bsdtar" + +RAR_FILE_EXTENSIONS: Final = (".rar",) + +# libarchive escapes the backslash and every byte outside printable ASCII as a +# 3-digit octal sequence in the mtree listings we parse RAR members from. +_MTREE_OCTAL_ESCAPE: Final = re.compile(rb"\\([0-7]{3})") + +# bsdtar treats member arguments as glob patterns, where a backslash escapes +# the character that follows it. +_BSDTAR_GLOB_METACHARACTERS: Final = frozenset("\\*?[]") # Known compressed file MIME types COMPRESSED_MIME_TYPES: Final = frozenset( @@ -296,64 +310,31 @@ def _stream_7z_chunks( yield chunk -def read_7z_archive_files( - file_path: Path, +def _is_member_excluded( + member: str, excluded_names: list[str], excluded_exts: list[str], +) -> bool: + base_name = Path(member).name + lower = base_name.lower() + if any(lower.endswith("." + ext) for ext in excluded_exts): + return True + return any( + base_name == exc or fnmatch.fnmatch(base_name, exc) for exc in excluded_names + ) + + +def _stream_archive_members( + file_path: Path, + entries: list[tuple[str, int]], ) -> Iterator[tuple[str, int, Iterator[bytes]]]: - """Yield eligible files from a 7z archive in ASCII path order. + """Stream each listed member of an archive, one subprocess at a time. - Each yielded `(internal_name, file_size_bytes, chunks)` streams its - member's bytes lazily; chunks must be fully consumed before advancing - to the next entry, since the underlying subprocess is reaped at that point. + Members are yielded in ASCII path order under a single time budget shared + by the whole archive. """ - try: - result = subprocess.run( - [SEVEN_ZIP_PATH, "l", "-slt", "-ba", str(file_path)], - capture_output=True, - text=True, - check=True, - timeout=SEVEN_ZIP_TIMEOUT, - shell=False, # trunk-ignore(bandit/B603) - ) - except ( - subprocess.TimeoutExpired, - subprocess.CalledProcessError, - FileNotFoundError, - ) as e: - log.error(f"Error listing 7z archive {file_path}: {e}") - return + entries = sorted(entries, key=lambda e: e[0]) - entries: list[tuple[str, int]] = [] - current_file: str | None = None - current_size = 0 - - for line in result.stdout.split("\n"): - line = line.lstrip() - if line.startswith("Path = "): - current_file = line.split(" = ", 1)[1] - elif line.startswith("Size = "): - try: - current_size = int(line.split(" = ")[1].strip()) - except ValueError: - current_size = 0 - elif line.startswith("Attributes = "): - attrs = line.split(" = ")[1].strip() - if current_file and not attrs.startswith("D"): - base_name = Path(current_file).name - lower = base_name.lower() - if not any(lower.endswith("." + ext) for ext in excluded_exts): - if not any( - base_name == exc or fnmatch.fnmatch(base_name, exc) - for exc in excluded_names - ): - entries.append((current_file, current_size)) - current_file = None - current_size = 0 - - entries.sort(key=lambda e: e[0]) - - # Single time budget for reading every member of this archive. deadline = time.monotonic() + SEVEN_ZIP_TIMEOUT timed_out = False @@ -370,7 +351,7 @@ def read_7z_archive_files( break try: with subprocess.Popen( - [SEVEN_ZIP_PATH, "e", str(file_path), name, "-so", "-y", "-spd"], + _archive_member_command(file_path, name), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=False, # trunk-ignore(bandit/B603) @@ -382,7 +363,8 @@ def read_7z_archive_files( # expected then and is covered by the single log below. if not timed_out and process.returncode != 0: log.error( - f"7z extraction of {name} failed with code {process.returncode}" + f"Extraction of {name} from {file_path} failed " + f"with code {process.returncode}" ) return except (OSError, ValueError) as e: @@ -390,7 +372,26 @@ def read_7z_archive_files( continue if timed_out: - log.error(f"7z extraction timed out reading members of {file_path}") + log.error(f"Extraction timed out reading members of {file_path}") + + +def read_7z_archive_files( + file_path: Path, + excluded_names: list[str], + excluded_exts: list[str], +) -> Iterator[tuple[str, int, Iterator[bytes]]]: + """Yield eligible files from a 7z archive in ASCII path order. + + Each yielded `(internal_name, file_size_bytes, chunks)` streams its + member's bytes lazily; chunks must be fully consumed before advancing + to the next entry, since the underlying subprocess is reaped at that point. + """ + entries = [ + (name, size) + for name, size in _list_archive_file_members(file_path) + if not _is_member_excluded(name, excluded_names, excluded_exts) + ] + yield from _stream_archive_members(file_path, entries) def read_rar_archive_files( @@ -398,13 +399,104 @@ def read_rar_archive_files( excluded_names: list[str], excluded_exts: list[str], ) -> Iterator[tuple[str, int, Iterator[bytes]]]: - """Yield eligible files from a RAR archive, sorted by internal path (ASCII). + """Yield eligible files from a RAR archive in ASCII path order. - Delegates to the 7zz binary. Listing RAR members always works, but - streaming their contents requires a 7zz build with the RAR codec; the - bundled Alpine build lacks it, so content reads fail and log an error. + Same contract as `read_7z_archive_files`, but listing and extraction go + through bsdtar since the bundled 7zz has no RAR codec. """ - return read_7z_archive_files(file_path, excluded_names, excluded_exts) + entries = [ + (name, size) + for name, size in _list_rar_file_members(file_path) + if not _is_member_excluded(name, excluded_names, excluded_exts) + ] + yield from _stream_archive_members(file_path, entries) + + +def _is_rar_archive(file_path: Path) -> bool: + return str(file_path).lower().endswith(RAR_FILE_EXTENSIONS) + + +def _bsdtar_member_pattern(member: str) -> str: + """Escape a member name so bsdtar matches it literally. + + Without this, a stored name containing `*` or `?` is a glob that can + select (and concatenate) other members of the same archive. + """ + return "".join( + f"\\{char}" if char in _BSDTAR_GLOB_METACHARACTERS else char for char in member + ) + + +def _unescape_mtree_path(raw: bytes) -> str: + unescaped = _MTREE_OCTAL_ESCAPE.sub(lambda m: bytes([int(m.group(1), 8)]), raw) + # surrogateescape round-trips names that aren't valid UTF-8, so they still + # encode back to the original bytes when passed to bsdtar. + return unescaped.decode("utf-8", errors="surrogateescape") + + +def _list_rar_file_members(file_path: Path) -> list[tuple[str, int]]: + """List `(member_path, size)` for every file member of a RAR archive. + + Rewriting the archive as an mtree listing on stdout is libarchive's only + machine-readable listing; `bsdtar -tv` output is ls-style and ambiguous + for names containing spaces. Only headers are read, so listing stays cheap + regardless of archive size. + """ + try: + result = subprocess.run( + [ + BSDTAR_PATH, + "-cf", + "-", + "--format=mtree", + "--options=!all,type,size", + f"@{file_path}", + ], + capture_output=True, + check=True, + timeout=SEVEN_ZIP_TIMEOUT, + shell=False, # trunk-ignore(bandit/B603): bsdtar path is hardcoded, args are validated + ) + except ( + subprocess.TimeoutExpired, + subprocess.CalledProcessError, + FileNotFoundError, + ) as e: + log.error(f"Error listing RAR archive {file_path}: {e}") + return [] + + entries: list[tuple[str, int]] = [] + for line in result.stdout.splitlines(): + # "#" starts a comment, "/" an mtree directive; neither is an entry. + if not line or line.startswith((b"#", b"/")): + continue + raw_path, _, keywords = line.partition(b" ") + fields = dict( + field.split(b"=", 1) for field in keywords.split() if b"=" in field + ) + if fields.get(b"type") != b"file": + continue + try: + size = int(fields.get(b"size", b"0")) + except ValueError: + size = 0 + # mtree paths are archive-root relative ("./game.gba"), stored member + # names are not. + name = _unescape_mtree_path(raw_path).removeprefix("./") + if name: + entries.append((name, size)) + + return entries + + +def _archive_member_command(file_path: Path, member: str) -> list[str]: + """Build the command streaming a single archive member to stdout.""" + if _is_rar_archive(file_path): + return [BSDTAR_PATH, "-xOf", str(file_path), _bsdtar_member_pattern(member)] + + # "-spd" disables wildcard matching so a member name containing "*" or "?" + # can't select (and concatenate) other members. + return [SEVEN_ZIP_PATH, "e", str(file_path), member, "-so", "-y", "-spd"] def _list_archive_file_members(file_path: Path) -> list[tuple[str, int]]: @@ -414,6 +506,9 @@ def _list_archive_file_members(file_path: Path) -> list[tuple[str, int]]: entry is committed when the next `Path =` starts or the listing ends, and only dropped when its attributes mark it as a directory. """ + if _is_rar_archive(file_path): + return _list_rar_file_members(file_path) + try: result = subprocess.run( [SEVEN_ZIP_PATH, "l", "-slt", "-ba", str(file_path)], @@ -464,24 +559,22 @@ def _extract_member_to_dir( ) -> Path | None: """Stream one archive member into `dest_dir`, named after its basename. - Returns None (leaving no partial file) when extraction fails, exceeds - the deadline, or the 7zz build lacks the codec (it then streams nothing - and exits non-zero, e.g. for RAR). + Returns None (leaving no partial file) when extraction fails or exceeds + the deadline. """ dest_path = dest_dir / Path(member).name try: # stderr goes to an unlinked temp file rather than a pipe: a pipe can - # fill and block 7zz while this thread waits on stdout, stalling past - # the deadline. "-spd" disables wildcard matching so a member name - # containing "*" or "?" can't select (and concatenate) other members. + # fill and block the extractor while this thread waits on stdout, + # stalling past the deadline. with tempfile.TemporaryFile() as stderr_file: with ( open(dest_path, "wb") as dest_file, subprocess.Popen( - [SEVEN_ZIP_PATH, "e", str(file_path), member, "-so", "-y", "-spd"], + _archive_member_command(file_path, member), stdout=subprocess.PIPE, stderr=stderr_file, - shell=False, # trunk-ignore(bandit/B603): 7z path is hardcoded, args are validated + shell=False, # trunk-ignore(bandit/B603): binary paths are hardcoded, args are validated ) as process, ): assert process.stdout is not None @@ -494,9 +587,8 @@ def _extract_member_to_dir( dest_file.write(chunk) if process.returncode != 0: - # Surface the 7zz reason (e.g. "Unsupported Method" from a - # build without the RAR codec) so scan logs explain the - # missing hash. + # Surface the extractor's own reason (e.g. "Unsupported + # Method") so scan logs explain the missing hash. stderr_file.seek(0) detail = stderr_file.read().decode(errors="replace").strip() log.error( diff --git a/docker/Dockerfile b/docker/Dockerfile index c80d9fb6b..5e932478f 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -165,8 +165,11 @@ RUN git clone https://github.com/evanmiller/mod_zip.git && \ FROM nginx:${NGINX_VERSION}-alpine${ALPINE_VERSION}@sha256:${NGINX_SHA256} AS production-stage ARG WEBSERVER_FOLDER=/var/www/html +# libarchive-tools provides bsdtar, which reads RAR/RAR5 archives; Alpine's +# 7zip is built without the RAR codec, so 7zz can neither list nor extract them RUN apk add --no-cache \ bash \ + libarchive-tools \ libmagic \ mariadb-connector-c \ libpq \