pe: harden atomic temp cleanup and error paths in intake_exe with unit and CLI suite #2350
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2358"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T13:34:28Z.
Change
Harden path checks, report-directory creation, and atomic temp-file cleanup in
tools/intake_exe.pyand add a dedicated pure-Python unit and CLI hardening suite (tests/intake_exe_check.py, registered aspe_intake_checksinCMakeLists.txt):inside()by resolving the root base explicitly and rejecting leading/or\prefixes (in addition toPath.is_absolute(), traversal outsidebase, andpath == base).args.build.resolve()/args.reports.resolve()againstOSErrorinmain(argv=None) -> int, exiting cleanly with code 1 and zero raw tracebacks.reports.mkdir(parents=True, exist_ok=True),NamedTemporaryFile+json.dump, andos.replace(tmp_path, dest)insidetry/except (OSError, TypeError, ValueError)withfinally: tmp_path.unlink(missing_ok=True)so partial.tmpfiles are always removed on write or replace failure.Verification
Verified on commit
a1e2eaee(rebased ontoupstream/main43608f2e):out/pe-env/Scripts/python.exe -B tests/intake_exe_check.py -v->Ran 5 tests in 0.314s/OK(exit 0)out/pe-env/Scripts/python.exe -B tests/intake/routing_check.py->intake routing synthetic checks passed(exit 0)out/pe-env/Scripts/python.exe -B tests/intake/import_forms_check.py->named, ordinal and delay-load import form checks passed(exit 0)out/pe-env/Scripts/python.exe -B tests/pe_inventory_cli_check.py->Ran 7 tests in 2.139s/OK(exit 0)out/pe-env/Scripts/python.exe -B tests/pe_run_package_plan_check.py->Ran 11 tests in 1.173s/OK(exit 0)out/pe-env/Scripts/python.exe -B tests/pe_run_package_invalid_check.py->Ran 3 tests in 0.502s/OK(exit 0)out/pe-env/Scripts/python.exe -B tests/pe_package_preflight_check.py->Ran 14 tests in 0.371s/OK(exit 0)out/pe-env/Scripts/python.exe -B tests/pe_package_ordinal_plan_check.py->Ran 3 tests in 0.001s/OK(exit 0)out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py->form coverage checks passed(exit 0)out/pe-env/Scripts/python.exe -B tests/docs_links_check.py->Self-test passed. Checked 19 Markdown files and 459 links.(exit 0)out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py->Self-test passed. Checked 1646 tracked source, script and build/CI files: no bidirectional controls.(exit 0)Evidence and follow-up
tests/intake_exe_check.pyare synthetic in-memory PE32+ binaries (.text,.rdatanormal/delay imports, TLS callback table,.pdataruntime-function records) and syntheticMicrosoftGame.Config/layout.xml/baseline/integrity JSON files created inside temporary directories.inside(), absent/misaligned/overlapping.pdatafallback, non-x64/DLL/truncated PE rejection, missing candidate and malformed XML error reports (exit 2),--reportscolliding with build directory (exit 2) or pointing to an existing file (exit 1),--require-routingunsupported imports (exit 1),Path.resolveOSError(exit 1), and temp.tmpunlink verification on bothjson.dumpandos.replacefailures.Checklist
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T13:40:30Z.
Review: COMMENTED
Review of head
a1e2eaeee91f40894cb2580b0192817d5df6e4ae(Linux, local).Scope:
tools/intake_exe.py(+28/-11), newtests/intake_exe_check.py,CMakeLists.txt(pe_intake_checks). Title is neutral. MERGEABLE. CI: Linux checks, gfx1013 fixtures and Windows build/test pass.Commands executed locally (venv with
pefile):python -B tests/intake_exe_check.py->Ran 5 tests/OKpython -B tests/intake/routing_check.py->intake routing synthetic checks passedpython -B tests/pe_inventory_cli_check.py-> 4 errors, allKeyError: 'SystemRoot'(Windows-only env var on Linux; file not touched by this PR, so not a regression)python3 tests/docs_links_check.py-> pass (459 links)python3 tests/bidi_controls_check.py-> passgit diff --check origin/main...HEAD-> cleanMutations (all reverted):
os.replace(tmp_path, dest)replaced byraise OSError-> suite FAILED (2 failures), killedfinallytemp cleanup disabled (if tmp_path is not None->if False) -> FAILED (1), killed/or\check removed frominside()-> FAILED (1), killedFindings:
tools/intake_exe.py(main,os.replace): replace now overwrites an existing<name>.<status>.json, where the oldrenamefailed on Windows. Harmless since temp names are random, but worth one line in the description.tools/intake_exe.py(inside): thestartswith(("/", "\\"))check matters on Windows (rooted but not absolute paths such as\x). On Linux the new test only exercises the/half, so the\half is covered only by the Windows CI job.error:and returns 1 while the argparse path usesparser.error(exit 2). Fine, just inconsistent.No Windows run needed: pure Python, no shader or C++ change.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T13:55:34Z.
Four mutations: two caught, two equivalent — and I checked the equivalence rather than reporting it as a gap
Baseline on this branch (
tests/intake_exe_check.py→OK, exit 0).base = root.resolve()→base = rootpath == base→False(the root itself is accepted)Path(name).is_absolute()→Falseexcept OSError→except ZeroDivisionErrorinmainWhy the two survivors are equivalent, measured rather than assumed
is_absolute()removed. Every absolute form is still refused by the other clauses:C:/Windows/win.iniis the interesting one: it is notstartswith(('/', '\\')), so the clause that saves it isis_relative_to(base)— joining an absolutename with a base yields the absolute path, which is not relative to the base. So two of the three clauses are redundant for the cases I could construct.
base = root(noresolve). With an unresolved root the check gets stricter, not looser —inside()then refuses even a plain name, because thesymlink-free
..in the root does not normalise:So it is not an escape, it is a false refusal — which is why the mutant survives: no test asserts that a resolved root makes a plain name work. The
resolve()on line 43 is therefore load-bearing in the opposite direction from the one a reader might assume, and that is worth one sentence in thedocstring of
inside().The one thing I would add
inside()is the security boundary for package paths, and today its guarantee is provided by three clauses of which any one would do for the inputs I tried.That is fine as defence in depth, but it means a future "simplify this" edit cannot be caught by construction — only by the cases that happen to be in the
test. A table with one row per rejection reason would pin the intent rather than the current overlap:
Your check already covers behaviour; this would make the reasons explicit, which is what a security helper's test is for.
What the change gets right that is easy to miss
except OSErroraroundargs.build.resolve()is the correct catch:resolve()raisesOSError(notFileNotFoundErrorspecifically) for a path thatcannot be resolved, and returning 1 with
error: …on stderr instead of a traceback is what a CLI should do. The temp-file rework also drops theNamedTemporaryFile(delete=False)+renamepattern that leaked the file when the rename failed — I checked that the two survivors above do not weakenthat path, since it is the part of the diff most likely to lose a
.tmpon an exception.AI-assisted; the four mutations and both probes were run locally on Windows 11 build 26300.9974.
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T14:02:16Z.
Addressed in commit
5ce306b9:inside()intools/intake_exe.pynoting whyroot.resolve()is required beforeis_relative_to(base).test_inside_rejects_traversal_root_and_absolute_pathsintests/intake_exe_check.pywith explicit refusal cases (absolute_posix,absolute_drive,absolute_unc,escape_parent,escape_nested,root_dot,empty) and added an unresolvedroot / "sub" / ".."positive case that pinsbase = root.resolve()(verified mutatingbase = root.resolve()tobase = rootnow fails with 1 error).tests/intake_exe_check.py(5/5 OK),tests/intake/routing_check.py,tests/intake/import_forms_check.py,tests/pe_inventory_cli_check.py(7/7 OK),tests/form_coverage_check.py,tests/docs_links_check.py(19 files, 459 links), andtests/bidi_controls_check.py(1646 files).AI-assisted.