mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 02:22:44 +01:00
fix(archives): address extraction review findings from PR #3813
Disable 7zz wildcard matching (-spd) when extracting a named member: a member literally named with "*" or "?" would otherwise act as a pattern, matching other members and concatenating them into the -so stream, which would hash plausible-but-wrong data. Applied to all three extraction call sites, including the two pre-existing readers that share the hazard. Verified against the production Alpine 7zz build. Route extraction stderr to an unlinked temp file instead of a pipe: a filled pipe blocks 7zz while the parent waits on stdout.read(), which the chunk-based deadline never interrupts. A file-backed stderr applies no backpressure and is still read back for the error log on failure. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -33,16 +33,29 @@ def _mock_popen_streaming(
|
||||
stderr: bytes = b"",
|
||||
):
|
||||
"""Build a subprocess.Popen mock whose consecutive context-managed calls
|
||||
stream the given chunk lists and finish with the given return codes."""
|
||||
stream the given chunk lists and finish with the given return codes.
|
||||
|
||||
`stderr` bytes are written into the file object the caller passes as the
|
||||
`stderr` argument, mirroring 7zz writing diagnostics to a file-backed
|
||||
stderr."""
|
||||
popen = MagicMock()
|
||||
processes = []
|
||||
for chunks, returncode in zip(chunk_streams, returncodes, strict=True):
|
||||
process = MagicMock()
|
||||
process.stdout.read.side_effect = [*chunks, b""]
|
||||
process.stderr.read.return_value = stderr
|
||||
process.returncode = returncode
|
||||
processes.append(process)
|
||||
popen.return_value.__enter__.side_effect = processes
|
||||
process_iter = iter(processes)
|
||||
|
||||
def _popen_call(*args, **kwargs):
|
||||
stderr_target = kwargs.get("stderr")
|
||||
if stderr and stderr_target is not None:
|
||||
stderr_target.write(stderr)
|
||||
context = MagicMock()
|
||||
context.__enter__.return_value = next(process_iter)
|
||||
return context
|
||||
|
||||
popen.side_effect = _popen_call
|
||||
return popen
|
||||
|
||||
|
||||
@@ -120,9 +133,12 @@ class TestExtractLargestArchiveMember:
|
||||
assert result is not None
|
||||
assert result == tmp_path / "game.gba"
|
||||
assert result.read_bytes() == b"abcdef"
|
||||
# The largest member, not the first, must be requested from 7zz.
|
||||
# The largest member, not the first, must be requested from 7zz, with
|
||||
# wildcard matching disabled so a member name containing "*" or "?"
|
||||
# can't select (and concatenate) other members.
|
||||
extract_args = popen.call_args[0][0]
|
||||
assert "game.gba" in extract_args
|
||||
assert "-spd" in extract_args
|
||||
|
||||
def test_member_folder_prefix_is_stripped_from_dest_name(self, tmp_path):
|
||||
"""Members nested in archive folders extract to a flat file name."""
|
||||
|
||||
@@ -5,6 +5,7 @@ import fnmatch
|
||||
import os
|
||||
import subprocess
|
||||
import tarfile
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import zipfile
|
||||
@@ -155,7 +156,7 @@ def _process_largest_7z_member(
|
||||
start_decompression_time = time.monotonic()
|
||||
|
||||
with subprocess.Popen(
|
||||
[SEVEN_ZIP_PATH, "e", str(file_path), largest_file, "-so", "-y"],
|
||||
[SEVEN_ZIP_PATH, "e", str(file_path), largest_file, "-so", "-y", "-spd"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
shell=False, # trunk-ignore(bandit/B603): 7z path is hardcoded, args are validated
|
||||
@@ -369,7 +370,7 @@ def read_7z_archive_files(
|
||||
break
|
||||
try:
|
||||
with subprocess.Popen(
|
||||
[SEVEN_ZIP_PATH, "e", str(file_path), name, "-so", "-y"],
|
||||
[SEVEN_ZIP_PATH, "e", str(file_path), name, "-so", "-y", "-spd"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
shell=False, # trunk-ignore(bandit/B603)
|
||||
@@ -468,38 +469,42 @@ def _extract_member_to_dir(
|
||||
and exits non-zero, e.g. for RAR).
|
||||
"""
|
||||
dest_path = dest_dir / Path(member).name
|
||||
stderr_output = b""
|
||||
try:
|
||||
with (
|
||||
open(dest_path, "wb") as dest_file,
|
||||
subprocess.Popen(
|
||||
[SEVEN_ZIP_PATH, "e", str(file_path), member, "-so", "-y"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
shell=False, # trunk-ignore(bandit/B603): 7z path is hardcoded, args are validated
|
||||
) as process,
|
||||
):
|
||||
assert process.stdout is not None
|
||||
while chunk := process.stdout.read(FILE_READ_CHUNK_SIZE):
|
||||
if time.monotonic() > deadline:
|
||||
process.terminate()
|
||||
log.error(f"Extraction of {member} from {file_path} timed out")
|
||||
dest_path.unlink(missing_ok=True)
|
||||
return None
|
||||
dest_file.write(chunk)
|
||||
if process.stderr is not None:
|
||||
stderr_output = process.stderr.read()
|
||||
# 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.
|
||||
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"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=stderr_file,
|
||||
shell=False, # trunk-ignore(bandit/B603): 7z path is hardcoded, args are validated
|
||||
) as process,
|
||||
):
|
||||
assert process.stdout is not None
|
||||
while chunk := process.stdout.read(FILE_READ_CHUNK_SIZE):
|
||||
if time.monotonic() > deadline:
|
||||
process.terminate()
|
||||
log.error(f"Extraction of {member} from {file_path} timed out")
|
||||
dest_path.unlink(missing_ok=True)
|
||||
return None
|
||||
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.
|
||||
detail = stderr_output.decode(errors="replace").strip()
|
||||
log.error(
|
||||
f"Extraction of {member} from {file_path} failed "
|
||||
f"with code {process.returncode}: {detail or 'no error output'}"
|
||||
)
|
||||
dest_path.unlink(missing_ok=True)
|
||||
return None
|
||||
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.
|
||||
stderr_file.seek(0)
|
||||
detail = stderr_file.read().decode(errors="replace").strip()
|
||||
log.error(
|
||||
f"Extraction of {member} from {file_path} failed "
|
||||
f"with code {process.returncode}: {detail or 'no error output'}"
|
||||
)
|
||||
dest_path.unlink(missing_ok=True)
|
||||
return None
|
||||
|
||||
return dest_path
|
||||
except OSError as e:
|
||||
|
||||
Reference in New Issue
Block a user