Skip to content

fspx_unpack: validate inputs and --out before writing and refuse clobbering inputs - #2061

Open
luisbaker wants to merge 1 commit into
ItsNotRexed:mainfrom
luisbaker:build-fspx-unpack-out-guard
Open

luisbaker wants to merge 1 commit into
ItsNotRexed:mainfrom
luisbaker:build-fspx-unpack-out-guard

Conversation

@luisbaker

Copy link
Copy Markdown
Contributor

Fixes unhandled tracebacks and silent input-file clobbering in \ ools/fspx_unpack.py:

  • Validates --out, existing ancestor components, and required \ spx/\disasm\ subfolders before creating directories or writing files (returns exit code 1 with a clean stderr error if any path is a regular file).
  • Validates all input paths in \ iles\ before creating --out\ (missing files and directories exit 1 cleanly without leaving empty --out/fspx\ folders behind).
  • Splits unpacking into a planning phase and a write phase, refusing any planned output (<name>.dxbc, \ spx/.code, \ spx/.json, \disasm/.s, \index.json) that collides with any input file (via
    ealpath/\samefile\ and case-folded / macOS NFD checks), leaving all inputs untouched.
  • Adds 9 unit tests in \ ests/test_fspx_unpack.py.

Tested

  • \python -I tests/test_fspx_unpack.py\ — 31/31 OK (Windows 11, Python 3.11, CPU only, no game files)
  • \python -I tests/test_text_write_line_endings.py\ — 2/2 OK

Claim: #1902 (comment)

AI-assisted.

@FreddyxS

Copy link
Copy Markdown
Contributor

Independent Windows review of 979d792d against main 4eb6155d. Coordination claim.

Correctness: 42 tests passed across test_fspx_unpack.py, test_pe_shader_containers.py and test_text_write_line_endings.py. The nine new tests all failed on main (four assertions, five exceptions). In a separate case-variant alias check, main overwrote a synthetic input with a trailing marker. The PR returned 1, preserved the exact bytes and created no extra outputs. Windows denied creation of a hard link in this environment, so that alias case remains unverified here.

Batch regression: I ran each revision’s main() on distinct files generated with Containers().container() from the test suite, using a fresh --out directory per run. Output trees were byte-identical. Approximate wall times on this machine:

Inputs main PR Alias comparisons
12 0.029 s 0.504 s 444
48 0.087 s 6.554 s 6,960
96 0.175 s 28.065 s 27,744

The preflight loop checks all inputs against each output and repeats filesystem queries in every comparison. I would index the input path keys and existing file identities once, then look up each output. This would preserve the collision protection while avoiding the measured quadratic cost.

Integration: git merge-tree against #1911 (d9f7ec41) reports a content conflict in tools/fspx_unpack.py. When resolving it, include #1911’s new fspx/<name>.state output in the preflight collision check.

CPython 3.13.15 on Windows, CPU only, synthetic inputs. compileall and git diff --check passed. AI-assisted (Codex).

@papaipits

Copy link
Copy Markdown
Contributor

Read the diff + tested 979d792d on Windows 11 (Python 3.11.2): python -I tests/test_fspx_unpack.py → 31 passed.

Validates inputs and --out before writing: a missing input (first or second), a directory given as input, and an existing file as --out each return non-zero and leave no output / do not clobber the existing file — the tests assert os.path.exists(out) is False and the existing file is untouched. Good fail-before-write behavior. AI-assisted. No game files.

…bering inputs

Validate --out (including existing non-directory ancestors and fspx/disasm subdirs) and all input file paths before creating any output directory, and check every planned output path against all input files (including realpath/samefile and case-folded collisions) before writing so unpacking a container into its own directory cannot truncate it in place. Adds 9 CLI tests in tests/test_fspx_unpack.py. No game files.
@luisbaker
luisbaker force-pushed the build-fspx-unpack-out-guard branch from 979d792 to 58c3db8 Compare November 23, 2026 00:42
@luisbaker

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (03b09a08) to fix the conflict. Two main changes overlapped with this PR:

  • The unreadable-input handling from 9ff3024c is kept as main has it: a one-line cannot read input: ... message and a counted failed input. The new up-front checks still reject missing paths and directories before --out is created.
  • The fspx/<name>.state sidecar from 6a16ac14 now goes through the same plan-then-write path, so it gets the same checks as .dxbc, .code, .json, .s and index.json: output-is-a-directory, and refuse-to-overwrite-an-input.

Tests:

  • python -I tests/test_fspx_unpack.py passes 32 tests, including main's locked-input test.
  • The 9 new tests fail against main's unpatched tools/fspx_unpack.py (7 failures, 2 errors).
  • test_pe_shader_containers.py (16) and test_text_write_line_endings.py (3) pass.
  • python -I -m compileall -q tools tests is clean.

No game files. AI-assisted (Claude Code).

@sds-228 sds-228 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.

Code review of 58c3db8: no issues found. Inputs and output paths are validated before anything is created, and output/input collisions are checked case-insensitively before the first write. Targeted suite: 32 tests OK; compileall and diff check clean.

AI-assisted (OpenCode).

@SebSilent

Copy link
Copy Markdown

Reviewed 58c3db89 on Linux, Python 3.12, CPU only, synthetic containers, no game files.

Red/green. With the head's test file against main's tool: 9 failures/errors, exactly the 9 new tests, everything else passes. On the head: python -I tests/test_fspx_unpack.py gives 32 tests, 31 pass + 1 skip, OK. compileall clean, git diff --check clean.

Manual repros beyond the tests, all with one stderr line, exit 1, no traceback, nothing written:

  • --out an existing file, and --out nested under an existing file: both caught by the ancestor walk, the blocker file untouched.
  • directory as input: clean is a directory line, no --out created.
  • hardlink and symlink of a planned output passed as input: both refused (samefile/realpath do their job), exit 1.
  • normal unpack unchanged, exit 0.

Notes, nothing blocking.

  • Exit 1 rather than the repo-wide input-error convention of 2: correct call here, this tool has always returned 1 for bad inputs (return 1 if bad else 0) and you kept main's cannot read input path as-is. Consistent.
  • The case-variant refusal fires on case-sensitive filesystems too (Linux: FOO.DXBC input vs FOO.dxbc output would not actually clobber). Conservative and clearly deliberate since the test pins it from Linux CI, fine, just flagging that it is a false positive by design.
  • A parse-failing input (valid path, no FSPX inside) still creates --out/fspx before exiting 1. Pre-existing behavior, not a regression, the PR only promises no writes for path-invalid inputs and it holds.
  • Checked make_game.py's FSPX stage (py("fspx_unpack.py", dll, "--out", unpacked, "--disasm")): input and outputs live in different directories, no false collision, and a nonzero exit was already a stage failure, so the earlier bail-out changes nothing there.
  • Cosmetic: the PR body's backticks render as literal backslashes (\tools/fspx_unpack.py\), probably a shell-escaping accident.

Overall the two-phase plan/write split is the right shape, collision checks cover exact path, hardlink, symlink and case/NFD variants, and the tests pin each one separately. LGTM.

AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 58c3db89 merged with main 03b09a08 on Linux (WSL2 Ubuntu), Python 3.12.16, and native Windows 11, Python 3.13.1: pass.

  • tests/test_fspx_unpack.py: Linux 32 tests OK (1 skipped); Windows 32 OK (0 skipped). Merge was clean.
  • Negative control: restoring main's tools/fspx_unpack.py makes 7 failures and 2 errors on Linux (and 7 failures, 2 errors on Windows); the tests drive the real CLI, not an embedded model.
  • Read the diff: validation runs before any write, and the collision check runs over the whole planned output list before the first file is created. No bug found.
  • Minor note: _collides_with_input casefolds names on every platform, so on case-sensitive Linux an input FOO.DXBC and an output foo.dxbc in the same directory (two distinct files) are refused (checked: _collides_with_input('/tmp/x/foo.dxbc','/tmp/x/FOO.dxbc') is True). Conservative rather than wrong.

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.

6 participants