[PR #1535] restofworld_index: validate scan bounds, fix carve index .bk2/BOM, harden I/O (RESTOFWORLD-INDEX-VALIDATION-AND-IO) #1564

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

📋 Pull Request Information

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

Base: main ← Head: fix/restofworld-index-hardening


📝 Commits (1)

  • 1ca8b83 restofworld_index: validate scan bounds, fix carve index .bk2/BOM, harden I/O

📊 Changes

2 files changed (+169 additions, -29 deletions)

View changed files

📝 tests/test_restofworld_index.py (+80 -0)
📝 tools/restofworld_index.py (+89 -29)

📄 Description

What changed

  • Python API bound validation (tools/restofworld_index.py:scan_rpf, scan_dir): reject bool, <= 0 limit/max_inflate, and non-integer start with ValueError. Previously scan_rpf(..., limit=0) silently scanned the entire archive, negative limit produced a negative span, and max_inflate <= 0 silently skipped every stream.
  • Bink 2 header window check (tools/restofworld_index.py:scan_rpf): require s + 0x1C <= end before reading header dimensions (matching tools/rpf9_carve.py) and bound the slice passed to classify() by s + blen.
  • Carve index BOM, .bk2 mapping, and summary validation (tools/restofworld_index.py:_carve_index):
    • Open index.json with encoding="utf-8-sig" so UTF-8 BOM-prefixed indexes are not silently ignored.
    • Map type == "bink2" rows to <off>.bk2 (matching rpf9_carve.py) instead of hardcoding .bin.
    • Validate off, stored, span, zero_padding, and unread_bytes are non-negative non-bool integers so malformed summary values fall back cleanly instead of crashing scan_dir().
  • Source/payload overwrite guard and atomic JSON hardening (tools/restofworld_index.py:write_output, main):
    • Use report_paths.same_report_path (with os.path.samestat for hardlinks) so case-folding, NFD, and dangling symlink aliases to the source archive or carved directory payloads/index.json are caught before scanning.
    • Serialize JSON with allow_nan=False and a trailing LF, publish via replace_retry.replace, allow optional argv=None in main(argv=None), and catch ValueError alongside OSError when writing.
  • Tests (tests/test_restofworld_index.py): added 3 synthetic regression tests covering API validation, BOM/.bk2/malformed-summary carve indexes, strict JSON/trailing LF, and dangling-symlink overwrite guards.

Claimed in the coordination issue as:

RESTOFWORLD-INDEX-VALIDATION-AND-IO (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7152205891)

How it was tested

  • python3 -I -W error tests/test_restofworld_index.py: 37/37 passed (0 warnings).
  • python3 -I tests/test_bad_input_errors.py: 11/11 passed.
  • python3 -I tests/test_csv_line_endings.py: 2/2 passed.
  • python3 -I tests/test_limit_window.py: 1/1 passed.
  • python3 -I tests/test_printable_bytes.py: 4/4 passed.
  • python3 -I tests/test_zero_span.py: 3/3 passed.
  • python3 -I tests/test_text_write_line_endings.py: 3/3 passed.
  • python3 -I scripts/check_no_game_data.py tools/restofworld_index.py tests/test_restofworld_index.py: passed.
  • python3 -m compileall -q tools/restofworld_index.py tests/test_restofworld_index.py: passed.
  • Mutation check against unpatched main:tools/restofworld_index.py: all 3 new tests fail (FAILED (failures=3)), all 34 baseline tests pass.

Metadata

  • AI-assisted: yes
  • GPUs tested: none (CPU-only synthetic archive / directory / CLI unit tests on macOS arm64; no game files used)

🔄 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/1535 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/restofworld-index-hardening` --- ### 📝 Commits (1) - [`1ca8b83`](https://github.com/ubiquitarianism/leonida/commit/1ca8b83b4a553f21708b4e61ef06a1d1a1f924e9) restofworld_index: validate scan bounds, fix carve index .bk2/BOM, harden I/O ### 📊 Changes **2 files changed** (+169 additions, -29 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_restofworld_index.py` (+80 -0) 📝 `tools/restofworld_index.py` (+89 -29) </details> ### 📄 Description ## What changed - **Python API bound validation (`tools/restofworld_index.py:scan_rpf`, `scan_dir`)**: reject `bool`, `<= 0` `limit`/`max_inflate`, and non-integer `start` with `ValueError`. Previously `scan_rpf(..., limit=0)` silently scanned the entire archive, negative `limit` produced a negative `span`, and `max_inflate <= 0` silently skipped every stream. - **Bink 2 header window check (`tools/restofworld_index.py:scan_rpf`)**: require `s + 0x1C <= end` before reading header dimensions (matching `tools/rpf9_carve.py`) and bound the slice passed to `classify()` by `s + blen`. - **Carve index BOM, `.bk2` mapping, and summary validation (`tools/restofworld_index.py:_carve_index`)**: - Open `index.json` with `encoding="utf-8-sig"` so UTF-8 BOM-prefixed indexes are not silently ignored. - Map `type == "bink2"` rows to `<off>.bk2` (matching `rpf9_carve.py`) instead of hardcoding `.bin`. - Validate `off`, `stored`, `span`, `zero_padding`, and `unread_bytes` are non-negative non-bool integers so malformed summary values fall back cleanly instead of crashing `scan_dir()`. - **Source/payload overwrite guard and atomic JSON hardening (`tools/restofworld_index.py:write_output`, `main`)**: - Use `report_paths.same_report_path` (with `os.path.samestat` for hardlinks) so case-folding, NFD, and dangling symlink aliases to the source archive or carved directory payloads/`index.json` are caught before scanning. - Serialize JSON with `allow_nan=False` and a trailing LF, publish via `replace_retry.replace`, allow optional `argv=None` in `main(argv=None)`, and catch `ValueError` alongside `OSError` when writing. - **Tests (`tests/test_restofworld_index.py`)**: added 3 synthetic regression tests covering API validation, BOM/`.bk2`/malformed-summary carve indexes, strict JSON/trailing LF, and dangling-symlink overwrite guards. ## Claimed in the coordination issue as: `RESTOFWORLD-INDEX-VALIDATION-AND-IO` (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7152205891) ## How it was tested - `python3 -I -W error tests/test_restofworld_index.py`: 37/37 passed (0 warnings). - `python3 -I tests/test_bad_input_errors.py`: 11/11 passed. - `python3 -I tests/test_csv_line_endings.py`: 2/2 passed. - `python3 -I tests/test_limit_window.py`: 1/1 passed. - `python3 -I tests/test_printable_bytes.py`: 4/4 passed. - `python3 -I tests/test_zero_span.py`: 3/3 passed. - `python3 -I tests/test_text_write_line_endings.py`: 3/3 passed. - `python3 -I scripts/check_no_game_data.py tools/restofworld_index.py tests/test_restofworld_index.py`: passed. - `python3 -m compileall -q tools/restofworld_index.py tests/test_restofworld_index.py`: passed. - Mutation check against unpatched `main:tools/restofworld_index.py`: all 3 new tests fail (`FAILED (failures=3)`), all 34 baseline tests pass. ## Metadata - AI-assisted: yes - GPUs tested: none (CPU-only synthetic archive / directory / CLI unit tests on macOS arm64; no game files used) --- <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#1564
No description provided.