ped_catalogues: fix filter/BOM/traversal/depth-cap bugs and atomic JSON I/O (PED-CATALOGUES-VALIDATION-AND-IO-HARDENING) #1522

Merged
leonida/mandy
quietfold merged 1 commit from fix/ped-catalogues-hardening into main 2026-11-28 14:44:48 +00:00

Pull request successfully merged and closed

quietfold commented 2026-11-25 03:19:24 +00:00

Summary

Fixes several correctness bugs and hardens file/CLI I/O in tools/ped_catalogues.py:

  • Keep name filtering active on empty --names-file / blank --name (_cmd_resolve): passing a comment-only/blank --names-file or blank --name previously passed drawable_names=None to resolve_preset_bindings, disabling filtering and emitting every binding instead of 0 matches.
  • UTF-8 BOM & decode safety (_cmd_resolve, iter_catalogues_in_dir): opens --names-file and carved index.json with encoding="utf-8-sig" (so a leading UTF-8 BOM does not corrupt the first hashed drawable name or break index.json parsing) and catches UnicodeDecodeError cleanly.
  • Path traversal guard in carved index.json (iter_catalogues_in_dir): verifies both primary and continuation row paths stay inside the carved folder via _is_under_dir.
  • Off-by-one MAX_PARENT_DEPTH warning & max_depth validation (parse_catalogue_blocks, resolve_ancestor_tags, resolve_effective_tags, resolve_preset_bindings): checks whether parent.parent_id is non-zero before emitting the depth-cap warning so clean exact-depth chains do not warn falsely, and rejects negative or bool max_depth.
  • Scanner bounds & inspect --max-inflate (iter_catalogues_in_file, iter_catalogues_in_dir, iter_catalogues_in_rpf, _cmd_inspect): validates integer bounds on start, limit, and max_inflate (rejecting non-positive or bool values), adds --max-inflate to inspect, enforces the file-size cap before reading, and reports zlib inflate/cap failures explicitly.
  • Atomic JSON reports & CLI write-error handling (_write_json, _cmd_census, _cmd_inspect, _cmd_resolve): stages JSON output via sibling temp file with newline="\n", allow_nan=False, file_mode.published_mode, and replace_retry.replace, and catches write (OSError, ValueError) with exit code 2 instead of an uncaught traceback.

Coordination

Testing

  • python3 -I tests/test_ped_catalogues.py -v — 16/16 OK (including 6 new regression tests in PedCataloguesHardeningTests)
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • python3 -I scripts/check_no_game_data.py tools/ped_catalogues.py tests/test_ped_catalogues.py — clean
  • python3 -m compileall -q tools/ped_catalogues.py tests/test_ped_catalogues.py — clean
  • Mutation check against unpatched main:tools/ped_catalogues.py: 6/6 tests in PedCataloguesHardeningTests fail (FAILED (failures=12)).

Checklist

  • AI-assisted: yes
  • GPUs tested: none required (pure standard-library CPU container/CLI tests, zero game files)
  • No game data, keys, or online services included
## Summary Fixes several correctness bugs and hardens file/CLI I/O in `tools/ped_catalogues.py`: - **Keep name filtering active on empty `--names-file` / blank `--name`** (`_cmd_resolve`): passing a comment-only/blank `--names-file` or blank `--name` previously passed `drawable_names=None` to `resolve_preset_bindings`, disabling filtering and emitting every binding instead of 0 matches. - **UTF-8 BOM & decode safety** (`_cmd_resolve`, `iter_catalogues_in_dir`): opens `--names-file` and carved `index.json` with `encoding="utf-8-sig"` (so a leading UTF-8 BOM does not corrupt the first hashed drawable name or break `index.json` parsing) and catches `UnicodeDecodeError` cleanly. - **Path traversal guard in carved `index.json`** (`iter_catalogues_in_dir`): verifies both primary and continuation row paths stay inside the carved folder via `_is_under_dir`. - **Off-by-one `MAX_PARENT_DEPTH` warning & `max_depth` validation** (`parse_catalogue_blocks`, `resolve_ancestor_tags`, `resolve_effective_tags`, `resolve_preset_bindings`): checks whether `parent.parent_id` is non-zero before emitting the depth-cap warning so clean exact-depth chains do not warn falsely, and rejects negative or `bool` `max_depth`. - **Scanner bounds & `inspect --max-inflate`** (`iter_catalogues_in_file`, `iter_catalogues_in_dir`, `iter_catalogues_in_rpf`, `_cmd_inspect`): validates integer bounds on `start`, `limit`, and `max_inflate` (rejecting non-positive or `bool` values), adds `--max-inflate` to `inspect`, enforces the file-size cap before reading, and reports zlib inflate/cap failures explicitly. - **Atomic JSON reports & CLI write-error handling** (`_write_json`, `_cmd_census`, `_cmd_inspect`, `_cmd_resolve`): stages JSON output via sibling temp file with `newline="\n"`, `allow_nan=False`, `file_mode.published_mode`, and `replace_retry.replace`, and catches write `(OSError, ValueError)` with exit code 2 instead of an uncaught traceback. ## Coordination - Claimed in the coordination issue as: `PED-CATALOGUES-VALIDATION-AND-IO-HARDENING` (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7151759976) - Depends on: none ## Testing - `python3 -I tests/test_ped_catalogues.py -v` — 16/16 OK (including 6 new regression tests in `PedCataloguesHardeningTests`) - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - `python3 -I scripts/check_no_game_data.py tools/ped_catalogues.py tests/test_ped_catalogues.py` — clean - `python3 -m compileall -q tools/ped_catalogues.py tests/test_ped_catalogues.py` — clean - Mutation check against unpatched `main:tools/ped_catalogues.py`: 6/6 tests in `PedCataloguesHardeningTests` fail (`FAILED (failures=12)`). ## Checklist - AI-assisted: yes - GPUs tested: none required (pure standard-library CPU container/CLI tests, zero game files) - No game data, keys, or online services included
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 03:19:36 +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.
ytsodacan commented 2026-11-25 03:34:03 +00:00

[KEEP]

  • Touches only tools/ped_catalogues.py and tests/test_ped_catalogues.py (about 500 diff lines) and bundles several unrelated fixes: filter, BOM, traversal, depth cap and atomic JSON I/O.
  • no-game-data and change-scope checks were still pending when triaged, so there is no green CI signal yet. Merge once they pass.
  • No conflicts reported and no duplicate or overlapping open PR found.
  • A maintainer pass over the traversal and depth-cap behaviour changes is advisable given the size.
**[KEEP]** - Touches only `tools/ped_catalogues.py` and `tests/test_ped_catalogues.py` (about 500 diff lines) and bundles several unrelated fixes: filter, BOM, traversal, depth cap and atomic JSON I/O. - `no-game-data` and `change-scope` checks were still pending when triaged, so there is no green CI signal yet. Merge once they pass. - No conflicts reported and no duplicate or overlapping open PR found. - A maintainer pass over the traversal and depth-cap behaviour changes is advisable given the size.
whoalin1 commented 2026-11-25 07:53:21 +00:00

Independent verification (native Windows, Python 3.11.9 — stdlib-only, representative): diff vs merge-base reviewed; empty names-file filter stays active, utf-8-sig, _is_under_dir traversal guard, depth-cap warning fix, --max-inflate, atomic _write_json all present; tests 16/16+3/3+2/2, mutation reproduces FAILED(12). compileall + check_no_game_data clean. AI-assisted verification.

Independent verification (native Windows, Python 3.11.9 — stdlib-only, representative): diff vs merge-base reviewed; empty names-file filter stays active, utf-8-sig, _is_under_dir traversal guard, depth-cap warning fix, --max-inflate, atomic _write_json all present; tests 16/16+3/3+2/2, mutation reproduces FAILED(12). compileall + check_no_game_data clean. AI-assisted verification.
MoonEyes commented 2026-11-28 14:44:42 +00:00

Red/green: pytest tests/test_ped_catalogues.py fails on main (12 failed, 10 passed, 1 subtests passed) and passes on head 8ab99b80f913 merged with main (16 passed, 10 subtests passed).
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

Red/green: `pytest tests/test_ped_catalogues.py` fails on main (12 failed, 10 passed, 1 subtests passed) and passes on head 8ab99b80f913 merged with main (16 passed, 10 subtests passed). Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No description provided.