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

Open
opened 2026-11-26 20:21:36 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1509
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/smh-groom-hardening


📝 Commits (1)

  • a049209 smh_groom: cap zlib inflation, validate bounds/args/polylines, and harden CLI guards

📊 Changes

2 files changed (+434 additions, -35 deletions)

View changed files

📝 tests/test_smh_groom.py (+293 -0)
📝 tools/smh_groom.py (+141 -35)

📄 Description

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1509 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/smh-groom-hardening` --- ### 📝 Commits (1) - [`a049209`](https://github.com/ubiquitarianism/leonida/commit/a049209a73907274ac3f8a940f5365a4e9ee7c52) smh_groom: cap zlib inflation, validate bounds/args/polylines, and harden CLI guards ### 📊 Changes **2 files changed** (+434 additions, -35 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_smh_groom.py` (+293 -0) 📝 `tools/smh_groom.py` (+141 -35) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1538
No description provided.