GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — vfs: reject rename into descendant directory · nodejs/node@d5f01bb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/vfs/providers
    • memory.js
  • test/parallel
    • test-vfs-rename.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/vfs/providers/memory.js
+6Lines 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
const entry = this.#getEntry(normalizedOld, 'rename', false);
796797

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

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

Copy file name to clipboardExpand all lines: test/parallel/test-vfs-rename.js
+14Lines 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+
const myVfs = 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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.