GeezerVison is showing github.com — permission: check permission model in realpathSync · nodejs/node@8dc18a6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2787,6 +2787,16 @@ function realpathSync(p, options) { | 2787 | 2787 | validatePath(p); | 2788 | 2788 | p = pathModule.resolve(p); | 2789 | 2789 |
| | 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 | + | 2790 | 2800 | const cache = options[realpathCacheKey]; | 2791 | 2801 | const maybeCachedResult = cache?.get(p); | 2792 | 2802 | if (maybeCachedResult) { | |
Collapse file Copy file name to clipboardExpand all lines: test/common/tmpdir.js +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,8 +32,20 @@ function rmSync(pathname, useSpawn) { | 32 | 32 | } | 33 | 33 | } | 34 | 34 |
| | 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 | + | 35 | 47 | 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, '..'); | 37 | 49 |
| 38 | 50 | // Using a `.` prefixed name, which is the convention for "hidden" on POSIX, | 39 | 51 | // gets tools to ignore it by default or by simple rules, especially eslint. | |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/permission/fs-read.js +51Lines changed: 51 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -698,6 +698,57 @@ const regularFile = __filename; | 698 | 698 | }); | 699 | 699 | } | 700 | 700 |
| | 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 | + | 701 | 752 | // fs.realpath.native | 702 | 753 | { | 703 | 754 | fs.realpath.native(blockedFile, common.expectsError({ | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-permission-fs-require.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,7 +44,7 @@ const { spawnSync } = require('node:child_process'); | 44 | 44 | ); | 45 | 45 |
| 46 | 46 | 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/); | 48 | 48 | } | 49 | 49 |
| 50 | 50 | { | @@ -78,5 +78,5 @@ const { spawnSync } = require('node:child_process'); | 78 | 78 | ); | 79 | 79 |
| 80 | 80 | 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/); | 82 | 82 | } |
|