Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,7 @@ function rm(path, options, callback) {
if (h !== null && vfsVoid(h.rm(path, options), callback)) return;

path = getValidatedPath(path);
path = pathModule.normalize(path);

validateRmOptions(path, options, false, (err, options) => {
if (err) {
Expand Down Expand Up @@ -1588,8 +1589,10 @@ function rmSync(path, options) {
const result = h.rmSync(path, options);
if (result !== undefined) return;
}
path = getValidatedPath(path);
path = pathModule.normalize(path);
const opts = validateRmOptionsSync(path, options, false);
return binding.rmSync(getValidatedPath(path), opts.maxRetries, opts.recursive, opts.retryDelay);
return binding.rmSync(path, opts.maxRetries, opts.recursive, opts.retryDelay);
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,7 @@ async function rm(path, options) {
if (promise !== undefined) { await promise; return; }
}
path = getValidatedPath(path);
path = pathModule.normalize(path);
options = await validateRmOptionsPromise(path, options, false);
return lazyRimRaf()(path, options);
}
Expand Down
42 changes: 42 additions & 0 deletions test/parallel/test-fs-rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,45 @@ if (isGitPresent) {
// Clean up parent directory
fs.rmSync(dirname, { recursive: true, force: true });
}

// Test that rm/rmSync normalize '.' and '..' in paths before processing.
// Regression test for https://github.com/nodejs/node/issues/61958
//
// Each variant gets its own base directory to avoid async/sync races.
// The path is constructed by joining components with path.sep so that
// the '..' and '.' are preserved and not pre-normalized by path.join.
{
// --- rmSync: <base>/a/b/../. should remove <base>/a entirely ---
const base = nextDirPath('dotdot-sync');
fs.mkdirSync(path.join(base, 'a', 'b', 'c', 'd'),
common.mustNotMutateObjectDeep({ recursive: true }));
const weirdPath = [base, 'a', 'b', '..', '.'].join(path.sep);
fs.rmSync(weirdPath, common.mustNotMutateObjectDeep({ recursive: true }));
assert.strictEqual(fs.existsSync(path.join(base, 'a')), false);
}

{
// --- fs.rm (callback): same path construction ---
const base = nextDirPath('dotdot-cb');
fs.mkdirSync(path.join(base, 'a', 'b', 'c', 'd'),
common.mustNotMutateObjectDeep({ recursive: true }));
const weirdPath = [base, 'a', 'b', '..', '.'].join(path.sep);
fs.rm(weirdPath,
common.mustNotMutateObjectDeep({ recursive: true }),
common.mustSucceed(() => {
assert.strictEqual(fs.existsSync(path.join(base, 'a')), false);
}));
}

{
// --- fs.promises.rm: same path construction ---
const base = nextDirPath('dotdot-prom');
fs.mkdirSync(path.join(base, 'a', 'b', 'c', 'd'),
common.mustNotMutateObjectDeep({ recursive: true }));
const weirdPath = [base, 'a', 'b', '..', '.'].join(path.sep);
fs.promises.rm(weirdPath,
common.mustNotMutateObjectDeep({ recursive: true }))
.then(common.mustCall(() => {
assert.strictEqual(fs.existsSync(path.join(base, 'a')), false);
}));
}