Skip to content

fix(mods): re-check destination folder before reading or copying mod configs (#626) - #796

Merged
Zaldaryon merged 2 commits into
devfrom
fix/issue-626-mod-config-apply-swapped-link
Nov 10, 2026
Merged

Zaldaryon merged 2 commits into
devfrom
fix/issue-626-mod-config-apply-swapped-link

Conversation

@Zaldaryon

Copy link
Copy Markdown
Collaborator

Summary

Revalidates the destination directory with assertManagedPath in applyOneConfigEntry before running lstat, readFile, or fse.copy.

Previously, assertManagedPath was called only during the initial pre-pass over all pending configs and immediately before the atomic write in the write loop. If a folder under ModConfig was replaced with a symbolic link after the pre-pass, lstat on the destination resolved through the directory link to an outside target file, which was read and copied into the recovery backup folder before the subsequent write was refused. Re-checking the destination directory immediately before lstat and read operations halts processing on the swapped path, refusing it as write-failed without reading outside files or copying them into backups.

Type

  • Bug fix
  • Feature
  • Performance
  • Refactor or cleanup
  • Tests only
  • Docs or build

Checklist

  • Targets dev, not main.
  • npm run typecheck passes.
  • npm run lint:ci passes.
  • npm run format:check passes.
  • npm run test:coverage passes, coverage at or above the floor in vitest.config.ts.
  • npm run build:unpack passes.

Testing

Executed the full test suite and build verification:

  • npm run typecheck: 0 errors.
  • npm run lint:ci: 0 errors.
  • npm run format:check: all files formatted.
  • npm run test:coverage: 304 test files passed (1919 tests passed, 2 skipped). Coverage floors exceeded across all metrics:
    • Statements: 95.07% (floor: 94.0%)
    • Branches: 91.29% (floor: 89.0%)
    • Functions: 95.89% (floor: 94.0%)
    • Lines: 97.02% (floor: 96.0%)
  • npm run build:unpack: unpacked Linux binary built successfully in dist/linux-unpacked.
  • Added regression tests in tests/ipc/modConfigs.test.ts covering:
    • A destination folder swapped for a symbolic link to an outside directory after the pre-pass during displacement of a prior file. Confirmed that no outside files are read or copied, the swapped target is recorded as write-failed, and the recovery backup retains only valid files.
    • A destination file swapped for a symbolic link to an outside file after the pre-pass. Confirmed that the link is refused as write-failed without reading or copying outside bytes.

Related issues

Fixes #626

@Zaldaryon
Zaldaryon requested a review from Pixnop October 27, 2026 17:23

@Pixnop Pixnop 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.

I reviewed 0e3d026b against dev adab6b96: ran your new tests against dev's code, then swapped a ModConfig folder for a link at three later points: between the lstat and the readFile, while the recovery folder is prepared, and during an export.

The check sits where #626 asks for it, right before the lstat (modConfigs.ts:729). A throw there lands in the per-entry catch, so the entry is refused as write-failed, the other entries still apply, and the lease is released as before. In the scenario the issue describes, a swap while an earlier entry is being displaced, the outside file is no longer read or copied. On dev's code the folder-swap test fails (1 failed, 75 passed in modConfigs.test.ts): the outside file ends up in the recovery folder. On this head all 76 pass. It merges cleanly with #793, which also edits modConfigs.ts, and it touches no locale file.

Approving. Note 1 (at least the comment) and note 3 are worth taking here. Note 2 fits a follow-up issue.

Not blocking

  1. A swap that lands after the new check still leaves the outside file in a kept recovery folder. The check runs once, then lstat (:731), readFile (:745), recoveryFolderFor() (:753), ensureDir (:765) and fse.copy (:769) each await. I swapped swapped/ for a link right after the lstat, with legit.json in the same pack: the result was applied: [legit.json replace], failed: [swapped/target.json write-failed], and the kept recovery folder held swapped/target.json with the outside file's bytes. Both the read and the copy see the same outside bytes, so the digest check passes. A swap during the ensureDir instead makes fse.copy read the outside file, then the digest check removes it and reports not-landed, which is misleading but keeps none of the outside bytes. Path checks can't close this completely in Node, so I'd keep it proportional: run assertManagedPath(dirname(entry.destination), ...) once more after the copy is verified, and on failure remove backupPath and refuse with write-failed. Either way, the comment at :727-728 should not claim the copy is covered.

  2. The export has the same gap. walkModConfigs lstats every file, but collectModConfigs reads each one later (:346) with no new check, and readFile follows links. I put a.json and sub/b.json in ModConfig, then swapped sub/ for a link to an outside folder during the read of a.json. The export returned ok: true with the outside b.json under sub/b.json. A pack is meant to be shared, so this goes further than the apply case, where the bytes stay in the player's own backups. Re-asserting dirname(file.entryPath) and checking that an lstat still reports a regular file right before the read would match this PR. Since #793 also changes walkModConfigs, a separate issue after it lands is probably simpler.

  3. The file-swap test passes on dev, and some of the read and copy checks can't fail. The swap in the test at :1295 is refused on dev too, by the existing isSymbolicLink() check at :739. That makes it a fine guard for :739, but not a test of this fix, so a title saying so would help. The handler only ever reads and copies the in-folder path, so startsWith(outside) at :1281, :1284, :1335 and :1338 never matches. On Windows, includes("swapped/target.json") at :1281 and :1284 doesn't match a path built with path.join either, which leaves only the backupFolder checks guarding the regression there. Matching on join("swapped", "target.json") and dropping the startsWith(outside) branches fixes both.

  4. The comment at :714 lost its reason. The old four lines said why a refused key must not be re-examined: the lstat, readFile and copy on such a path read someone else's file and put a copy in the recovery folder. The new line says what the code does, not why. I'd keep the old comment. The new #626 comment below it covers the extra check.

@Zaldaryon
Zaldaryon merged commit d896c23 into dev Nov 10, 2026
11 checks passed
@Zaldaryon
Zaldaryon deleted the fix/issue-626-mod-config-apply-swapped-link branch November 10, 2026 23:05
@Pixnop

Pixnop commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

I reviewed 121758c5 against 0e3d026b: read the new commit, ran modConfigs.test.ts on the head and with the head's tests on dev before the merge (33e45b25), put each of the three path checks back out one at a time, and replayed my earlier swaps (plus one that swaps three times) against both heads.

Round one

A swap after the first check no longer leaves the outside file in the recovery folder. The check after the ensureDir (modConfigs.ts:769) and the one after the verified copy (:792-798) close it. When the second one throws, it drops the backup and the entry is refused as write-failed. I swapped swapped/ for a link during the readFile. On 0e3d026b the kept recovery folder held swapped/target.json with the outside bytes. On this head it holds no copy, legit.json is still replaced, and the comment at :730-731 no longer claims the copy is covered.

The swap tests now fail where they should. The read and copy checks at :1281, :1284, :1335 and :1338 compare against a join(...) path, so they hold on Windows too. On 33e45b25 the folder-swap test fails at :1277. With the state checks commented out, it fails on outside file must not be read, then on outside file must not be copied. The new title at :1295 says what that test guards.

The comment at :714 is back. The export gap is in #808, which is the right place for it.

Still approved. It is merged already, so the point below can go in a small follow-up.

Not blocking

  1. No test covers the two checks this push added. If I delete :769 and :792-798, all 76 tests still pass. The swap tests swap during legit.json's copy, and the checks at :732 and :742 already refuse those. The swap during the readFile above makes a test that fails once both are gone. Assert failed is [swapped/target.json write-failed] and existsSync(join(backupFolder, "swapped", "target.json")) is false. A one-line comment above :792 saying why the folder is checked again after the copy would help the next reader.

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.

2 participants