Skip to content

check_no_game_data: look inside zip, gzip, bzip2 and xz files - #4040

Merged
ItsNotRexed merged 1 commit into
ProjectLeonida:mainfrom
Exopus-0x65f7ad0:test/no-game-data-archives
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ProjectLeonida:mainfrom
Exopus-0x65f7ad0:test/no-game-data-archives

Conversation

@Exopus-0x65f7ad0

Copy link
Copy Markdown
Contributor

Claim: #3918 (comment) (new TEST item)
Depends on: none. Touches the same file as #4001 (read_head, the PE straddle): this PR moves read_head's body into read_head_from(f) unchanged, so whichever lands second needs a small rebase.

Problem

scripts/check_no_game_data.py matches magics at the start of each tracked file, and .github/workflows/no-game-data.yml matches extensions. A compressed file passes both: a DDS, RPF, FSSM or PE inside a .zip, .gz, .bz2 or .xz (or under any other name) is never looked at. classify() returns None for PK\x03\x04, \x1f\x8b, BZh, \xfd7zXZ\0, 7z\xbc\xaf\x27\x1c, (\xb5/\xfd and Rar!\x1a\x07, so the "no game data" check has a hole as wide as one zip command.

Change

  • Archives the standard library opens are looked into: each member of a zip and the stream of a gzip, bzip2 or xz file is classified with the existing table, from its first decompressed 0x208 bytes 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).
  • 7-Zip, Zstandard and RAR files (no standard-library reader), archives that fail to open (damaged, encrypted zip members) and nesting deeper than three levels are reported as archives whose contents can't be checked, so the check fails closed instead of passing them.
  • classify(head) itself is unchanged; check() goes through the new classify_file(path).
  • tests/test_check_no_game_data.py removes its temporary git repository with a handler that clears the read-only bit, so test_default_mode_reads_tracked_files_only no 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:

old script this branch
13 archive cases that must be caught (zip, gzip, bzip2, xz, nested zip-in-gzip, far PE in gzip, 7z, zstd, rar, damaged zip, two damaged gzip, four-deep gzip) 13 fail (nothing reported) pass
5 clean archives (text in zip/gz/bz2/xz, zip in gzip) pass pass
the existing 5 tests 4 pass, 1 error on Windows (cleanup) 7/7 OK

Also:

  • 10,000 random inputs after each archive magic (zip, gzip, bzip2, xz, a zip local header with deflate): 0 exceptions escape classify_open (the first fuzz run found zlib.error uncaught, now handled).
  • python -I scripts/check_no_game_data.py over 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

  • 7-Zip/Zstandard/RAR are reported, not inspected (no standard-library reader; Python 3.14's compression.zstd could take Zstandard later).
  • Only the first match inside an archive is reported, as for a plain file.
  • The extension list in no-game-data.yml is unchanged; the magic check now covers archives under any name.

AI-assisted (Claude Code). Tested on Windows 11 (no GPU needed).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@LocranKing

Copy link
Copy Markdown
Contributor

Tested 567fc3d5 on Windows 11 Build 26200, RTX 3060 Ti (driver 621.01), Python 3.12: pass

  • python -I -m compileall -q make_game.py tools scripts tests: exit 0
  • python -I make_game.py --help: exit 0
  • python -I tests/test_check_no_game_data.py -v: 7/7 passed (4.56s)
  • python -I scripts/check_no_game_data.py: exit 0 (clean repository check)

AI-assisted. No game files.

@ray3242

ray3242 commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

At 567fc3d5, scripts/check_no_game_data.py:123 treats a file as a ZIP only when head.startswith(ZIP). A ZIP with bytes prepended in front of it is still a valid archive (zipfile.is_zipfile and ZipFile read it, as they do self-extracting archives), but the checker returns None for it without looking inside. I built two synthetic archives, each holding the same member with only a hand-made DDS header, and ran them on this head and on its merge with main:

plain.txt     zipfile.is_zipfile=True   check exit=1   (member flagged)
prefixed.txt  zipfile.is_zipfile=True   check exit=0   (not inspected)

A benign ZIP is still allowed in both cases, and the .txt name also passes the workflow's extension filter. Falling back to zipfile.is_zipfile() (it finds the end-of-central-directory record) when the magic doesn't match at offset 0, plus a prefixed-ZIP case in tests/test_check_no_game_data.py, would close the gap. This is incomplete new coverage, not a regression from main.

AI-assisted (Devin); Linux, Python 3.12.16, synthetic fixtures only.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

@JRamos1993

Copy link
Copy Markdown
Contributor

Ran this on main aa2988c (Windows 11, Python 3.14): tests/test_check_no_game_data.py 7 OK, and the repository check exits 0. With main's script, the 13 archive cases fail. MAX_DEPTH = 4 and dropping the far-PE re-read for streams are both caught.

  • Prepended bytes: I confirm ray3242's point. A zip holding a DDS member is reported (rc 1), but the same zip with 4 bytes in front exits 0, and zipfile opens it fine. A zip that starts with the PK\x07\x08 spanning marker is skipped the same way.
  • Test gap: removing zlib.error from ARCHIVE_ERRORS isn't caught. No test has a zip with corrupt deflate data, although such a zip is correctly reported as "can't be opened".
  • Cost stays bounded on small synthetic inputs, since nothing is extracted or written:
    • a gzip of PK\x03\x04 + 256 MiB of zeros: 0.13 s, heap peak under 1 MiB
    • an 8000-member zip: 0.65 s
    • member names like ../../x are only printed
  • Cosmetic: a newline in a member name splits the output line (exit code still 1).

AI-assisted (Claude Code).

@paratustra

Copy link
Copy Markdown
Contributor

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 .tar.gz that head is the 512-byte tar header, so nothing matches and no "can't be checked" line is printed either. classify_open never opens tar itself. tar is probably the most common way to bundle a folder, so it is the archive form this guard most needs to handle.

Reproducer (synthetic: one member holding only a hand-made 128-byte DDS header):

file exit
plain.dds 1
x.zip holding the same member 1
x.tar 0
x.tar.gz 0
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:

  1. Open tar members with tarfile, under the same depth and size bounds as zip. That also covers .tar.bz2 and .tar.xz.
  2. If tar stays out of scope, fail closed: report a file whose head, raw or decompressed, has ustar at offset 257 as "can't be checked".

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).

Exopus-0x65f7ad0 added a commit to Exopus-0x65f7ad0/Leonida that referenced this pull request Nov 23, 2026
…ession budget

Review of ProjectLeonida#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>
@Exopus-0x65f7ad0

Copy link
Copy Markdown
Contributor Author

Thanks all: pushed 9a641513 addressing each point.

  • tar (@paratustra): plain tar, and tar inside gzip/bzip2/xz, is opened with tarfile and its members are checked like a zip's. x.tar → DDS texture in tar archive (x.dds), x.tar.gz → FSSM shader in tar archive (y.bin) in gzip stream.
  • Bytes in front of a zip (@ray3242, @JRamos1993): when the magic isn't at offset 0, zipfile.is_zipfile looks for the end-of-central-directory record, so a prefixed zip, a shell-prefixed self-extractor and a prefixed zip inside a zip are all opened.
  • Bounded work (@Flyander): everything inside an archive (a zip member, a tar member, a decompressed gzip/bzip2/xz stream) is read into memory under a budget per checked file before it's looked at: 64 MiB expanded, 10,000 members. ZipFile therefore never seeks to the end of an unbounded decompressing stream. A file over the budget is reported as <kind> too large to check (...), failing closed, not passed. Tests: a gzip of 65 MiB of zeros starting PK\x03\x04 (64 KiB on disk), and a 10,001-member zip.
  • zlib.error coverage (@JRamos1993): a zip whose deflate data starts with an invalid block type → zip archive that can't be opened.
  • Newline in a member name: printed as ?, so each finding stays on one line.

python -I tests/test_check_no_game_data.py: 8 tests OK (Windows 11, Python 3.12). On the previous commit (567fc3d5), 9 of the new cases fail. 9,000 random inputs after zip/gzip/bzip2/xz/tar/junk headers: no exception escapes. Repository scan: exit 0, 0.19 s over 1,688 files.

@ray3242

ray3242 commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Re-checked at 9a641513 merged with main aa2988cc (merges cleanly): my prepended-bytes point is resolved. prefixed.txt (a shell-script prefix before a ZIP holding a hand-made DDS header) now exits 1 with DDS texture in zip archive (x.dds). python -I tests/test_check_no_game_data.py passes.

Every synthetic case I ran with the same DDS member is now flagged with exit 1:

  • tar in the GNU, PAX and ustar formats;
  • .tar.gz, .tar.bz2 and .tar.xz;
  • a zip inside a tar, a tar inside a zip, and a .tar.gz inside a tar.

A benign .tar and .tar.gz still exit 0.

Linux, Python 3.12.16, synthetic fixtures only. AI-assisted (Devin).

Archives are opened with the standard library and every member is
classified like a tracked file, up to three levels deep, under a
decompression budget (64 MiB expanded, 10,000 members). Formats the
standard library can't open (7-Zip, Zstandard, RAR) and archives that
can't be read are reported as unchecked rather than passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Exopus-0x65f7ad0
Exopus-0x65f7ad0 force-pushed the test/no-game-data-archives branch from 9a64151 to fce5ca5 Compare November 23, 2026 03:46
@Exopus-0x65f7ad0

Copy link
Copy Markdown
Contributor Author

Rebased onto main (7b7d73f8) to clear the conflict with #367's PE-signature change, and squashed to one commit (fce5ca5b). The archive code now uses main's (head, pe_signature) from read_head: read_head_from(f) is that same reader on an open file, so a PE inside an archive is matched the way #367 matches a tracked one (signature wherever e_lfanew points, straddling the first bytes included), and the duplicate remove_tree helper in the test went in favour of main's rmtree. python -I tests/test_check_no_game_data.py: 8/8, and the scan of the tree exits 0. AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Collaborator

Tested fce5ca5 merged with main d974c88 on Linux Python 3.12.16 and native Windows Python 3.13.1: pass

tests/test_check_no_game_data.py 8/8 on both. scripts/check_no_game_data.py over the repo's tracked files exits 0 on both (so no tracked archive is newly flagged).
Negative control: with scripts/check_no_game_data.py from main the new tests fail (8 tests, 23 failures incl. subtests, e.g. test_game_data_inside_archives_is_caught: 0 != 1); restored, 8/8. No issues found in the diff.

@bobcatchris

Copy link
Copy Markdown
Contributor

Independent macOS code read and test run at head fce5ca5 (Apple M2, Python 3.13.18). The checker now recognises zip, tar, gzip, bzip2 and xz by magic bytes and a zip with a prefix via zipfile.is_zipfile, recurses to a bounded depth with a shared byte budget, and reports an over-budget or malformed container as "too large to check" rather than passing it. That is the safe direction for a pre-commit guard: a packed archive cannot hide game data from the scan, and a zip bomb cannot stall it. pytest -q tests/test_check_no_game_data.py: 8 passed, 50 subtests passed, 0 failed. No issues found; synthetic archives only.

AI-assisted.

@ItsNotRexed
ItsNotRexed merged commit 67f00bb into ProjectLeonida:main Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants