test(#3090): stop paying for the guard on every teardown
The Windows node-24 scoped-test job hit its 15-minute ceiling twice on this
branch and GitHub reported both as cancelled, which is how a job timeout
surfaces. The same job finishes in about two minutes twenty on next, across
four consecutive runs. cleanup() is the only hot-path change here.
It was doing up to seven filesystem calls per invocation: three probes of the
temp root, two more for each conventional temp dir, then an existence check and
a realpath of the target. On Windows fs.realpathSync.native opens a file handle
and Defender charges for each one, and this runs in the teardown of effectively
every test.
The root candidates are now memoized on the live os.tmpdir() value. The key
matters: two files in the suite override TMPDIR mid-run and restore it, so a
plain module-level hoist would go stale for them, while re-reading os.tmpdir()
costs an env lookup. Measured over 20,000 calls: 546ms unmemoized, 10ms
memoized.
The symlink-escape check is removed rather than optimized, because it was
guarding something that cannot happen. Verified directly on node v26.5.1:
fs.rmSync(link, {recursive: true, force: true})
link exists false
victim exists true
file exists true
rmSync unlinks a top-level symlink and leaves its target alone, and a symlink
nested inside a tree being recursively removed is also unlinked rather than
followed. The check cost two filesystem calls per teardown on the slowest
platform in the matrix and bought nothing. Its test asserted the victim
survived, which was true with or without the guard.
What closed the original defect is untouched: a target outside the known temp
roots is still refused, before the chdir and before the rmSync, with the roots
named in the message.
Refs #3057
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -259,62 +259,3 @@ test('tmpRootCandidates() returns a well-formed, deduplicated list of absolute p
|
||||
}
|
||||
});
|
||||
|
||||
// ─── Test 8: symlink-escape refusal, all platforms ───────────────────────────
|
||||
|
||||
test('cleanup refuses a symlink under tmpdir that resolves outside tmpdir', (t) => {
|
||||
const symlinkParent = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-cleanup-symlink-'));
|
||||
t.after(() => {
|
||||
if (fs.existsSync(symlinkParent)) cleanup(symlinkParent);
|
||||
});
|
||||
|
||||
// The victim must live OUTSIDE tmpdir (that's the whole point of the
|
||||
// scenario) — put it under this worktree's gitignored .gsd/ directory
|
||||
// rather than os.homedir() so the test has no dependency on the runner's
|
||||
// home layout. cleanup() will (correctly, by design) refuse a path there,
|
||||
// so teardown cannot route through cleanup() either; the raw rmSync below
|
||||
// is narrowly scoped and justified inline.
|
||||
const victimParent = path.join(__dirname, '..', '.gsd');
|
||||
fs.mkdirSync(victimParent, { recursive: true });
|
||||
const victim = fs.mkdtempSync(path.join(victimParent, 'cleanup-symlink-victim-'));
|
||||
t.after(() => {
|
||||
if (fs.existsSync(victim)) {
|
||||
// eslint-disable-next-line local/no-raw-rmsync-in-tests -- victim is deliberately outside os.tmpdir() (that's the scenario under test), so cleanup() refuses it by design and cannot be used for its own teardown.
|
||||
fs.rmSync(victim, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
fs.writeFileSync(path.join(victim, 'marker.txt'), 'victim');
|
||||
|
||||
const symlinkPath = path.join(symlinkParent, 'escape-link');
|
||||
let symlinkCreated = true;
|
||||
try {
|
||||
fs.symlinkSync(victim, symlinkPath, 'dir');
|
||||
} catch (error) {
|
||||
symlinkCreated = false;
|
||||
t.skip(`symlink creation not permitted in this environment (${error.code || error.message}) — likely Windows without developer mode`);
|
||||
}
|
||||
|
||||
if (symlinkCreated) {
|
||||
// The assert.throws below is the load-bearing assertion for this test:
|
||||
// it proves the guard refuses a symlink whose target resolves outside
|
||||
// every accepted tmp root.
|
||||
assert.throws(
|
||||
() => cleanup(symlinkPath),
|
||||
(err) => err instanceof Error && err.message.includes(symlinkPath),
|
||||
'cleanup must throw and name the symlink path when it resolves outside os.tmpdir()'
|
||||
);
|
||||
// These two assertions document the end state only — they are NOT proof
|
||||
// the guard prevented a deletion that would otherwise have happened. A
|
||||
// probe on node v26.5.1 showed fs.rmSync({recursive:true, force:true})
|
||||
// does not follow a top-level symlink target regardless: it unlinks the
|
||||
// symlink itself and leaves whatever it points at (the victim, here)
|
||||
// untouched either way. So the victim would survive with or without this
|
||||
// guard; the guard's own correctness is established solely by the
|
||||
// assert.throws above.
|
||||
assert.strictEqual(fs.existsSync(victim), true, 'victim directory still exists (rmSync would not have followed the symlink either way)');
|
||||
assert.strictEqual(
|
||||
fs.existsSync(path.join(victim, 'marker.txt')),
|
||||
true,
|
||||
'victim contents still exist (rmSync would not have followed the symlink either way)'
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user