psocache_extract: stop at --max-streams, guard output path/overwrites, fix MAX_GAP & inside_tempdir, and write atomically (PSOCACHE-EXTRACT-GUARDS-AND-IO) #1575

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

@⁠IlyassRS3 on GitHub · 2026-11-25 04:52 UTC · original
IlyassRS3:fix/psocache-extract-guards-and-io → main · +184 −13 · 2 files · 1 commits · open

Item ID: PSOCACHE-EXTRACT-GUARDS-AND-IO (new item; coordination issue #14 is at GitHub's 2,500-comment cap with no open successor at PR time — confirmed 0 open PRs touch tools/psocache_extract.py)

Summary

  • Stop archive loop once --max-streams is reached: extract() previously checked only left <= 0, continuing to open, mmap, and report 0 streams for every later archive in a folder even after hitting --max-streams.
  • Validate out before scanning and guard source archives: reject empty/whitespace paths, existing directories, missing parent directories, and any out path aliasing source or an archive in source (using report_paths.same_report_path) so extract() can never overwrite a source archive inside the temp directory or scan gigabytes before failing on a bad output path.
  • Fix inside_tempdir() edge cases: reject empty/whitespace paths (previously inside_tempdir("") returned True when cwd was inside the temp dir), use os.path.commonpath with case-folding on Windows/APFS so TMPDIR=/ and case-variant paths work properly.
  • Fix off-by-one MAX_GAP padding check in assemble(): allow up to MAX_GAP (1 MiB) zero bytes between contiguous streams as documented, rejecting MAX_GAP + 1.
  • Preserve privacy on unreadable folders and write cache output atomically: catch OSError in archives() without leaking the package path, close the archive mmap before writing, and publish the output atomically via NamedTemporaryFile + file_mode.published_mode + replace_retry.replace. Also guard header is None and non-positive budget/max_streams in extract().

Testing

  • AI-assisted: yes
  • GPUs tested: none (CPU/Python stdlib tool; no GPU, no game files)
  • python3 -W error -I tests/test_psocache_extract.py -v: 12/12 passed
  • python3 -W error -I tests/test_text_write_line_endings.py -v: 3/3 passed
  • python3 -I scripts/check_no_game_data.py tools/psocache_extract.py tests/test_psocache_extract.py: passed
  • python3 -m compileall -q make_game.py tools tests: passed
  • Mutation check against unpatched main:tools/psocache_extract.py: all 5 new tests fail (FAILED (failures=3, errors=2)).
**@⁠IlyassRS3** on GitHub · 2026-11-25 04:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1575) `IlyassRS3:fix/psocache-extract-guards-and-io` → `main` · +184 −13 · 2 files · 1 commits · open Item ID: `PSOCACHE-EXTRACT-GUARDS-AND-IO` (new item; coordination issue #14 is at GitHub's 2,500-comment cap with no open successor at PR time — confirmed 0 open PRs touch `tools/psocache_extract.py`) ## Summary - **Stop archive loop once `--max-streams` is reached**: `extract()` previously checked only `left <= 0`, continuing to open, mmap, and report `0 streams` for every later archive in a folder even after hitting `--max-streams`. - **Validate `out` before scanning and guard source archives**: reject empty/whitespace paths, existing directories, missing parent directories, and any `out` path aliasing `source` or an archive in `source` (using `report_paths.same_report_path`) so `extract()` can never overwrite a source archive inside the temp directory or scan gigabytes before failing on a bad output path. - **Fix `inside_tempdir()` edge cases**: reject empty/whitespace paths (previously `inside_tempdir("")` returned `True` when `cwd` was inside the temp dir), use `os.path.commonpath` with case-folding on Windows/APFS so `TMPDIR=/` and case-variant paths work properly. - **Fix off-by-one `MAX_GAP` padding check in `assemble()`**: allow up to `MAX_GAP` (1 MiB) zero bytes between contiguous streams as documented, rejecting `MAX_GAP + 1`. - **Preserve privacy on unreadable folders and write cache output atomically**: catch `OSError` in `archives()` without leaking the package path, close the archive mmap before writing, and publish the output atomically via `NamedTemporaryFile` + `file_mode.published_mode` + `replace_retry.replace`. Also guard `header is None` and non-positive `budget`/`max_streams` in `extract()`. ## Testing - AI-assisted: yes - GPUs tested: none (CPU/Python stdlib tool; no GPU, no game files) - `python3 -W error -I tests/test_psocache_extract.py -v`: **12/12 passed** - `python3 -W error -I tests/test_text_write_line_endings.py -v`: **3/3 passed** - `python3 -I scripts/check_no_game_data.py tools/psocache_extract.py tests/test_psocache_extract.py`: passed - `python3 -m compileall -q make_game.py tools tests`: passed - Mutation check against unpatched `main:tools/psocache_extract.py`: all 5 new tests fail (`FAILED (failures=3, errors=2)`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:57:00 +00:00
Author
Owner

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

Merge

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