Skip to content

world_interior_names: protect directory inputs from external report aliases - #5274

Open
Zunraz wants to merge 2 commits into
ItsNotRexed:mainfrom
Zunraz:fix/interior-report-directory-alias
Open

Zunraz wants to merge 2 commits into
ItsNotRexed:mainfrom
Zunraz:fix/interior-report-directory-alias

Conversation

@Zunraz

@Zunraz Zunraz commented Nov 23, 2026 •

Copy link
Copy Markdown

An --out name outside a scanned directory can still be a hard link to one of its members, or a dangling symlink whose target is a new file inside it. Without these checks the CLI overwrites the linked resource or creates a report among its inputs and exits 0. Refuse these cases with exit 2 before scanning or writing.

For existing outputs, compare the report's file identity against the directory inventory and refuse an incomplete/unreadable inventory. For containment, check both the resolved leaf and the resolved parent directory, so inward targets and inward names with outward dangling targets are protected. Legitimate independent/outside reports remain allowed.

Claim: directory report alias guard. Credit to memorypressure for identifying the directory-member hard-link gap, and King1565 for the symlink coverage/dangling-target finding.

Rebased onto main e6190e7b5 at 100de6026. The current diff contains only the directory guards and their standalone regression file; main already contains the original #4690 CLI guards. There is no remaining prerequisite. The update preserves main's newer scaled-room resolution and parser tests, and follows its return-2 error convention.

Validation on synthetic inputs:

  • Windows, CPython 3.14.7: 73 tests run, 67 passed / 6 skipped across test_world_interior_report_paths, test_world_interior_names, test_report_paths and test_ci_registration. Five skips require symlink creation permission; the remaining report-path skip is platform-specific.
  • Ubuntu/WSL, CPython 3.14.4: the same four suites 73 passed, no skips, including real hard links, inward/outward and dangling symlinks.
  • Negative control on unchanged e6190e7b5: the new suite has four failing Windows assertions/subcases, including both actual hard-link CLI runs exiting 0 and the scan-before-refusal checks. Synthetic fixture bytes only.
  • Touched Python files compile; whitespace and no-game-data checks pass.

AI-assisted (Codex). CPU only, no GPU/driver or game files used. The existing time-of-check/time-of-use window remains a separate nonblocking observation from review.

@LocranKing

Copy link
Copy Markdown
Contributor

Tested 6ab022365e1c0ae0e249ae82f31b4223b7ef37d4 (6ab022365) on Windows 11 Home Build 26200 x64, Python 3.12.10 (NVIDIA RTX 3060 Ti, driver 621.01): pass

  • git diff --check HEAD~1..HEAD: clean
  • python -I -m compileall -q make_game.py tools tests: exit 0
  • python -I make_game.py --help: exit 0
  • python -I tests/test_world_interior_report_paths.py -v: 5/5 OK (0.594s)
  • python -I tests/test_world_interior_names.py -v: 16/16 OK (0.346s)

AI-assisted. No game files.

@x2Tweek x2Tweek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second check on PR #5274 — world_interior_names: protect directory inputs from external report aliases.

Verified on Windows (no GPU, no game data):

  • python -m unittest tests.test_world_interior_names tests.test_world_interior_report_paths — 21 passed
  • python -m compileall -q — clean on the touched tools modules
  • git diff --check — clean

The change closes the outside-hard-link case left by #4690:

  • Before scanning, it refuses --out naming any input (archive or names JSON), --out naming a folder, and --out inside a scanned directory.
  • For directory input with an existing file output, it stat-compares the output against every directory member (one stat per member) and exits 2 on an alias — the hard-link case the parent-chain guard missed.
  • Invalid --start/--limit windows and unreadable inventories are refused before any work or write.
  • Fresh reports and independent existing reports still pass.

Tests cover flat and nested external hard links, identical-bytes reports allowed, failed-directory-check preserving prior reports, and new reports in new parents allowed.

AI-assisted (Codex). Synthetic inputs only; no GPU/driver or game files used. Tested on current main 3faac557, Windows/Python 3.14.

🤖 Generated with Claude Code

@King1565

Copy link
Copy Markdown

Tested 6ab0223 on Linux, Python 3.13 (CPU only, no GPU, no game files): pass

Ran

  • python3 -I tests/test_world_interior_report_paths.py: head 5/5 OK. With the new file copied onto main 3faac55, and separately onto the world_interior_names: reject the arguments the run cannot honour #4690 head df72324: 4 of 5 fail (both hard-link subtests exit 0 instead of 2, and "scan ran before the refusal" twice). Only the fresh-report test passes.
  • Negative control, head with just the new samestat block disabled: the same 4 fail.
  • tests/test_world_interior_names.py 16/16, test_ci_registration.py OK, test_text_write_line_endings.py OK, compileall -q on the 3 touched files, git diff --check: all clean.

Read
No defect found.

  • The guard is wider than the tests show. os.stat follows symlinks, so a member that is a symlink to an outside file is also protected when --out names that target. Synthetic repro: exit 2 on head; exit 0 on df72324, which overwrote the target. A subTest for this (skipped where symlinks can't be created) would keep the coverage.
  • Gap left over from world_interior_names: reject the arguments the run cannot honour #4690: _inside resolves only the parent of --out, not --out itself. A dangling symlink out.json -> carved/new.json exits 0 and writes a new file into the scanned folder. Using path_key(a.out) in place of its parent would close it. This doesn't block the PR.

AI-assisted (Claude Code).

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 6ab0223 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_world_interior_report_paths.py -> Ran 5 tests, OK
  • Negative control (tools/world_interior_names.py swapped to upstream/main): FAILED (failures=4)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@Zunraz

Zunraz commented Nov 23, 2026

Copy link
Copy Markdown
Author

Addressed King1565's review in 26f35e40.

The real dangling-symlink CLI cases (flat/nested targets) reproduce exit 0 on 6ab02236, then exit 2 with the resolved-leaf containment check. Both targets stay absent. I retained the parent-location check too: replacing it with the leaf alone would allow an output name inside the scanned folder when its dangling link points outside.

Five new synthetic methods cover external symlinks to existing inputs, scanned symlinks to outside resources, inward/outward dangling links, and allowed links between outside paths. Linux: all 40 focused/registration/LF tests pass. Windows: 35 pass / 5 symlink-method skips (WinError 1314); original hard-link cases pass. Compile, no-game-data and whitespace checks are clean. The PR description now reflects the final scope and validation. Thanks for the additional case. AI-assisted (Codex).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 6ab0223; the new change is 0886c58 (the rest is the #4690 prerequisite). For directory input with an existing --out, it now stats the output and every WI._directory_files member and refuses with exit 2 before scanning if any is the same file (hard link or alias); a failed inventory also refuses before scanning/writing.

Tested on Windows 11, Python 3.13.15:

  • python -I -m pytest tests/test_world_interior_report_paths.py tests/test_world_interior_names.py tests/test_ci_registration.py tests/test_text_write_line_endings.py -q -> 35 passed, 9 subtests.
  • python -I tests/test_world_interior_report_paths.py -> Ran 5 tests, OK.
  • git diff --check -> clean.

The new suite covers flat and nested external hard links (exit 2, stderr names the source, input bytes unchanged), refusal before scan, and an unreadable inventory; fresh reports and an independent existing report with identical bytes are allowed.

Verdict: correct; no blockers. Stacked on #4690 (its commits are in this diff) and complements #4748.

@King1565

Copy link
Copy Markdown

Re-tested 26f35e4 on Linux, Python 3.13 (CPU only, no game files): pass

  • tests/test_world_interior_report_paths.py 10 OK, tests/test_world_interior_names.py OK, git diff --check clean.
  • My earlier repro, --out out.json as a dangling symlink to carved/new.json: now exit 2 ("--out … is inside the scanned folder") and nothing is written; it was exit 0 on 6ab0223. The member-symlink case still exits 2.
  • Agreed on keeping the parent check as well as the resolved leaf; the leaf alone would let an inward name with an outward dangling target through. Thanks for the fix.

AI-assisted (Claude Code).


Generated by Claude Code

@Koro4e

Koro4e commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Reviewed 26f35e4 (Windows 11, CPU-only): pass - approve-able.

  • Ran the PR's own suites and the neighbours: test_world_interior_names.py 16/16, test_world_interior_report_paths.py 10/10, test_report_paths 15 OK, test_pool_workers 10 OK, test_instance_lists 16 OK, test_world_archetypes 19 OK, test_world_names 26 OK.
  • Negative control: checking out main's tools/world_interior_names.py under the PR's tests fails 8+10 - the new tests really catch the alias vectors (report written over the input, folder, hard link, dangling report symlink, window, JSON errors, jobs cap).
  • Tried extra vectors beyond the tests, all correctly rejected before the scan with exit 2 and intact inputs: a dangling symlink chain through an externally symlinked parent back into the input folder, a subst drive pointing at the input folder (ntpath realpath resolves it), a case-variant member name, and --out hard-linked to the names JSON. Legitimate runs (independent report, new parent dir, outside dangling link) still exit 0.
  • Guard order confirmed: refusals happen before any scan (mock-scan test asserts it), fail-closed on unreadable folders, and the code reuses report_paths.path_key/same_report_path and pool_workers.process_workers instead of duplicating them, following the world_archetypes.py pre-scan pattern.

One follow-up observation, not a blocker: there is a TOCTOU window between the checks and open(args.out, "w") - a symlink swapped in at that moment would still redirect the report. A temp-file write with os.replace after re-verifying would close it; fine for a local single-user tool today.

AI-assisted.

@Zunraz
Zunraz force-pushed the fix/interior-report-directory-alias branch from 26f35e4 to 100de60 Compare November 23, 2026 15:53
@Zunraz

Zunraz commented Nov 23, 2026

Copy link
Copy Markdown
Author

Rebased onto current main e6190e7b5; new head 100de6026. Resolved the parser/test conflicts by retaining main's room/scale changes and applying only the directory report-alias guards. The prerequisite CLI guards are already in main, so the PR now has two files and no prerequisite.

The four focused suites run 73 tests: native Windows 67 passed / 6 platform/permission skips; Ubuntu/WSL 73 passed with real symlinks. The guard errors now return 2 consistently with main, and the before-scan tests check that convention. The unchanged-main negative control still fails the real hard-link CLI cases. Compile, whitespace and no-game-data checks pass. AI-assisted (Codex); synthetic CPU tests only.

@illusionart453-max

Copy link
Copy Markdown

Windows / Python 3.15 verification at 100de60 in an isolated worktree:

The focused alias cases passed. Windows symlink coverage is limited by the five skipped cases in the focused suite. Synthetic CPU inputs, no GPU/game files. AI-assisted (Codex).

@skorex

skorex commented Nov 23, 2026

Copy link
Copy Markdown

Tested 100de60 on Linux (WSL, ext4), Python 3.13: pass.

Ran: python3 -I tests/test_world_interior_report_paths.py (10 tests, pass, no skips, so the symlink cases that skip on Windows without symlink permission ran here with real symlinks and hard links), tests/test_world_interior_names.py (27), tests/test_report_paths.py (22), tests/test_ci_registration.py (14): all pass. On the merge with current origin/main dde82f2 (git merge-tree is clean), the two world_interior suites pass too. With the new test file on origin/main, 8 of the 10 tests fail (both hard-link subcases, both dangling-symlink parents, the inside-dangling case, refusal before scanning, the member-symlink target and prior-report preservation); the two "allowed" tests pass on both, as they should.
Not run: Windows, a real carve.

Code read: _inside now checks both the resolved parent and the resolved leaf against path_key(folder) + os.sep. That covers an inward name with an outward dangling target and the reverse case, and drops the commonpath ValueError on different drives. The identity check walks the scanned folder with WI._directory_files, the same walk as the scan, and os.stat follows member symlinks, so a scanned symlink whose target is the existing report is refused too (test_report_target_of_a_scanned_symlink_is_preserved). A partial walk exits 2 before the report is opened.

AI-assisted review (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants