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

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

📋 Pull Request Information

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

Base: main ← Head: fix/glb-writer-validation-hardening


📝 Commits (1)

  • 39d3dc4 glb_writer: harden JSON/transform/primitive validation and shared path guard (GLB-WRITER-VALIDATION-HARDENING)

📊 Changes

2 files changed (+148 additions, -17 deletions)

View changed files

📝 tests/test_glb_writer.py (+88 -0)
📝 tools/glb_writer.py (+60 -17)

📄 Description

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.

🔄 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/1438 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/glb-writer-validation-hardening` --- ### 📝 Commits (1) - [`39d3dc4`](https://github.com/ubiquitarianism/leonida/commit/39d3dc489b096555660eb57b43bfacbbe861fcad) glb_writer: harden JSON/transform/primitive validation and shared path guard (GLB-WRITER-VALIDATION-HARDENING) ### 📊 Changes **2 files changed** (+148 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_glb_writer.py` (+88 -0) 📝 `tools/glb_writer.py` (+60 -17) </details> ### 📄 Description 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. --- <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#1468
No description provided.