Skip to content

Commit d5f01bb

Browse files
trivikrrichardlau
authored andcommitted
vfs: reject rename into descendant directory
MemoryProvider allowed renaming a directory into its own descendant, which detached the subtree from the root. Reject this case with EINVAL before mutating the tree. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.5 PR-URL: #64285Fixes: #64284 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 7f7e586 commit d5f01bb

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎lib/internal/vfs/providers/memory.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
SafeMap,
88
SafeSet,
99
StringPrototypeReplaceAll,
10+
StringPrototypeStartsWith,
1011
Symbol,
1112
}=primordials;
1213

@@ -794,6 +795,11 @@ class MemoryProvider extends VirtualProvider {
794795
// Get the entry (without following symlinks for the entry itself)
795796
constentry=this.#getEntry(normalizedOld,'rename',false);
796797

798+
if(entry.isDirectory()&&
799+
StringPrototypeStartsWith(normalizedNew,`${normalizedOld}/`)){
800+
throwcreateEINVAL('rename',oldPath);
801+
}
802+
797803
// Validate destination parent exists (do not auto-create)
798804
constnewParent=this.#ensureParent(normalizedNew,false,'rename');
799805
constnewName=pathPosix.basename(normalizedNew);

‎test/parallel/test-vfs-rename.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,17 @@ const vfs = require('node:vfs');
4444
assert.strictEqual(myVfs.existsSync('/d/a.txt'),false);
4545
assert.strictEqual(myVfs.existsSync('/d/b.txt'),true);
4646
}
47+
48+
// Renaming a directory into its own descendant throws EINVAL
49+
{
50+
constmyVfs=vfs.create();
51+
myVfs.mkdirSync('/a/b',{recursive: true});
52+
myVfs.writeFileSync('/a/file.txt','data');
53+
54+
assert.throws(()=>myVfs.renameSync('/a','/a/b/c'),{code: 'EINVAL'});
55+
assert.deepStrictEqual(myVfs.readdirSync('/'),['a']);
56+
assert.strictEqual(myVfs.existsSync('/a'),true);
57+
assert.strictEqual(myVfs.existsSync('/a/b'),true);
58+
assert.strictEqual(myVfs.existsSync('/a/b/c'),false);
59+
assert.strictEqual(myVfs.readFileSync('/a/file.txt','utf8'),'data');
60+
}

0 commit comments

Comments
 (0)