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

Merged
leonida/mandy
quietfold merged 1 commit from fix/glb-writer-validation-hardening into main 2026-11-28 10:14:05 +00:00

Pull request successfully merged and closed

quietfold commented 2026-11-25 00:14:51 +00:00

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.
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] reviewed 2026-11-25 00:15:05 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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

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)

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 description provided.