GeezerVison is showing github.com — permission: check permission model in realpathSync · nodejs/node@cc24413
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 +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3369,6 +3369,17 @@ function realpathSync(p, options) { | 3369 | 3369 | validatePath(p); | 3370 | 3370 | p = pathModule.resolve(p); | 3371 | 3371 |
| | 3372 | + // Check the resolved input path up front so that the lstat walk below cannot | | 3373 | + // be used to probe the existence of paths outside the granted set. | | 3374 | + if (permission.isEnabled() && !permission.has('fs.read', p) && | | 3375 | + !permission.isAuditMode()) { | | 3376 | + throw new ERR_ACCESS_DENIED( | | 3377 | + 'Access to this API has been restricted. Use --allow-fs-read to manage permissions.', | | 3378 | + 'FileSystemRead', | | 3379 | + p, | | 3380 | + ); | | 3381 | + } | | 3382 | + | 3372 | 3383 | const cache = options[realpathCacheKey]; | 3373 | 3384 | const maybeCachedResult = cache?.get(p); | 3374 | 3385 | 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 |
|---|
@@ -707,6 +707,57 @@ const regularFile = __filename; | 707 | 707 | }); | 708 | 708 | } | 709 | 709 |
| | 710 | +// fs.realpath (JS implementation, walks the path with lstat) | | 711 | +{ | | 712 | + assert.throws(() => { | | 713 | + fs.realpathSync(blockedFile); | | 714 | + }, common.expectsError({ | | 715 | + code: 'ERR_ACCESS_DENIED', | | 716 | + permission: 'FileSystemRead', | | 717 | + })); | | 718 | + assert.throws(() => { | | 719 | + fs.realpathSync(bufferBlockedFile); | | 720 | + }, common.expectsError({ | | 721 | + code: 'ERR_ACCESS_DENIED', | | 722 | + permission: 'FileSystemRead', | | 723 | + })); | | 724 | + assert.throws(() => { | | 725 | + fs.realpathSync(path.join(blockedFolder, 'anyfile')); | | 726 | + }, common.expectsError({ | | 727 | + code: 'ERR_ACCESS_DENIED', | | 728 | + permission: 'FileSystemRead', | | 729 | + })); | | 730 | + // Must not act as a file-existence oracle: a non-existent path inside a | | 731 | + // blocked folder must be denied, not reported as ENOENT. | | 732 | + assert.throws(() => { | | 733 | + fs.realpathSync(path.join(blockedFolder, 'does-not-exist')); | | 734 | + }, common.expectsError({ | | 735 | + code: 'ERR_ACCESS_DENIED', | | 736 | + permission: 'FileSystemRead', | | 737 | + })); | | 738 | + fs.realpath(blockedFile, common.expectsError({ | | 739 | + code: 'ERR_ACCESS_DENIED', | | 740 | + permission: 'FileSystemRead', | | 741 | + })); | | 742 | + assert.throws(() => { | | 743 | + fs.realpathSync(blockedFile); | | 744 | + }, common.expectsError({ | | 745 | + message: 'Access to this API has been restricted. Use --allow-fs-read to manage permissions.', | | 746 | + code: 'ERR_ACCESS_DENIED', | | 747 | + permission: 'FileSystemRead', | | 748 | + resource: blockedFile, | | 749 | + })); | | 750 | + assert.rejects(async () => { | | 751 | + await fsPromises.realpath(blockedFile); | | 752 | + }, common.expectsError({ | | 753 | + code: 'ERR_ACCESS_DENIED', | | 754 | + permission: 'FileSystemRead', | | 755 | + })).then(common.mustCall()); | | 756 | + | | 757 | + // doesNotThrow | | 758 | + assert.strictEqual(typeof fs.realpathSync(regularFile), 'string'); | | 759 | +} | | 760 | + | 710 | 761 | // fs.realpath.native | 711 | 762 | { | 712 | 763 | fs.realpath.native(blockedFile, common.expectsError({ | |
Collapse file Copy file name to clipboard +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Flags: --permission-audit | | 2 | +'use strict'; | | 3 | + | | 4 | +const common = require('../common'); | | 5 | +const assert = require('assert'); | | 6 | +const dc = require('diagnostics_channel'); | | 7 | +const fs = require('fs'); | | 8 | +const fixtures = require('../common/fixtures'); | | 9 | + | | 10 | +const file = fixtures.path('permission', 'deny', 'protected-file.md'); | | 11 | + | | 12 | +const denied = []; | | 13 | +dc.subscribe('node:permission-model:fs', common.mustCallAtLeast((message) => { | | 14 | + assert.strictEqual(message.permission, 'FileSystemRead'); | | 15 | + denied.push(message.resource); | | 16 | +})); | | 17 | + | | 18 | +assert.strictEqual(fs.realpathSync(file), file); | | 19 | +assert.ok(denied.includes(file), `realpathSync reported ${file}`); |
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 | } |
|