Skip to content

Commit ae51b1d

Browse files
committed
fs: normalize dot segments in rm and rmSync
When paths containing relative components (such as '.' or '..') are passed to fs.rm(), fs.rmSync(), or fs.promises.rm(), the different underlying implementations (native binding.rmSync vs JS rimraf) can produce divergent results or silent deletion failures because paths were used as-is without canonicalization. Add normalizePathForRm to resolve dot-segment components for both string and Buffer inputs using byte-safe latin1 preservation before reaching the underlying deletion routines. Fixes: nodejs#61958 Signed-off-by: Rohith Pariki <[email protected]>
1 parent 7fab656 commit ae51b1d

3 files changed

Lines changed: 127 additions & 1 deletion

File tree

‎lib/fs.js‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,29 @@ function rmdirSync(path, options) {
15371537
binding.rmdir(path);
15381538
}
15391539

1540+
/**
1541+
* Asynchronously removes files and
1542+
* directories (modeled on the standard POSIX `rm` utility).
1543+
* @param {string | Buffer | URL} path
1544+
* @param {{
1545+
* force?: boolean;
1546+
* maxRetries?: number;
1547+
* recursive?: boolean;
1548+
* retryDelay?: number;
1549+
* }} [options]
1550+
function normalizePathForRm(path) {
1551+
if (typeof path === 'string') {
1552+
return pathModule.normalize(path);
1553+
}
1554+
if (isArrayBufferView(path)) {
1555+
const buf = Buffer.from(path.buffer, path.byteOffset, path.byteLength);
1556+
const str = BufferToString(buf, 'latin1');
1557+
const normalized = pathModule.normalize(str);
1558+
return Buffer.from(normalized, 'latin1');
1559+
}
1560+
return path;
1561+
}
1562+
15401563
/**
15411564
* Asynchronously removes files and
15421565
* directories (modeled on the standard POSIX `rm` utility).
@@ -1560,6 +1583,7 @@ function rm(path, options, callback) {
15601583
if (h !== null && vfsVoid(h.rm(path, options), callback)) return;
15611584

15621585
path = getValidatedPath(path);
1586+
path = normalizePathForRm(path);
15631587

15641588
validateRmOptions(path, options, false, (err, options) => {
15651589
if (err) {
@@ -1588,8 +1612,10 @@ function rmSync(path, options) {
15881612
const result = h.rmSync(path, options);
15891613
if (result !== undefined) return;
15901614
}
1615+
path = getValidatedPath(path);
1616+
path = normalizePathForRm(path);
15911617
const opts = validateRmOptionsSync(path, options, false);
1592-
return binding.rmSync(getValidatedPath(path), opts.maxRetries, opts.recursive, opts.retryDelay);
1618+
return binding.rmSync(path, opts.maxRetries, opts.recursive, opts.retryDelay);
15931619
}
15941620

15951621
/**

‎lib/internal/fs/promises.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,13 +1600,27 @@ async function ftruncate(handle, len = 0) {
16001600
);
16011601
}
16021602

1603+
function normalizePathForRm(path) {
1604+
if (typeof path === 'string') {
1605+
return pathModule.normalize(path);
1606+
}
1607+
if (isArrayBufferView(path)) {
1608+
const buf = Buffer.from(path.buffer, path.byteOffset, path.byteLength);
1609+
const str = BufferToString(buf, 'latin1');
1610+
const normalized = pathModule.normalize(str);
1611+
return Buffer.from(normalized, 'latin1');
1612+
}
1613+
return path;
1614+
}
1615+
16031616
async function rm(path, options) {
16041617
const h = vfsState.handlers;
16051618
if (h !== null) {
16061619
const promise = h.rm(path, options);
16071620
if (promise !== undefined) { await promise; return; }
16081621
}
16091622
path = getValidatedPath(path);
1623+
path = normalizePathForRm(path);
16101624
options = await validateRmOptionsPromise(path, options, false);
16111625
return lazyRimRaf()(path, options);
16121626
}

‎test/parallel/test-fs-rm.js‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,89 @@ if (isGitPresent) {
657657
// Clean up parent directory
658658
fs.rmSync(dirname, { recursive: true, force: true });
659659
}
660+
661+
// Test that rm/rmSync normalize '.' and '..' in paths before processing.
662+
// Regression test for https://github.com/nodejs/node/issues/61958
663+
{
664+
const cases = [
665+
{ segments: ['a', 'b', '..', '.'], target: 'a' },
666+
{ segments: ['a', 'b', '..'], target: 'a' },
667+
{ segments: ['a', '.'], target: 'a' },
668+
{ segments: ['a', 'b', 'c', '.'], target: path.join('a', 'b', 'c') },
669+
{ segments: ['a', 'b', 'c', '..', '..'], target: 'a' },
670+
{ segments: ['a', 'b', 'c', 'd', '..', '..', '..'], target: 'a' },
671+
{ segments: ['a', 'b', '..', '.', 'b'], target: path.join('a', 'b') },
672+
{ segments: ['.', 'a'], target: 'a' },
673+
];
674+
675+
for (const { segments, target } of cases) {
676+
// rmSync with string path
677+
{
678+
const base = nextDirPath('rm-dot-sync-str');
679+
fs.mkdirSync(path.join(base, 'a', 'b', 'c', 'd'),
680+
common.mustNotMutateObjectDeep({ recursive: true }));
681+
const weirdPath = [base, ...segments].join(path.sep);
682+
fs.rmSync(weirdPath, common.mustNotMutateObjectDeep({ recursive: true, force: true }));
683+
assert.strictEqual(fs.existsSync(path.join(base, target)), false);
684+
}
685+
686+
// rmSync with Buffer path
687+
{
688+
const base = nextDirPath('rm-dot-sync-buf');
689+
fs.mkdirSync(path.join(base, 'a', 'b', 'c', 'd'),
690+
common.mustNotMutateObjectDeep({ recursive: true }));
691+
const weirdPath = Buffer.from([base, ...segments].join(path.sep));
692+
fs.rmSync(weirdPath, common.mustNotMutateObjectDeep({ recursive: true, force: true }));
693+
assert.strictEqual(fs.existsSync(path.join(base, target)), false);
694+
}
695+
696+
// fs.rm (callback) with string path
697+
{
698+
const base = nextDirPath('rm-dot-cb-str');
699+
fs.mkdirSync(path.join(base, 'a', 'b', 'c', 'd'),
700+
common.mustNotMutateObjectDeep({ recursive: true }));
701+
const weirdPath = [base, ...segments].join(path.sep);
702+
fs.rm(weirdPath,
703+
common.mustNotMutateObjectDeep({ recursive: true, force: true }),
704+
common.mustSucceed(() => {
705+
assert.strictEqual(fs.existsSync(path.join(base, target)), false);
706+
}));
707+
}
708+
709+
// fs.promises.rm with string path
710+
{
711+
const base = nextDirPath('rm-dot-prom-str');
712+
fs.mkdirSync(path.join(base, 'a', 'b', 'c', 'd'),
713+
common.mustNotMutateObjectDeep({ recursive: true }));
714+
const weirdPath = [base, ...segments].join(path.sep);
715+
fs.promises.rm(weirdPath,
716+
common.mustNotMutateObjectDeep({ recursive: true, force: true }))
717+
.then(common.mustCall(() => {
718+
assert.strictEqual(fs.existsSync(path.join(base, target)), false);
719+
}));
720+
}
721+
722+
// fs.promises.rm with Buffer path
723+
{
724+
const base = nextDirPath('rm-dot-prom-buf');
725+
fs.mkdirSync(path.join(base, 'a', 'b', 'c', 'd'),
726+
common.mustNotMutateObjectDeep({ recursive: true }));
727+
const weirdPath = Buffer.from([base, ...segments].join(path.sep));
728+
fs.promises.rm(weirdPath,
729+
common.mustNotMutateObjectDeep({ recursive: true, force: true }))
730+
.then(common.mustCall(() => {
731+
assert.strictEqual(fs.existsSync(path.join(base, target)), false);
732+
}));
733+
}
734+
}
735+
736+
// Non-UTF-8 Buffer path byte preservation test
737+
{
738+
const base = nextDirPath('rm-dot-buf-latin1');
739+
fs.mkdirSync(path.join(base, 'a', 'b'),
740+
common.mustNotMutateObjectDeep({ recursive: true }));
741+
const bufPath = Buffer.from([base, 'a', 'b', '..', '.'].join(path.sep));
742+
fs.rmSync(bufPath, common.mustNotMutateObjectDeep({ recursive: true, force: true }));
743+
assert.strictEqual(fs.existsSync(path.join(base, 'a')), false);
744+
}
745+
}

0 commit comments

Comments
 (0)