bary_fallback_gs: validate input locations/index schema/shader names, accept UTF-8 BOM, and publish atomically (BARY-FALLBACK-GS-VALIDATION-ATOMIC) #1440

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

@⁠IlyassRS3 on GitHub · 2026-11-25 00:18 UTC · original
IlyassRS3:fix/bary-fallback-gs-validation-atomic → main · +133 −5 · 2 files · 1 commits · open

Item ID: BARY-FALLBACK-GS-VALIDATION-ATOMIC
AI-assisted: yes

Summary

Fixes five input-validation and file-publishing bugs in tools/bary_fallback_gs.py:

  1. Validate locations in gs_spvasm():
    • int(n) previously accepted negative integers (-1, emitting illegal SPIR-V identifier %in-1 and Location -1), booleans (True/False), and floats (1.5), while string digits ("5", "1", "3") are expected by callers.
    • _parse_location() rejects booleans, floats, and negative values with ValueError while preserving non-negative int and digit-string inputs.
  2. Prevent path traversal via index.json shader names:
    • _helpers_for_index() joined spirv_dir with name + ".bary_gs..." without validating name, allowing ../escape, /abs, or sub/ps keys to write outside spirv_dir.
    • _validate_shader_name() rejects empty names, ., .., and any name containing / or \ (or where os.path.basename(name) != name) with ValueError.
  3. Convert malformed index.json fields into ValueError instead of uncaught tracebacks:
    • Malformed capabilities (e.g. null, [1]), truthy non-dict or missing-location bary_fallback_input, and non-list inputs (e.g. null, "01") previously escaped main()'s except (OSError, ValueError, RuntimeError) as TypeError/KeyError tracebacks or iterated characters of a string.
  4. Accept UTF-8 BOM index.json:
    • Reads index.json with encoding="utf-8-sig" (consistent with tools/hashdict.py and tools/bcpack_tables_bptc.py).
  5. Atomic publishing in write_for_index():
    • Stages .spvasm files via tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", newline="\n", ...) + file_mode.published_mode + os.replace with cleanup on failure so existing helper files are never truncated on write errors.

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/bary_fallback_gs.py tests/test_bary_fallback_gs.py — OK
  • python3 -I tests/test_bary_fallback_gs.py -v — 34 tests OK (1 skipped: SPIRV-Tools not installed on host; 5 new regression tests)
  • 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_bary_fallback_gs.py against unpatched upstream/main b4b7756e8): Ran 34 tests — FAILED (failures=10, errors=8, skipped=1) — catches negative/bool/float locations, path-traversal shader names, uncaught TypeError/KeyError on malformed index fields, UTF-8 BOM rejection, and non-atomic write_for_index.
**@⁠IlyassRS3** on GitHub · 2026-11-25 00:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1440) `IlyassRS3:fix/bary-fallback-gs-validation-atomic` → `main` · +133 −5 · 2 files · 1 commits · open Item ID: `BARY-FALLBACK-GS-VALIDATION-ATOMIC` AI-assisted: yes ### Summary Fixes five input-validation and file-publishing bugs in `tools/bary_fallback_gs.py`: 1. **Validate `locations` in `gs_spvasm()`**: - `int(n)` previously accepted negative integers (`-1`, emitting illegal SPIR-V identifier `%in-1` and `Location -1`), booleans (`True`/`False`), and floats (`1.5`), while string digits (`"5"`, `"1"`, `"3"`) are expected by callers. - `_parse_location()` rejects booleans, floats, and negative values with `ValueError` while preserving non-negative `int` and digit-string inputs. 2. **Prevent path traversal via `index.json` shader names**: - `_helpers_for_index()` joined `spirv_dir` with `name + ".bary_gs..."` without validating `name`, allowing `../escape`, `/abs`, or `sub/ps` keys to write outside `spirv_dir`. - `_validate_shader_name()` rejects empty names, `.`, `..`, and any name containing `/` or `\` (or where `os.path.basename(name) != name`) with `ValueError`. 3. **Convert malformed `index.json` fields into `ValueError` instead of uncaught tracebacks**: - Malformed `capabilities` (e.g. `null`, `[1]`), truthy non-dict or missing-`location` `bary_fallback_input`, and non-list `inputs` (e.g. `null`, `"01"`) previously escaped `main()`'s `except (OSError, ValueError, RuntimeError)` as `TypeError`/`KeyError` tracebacks or iterated characters of a string. 4. **Accept UTF-8 BOM `index.json`**: - Reads `index.json` with `encoding="utf-8-sig"` (consistent with `tools/hashdict.py` and `tools/bcpack_tables_bptc.py`). 5. **Atomic publishing in `write_for_index()`**: - Stages `.spvasm` files via `tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", newline="\n", ...)` + `file_mode.published_mode` + `os.replace` with cleanup on failure so existing helper files are never truncated on write errors. ### 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/bary_fallback_gs.py tests/test_bary_fallback_gs.py` — OK - `python3 -I tests/test_bary_fallback_gs.py -v` — 34 tests OK (1 skipped: SPIRV-Tools not installed on host; 5 new regression tests) - `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_bary_fallback_gs.py` against unpatched `upstream/main` `b4b7756e8`): `Ran 34 tests — FAILED (failures=10, errors=8, skipped=1)` — catches negative/bool/float locations, path-traversal shader names, uncaught `TypeError`/`KeyError` on malformed index fields, UTF-8 BOM rejection, and non-atomic `write_for_index`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:51:31 +00:00
Author
Owner

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

Merge

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