Skip to content

Commit 94fba7f

Browse files
fs: fix globSync traverse on allowed directory
Fix an issue where fs.globSync failed to find files when read access was granted only for a certain directory via the --allow-fs-read flag. Fixes: #61499
1 parent e155415 commit 94fba7f

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

lib/internal/fs/glob.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ class Glob {
408408
this.#results.add(path);
409409
}
410410

411-
if (!isDirectory) {
411+
if (!isDirectory && stat !== null) {
412412
return;
413413
}
414414

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const assert = require('node:assert');
6+
const fs = require('node:fs');
7+
const path = require('node:path');
8+
9+
const { spawnSyncAndAssert } = require('../common/child_process');
10+
const tmpdir = require('../common/tmpdir');
11+
12+
// This test verifies that fs.globSync() works correctly udner permission model
13+
// when --allow-fs-read is granted only to the certain directory
14+
15+
// Example:
16+
// Directory structure:
17+
// somedir/
18+
// file1.js
19+
// sample.js
20+
//
21+
// Command:
22+
// node --permission --allow-fs-read=somedir/ sample.js
23+
//
24+
// Code:
25+
// fs.globSync('somedir/*') <- sould find file1.js
26+
27+
tmpdir.refresh();
28+
29+
const someDir = tmpdir.resolve('somedir');
30+
const script = tmpdir.resolve('sample.js');
31+
32+
fs.mkdirSync(someDir);
33+
fs.writeFileSync(
34+
path.join(someDir, 'file1.js'),
35+
'console.log("test");'
36+
);
37+
38+
fs.writeFileSync(
39+
script,
40+
`
41+
'use strict';
42+
const fs = require('fs');
43+
const matches = fs.globSync('somedir/*');
44+
console.log(JSON.stringify(matches));
45+
`
46+
);
47+
48+
spawnSyncAndAssert(
49+
process.execPath,
50+
[
51+
'--permission',
52+
`--allow-fs-read=${someDir}`,
53+
script,
54+
],
55+
{
56+
cwd: tmpdir.path,
57+
},
58+
{
59+
stdout(output) {
60+
assert.deepStrictEqual(
61+
JSON.parse(output),
62+
['somedir/file1.js']
63+
);
64+
return true;
65+
},
66+
stderr(output) {
67+
assert.doesNotMatch(output, /ERR_ACCESS_DENIED/);
68+
return true;
69+
},
70+
}
71+
);

0 commit comments

Comments
 (0)