Skip to content
Open
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
12 changes: 8 additions & 4 deletions lib/internal/vfs/providers/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ class MemoryProvider extends VirtualProvider {
#readdirRecursive(dirEntry, dirPath, withFileTypes) {
const results = [];

const walk = (entry, currentPath, relativePath) => {
const walk = (entry, currentPath, relativePath, symlinkDepth) => {
this.#ensurePopulated(entry, currentPath);

for (const { 0: name, 1: childEntry } of entry.children) {
Expand All @@ -636,6 +636,7 @@ class MemoryProvider extends VirtualProvider {

// Follow symlinks to directories for recursive traversal
let resolvedChild = childEntry;
let childSymlinkDepth = symlinkDepth;
if (childEntry.isSymbolicLink()) {
const targetPath = this.#resolveSymlinkTarget(
pathPosix.join(currentPath, name), childEntry.target,
Expand All @@ -644,15 +645,18 @@ class MemoryProvider extends VirtualProvider {
if (result.entry) {
resolvedChild = result.entry;
}
// Bound symlink hops to avoid unbounded recursion on cycles.
childSymlinkDepth = symlinkDepth + 1;
}
if (resolvedChild.isDirectory()) {
if (resolvedChild.isDirectory() &&
childSymlinkDepth <= kMaxSymlinkDepth) {
const childPath = pathPosix.join(currentPath, name);
walk(resolvedChild, childPath, childRelative);
walk(resolvedChild, childPath, childRelative, childSymlinkDepth);
}
}
};

walk(dirEntry, dirPath, '');
walk(dirEntry, dirPath, '', 0);
return results;
}

Expand Down
56 changes: 55 additions & 1 deletion test/parallel/test-vfs-readdir-symlink-recursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// Recursive readdir must follow symlinks to directories.

require('../common');
const common = require('../common');
const assert = require('assert');
const vfs = require('node:vfs');

Expand Down Expand Up @@ -52,3 +52,57 @@ assert.ok(
assert.ok(dirents.some((d) => d.name === 'sub' && d.isDirectory()));
assert.ok(dirents.some((d) => d.name === 'lnk' && d.isSymbolicLink()));
}

// Recursive readdir on circular symlinks must terminate, not overflow the
// stack. Regression test for https://github.com/nodejs/node/issues/64148

// Self-referential symlink: /dir/loop -> /dir
{
const v = vfs.create();
v.mkdirSync('/dir');
v.symlinkSync('/dir', '/dir/loop');

const entries = v.readdirSync('/', { recursive: true });
// Terminates, follows the symlink at least one level, stays bounded.
assert.ok(entries.includes('dir'));
assert.ok(entries.includes('dir/loop'));
assert.ok(entries.includes('dir/loop/loop'));
assert.ok(entries.length < 100, `unbounded result: ${entries.length}`);
}

// Mutual circular chain: /a/link_to_b -> /b and /b/link_to_a -> /a
{
const v = vfs.create();
v.mkdirSync('/a');
v.mkdirSync('/b');
v.symlinkSync('/a', '/b/link_to_a');
v.symlinkSync('/b', '/a/link_to_b');

const entries = v.readdirSync('/', { recursive: true });
assert.ok(entries.includes('a'));
assert.ok(entries.includes('b'));
assert.ok(entries.length < 200, `unbounded result: ${entries.length}`);
}

// withFileTypes:true on a circular symlink must also terminate.
{
const v = vfs.create();
v.mkdirSync('/dir');
v.symlinkSync('/dir', '/dir/loop');

const dirents = v.readdirSync('/', { withFileTypes: true, recursive: true });
assert.ok(dirents.some((d) => d.name === 'loop' && d.isSymbolicLink()));
assert.ok(dirents.length < 100, `unbounded result: ${dirents.length}`);
}

// Async promises.readdir variant must reject-free terminate as well.
{
const v = vfs.create();
v.mkdirSync('/dir');
v.symlinkSync('/dir', '/dir/loop');

v.promises.readdir('/', { recursive: true }).then(common.mustCall((entries) => {
assert.ok(entries.includes('dir/loop'));
assert.ok(entries.length < 100, `unbounded result: ${entries.length}`);
}));
}
Loading