shader_stats: reject traversal shader names, support UTF-8 BOM, and harden CLI/number guards (SHADER-STATS-VALIDATION-AND-CLI) #1607

Merged
IlyassRS3 merged 1 commit from upstream-pr/1607 into main 2026-11-28 10:36:21 +00:00
IlyassRS3 commented 2026-11-25 05:48:37 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T05:48:37Z.


Item ID: SHADER-STATS-VALIDATION-AND-CLI (unlisted; coordination issue #14 is at GitHub's 2,500-comment cap with no open successor at submission time, and no open PR touches scripts/shader_stats.py).

What changed

  • Reject path-escaping / invalid shader names (scripts/shader_stats.py, _safe_shader_name, load_stats): previously os.path.join(disasm, name + ".s") allowed absolute paths or ../ entries in stats.csv to read .s files outside <disasm dir>. Now load_stats() validates that each row's name is a single non-empty filename stem with no path separators, drive prefix, NUL byte, . or ...
  • Accept UTF-8 BOM CSV inputs (load_stats): decode stats.csv with utf-8-sig so BOM-prefixed CSVs do not fail with missing required header column(s): name.
  • Validate <disasm dir> before matching listings (main): report <disasm dir>: not a directory instead of silently skipping every shader and exiting with "no shader is in both...".
  • Guard bool in number() and empty vlist in original(): number(True) previously crashed inside Decimal(True) with an uncaught TypeError, and an empty vlist crashed max().
  • Harden main(argv=None) option and path parsing: accept optional argv, check "--details" in argv instead of sys.argv, reject unknown single-dash flags (e.g. -d) as unknown option instead of misparsing them as positional arguments, and reject whitespace-only <stats.csv> / <disasm dir>.
  • Synthetic regression tests (tests/test_shader_stats.py): 6 new test methods covering all of the above (no game files, no GPU).

Verification

  • AI-assisted: yes
  • GPUs tested: none (CPU-only Python tool on macOS)
  • python3 -W error -I tests/test_shader_stats.py -v: 29 passed
  • python3 -W error -I tests/test_text_write_line_endings.py: passed
  • python3 -I scripts/check_no_game_data.py scripts/shader_stats.py tests/test_shader_stats.py: clean
  • python3 -m compileall -q scripts/shader_stats.py tests/test_shader_stats.py: clean
  • Mutation test (tests/test_shader_stats.py against unpatched main:scripts/shader_stats.py): 11 failures, 1 error across the 29 tests (all 6 new tests fail on unpatched main).
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5485946800 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1607). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T05:48:37Z. --- Item ID: `SHADER-STATS-VALIDATION-AND-CLI` (unlisted; coordination issue #14 is at GitHub's 2,500-comment cap with no open successor at submission time, and no open PR touches `scripts/shader_stats.py`). ## What changed - **Reject path-escaping / invalid shader names (`scripts/shader_stats.py`, `_safe_shader_name`, `load_stats`)**: previously `os.path.join(disasm, name + ".s")` allowed absolute paths or `../` entries in `stats.csv` to read `.s` files outside `<disasm dir>`. Now `load_stats()` validates that each row's `name` is a single non-empty filename stem with no path separators, drive prefix, NUL byte, `.` or `..`. - **Accept UTF-8 BOM CSV inputs (`load_stats`)**: decode `stats.csv` with `utf-8-sig` so BOM-prefixed CSVs do not fail with `missing required header column(s): name`. - **Validate `<disasm dir>` before matching listings (`main`)**: report `<disasm dir>: not a directory` instead of silently skipping every shader and exiting with `"no shader is in both..."`. - **Guard `bool` in `number()` and empty `vlist` in `original()`**: `number(True)` previously crashed inside `Decimal(True)` with an uncaught `TypeError`, and an empty `vlist` crashed `max()`. - **Harden `main(argv=None)` option and path parsing**: accept optional `argv`, check `"--details" in argv` instead of `sys.argv`, reject unknown single-dash flags (e.g. `-d`) as `unknown option` instead of misparsing them as positional arguments, and reject whitespace-only `<stats.csv>` / `<disasm dir>`. - **Synthetic regression tests (`tests/test_shader_stats.py`)**: 6 new test methods covering all of the above (no game files, no GPU). ## Verification - AI-assisted: yes - GPUs tested: none (CPU-only Python tool on macOS) - `python3 -W error -I tests/test_shader_stats.py -v`: **29 passed** - `python3 -W error -I tests/test_text_write_line_endings.py`: passed - `python3 -I scripts/check_no_game_data.py scripts/shader_stats.py tests/test_shader_stats.py`: clean - `python3 -m compileall -q scripts/shader_stats.py tests/test_shader_stats.py`: clean - Mutation test (`tests/test_shader_stats.py` against unpatched `main:scripts/shader_stats.py`): **11 failures, 1 error** across the 29 tests (all 6 new tests fail on unpatched `main`).
copilot-pull-request-reviewer[bot] commented 2026-11-25 05:48:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T05:48:47Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6052482082 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1607#pullrequestreview-6052482082). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T05:48:47Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
MoonEyes commented 2026-11-28 10:36:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T10:36:14Z.


Red/green: the PR's tests fail on main and pass with this PR (Linux, Python 3.13.18).
Targeted batch of 18 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238003632 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1607#issuecomment-7238003632). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T10:36:14Z. --- Red/green: the PR's tests fail on main and pass with this PR (Linux, Python 3.13.18). Targeted batch of 18 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13. AI-assisted (Claude Code, Claude Opus 5.5)
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
ProjectLeonida/mandy!1607
No description provided.