Skip to content

Commit a429fd0

Browse files
RafaelGSSaduh95
authored andcommitted
child_process: propagate config-file permission flags to children
Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #66830 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 53ae9e5 commit a429fd0

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎lib/child_process.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const {
3939
ObjectPrototypeHasOwnProperty,
4040
PromiseWithResolvers,
4141
RegExpPrototypeExec,
42+
RegExpPrototypeSymbolReplace,
4243
SafeSet,
4344
StringPrototypeIncludes,
4445
StringPrototypeIndexOf,
@@ -77,6 +78,7 @@ const {
7778
} = require('internal/errors');
7879
const { clearTimeout, setTimeout } = require('timers');
7980
const { getValidatedPath } = require('internal/fs/utils');
81+
const { getOptionValue } = require('internal/options');
8082
const {
8183
isInt32,
8284
validateAbortSignal,
@@ -583,6 +585,24 @@ function copyPermissionModelFlagsToEnv(env, key, args) {
583585
}
584586
}
585587
}
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+
}
586606
}
587607

588608
let emittedDEP0190Already = false;

‎test/parallel/test-permission-config-file.mjs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,40 @@ describe('Permission model config file support', () => {
6464
}
6565
});
6666

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+
67101
it('should load network and inspector permissions from config file', async () => {
68102
const configPath = fixtures.path('permission/config-net-inspector.json');
69103
const readOnlyConfigPath = fixtures.path('permission/config-fs-read-only.json');

0 commit comments

Comments
 (0)