[PR #1512] skinned_glb: validate GLB chunks, accessor bounds, skeleton cycles, skin streams, and indices (SKINNED-GLB-VALIDATION-HARDENING) #1542

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/1512
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/skinned-glb-hardening


📝 Commits (1)

  • ced0d9e skinned_glb: validate GLB chunks, accessor bounds, skeleton cycles, skin streams, and indices

📊 Changes

2 files changed (+447 additions, -46 deletions)

View changed files

📝 tests/test_skinned_glb.py (+277 -0)
📝 tools/skinned_glb.py (+170 -46)

📄 Description

Claimed in the coordination issue as: SKINNED-GLB-VALIDATION-HARDENING (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7151315288)

What and why

Hardens tools/skinned_glb.py against malformed GLB containers, out-of-bounds accessors, cyclic skeletons, short skin headers, bool-as-int bugs, and invalid glTF primitive indices/attributes:

  1. GLB container validation (read_glb):
    • Validates GLB v2 version, declared container total_length against file size, CHUNK_JSON / CHUNK_BIN chunk types, and JSON/BIN chunk bounds (plus RecursionError on malformed JSON), raising SkinError instead of accepting truncated or non-BIN chunks.
  2. Accessor & bufferView bounds (accessor_bytes):
    • Rejects negative and bool indices, counts, strides, and byte offsets, validates bufferView.byteLength against bin_data, and ensures the accessor span does not extend past its bufferView.
  3. Skeleton hierarchy & matrix guards (_finite_matrix, _bone_name_map, JointSet, PaletteResolver, own_parent_name):
    • _finite_matrix checks isinstance(m, (list, tuple)) and rejects bool elements instead of raising raw TypeError when bind_matrix is None.
    • JointSet.__init__ rejects non-dict bone entries, ignores bool parent values, and rejects self-parenting and multi-bone parent cycles with SkinError instead of producing cyclic glTF node graphs; JointSet.add rejects bool parent.
    • PaletteResolver and own_parent_name guard against non-dict/non-list drawable/skeleton/strings fields and bool parent indices.
  4. Skin stream & weight validation (read_skin_streams, normalise_weights):
    • Rejects short graphics buffers (base + 92 > len(blob)) with SkinError before reading the 23-word section header (previously raised uncaught struct.error on CLI).
    • Rejects negative/bool geometry/model/vertex_buffer indices, bool bone_palette/vertex_count/gfx_off/mesh_blob.sections entries, non-string buffer_files entries, and bool arguments in normalise_weights (and removes a duplicate zeroing line in the NumPy path).
  5. glTF primitive consistency & IBM float32 overflow (build_skinned_glb, _validate_indices):
    • Validates that every copied vertex attribute (NORMAL, TANGENT, TEXCOORD_*, COLOR_0) matches streams["count"], not just POSITION.
    • Validates source index accessors are SCALAR with integer componentType (UNSIGNED_BYTE/UNSIGNED_SHORT/UNSIGNED_INT), positive multiple-of-3 count, and all indices < vertex_count.
    • Catches float32 OverflowError when packing inverseBindMatrices and converts it to SkinError.
  6. CLI upfront directory check (main):
    • Rejects an existing output directory with exit code 2 before building the GLB, and removes the duplicate sys.path.insert.

Tested

  • python3 -I tests/test_skinned_glb.py -v (stdlib path): 38/38 OK (30 existing + 8 new tests in ValidationHardeningTests).
  • python3 tests/test_skinned_glb.py (with NumPy installed): 38/38 OK.
  • Mutation check against unpatched main:tools/skinned_glb.py: all 8 new tests in ValidationHardeningTests fail (FAILED (failures=17, errors=4)).
  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
  • python3 -I scripts/check_no_game_data.py tools/skinned_glb.py tests/test_skinned_glb.py: clean.
  • python3 -m compileall -q tools/skinned_glb.py tests/test_skinned_glb.py: clean.

GPUs tested: n/a (Python 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/1512 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/skinned-glb-hardening` --- ### 📝 Commits (1) - [`ced0d9e`](https://github.com/ubiquitarianism/leonida/commit/ced0d9e4239da14d60261bc52eb9b7f3f4db044c) skinned_glb: validate GLB chunks, accessor bounds, skeleton cycles, skin streams, and indices ### 📊 Changes **2 files changed** (+447 additions, -46 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_skinned_glb.py` (+277 -0) 📝 `tools/skinned_glb.py` (+170 -46) </details> ### 📄 Description Claimed in the coordination issue as: SKINNED-GLB-VALIDATION-HARDENING (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7151315288) ### What and why Hardens `tools/skinned_glb.py` against malformed GLB containers, out-of-bounds accessors, cyclic skeletons, short skin headers, `bool`-as-`int` bugs, and invalid glTF primitive indices/attributes: 1. **GLB container validation (`read_glb`)**: - Validates GLB v2 version, declared container `total_length` against file size, `CHUNK_JSON` / `CHUNK_BIN` chunk types, and JSON/BIN chunk bounds (plus `RecursionError` on malformed JSON), raising `SkinError` instead of accepting truncated or non-BIN chunks. 2. **Accessor & bufferView bounds (`accessor_bytes`)**: - Rejects negative and `bool` indices, counts, strides, and byte offsets, validates `bufferView.byteLength` against `bin_data`, and ensures the accessor span does not extend past its `bufferView`. 3. **Skeleton hierarchy & matrix guards (`_finite_matrix`, `_bone_name_map`, `JointSet`, `PaletteResolver`, `own_parent_name`)**: - `_finite_matrix` checks `isinstance(m, (list, tuple))` and rejects `bool` elements instead of raising raw `TypeError` when `bind_matrix` is `None`. - `JointSet.__init__` rejects non-dict bone entries, ignores `bool` `parent` values, and rejects self-parenting and multi-bone parent cycles with `SkinError` instead of producing cyclic glTF node graphs; `JointSet.add` rejects `bool` `parent`. - `PaletteResolver` and `own_parent_name` guard against non-dict/non-list `drawable`/`skeleton`/`strings` fields and `bool` parent indices. 4. **Skin stream & weight validation (`read_skin_streams`, `normalise_weights`)**: - Rejects short graphics buffers (`base + 92 > len(blob)`) with `SkinError` before reading the 23-word section header (previously raised uncaught `struct.error` on CLI). - Rejects negative/`bool` `geometry`/`model`/`vertex_buffer` indices, `bool` `bone_palette`/`vertex_count`/`gfx_off`/`mesh_blob.sections` entries, non-string `buffer_files` entries, and `bool` arguments in `normalise_weights` (and removes a duplicate zeroing line in the NumPy path). 5. **glTF primitive consistency & IBM float32 overflow (`build_skinned_glb`, `_validate_indices`)**: - Validates that every copied vertex attribute (`NORMAL`, `TANGENT`, `TEXCOORD_*`, `COLOR_0`) matches `streams["count"]`, not just `POSITION`. - Validates source index accessors are `SCALAR` with integer `componentType` (`UNSIGNED_BYTE`/`UNSIGNED_SHORT`/`UNSIGNED_INT`), positive multiple-of-3 count, and all indices `< vertex_count`. - Catches float32 `OverflowError` when packing `inverseBindMatrices` and converts it to `SkinError`. 6. **CLI upfront directory check (`main`)**: - Rejects an existing output directory with exit code 2 before building the GLB, and removes the duplicate `sys.path.insert`. ### Tested - `python3 -I tests/test_skinned_glb.py -v` (stdlib path): **38/38 OK** (30 existing + 8 new tests in `ValidationHardeningTests`). - `python3 tests/test_skinned_glb.py` (with NumPy installed): **38/38 OK**. - Mutation check against unpatched `main:tools/skinned_glb.py`: all 8 new tests in `ValidationHardeningTests` fail (`FAILED (failures=17, errors=4)`). - `python3 -I tests/test_text_write_line_endings.py`: **3/3 OK**. - `python3 -I scripts/check_no_game_data.py tools/skinned_glb.py tests/test_skinned_glb.py`: clean. - `python3 -m compileall -q tools/skinned_glb.py tests/test_skinned_glb.py`: clean. GPUs tested: n/a (Python 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#1542
No description provided.