GeezerVison is showing github.com — child_process: copy split permission flag values · nodejs/node@faec8f0
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 +28-8Lines changed: 28 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -572,6 +572,11 @@ function hasPermissionFlagInEnv(nodeOptions) { | 572 | 572 | ); | 573 | 573 | } | 574 | 574 |
| | 575 | +function quoteNodeOption(flag, value) { | | 576 | + const quoted = RegExpPrototypeSymbolReplace(/["\\]/g, value, '\\$&'); | | 577 | + return `"${flag}=${quoted}"`; | | 578 | +} | | 579 | + | 575 | 580 | function copyPermissionModelFlagsToEnv(env, key, args) { | 576 | 581 | // Do not override if permission was already passed to file | 577 | 582 | if (args.includes('--permission') || args.includes('--permission-audit') || | @@ -586,15 +591,33 @@ function copyPermissionModelFlagsToEnv(env, key, args) { | 586 | 591 |
| 587 | 592 | const flagsToCopy = getPermissionModelFlagsToCopy(); | 588 | 593 | const copiedFlags = new SafeSet(); | 589 | | - for (const arg of process.execArgv) { | | 594 | + const execArgv = process.execArgv; | | 595 | + for (let i = 0; i < execArgv.length; i++) { | | 596 | + const arg = execArgv[i]; | 590 | 597 | if (allowAllEnv && arg.startsWith('--allow-env')) { | 591 | 598 | continue; | 592 | 599 | } | 593 | 600 | for (const flag of flagsToCopy) { | 594 | | - if (arg.startsWith(flag)) { | 595 | | - copiedFlags.add(flag); | 596 | | - env[key] = `${env[key] ? env[key] + ' ' + arg : arg}`; | | 601 | + if (!arg.startsWith(flag)) { | | 602 | + continue; | | 603 | + } | | 604 | + copiedFlags.add(flag); | | 605 | + if (ArrayIsArray(getOptionValue(flag))) { | | 606 | + let value; | | 607 | + if (arg === flag && i + 1 < execArgv.length) { | | 608 | + value = execArgv[++i]; | | 609 | + if (value[0] === '\\' && value[1] === '-') { | | 610 | + value = StringPrototypeSlice(value, 1); | | 611 | + } | | 612 | + } else if (arg[flag.length] === '=') { | | 613 | + value = StringPrototypeSlice(arg, flag.length + 1); | | 614 | + } | | 615 | + if (value !== undefined) { | | 616 | + env[key] = `${env[key] ? env[key] + ' ' : ''}${quoteNodeOption(flag, value)}`; | | 617 | + break; | | 618 | + } | 597 | 619 | } | | 620 | + env[key] = `${env[key] ? env[key] + ' ' + arg : arg}`; | 598 | 621 | } | 599 | 622 | } | 600 | 623 |
| @@ -608,10 +631,7 @@ function copyPermissionModelFlagsToEnv(env, key, args) { | 608 | 631 | env[key] = `${env[key] ? env[key] + ' ' : ''}${flag}`; | 609 | 632 | } else if (ArrayIsArray(value)) { | 610 | 633 | for (const item of value) { | 611 | | - // Values may contain spaces or quotes (e.g. the implicitly allowed | 612 | | - // entry point), so quote them as NODE_OPTIONS expects. | 613 | | - const quoted = RegExpPrototypeSymbolReplace(/["\\]/g, item, '\\$&'); | 614 | | - env[key] = `${env[key] ? env[key] + ' ' : ''}"${flag}=${quoted}"`; | | 634 | + env[key] = `${env[key] ? env[key] + ' ' : ''}${quoteNodeOption(flag, item)}`; | 615 | 635 | } | 616 | 636 | } | 617 | 637 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-permission-child-process-inherit-flags.js +45Lines changed: 45 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -100,6 +100,51 @@ const childProcess = require('child_process'); | 100 | 100 | assert.strictEqual(worker, 'false'); | 101 | 101 | } | 102 | 102 |
| | 103 | +{ | | 104 | + const tmpdir = require('../common/tmpdir'); | | 105 | + const dir = tmpdir.resolve('inherit flags dir'); | | 106 | + const file = `${dir}/file.txt`; | | 107 | + | | 108 | + const grandchildSource = ` | | 109 | + console.log(process.permission.has('fs.read')); | | 110 | + console.log(process.permission.has('fs.read', ${JSON.stringify(file)})); | | 111 | + console.log(process.permission.has('fs.read', ${JSON.stringify(__filename)})); | | 112 | + `; | | 113 | + const childSource = ` | | 114 | + const { status, stdout, stderr } = require('child_process').spawnSync( | | 115 | + process.execPath, ['-e', ${JSON.stringify(grandchildSource)}]); | | 116 | + console.log(JSON.stringify({ | | 117 | + status, | | 118 | + stderr: stderr.toString(), | | 119 | + permissions: stdout.toString().trim().split('\\n'), | | 120 | + })); | | 121 | + `; | | 122 | + | | 123 | + function run(args) { | | 124 | + const { status, stdout, stderr } = childProcess.spawnSync( | | 125 | + process.execPath, | | 126 | + ['--permission', '--allow-child-process', ...args, '-e', childSource], | | 127 | + ); | | 128 | + assert.strictEqual(status, 0, stderr.toString()); | | 129 | + const grandchild = JSON.parse(stdout.toString()); | | 130 | + assert.strictEqual(grandchild.status, 0, grandchild.stderr); | | 131 | + return grandchild.permissions; | | 132 | + } | | 133 | + | | 134 | + assert.deepStrictEqual(run(['--allow-fs-read', '*']), | | 135 | + ['true', 'true', 'true']); | | 136 | + | | 137 | + assert.deepStrictEqual(run(['--allow-fs-read', `${dir}/*`]), | | 138 | + ['false', 'true', 'false']); | | 139 | + | | 140 | + assert.deepStrictEqual(run([`--allow-fs-read=${dir}/*`]), | | 141 | + ['false', 'true', 'false']); | | 142 | + | | 143 | + assert.deepStrictEqual( | | 144 | + run(['--allow-fs-read', `${dir}/*`, `--allow-fs-read=${__filename}`]), | | 145 | + ['false', 'true', 'true']); | | 146 | +} | | 147 | + | 103 | 148 | { | 104 | 149 | assert.strictEqual(process.env.NODE_OPTIONS, undefined); | 105 | 150 | } |
|