GeezerVison is showing github.com — child_process: propagate config-file permission flags to children · nodejs/node@a429fd0
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/child_process.js +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,7 @@ const { | 39 | 39 | ObjectPrototypeHasOwnProperty, | 40 | 40 | PromiseWithResolvers, | 41 | 41 | RegExpPrototypeExec, | | 42 | + RegExpPrototypeSymbolReplace, | 42 | 43 | SafeSet, | 43 | 44 | StringPrototypeIncludes, | 44 | 45 | StringPrototypeIndexOf, | @@ -77,6 +78,7 @@ const { | 77 | 78 | } = require('internal/errors'); | 78 | 79 | const { clearTimeout, setTimeout } = require('timers'); | 79 | 80 | const { getValidatedPath } = require('internal/fs/utils'); | | 81 | +const { getOptionValue } = require('internal/options'); | 80 | 82 | const { | 81 | 83 | isInt32, | 82 | 84 | validateAbortSignal, | @@ -583,6 +585,24 @@ function copyPermissionModelFlagsToEnv(env, key, args) { | 583 | 585 | } | 584 | 586 | } | 585 | 587 | } | | 588 | + | | 589 | + // Flags set through --config-file are not part of process.execArgv. | | 590 | + for (const flag of flagsToCopy) { | | 591 | + if (copiedFlags.has(flag)) { | | 592 | + continue; | | 593 | + } | | 594 | + const value = getOptionValue(flag); | | 595 | + if (value === true) { | | 596 | + env[key] = `${env[key] ? env[key] + ' ' : ''}${flag}`; | | 597 | + } else if (ArrayIsArray(value)) { | | 598 | + for (const item of value) { | | 599 | + // Values may contain spaces or quotes (e.g. the implicitly allowed | | 600 | + // entry point), so quote them as NODE_OPTIONS expects. | | 601 | + const quoted = RegExpPrototypeSymbolReplace(/["\\]/g, item, '\\$&'); | | 602 | + env[key] = `${env[key] ? env[key] + ' ' : ''}"${flag}=${quoted}"`; | | 603 | + } | | 604 | + } | | 605 | + } | 586 | 606 | } | 587 | 607 |
| 588 | 608 | let emittedDEP0190Already = false; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-permission-config-file.mjs +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,6 +64,40 @@ describe('Permission model config file support', () => { | 64 | 64 | } | 65 | 65 | }); | 66 | 66 |
| | 67 | + it('should propagate config file permissions to child processes', { | | 68 | + skip: process.config.variables.node_without_node_options && 'missing NODE_OPTIONS support', | | 69 | + }, async () => { | | 70 | + const childTestPath = fixtures.path('permission/child-process-inherit-test.js'); | | 71 | + const expected = JSON.stringify(['object', true, false, true, true]); | | 72 | + | | 73 | + // --permission set in the config file | | 74 | + { | | 75 | + const configPath = fixtures.path('permission/config-child-inherit.json'); | | 76 | + const result = await spawnPromisified(process.execPath, [ | | 77 | + `--config-file=${configPath}`, | | 78 | + childTestPath, | | 79 | + ]); | | 80 | + assert.strictEqual(result.code, 0, result.stderr); | | 81 | + const child = JSON.parse(result.stdout); | | 82 | + assert.strictEqual(child.status, 0, child.stderr); | | 83 | + assert.strictEqual(child.stdout, expected); | | 84 | + } | | 85 | + | | 86 | + // --permission set in the command line | | 87 | + { | | 88 | + const configPath = fixtures.path('permission/config-child-inherit-allow-only.json'); | | 89 | + const result = await spawnPromisified(process.execPath, [ | | 90 | + '--permission', | | 91 | + `--config-file=${configPath}`, | | 92 | + childTestPath, | | 93 | + ]); | | 94 | + assert.strictEqual(result.code, 0, result.stderr); | | 95 | + const child = JSON.parse(result.stdout); | | 96 | + assert.strictEqual(child.status, 0, child.stderr); | | 97 | + assert.strictEqual(child.stdout, expected); | | 98 | + } | | 99 | + }); | | 100 | + | 67 | 101 | it('should load network and inspector permissions from config file', async () => { | 68 | 102 | const configPath = fixtures.path('permission/config-net-inspector.json'); | 69 | 103 | const readOnlyConfigPath = fixtures.path('permission/config-fs-read-only.json'); | |
|