pe: sort package scan and harden CLI path guards in pe_package_preflight with CLI suite #2601

Merged
Ammmae0x merged 2 commits from upstream-pr/2609 into main 2026-11-25 23:33:57 +00:00
Ammmae0x commented 2026-11-25 23:18:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T23:18:13Z.


Change

Make PE package discovery deterministic and harden CLI path validation in tools/pe_package_preflight.py, and add a synthetic PE32+ CLI/error test suite in tests/pe_package_preflight_cli_check.py (registered as pe_package_preflight_cli_check in CMakeLists.txt with TIMEOUT 60):

  • Sort _paths(root) deterministically using key=lambda p: (p.as_posix().casefold(), p.as_posix()) so image-count and total-byte cutoffs do not depend on filesystem traversal order.
  • Validate root.is_dir() and non-empty entry upfront before inspect(), and validate --output before temp staging (reject symlinks, non-directory parent/ancestors, directories/empty path, and paths inside the inspected package).
  • Catch struct.error alongside (OSError, ValueError, KeyError, pefile.PEFormatError) cleanly on stderr with exit code 1 and no tracebacks, preserving existing tests and message strings.

AI-assisted.

Verification

Tested on commit fbc33e67 (rebased on upstream/main) with out\pe-env\Scripts\python.exe -B:

  • out\pe-env\Scripts\python.exe -B tests\pe_package_preflight_check.py -v: 14 tests OK (exit 0).
  • out\pe-env\Scripts\python.exe -B tests\pe_package_preflight_cli_check.py -v: 8 tests OK (exit 0), covering stdout and atomic --output with 0 leftover temps, sys.argv / __main__ entrypoint, unsorted rglob deterministic ordering and cutoff, root.is_dir() / blank entry rejection, --output parent-file/directory/symlink/inside-package rejections before temp staging, atomic replace failure cleanup, invalid PE / missing entry, and Python 3.14-safe argparse errors.
  • Spot-mutation negative control: temporarily disabled 5 guards one by one (_paths sort, _validate_entry, output.is_symlink(), parent-not-directory check, and output.is_dir()); 5/5 mutations killed by tests\pe_package_preflight_cli_check.py and restored cleanly.
  • out\pe-env\Scripts\python.exe -B tests\form_coverage_check.py: passed (exit 0).
  • out\pe-env\Scripts\python.exe -B tests\docs_links_check.py: passed (exit 0).
  • out\pe-env\Scripts\python.exe -B tests\bidi_controls_check.py: passed (exit 0).

Evidence and follow-up

All PE32+ fixtures in tests/pe_package_preflight_cli_check.py are synthetically generated in-memory with struct (MIT License). No CLI flags, JSON schema, or build prerequisites changed.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6615750170 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2609). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T23:18:13Z. --- ## Change Make PE package discovery deterministic and harden CLI path validation in `tools/pe_package_preflight.py`, and add a synthetic PE32+ CLI/error test suite in `tests/pe_package_preflight_cli_check.py` (registered as `pe_package_preflight_cli_check` in `CMakeLists.txt` with `TIMEOUT 60`): - Sort `_paths(root)` deterministically using `key=lambda p: (p.as_posix().casefold(), p.as_posix())` so image-count and total-byte cutoffs do not depend on filesystem traversal order. - Validate `root.is_dir()` and non-empty `entry` upfront before `inspect()`, and validate `--output` before temp staging (reject symlinks, non-directory parent/ancestors, directories/empty path, and paths inside the inspected package). - Catch `struct.error` alongside `(OSError, ValueError, KeyError, pefile.PEFormatError)` cleanly on stderr with exit code 1 and no tracebacks, preserving existing tests and message strings. AI-assisted. ## Verification Tested on commit `fbc33e67` (rebased on `upstream/main`) with `out\pe-env\Scripts\python.exe -B`: - `out\pe-env\Scripts\python.exe -B tests\pe_package_preflight_check.py -v`: 14 tests OK (exit 0). - `out\pe-env\Scripts\python.exe -B tests\pe_package_preflight_cli_check.py -v`: 8 tests OK (exit 0), covering stdout and atomic `--output` with 0 leftover temps, `sys.argv` / `__main__` entrypoint, unsorted `rglob` deterministic ordering and cutoff, `root.is_dir()` / blank `entry` rejection, `--output` parent-file/directory/symlink/inside-package rejections before temp staging, atomic replace failure cleanup, invalid PE / missing entry, and Python 3.14-safe `argparse` errors. - Spot-mutation negative control: temporarily disabled 5 guards one by one (`_paths` sort, `_validate_entry`, `output.is_symlink()`, parent-not-directory check, and `output.is_dir()`); 5/5 mutations killed by `tests\pe_package_preflight_cli_check.py` and restored cleanly. - `out\pe-env\Scripts\python.exe -B tests\form_coverage_check.py`: passed (exit 0). - `out\pe-env\Scripts\python.exe -B tests\docs_links_check.py`: passed (exit 0). - `out\pe-env\Scripts\python.exe -B tests\bidi_controls_check.py`: passed (exit 0). ## Evidence and follow-up All PE32+ fixtures in `tests/pe_package_preflight_cli_check.py` are synthetically generated in-memory with `struct` (MIT License). No CLI flags, JSON schema, or build prerequisites changed. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets.
upstream-sync closed this pull request 2026-11-26 00:00:56 +00:00
tedin7 commented 2026-11-26 00:00:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T23:30:05Z.

Review: COMMENTED


Review of head fbc33e67 (comment only).

Checked against origin/main:

  • The deterministic sort in _paths, the upfront is_dir() and entry checks, _validate_output (symlink, non-directory ancestor, directory, inside-package) and the added struct.error catch are correct. Error messages and exit code 1 stay as before; no tracebacks.
  • The new suite guards caller-visible behavior (exit code, stderr, output file). No other open or merged PR touches pe_package_preflight.
  • CTest registration sits next to pe_package_import_closure, as expected.

Commands run (Linux, pefile in a throwaway venv):

  • tests/pe_package_preflight_check.py: 14 OK
  • tests/pe_package_preflight_cli_check.py: 8 OK
  • python3 tests/docs_links_check.py, python3 tests/bidi_controls_check.py, git diff --check origin/main...HEAD: clean
  • Mutations, each made the new suite fail and was reverted: sort removed (1 failure), symlink output check off (2 failures), empty-entry check off (4 failures).

No blocking findings. Windows dev+release run to follow before any merge.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6066432097 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2609#pullrequestreview-6066432097). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T23:30:05Z. **Review:** COMMENTED --- Review of head fbc33e67 (comment only). Checked against origin/main: - The deterministic sort in `_paths`, the upfront `is_dir()` and entry checks, `_validate_output` (symlink, non-directory ancestor, directory, inside-package) and the added `struct.error` catch are correct. Error messages and exit code 1 stay as before; no tracebacks. - The new suite guards caller-visible behavior (exit code, stderr, output file). No other open or merged PR touches `pe_package_preflight`. - CTest registration sits next to `pe_package_import_closure`, as expected. Commands run (Linux, `pefile` in a throwaway venv): - `tests/pe_package_preflight_check.py`: 14 OK - `tests/pe_package_preflight_cli_check.py`: 8 OK - `python3 tests/docs_links_check.py`, `python3 tests/bidi_controls_check.py`, `git diff --check origin/main...HEAD`: clean - Mutations, each made the new suite fail and was reverted: sort removed (1 failure), symlink output check off (2 failures), empty-entry check off (4 failures). No blocking findings. Windows dev+release run to follow before any merge. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-26 00:01:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T23:31:14Z.


Win2025 WARP receipt for head 623bf8b0 after updating with main.

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit 623bf8b06becc39d209b97eec24808d9ca908b00:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev -- -j 2): 15.25s (configure 2.08s)
    • Test (ctest --preset dev -R 'pe_package_preflight_cli_check|pe_package_import_closure' --parallel 4 --no-tests=error --output-on-failure): 2/2 passed (4.74s: pe_package_import_closure 0.86s, pe_package_preflight_cli_check 4.62s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release -- -j 2): 12.66s (configure 1.90s)
    • Test (ctest --preset release -R 'pe_package_preflight_cli_check|pe_package_import_closure' --parallel 4 --no-tests=error --output-on-failure): 2/2 passed (4.25s: pe_package_import_closure 0.95s, pe_package_preflight_cli_check 4.13s)
  • Windows Python checks: tests/pe_package_preflight_check.py ok (0.8s), tests/pe_package_preflight_cli_check.py ok (4.5s)
  • sccache: 24/68 hits; wall 46.8s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7177675470 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2609#issuecomment-7177675470). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T23:31:14Z. --- Win2025 WARP receipt for head 623bf8b0 after updating with main. Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `623bf8b06becc39d209b97eec24808d9ca908b00`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev -- -j 2`): `15.25s` (configure `2.08s`) - Test (`ctest --preset dev -R 'pe_package_preflight_cli_check|pe_package_import_closure' --parallel 4 --no-tests=error --output-on-failure`): **2/2 passed** (`4.74s`: `pe_package_import_closure` `0.86s`, `pe_package_preflight_cli_check` `4.62s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release -- -j 2`): `12.66s` (configure `1.90s`) - Test (`ctest --preset release -R 'pe_package_preflight_cli_check|pe_package_import_closure' --parallel 4 --no-tests=error --output-on-failure`): **2/2 passed** (`4.25s`: `pe_package_import_closure` `0.95s`, `pe_package_preflight_cli_check` `4.13s`) - **Windows Python checks**: `tests/pe_package_preflight_check.py` ok (`0.8s`), `tests/pe_package_preflight_cli_check.py` ok (`4.5s`) - `sccache`: 24/68 hits; wall `46.8s` AI-assisted; commands listed were executed locally.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2601
No description provided.