node/test/parallel/test-permission-allow-child-process-cli.js
Rafael Gonzaga 8173d9d72b
permission: propagate permission model flags on spawn
Previously, only child_process.fork propagated the exec
arguments (execvArgs) to the child process.
This commit adds support for spawn and spawnSync to
propagate permission model flags — except when they are
already provided explicitly via arguments or through
NODE_OPTIONS.

Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com>
PR-URL: https://github.com/nodejs/node/pull/58853
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
2025-07-02 02:32:20 +00:00

46 lines
1.3 KiB
JavaScript

// Flags: --permission --allow-child-process --allow-fs-read=*
'use strict';
const common = require('../common');
const { isMainThread } = require('worker_threads');
if (!isMainThread) {
common.skip('This test only works on a main thread');
}
const assert = require('assert');
const childProcess = require('child_process');
const fs = require('fs');
// Child Process (and fork) should inherit permission model flags
if (process.argv[2] === 'child') {
assert.throws(() => {
fs.writeFileSync(__filename, 'should not write');
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}));
process.exit(0);
}
// Guarantee the initial state
{
assert.ok(process.permission.has('child'));
}
// When a permission is set by cli, the process shouldn't be able
// to spawn unless --allow-child-process is sent
{
// doesNotThrow
childProcess.spawnSync(process.execPath, ['--version']);
childProcess.execSync(...common.escapePOSIXShell`"${process.execPath}" --version`);
childProcess.execFileSync(process.execPath, ['--version']);
// Guarantee permission model flags are inherited
const child = childProcess.fork(__filename, ['child']);
child.on('close', common.mustCall());
const { status } = childProcess.spawnSync(process.execPath, [__filename, 'child']);
assert.strictEqual(status, 0);
}