Repository navigation
check_no_game_data: look inside zip, gzip, bzip2 and xz files - #4040
Exopus-0x65f7ad0 wants to merge 2 commits into
Conversation
A DDS, RPF, FSSM or PE zipped or gzipped passed both the magic check and the extension check in no-game-data.yml. The script now opens what the standard library can (zip members, gzip/bzip2/xz streams, three levels deep), classifies the first decompressed bytes of each with the existing table, and reports 7-Zip, Zstandard and RAR files, damaged archives and encrypted zip members as archives whose contents can't be checked. Reads stay bounded to the first 0x208 bytes of each member. tests/test_check_no_game_data.py: 13 archive cases that must be caught (all 13 pass unnoticed on the old script) and 5 clean archives that must pass; its temporary git repository is now removed on Windows too, where git's read-only object files made the cleanup fail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Kam471
left a comment
There was a problem hiding this comment.
Ran this at 567fc3d5 on Windows 11: python -I tests/test_check_no_game_data.py -> 7 tests, OK; compileall and git diff --check clean.
The recursion is bounded and conservative: only the first HEAD bytes are read from each member (read_head_from), zip/gzip/bzip2/xz are opened up to depth 3, and a damaged or unsupported archive (7-Zip, Zstandard, RAR) is reported as "contents can't be checked" rather than passing silently. The nested-match line names the outer archive and member, and the existing magics still take precedence over the container format.
One minor thought, not blocking: a zip member name can contain newlines or control characters, which would break the one-match-per-line output; sanitizing info.filename for the message would keep the CI log parseable. No game files; no blocker.
|
Tested
AI-assisted. No game files. |
|
At A benign ZIP is still allowed in both cases, and the AI-assisted (Devin); Linux, Python 3.12.16, synthetic fixtures only. |
Flyander
left a comment
There was a problem hiding this comment.
The archive classification is useful, but please bound decompression work on nested streams.
| try: | ||
| f.seek(0) | ||
| if is_zip: | ||
| with zipfile.ZipFile(f) as archive: |
There was a problem hiding this comment.
[P2] Bound nested decompression work before opening this stream as a ZIP. ZipFile seeks to EOF to locate the central directory; when this stream is backed by gzip/bz2/xz, that seek forces the decompressor to consume the entire expanded payload. The header read limit and MAX_DEPTH therefore do not cap CPU or memory use. A 32,752-byte gzip expanding to a 33,554,552-byte ZIP reproduced the unbounded EOF traversal and classification returned None. A crafted nested archive can make this checker spend unbounded time/memory. Cap total expanded bytes and archive entries (or parse from a bounded spool), and add a regression test.
|
Ran this on
AI-assisted (Claude Code). |
|
Checked 567fc3d on macOS, Python 3.15.2. A tar archive gets past the new check, compressed or not. The gzip/bzip2/xz path decompresses the stream and then tests the decompressed head for magic. For a Reproducer (synthetic: one member holding only a hand-made 128-byte DDS header):
import io, tarfile, gzip, zipfile
member = b"DDS " + bytes([124, 0, 0, 0]) + b"\0" * 120
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w") as t:
ti = tarfile.TarInfo("x.dds"); ti.size = len(member); t.addfile(ti, io.BytesIO(member))
open("x.tar", "wb").write(buf.getvalue())
open("x.tar.gz", "wb").write(gzip.compress(buf.getvalue()))There are two possible fixes:
This is separate from the prepended-bytes zip case ray3242 raised. That case also holds one level down: a zip with junk in front, stored inside another zip, exits 0 as well. No game files. AI-assisted (Claude Code). |
…ession budget Review of ItsNotRexed#4040: - tar archives (plain, or inside gzip/bzip2/xz) are opened with tarfile and their members checked like a zip's (paratustra); - a zip is also found by its end-of-central-directory record (zipfile.is_zipfile), so one with bytes in front of it, a self-extracting archive or a spanned zip, is opened (ray3242, JRamos1993); - everything inside an archive is read into memory under a budget per checked file, 64 MiB expanded and 10,000 members, before it is looked at: a zip inside a gzip no longer makes ZipFile drive the decompressor to the end of an unbounded stream, and a file over the budget is reported as too large to check (Flyander); - member names print with control characters as '?', one line each. Tests: tar, tar.gz, tar.xz, a prefixed zip, a shell-prefixed zip, a prefixed zip inside a zip, a zip with a corrupt deflate stream (covers zlib.error), a member name with a newline, clean tar/tar.bz2/prefixed zips, a 65 MiB gzip bomb and a 10,001-member zip. 9 of them fail on the previous commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks all: pushed
|
|
Re-checked at Every synthetic case I ran with the same DDS member is now flagged with exit 1:
A benign Linux, Python 3.12.16, synthetic fixtures only. AI-assisted (Devin). |
Claim: #3918 (comment) (new TEST item)
Depends on: none. Touches the same file as #4001 (
read_head, the PE straddle): this PR movesread_head's body intoread_head_from(f)unchanged, so whichever lands second needs a small rebase.Problem
scripts/check_no_game_data.pymatches magics at the start of each tracked file, and.github/workflows/no-game-data.ymlmatches extensions. A compressed file passes both: a DDS, RPF, FSSM or PE inside a.zip,.gz,.bz2or.xz(or under any other name) is never looked at.classify()returnsNoneforPK\x03\x04,\x1f\x8b,BZh,\xfd7zXZ\0,7z\xbc\xaf\x27\x1c,(\xb5/\xfdandRar!\x1a\x07, so the "no game data" check has a hole as wide as onezipcommand.Change
0x208bytes only (the far-PE re-read included), up to three levels of nesting. A match prints as<format> in <archive> (<member>), e.g.notes.zip.txt: DDS texture in zip archive (tex/a.png).classify(head)itself is unchanged;check()goes through the newclassify_file(path).tests/test_check_no_game_data.pyremoves its temporary git repository with a handler that clears the read-only bit, sotest_default_mode_reads_tracked_files_onlyno longer errors on Windows (PermissionError: [WinError 5]on git's read-only object files).Tests
python -I tests/test_check_no_game_data.py, Windows 11, Python 3.12:Also:
classify_open(the first fuzz run foundzlib.erroruncaught, now handled).python -I scripts/check_no_game_data.pyover the repository: exit 0 (no tracked archive today, so no new reports).python -I tests/test_text_write_line_endings.py,test_docs_repo_paths.py,test_ci_registration.py: OK;python -m compileall -q scripts tests/test_check_no_game_data.py: OK.No game files, no GPU. Every test input is a few hand-written header bytes compressed with the standard library.
Limitations
compression.zstdcould take Zstandard later).no-game-data.ymlis unchanged; the magic check now covers archives under any name.AI-assisted (Claude Code). Tested on Windows 11 (no GPU needed).