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

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

@⁠IlyassRS3 on GitHub · 2026-11-25 00:14 UTC · original
IlyassRS3:fix/glb-writer-validation-hardening → main · +148 −17 · 2 files · 1 commits · open

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.
**@⁠IlyassRS3** on GitHub · 2026-11-25 00:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1438) `IlyassRS3:fix/glb-writer-validation-hardening` → `main` · +148 −17 · 2 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:51:27 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 00:15 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 00:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1438#pullrequestreview-6049555630) · **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.
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/1438/head:github-bot/gh-1438'
git switch 'github-bot/gh-1438'

Merge

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