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

Open
github-bot wants to merge 1 commit from github-bot/gh-1509 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 02:47 UTC · original
IlyassRS3:fix/smh-groom-hardening → main · +434 −35 · 2 files · 1 commits · open

Claimed in the coordination issue as: SMH-GROOM-VALIDATION-HARDENING (#14)

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)
**@⁠IlyassRS3** on GitHub · 2026-11-25 02:47 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1509) `IlyassRS3:fix/smh-groom-hardening` → `main` · +434 −35 · 2 files · 1 commits · open Claimed in the coordination issue as: SMH-GROOM-VALIDATION-HARDENING (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:54:47 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 02:48 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 02:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1509#pullrequestreview-6051080694) · **reviewed** 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.
Author
Owner

@⁠ytsodacan on GitHub · 2026-11-25 03:34 UTC · original · edited 2026-11-25 03:44 UTC

[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.
**@⁠ytsodacan** on GitHub · 2026-11-25 03:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1509#issuecomment-7152036684) · edited 2026-11-25 03:44 UTC **[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.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-25 07:45 UTC · original

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.

**@⁠whoalin1** on GitHub · 2026-11-25 07:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1509#issuecomment-7156214197) 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1509/head:github-bot/gh-1509'
git switch 'github-bot/gh-1509'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1509'
git switch 'github-bot/gh-1509'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1509'
git switch 'github-bot/gh-1509'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1509'
git switch 'main'
git merge --squash 'github-bot/gh-1509'
git switch 'main'
git merge --ff-only 'github-bot/gh-1509'
git switch 'main'
git merge 'github-bot/gh-1509'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.