Skip to content

Commit db318e6

Browse files
Sylphy-0xd3acSylphy-0xd3ac
authored andcommitted
fs: fix glob TypeError on restricted dirs
When a directory cannot be read due to permission issues, the async version of fs.glob() returns null from readdir(), while the sync version returns an empty array. This causes a TypeError when trying to access the 'length' property of null.
1 parent 910a8af commit db318e6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/internal/fs/glob.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class Cache {
143143
if (cached) {
144144
return cached;
145145
}
146-
const promise = PromisePrototypeThen(readdir(path, { __proto__: null, withFileTypes: true }), null, () => null);
146+
const promise = PromisePrototypeThen(readdir(path, { __proto__: null, withFileTypes: true }), null, () => []);
147147
this.#readdirCache.set(path, promise);
148148
return promise;
149149
}

test/parallel/test-fs-glob.mjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,24 @@ describe('fsPromises glob - exclude', function() {
518518
});
519519
}
520520
});
521+
522+
describe('glob - with restricted directory', function() {
523+
test('*', async () => {
524+
const restrictedDir = tmpdir.resolve('restricted');
525+
await mkdir(restrictedDir, { recursive: true });
526+
chmodSync(restrictedDir, 0o000);
527+
try {
528+
const results = [];
529+
for await (const match of asyncGlob('*', { cwd: restrictedDir })) {
530+
results.push(match);
531+
}
532+
assert.ok(true, 'glob completed without throwing on readdir error');
533+
} finally {
534+
try {
535+
chmodSync(restrictedDir, 0o755);
536+
} catch {
537+
// ignore
538+
}
539+
}
540+
});
541+
});

0 commit comments

Comments
 (0)