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 — permission: check permission model in realpathSync · nodejs/node@8dc18a6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • fs.js
  • test
    • common
      • tmpdir.js
    • fixtures/permission
      • fs-read.js
    • parallel
      • test-permission-fs-require.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/fs.js‎

Copy file name to clipboardExpand all lines: lib/fs.js
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,6 +2787,16 @@ function realpathSync(p, options) {
27872787
validatePath(p);
27882788
p = pathModule.resolve(p);
27892789

2790+
// Check the resolved input path up front so that the lstat walk below cannot
2791+
// be used to probe the existence of paths outside the granted set.
2792+
if (permission.isEnabled() && !permission.has('fs.read', p)) {
2793+
throw new ERR_ACCESS_DENIED(
2794+
'Access to this API has been restricted. Use --allow-fs-read to manage permissions.',
2795+
'FileSystemRead',
2796+
p,
2797+
);
2798+
}
2799+
27902800
const cache = options[realpathCacheKey];
27912801
const maybeCachedResult = cache?.get(p);
27922802
if (maybeCachedResult) {
Collapse file

‎test/common/tmpdir.js‎

Copy file name to clipboardExpand all lines: test/common/tmpdir.js
+13-1Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,20 @@ function rmSync(pathname, useSpawn) {
3232
}
3333
}
3434

35+
// Under the permission model, fs.realpathSync() requires read access to the
36+
// path, which tests running with a restricted --allow-fs-read may not grant
37+
// for NODE_TEST_DIR.
38+
function resolveTestRoot(dir) {
39+
try {
40+
return fs.realpathSync(dir);
41+
} catch (err) {
42+
if (err.code !== 'ERR_ACCESS_DENIED') throw err;
43+
return path.resolve(dir);
44+
}
45+
}
46+
3547
const testRoot = process.env.NODE_TEST_DIR ?
36-
fs.realpathSync(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..');
48+
resolveTestRoot(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..');
3749

3850
// Using a `.` prefixed name, which is the convention for "hidden" on POSIX,
3951
// gets tools to ignore it by default or by simple rules, especially eslint.
Collapse file

‎test/fixtures/permission/fs-read.js‎

Copy file name to clipboardExpand all lines: test/fixtures/permission/fs-read.js
+51Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,57 @@ const regularFile = __filename;
698698
});
699699
}
700700

701+
// fs.realpath (JS implementation, walks the path with lstat)
702+
{
703+
assert.throws(() => {
704+
fs.realpathSync(blockedFile);
705+
}, common.expectsError({
706+
code: 'ERR_ACCESS_DENIED',
707+
permission: 'FileSystemRead',
708+
}));
709+
assert.throws(() => {
710+
fs.realpathSync(bufferBlockedFile);
711+
}, common.expectsError({
712+
code: 'ERR_ACCESS_DENIED',
713+
permission: 'FileSystemRead',
714+
}));
715+
assert.throws(() => {
716+
fs.realpathSync(path.join(blockedFolder, 'anyfile'));
717+
}, common.expectsError({
718+
code: 'ERR_ACCESS_DENIED',
719+
permission: 'FileSystemRead',
720+
}));
721+
// Must not act as a file-existence oracle: a non-existent path inside a
722+
// blocked folder must be denied, not reported as ENOENT.
723+
assert.throws(() => {
724+
fs.realpathSync(path.join(blockedFolder, 'does-not-exist'));
725+
}, common.expectsError({
726+
code: 'ERR_ACCESS_DENIED',
727+
permission: 'FileSystemRead',
728+
}));
729+
fs.realpath(blockedFile, common.expectsError({
730+
code: 'ERR_ACCESS_DENIED',
731+
permission: 'FileSystemRead',
732+
}));
733+
assert.throws(() => {
734+
fs.realpathSync(blockedFile);
735+
}, common.expectsError({
736+
message: 'Access to this API has been restricted. Use --allow-fs-read to manage permissions.',
737+
code: 'ERR_ACCESS_DENIED',
738+
permission: 'FileSystemRead',
739+
resource: blockedFile,
740+
}));
741+
assert.rejects(async () => {
742+
await fsPromises.realpath(blockedFile);
743+
}, common.expectsError({
744+
code: 'ERR_ACCESS_DENIED',
745+
permission: 'FileSystemRead',
746+
})).then(common.mustCall());
747+
748+
// doesNotThrow
749+
assert.strictEqual(typeof fs.realpathSync(regularFile), 'string');
750+
}
751+
701752
// fs.realpath.native
702753
{
703754
fs.realpath.native(blockedFile, common.expectsError({
Collapse file

‎test/parallel/test-permission-fs-require.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-permission-fs-require.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const { spawnSync } = require('node:child_process');
4444
);
4545

4646
assert.strictEqual(status, 1, stderr.toString());
47-
assert.match(stderr.toString(), /Error: Access to this API has been restricted/);
47+
assert.match(stderr.toString(), /Error(?: \[ERR_ACCESS_DENIED\])?: Access to this API has been restricted/);
4848
}
4949

5050
{
@@ -78,5 +78,5 @@ const { spawnSync } = require('node:child_process');
7878
);
7979

8080
assert.strictEqual(status, 1, stderr.toString());
81-
assert.match(stderr.toString(), /Error: Access to this API has been restricted/);
81+
assert.match(stderr.toString(), /Error(?: \[ERR_ACCESS_DENIED\])?: Access to this API has been restricted/);
8282
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — 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.