smh_groom: cap zlib inflation, validate bounds/args/polylines, and harden CLI guards (SMH-GROOM-VALIDATION-HARDENING) #1509

Merged
IlyassRS3 merged 1 commit from upstream-pr/1509 into main 2026-11-28 14:43:01 +00:00
IlyassRS3 commented 2026-11-25 02:47:53 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T02:47:53Z.


Claimed in the coordination issue as: SMH-GROOM-VALIDATION-HARDENING (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7151063354)

What and why

Hardens tools/smh_groom.py against malformed inputs, unbounded zlib inflation, invalid API/CLI arguments, and unsafe output paths:

  1. Bounded zlib decompression & GPU chunk caps (inflate_stream, _read_zlib_resource, read_resource):
    • inflate_stream previously called decoder.decompress(chunk) with no max_length, allowing a single 64 KiB compressed chunk to expand arbitrarily in memory before len(out) > MAX_INFLATE_BYTES was checked. It now passes max_length=remaining + 1, checks decoder.unconsumed_tail, supports a max_bytes override (resolving MAX_INFLATE_BYTES dynamically when omitted), and rejects negative or bool offsets.
    • _read_zlib_resource rejects declared GPU buffer sizes above MAX_INFLATE_BYTES upfront, caps cumulative multi-chunk GPU inflation at MAX_INFLATE_BYTES, and rejects zero-progress / empty zlib chunks instead of looping indefinitely.
  2. Non-finite system-page bounding box (parse_system_page):
    • Rejects NaN/±Inf in box_min (+0x30) and box_max (+0x40) with GroomError (preserving finite ±FLT_MAX child-rig sentinels in +0x50..+0x6f), preventing downstream ValueError in to_glb or invalid JSON in inspect --json.
  3. Input-overwrite & directory guards (load_groom, main):
    • export-glb now uses report_paths.same_report_path(args.path, args.output) (covering symlinks, hard links, case-folding volumes, and non-existent aliases) and rejects an output directory upfront before loading the input; load_groom also rejects directories cleanly with GroomError.
  4. Argument validation & class deduplication (Groom.seed, scatter, to_glb, _parse_classes, _parse_positive_float):
    • Rejects bool values (False/True previously passed in (0, 1) and int checks) in seed, scatter, and to_glb.
    • Validates density_scale as finite and non-negative (even when count=0 or max_accepted <= 0), and avoids OverflowError in _f32(float(c) * ds32) for large finite density_scale >= 1.0 while preserving bit-exact float32 thresholds for 0 < density_scale < 1.
    • Validates positive finite guide_radius/strand_radius (in both to_glb and CLI --guide-radius/--strand-radius), non-negative max_strands, positive vertex_budget, string up_axis, and deduplicates classes in to_glb so classes=(0, 0) does not emit duplicate strand primitives.
    • _parse_classes now raises argparse.ArgumentTypeError instead of raw ValueError on non-integer tokens like --classes foo or --classes 0,.
  5. Uniform polyline & live validation (build_tubes, build_ribbons, build_lines):
    • Rejects ragged polylines, mismatched live lengths, invalid radius/width/tip, and bool sides with GroomError instead of silently shifting indices or throwing IndexError.
  6. Census resilience & strict RFC 8259 JSON (run_census, main):
    • run_census validates limit >= 1 (non-bool) for direct Python callers and catches struct.error/OverflowError per resource so one corrupt file does not abort a multi-archive census.
    • inspect --json, census --json, and export-glb JSON output pass allow_nan=False.

Tested

  • python3 -I tests/test_smh_groom.py -v: 30/30 OK (22 existing + 8 new tests in ValidationHardeningTests).
  • Mutation check against unpatched main:tools/smh_groom.py: all 8 new tests in ValidationHardeningTests fail (FAILED (failures=16, errors=10)).
  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
  • python3 -I tests/test_report_paths.py: 22/22 OK.
  • python3 -I scripts/check_no_game_data.py tools/smh_groom.py tests/test_smh_groom.py: clean.
  • python3 -m compileall -q tools/smh_groom.py tests/test_smh_groom.py: clean.

GPUs tested: n/a (pure Python stdlib tool, synthetic unit tests only, no GPU or game data)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — no translator rule changes)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5484200491 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1509). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T02:47:53Z. --- Claimed in the coordination issue as: SMH-GROOM-VALIDATION-HARDENING (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7151063354) ### What and why Hardens `tools/smh_groom.py` against malformed inputs, unbounded zlib inflation, invalid API/CLI arguments, and unsafe output paths: 1. **Bounded zlib decompression & GPU chunk caps (`inflate_stream`, `_read_zlib_resource`, `read_resource`)**: - `inflate_stream` previously called `decoder.decompress(chunk)` with no `max_length`, allowing a single 64 KiB compressed chunk to expand arbitrarily in memory before `len(out) > MAX_INFLATE_BYTES` was checked. It now passes `max_length=remaining + 1`, checks `decoder.unconsumed_tail`, supports a `max_bytes` override (resolving `MAX_INFLATE_BYTES` dynamically when omitted), and rejects negative or `bool` offsets. - `_read_zlib_resource` rejects declared GPU buffer sizes above `MAX_INFLATE_BYTES` upfront, caps cumulative multi-chunk GPU inflation at `MAX_INFLATE_BYTES`, and rejects zero-progress / empty zlib chunks instead of looping indefinitely. 2. **Non-finite system-page bounding box (`parse_system_page`)**: - Rejects `NaN`/`±Inf` in `box_min` (`+0x30`) and `box_max` (`+0x40`) with `GroomError` (preserving finite `±FLT_MAX` child-rig sentinels in `+0x50..+0x6f`), preventing downstream `ValueError` in `to_glb` or invalid JSON in `inspect --json`. 3. **Input-overwrite & directory guards (`load_groom`, `main`)**: - `export-glb` now uses `report_paths.same_report_path(args.path, args.output)` (covering symlinks, hard links, case-folding volumes, and non-existent aliases) and rejects an output directory upfront before loading the input; `load_groom` also rejects directories cleanly with `GroomError`. 4. **Argument validation & class deduplication (`Groom.seed`, `scatter`, `to_glb`, `_parse_classes`, `_parse_positive_float`)**: - Rejects `bool` values (`False`/`True` previously passed `in (0, 1)` and `int` checks) in `seed`, `scatter`, and `to_glb`. - Validates `density_scale` as finite and non-negative (even when `count=0` or `max_accepted <= 0`), and avoids `OverflowError` in `_f32(float(c) * ds32)` for large finite `density_scale >= 1.0` while preserving bit-exact float32 thresholds for `0 < density_scale < 1`. - Validates positive finite `guide_radius`/`strand_radius` (in both `to_glb` and CLI `--guide-radius`/`--strand-radius`), non-negative `max_strands`, positive `vertex_budget`, string `up_axis`, and deduplicates `classes` in `to_glb` so `classes=(0, 0)` does not emit duplicate strand primitives. - `_parse_classes` now raises `argparse.ArgumentTypeError` instead of raw `ValueError` on non-integer tokens like `--classes foo` or `--classes 0,`. 5. **Uniform polyline & `live` validation (`build_tubes`, `build_ribbons`, `build_lines`)**: - Rejects ragged polylines, mismatched `live` lengths, invalid `radius`/`width`/`tip`, and `bool` `sides` with `GroomError` instead of silently shifting indices or throwing `IndexError`. 6. **Census resilience & strict RFC 8259 JSON (`run_census`, `main`)**: - `run_census` validates `limit >= 1` (non-`bool`) for direct Python callers and catches `struct.error`/`OverflowError` per resource so one corrupt file does not abort a multi-archive census. - `inspect --json`, `census --json`, and `export-glb` JSON output pass `allow_nan=False`. ### Tested - `python3 -I tests/test_smh_groom.py -v`: **30/30 OK** (22 existing + 8 new tests in `ValidationHardeningTests`). - Mutation check against unpatched `main:tools/smh_groom.py`: all 8 new tests in `ValidationHardeningTests` fail (`FAILED (failures=16, errors=10)`). - `python3 -I tests/test_text_write_line_endings.py`: **3/3 OK**. - `python3 -I tests/test_report_paths.py`: **22/22 OK**. - `python3 -I scripts/check_no_game_data.py tools/smh_groom.py tests/test_smh_groom.py`: clean. - `python3 -m compileall -q tools/smh_groom.py tests/test_smh_groom.py`: clean. GPUs tested: n/a (pure Python stdlib tool, synthetic unit tests only, no GPU or game data) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a — no translator rule changes)
copilot-pull-request-reviewer[bot] commented 2026-11-25 02:48:05 +00:00 (Migrated from github.com)

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

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:6051080694 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1509#pullrequestreview-6051080694). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T02:48:05Z. **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.
ytsodacan commented 2026-11-25 03:34:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ytsodacan on 2026-11-25T03:34:12Z.


[KEEP]

  • Scope is clean: only tools/smh_groom.py and tests/test_smh_groom.py (+434/-35), mergeable, and no other PR touches smh_groom.
  • The tests look targeted (zlib-bomb cap at MAX_INFLATE_BYTES with exact-boundary and +1 cases, negative/bool offsets, non-finite bounding boxes, no-progress empty chunks).
  • CI has not finished: change-scope and no-game-data are still pending, so there is no green signal yet.
  • Once CI is green this looks mergeable as-is; no changes requested so far.
<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7152036684 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1509#issuecomment-7152036684). Originally posted by [ytsodacan](https://github.com/ytsodacan) on 2026-11-25T03:34:12Z. --- **[KEEP]** - Scope is clean: only `tools/smh_groom.py` and `tests/test_smh_groom.py` (+434/-35), mergeable, and no other PR touches `smh_groom`. - The tests look targeted (zlib-bomb cap at `MAX_INFLATE_BYTES` with exact-boundary and +1 cases, negative/bool offsets, non-finite bounding boxes, no-progress empty chunks). - CI has not finished: `change-scope` and `no-game-data` are still pending, so there is no green signal yet. - Once CI is green this looks mergeable as-is; no changes requested so far.
whoalin1 commented 2026-11-25 07:45:16 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by whoalin1 on 2026-11-25T07:45:16Z.


Independent verification (native Windows, Python 3.11.9 — stdlib-only so representative): diff vs merge-base reviewed; all claimed hardening present — inflate_stream decompress cap + unconsumed-tail check, MAX_INFLATE_BYTES with zero-progress rejection, non-finite bbox rejection, dir rejection for load_groom, same_report_path for export-glb, bool/int/float validation, strict allow_nan=False JSON. Tests: test_smh_groom.py 30/30, test_report_paths.py 22 OK (1 skip), test_text_write_line_endings.py 3/3, compileall clean. AI-assisted verification.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7156214197 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1509#issuecomment-7156214197). Originally posted by [whoalin1](https://github.com/whoalin1) on 2026-11-25T07:45:16Z. --- Independent verification (native Windows, Python 3.11.9 — stdlib-only so representative): diff vs merge-base reviewed; all claimed hardening present — `inflate_stream` decompress cap + unconsumed-tail check, `MAX_INFLATE_BYTES` with zero-progress rejection, non-finite bbox rejection, dir rejection for `load_groom`, `same_report_path` for export-glb, bool/int/float validation, strict `allow_nan=False` JSON. Tests: `test_smh_groom.py` 30/30, `test_report_paths.py` 22 OK (1 skip), `test_text_write_line_endings.py` 3/3, compileall clean. AI-assisted verification.
MoonEyes commented 2026-11-28 14:42:58 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T14:42:58Z.


Red/green: pytest tests/test_smh_groom.py fails on main (26 failed, 23 passed, 36 subtests passed) and passes on head a049209a73 merged with main (30 passed, 92 subtests passed).
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7240876993 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1509#issuecomment-7240876993). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T14:42:58Z. --- Red/green: `pytest tests/test_smh_groom.py` fails on main (26 failed, 23 passed, 36 subtests passed) and passes on head a049209a7390 merged with main (30 passed, 92 subtests passed). Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure. 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!1509
No description provided.