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

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

📋 Pull Request Information

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

Base: main ← Head: fix/ped-catalogues-hardening


📝 Commits (1)

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

📊 Changes

2 files changed (+310 additions, -18 deletions)

View changed files

📝 tests/test_ped_catalogues.py (+232 -0)
📝 tools/ped_catalogues.py (+78 -18)

📄 Description

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

🔄 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/1522 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/ped-catalogues-hardening` --- ### 📝 Commits (1) - [`8ab99b8`](https://github.com/ubiquitarianism/leonida/commit/8ab99b80f913403f285053863b3008f764667ece) ped_catalogues: fix filter/BOM/traversal/depth-cap bugs and atomic JSON I/O (PED-CATALOGUES-VALIDATION-AND-IO-HARDENING) ### 📊 Changes **2 files changed** (+310 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_ped_catalogues.py` (+232 -0) 📝 `tools/ped_catalogues.py` (+78 -18) </details> ### 📄 Description ## 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 --- <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#1552
No description provided.