exe_sha: validate 64-hex SHA-256, reject bool size, and fail on unreadable --manifest (EXE-SHA-VALIDATE) #616

Merged
hamdog0203 merged 1 commit from upstream-pr/616 into main 2026-11-28 13:03:40 +00:00
hamdog0203 commented 2026-11-24 10:00:03 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by hamdog0203 on 2026-11-24T10:00:03Z.


Claimed in the coordination issue as: EXE-SHA-VALIDATE (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7128088818)
Depends on: none

What and why

Fixes four validation gaps in tools/exe_sha.py:

  • check() and recorded() only checked len == 64 for SHA-256 digests, so 64 non-hex characters (e.g. "z" * 64) bypassed the format check, hashed the entire file, and reported a hash mismatch instead of "is not a SHA-256 (64 hex digits)". recorded() also didn't strip surrounding whitespace.
  • recorded() accepted bool for expected_exe_size (isinstance(True, int) is True in Python).
  • A size-only manifest match left result["status"] as "unrecorded" instead of "match".
  • Passing a missing, malformed, or non-object JSON file via explicit --manifest silently fell back to unrecorded (exit 0) instead of exiting 2, and UTF-8 BOM manifests were rejected by json.load.

The fix

  • Added _valid_sha256() (strip + lowercase + 64 hex-digit check) and used it in both recorded() and check().
  • Rejected bool in recorded() (not isinstance(size, bool)), switched manifest reads in find_manifest() and recorded() to encoding="utf-8-sig", and added strict=(manifest is not None) so explicit --manifest errors propagate to main() and exit 2.
  • Marked size-only manifest matches as status = "match" in check().
  • Left if __name__ == "__main__": untouched to avoid conflicting with #605.
  • Added 5 regression tests in tests/test_exe_sha.py.

Tested

Windows 11 (26200.9974), Python 3.12.10, CPU only, no game files.

  • python -I tests/test_exe_sha.py -v: 16/16 OK
  • Negative control (the 5 new tests against main 21625bca8's tools/exe_sha.py): FAILED (failures=5)
  • python -m compileall -q tools/exe_sha.py tests/test_exe_sha.py: exit 0
  • python -I scripts/check_no_game_data.py tools/exe_sha.py tests/test_exe_sha.py: exit 0
  • python -I tests/test_text_write_line_endings.py: 3/3 OK
  • git diff --check: clean

AI-assisted: yes.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5469982426 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/616). Originally posted by [hamdog0203](https://github.com/hamdog0203) on 2026-11-24T10:00:03Z. --- Claimed in the coordination issue as: EXE-SHA-VALIDATE (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7128088818) Depends on: none ### What and why Fixes four validation gaps in `tools/exe_sha.py`: - `check()` and `recorded()` only checked `len == 64` for SHA-256 digests, so 64 non-hex characters (e.g. `"z" * 64`) bypassed the format check, hashed the entire file, and reported a hash mismatch instead of `"is not a SHA-256 (64 hex digits)"`. `recorded()` also didn't strip surrounding whitespace. - `recorded()` accepted `bool` for `expected_exe_size` (`isinstance(True, int)` is `True` in Python). - A size-only manifest match left `result["status"]` as `"unrecorded"` instead of `"match"`. - Passing a missing, malformed, or non-object JSON file via explicit `--manifest` silently fell back to `unrecorded` (`exit 0`) instead of exiting `2`, and UTF-8 BOM manifests were rejected by `json.load`. ### The fix - Added `_valid_sha256()` (strip + lowercase + 64 hex-digit check) and used it in both `recorded()` and `check()`. - Rejected `bool` in `recorded()` (`not isinstance(size, bool)`), switched manifest reads in `find_manifest()` and `recorded()` to `encoding="utf-8-sig"`, and added `strict=(manifest is not None)` so explicit `--manifest` errors propagate to `main()` and exit `2`. - Marked size-only manifest matches as `status = "match"` in `check()`. - Left `if __name__ == "__main__":` untouched to avoid conflicting with #605. - Added 5 regression tests in `tests/test_exe_sha.py`. ### Tested Windows 11 (26200.9974), Python 3.12.10, CPU only, no game files. - `python -I tests/test_exe_sha.py -v`: **16/16 OK** - Negative control (the 5 new tests against `main` `21625bca8`'s `tools/exe_sha.py`): **FAILED (failures=5)** - `python -m compileall -q tools/exe_sha.py tests/test_exe_sha.py`: exit 0 - `python -I scripts/check_no_game_data.py tools/exe_sha.py tests/test_exe_sha.py`: exit 0 - `python -I tests/test_text_write_line_endings.py`: **3/3 OK** - `git diff --check`: clean AI-assisted: yes.
madvillain91 commented 2026-11-24 10:30:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by madvillain91 on 2026-11-24T10:30:52Z.

Review: COMMENTED


Tested locally on Windows 11 — all 16 tests in \ ests/test_exe_sha.py\ pass. The _valid_sha256()\ helper, \ool\ size guard, \utf-8-sig\ BOM handling, and strict --manifest\ error path all look clean. LGTM.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6037582953 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/616#pullrequestreview-6037582953). Originally posted by [madvillain91](https://github.com/madvillain91) on 2026-11-24T10:30:52Z. **Review:** COMMENTED --- Tested locally on Windows 11 — all 16 tests in \ ests/test_exe_sha.py\ pass. The \_valid_sha256()\ helper, \ool\ size guard, \utf-8-sig\ BOM handling, and strict \--manifest\ error path all look clean. LGTM.
MoonEyes commented 2026-11-28 13:03:36 +00:00 (Migrated from github.com)

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


Red/green: tests/test_exe_sha.py fails on main (1 failed, -x), passes with the PR (16 passed).
Targeted batch of 30 PRs (their tests, the tests importing the changed modules, the docs tests, the translator suite) on main a997a3c: no new failure, Windows 11 / Python.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239659768 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/616#issuecomment-7239659768). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T13:03:36Z. --- Red/green: tests/test_exe_sha.py fails on main (1 failed, -x), passes with the PR (16 passed). Targeted batch of 30 PRs (their tests, the tests importing the changed modules, the docs tests, the translator suite) on main a997a3c: no new failure, Windows 11 / Python. 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!616
No description provided.