glb_writer: harden JSON/transform/primitive validation and shared path guard (GLB-WRITER-VALIDATION-HARDENING) #1438

Merged
IlyassRS3 merged 1 commit from upstream-pr/1438 into main 2026-11-28 10:14:05 +00:00
IlyassRS3 commented 2026-11-25 00:14:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T00:14:51Z.


Item ID: GLB-WRITER-VALIDATION-HARDENING
AI-assisted: yes

Summary

Fixes validation, numerical-stability, and CLI-safety bugs in tools/glb_writer.py:

  1. read_glb_summary() strict JSON & buffer-index validation:
    • Rejects non-finite JSON constants (NaN, Infinity, -Infinity) with GlbError via parse_constant.
    • Converts RecursionError from deeply nested JSON into GlbError("invalid JSON chunk: nesting is too deep") instead of leaking a raw traceback.
    • Rejects boolean/non-integer/negative bufferViews[i].buffer values (previously False == 0 slipped past bv.get("buffer", 0) == 0).
  2. build_instanced_glb() transform validation and numerical stability:
    • Validates exact (3, 4, 3) tuple shapes and rejects non-numeric/boolean components with GlbError instead of crashing with raw IndexError/ValueError or emitting malformed node TRS arrays in mode="nodes".
    • Uses math.hypot(*q) for quaternion normalization so large finite quaternions (e.g. (1e200, 0, 0, 0)) do not overflow v * v to inf.
    • Checks positive scale determinant by sign parity and zero-element check rather than multiplying sc[0] * sc[1] * sc[2] (which falsely underflowed valid tiny positive scales like (1e-120, 1e-120, 1e-120) to 0.0).
    • Rejects non-finite instance_bounds() outputs with GlbError instead of emitting ±inf in stats["bounds"].
  3. pack_primitive(), _pack_indices(), and _parse_base_color_factor() validation:
    • Raises GlbError (instead of raw ValueError: min() iterable argument is empty or TypeError) on empty positions/indices or boolean/non-integer indices.
    • Rejects boolean and non-numeric base_color_factor entries with GlbError (consistent with _parse_alpha_mode).
  4. CLI input-overwrite guard & staging cleanup:
    • Uses shared report_paths.same_report_path(args.in_obj, args.out_glb) and rejects existing directory outputs early with GlbError.
    • Resets staging temp file permissions (0o600) before unlinking in _write_bytes_atomic error cleanup so read-only targets clean up reliably on Windows.

Verification (macOS Apple M5, arm64, Python 3.14; no game files)

  • python3 -m compileall -q make_game.py tools tests — OK
  • python3 -I scripts/check_no_game_data.py tools/glb_writer.py tests/test_glb_writer.py — OK
  • python3 -I tests/test_glb_writer.py — 74/74 OK (4 new test methods)
  • tests/test_glb_atomic.py (12/12, 1 nt-only skip), tests/test_glb_writer_buffers.py (2/2), tests/test_glb_inspect.py (36/36, 1 skip), tests/test_glb_skinning.py (26/26), tests/test_batch_glb.py (16/16), tests/test_anim_glb.py (36/36), tests/test_skinned_glb.py (30/30) — all OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • python3 -I tests/test_ci_registration.py — 16/16 OK
  • Mutation proof (running the new tests/test_glb_writer.py against unpatched upstream/main 1154976d4): Ran 74 tests — FAILED (failures=1, errors=2) — catches NaN JSON acceptance, empty-index raw ValueError, and large-quaternion/tiny-scale rejection.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5482609426 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1438). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T00:14:51Z. --- Item ID: `GLB-WRITER-VALIDATION-HARDENING` AI-assisted: yes ### Summary Fixes validation, numerical-stability, and CLI-safety bugs in `tools/glb_writer.py`: 1. **`read_glb_summary()` strict JSON & buffer-index validation**: - Rejects non-finite JSON constants (`NaN`, `Infinity`, `-Infinity`) with `GlbError` via `parse_constant`. - Converts `RecursionError` from deeply nested JSON into `GlbError("invalid JSON chunk: nesting is too deep")` instead of leaking a raw traceback. - Rejects boolean/non-integer/negative `bufferViews[i].buffer` values (previously `False == 0` slipped past `bv.get("buffer", 0) == 0`). 2. **`build_instanced_glb()` transform validation and numerical stability**: - Validates exact `(3, 4, 3)` tuple shapes and rejects non-numeric/boolean components with `GlbError` instead of crashing with raw `IndexError`/`ValueError` or emitting malformed node TRS arrays in `mode="nodes"`. - Uses `math.hypot(*q)` for quaternion normalization so large finite quaternions (e.g. `(1e200, 0, 0, 0)`) do not overflow `v * v` to `inf`. - Checks positive scale determinant by sign parity and zero-element check rather than multiplying `sc[0] * sc[1] * sc[2]` (which falsely underflowed valid tiny positive scales like `(1e-120, 1e-120, 1e-120)` to `0.0`). - Rejects non-finite `instance_bounds()` outputs with `GlbError` instead of emitting `±inf` in `stats["bounds"]`. 3. **`pack_primitive()`, `_pack_indices()`, and `_parse_base_color_factor()` validation**: - Raises `GlbError` (instead of raw `ValueError: min() iterable argument is empty` or `TypeError`) on empty `positions`/`indices` or boolean/non-integer indices. - Rejects boolean and non-numeric `base_color_factor` entries with `GlbError` (consistent with `_parse_alpha_mode`). 4. **CLI input-overwrite guard & staging cleanup**: - Uses shared `report_paths.same_report_path(args.in_obj, args.out_glb)` and rejects existing directory outputs early with `GlbError`. - Resets staging temp file permissions (`0o600`) before unlinking in `_write_bytes_atomic` error cleanup so read-only targets clean up reliably on Windows. ### Verification (macOS Apple M5, arm64, Python 3.14; no game files) - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/glb_writer.py tests/test_glb_writer.py` — OK - `python3 -I tests/test_glb_writer.py` — 74/74 OK (4 new test methods) - `tests/test_glb_atomic.py` (12/12, 1 nt-only skip), `tests/test_glb_writer_buffers.py` (2/2), `tests/test_glb_inspect.py` (36/36, 1 skip), `tests/test_glb_skinning.py` (26/26), `tests/test_batch_glb.py` (16/16), `tests/test_anim_glb.py` (36/36), `tests/test_skinned_glb.py` (30/30) — all OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - `python3 -I tests/test_ci_registration.py` — 16/16 OK - **Mutation proof** (running the new `tests/test_glb_writer.py` against unpatched `upstream/main` `1154976d4`): `Ran 74 tests — FAILED (failures=1, errors=2)` — catches `NaN` JSON acceptance, empty-index raw `ValueError`, and large-quaternion/tiny-scale rejection.
copilot-pull-request-reviewer[bot] commented 2026-11-25 00:15:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T00:15: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:6049555630 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1438#pullrequestreview-6049555630). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T00:15: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.
MoonEyes commented 2026-11-28 10:13:57 +00:00 (Migrated from github.com)

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


Red/green: the PR's tests fail on main 4938673 and pass on head 39d3dc489b (Linux, Python 3.13.18).
Targeted batch of 11 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:7237760699 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1438#issuecomment-7237760699). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T10:13:57Z. --- Red/green: the PR's tests fail on main 4938673 and pass on head 39d3dc489b09 (Linux, Python 3.13.18). Targeted batch of 11 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!1438
No description provided.