diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index 63c1b718b45..a7ff17a6467 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -643,17 +643,17 @@ function initializePermission() { ObjectFreeze(require('path')); const { has, drop } = require('internal/process/permission'); const warnFlags = [ - '--allow-addons', - '--allow-child-process', - '--allow-inspector', - '--allow-wasi', - '--allow-worker', + { flag: '--allow-addons', enabled: true, code: 'PERM0001' }, + { flag: '--allow-child-process', enabled: true, code: 'PERM0002' }, + { flag: '--allow-inspector', enabled: true, code: 'PERM0004' }, + { flag: '--allow-wasi', enabled: true, code: 'PERM0005' }, + { flag: '--allow-worker', enabled: true, code: 'PERM0006' }, ]; - for (const flag of warnFlags) { - if (getOptionValue(flag)) { + for (const { flag, enabled, code } of warnFlags) { + if (enabled && getOptionValue(flag)) { process.emitWarning( `The flag ${flag} must be used with extreme caution. ` + - 'It could invalidate the permission model.', 'SecurityWarning'); + 'It could invalidate the permission model.', 'SecurityWarning', code); } } const warnCommaFlags = [ diff --git a/test/parallel/test-permission-warning-flags.js b/test/parallel/test-permission-warning-flags.js index 450597942c6..eeb00ecd351 100644 --- a/test/parallel/test-permission-warning-flags.js +++ b/test/parallel/test-permission-warning-flags.js @@ -21,6 +21,20 @@ for (const flag of warnFlags) { ] ); - assert.match(stderr.toString(), new RegExp(`SecurityWarning: The flag ${RegExp.escape(flag)} must be used with extreme caution`)); + assert.match(stderr.toString(), new RegExp(`\\[PERM\\d{4}\\] SecurityWarning: The flag ${RegExp.escape(flag)} must be used with extreme caution`)); assert.strictEqual(status, 0); } + +const { status, stderr } = spawnSync( + process.execPath, + [ + '--permission', '--allow-child-process', '--allow-wasi', '--disable-warning=PERM0002', '-e', + 'setTimeout(() => {}, 1)', + ] +); + +// Disabled warning does not appear +assert.doesNotMatch(stderr.toString(), new RegExp(`The flag --allow-child-process must be used with extreme caution`)); +// But non-disabled warnings still appear +assert.match(stderr.toString(), new RegExp(`The flag --allow-wasi must be used with extreme caution`)); +assert.strictEqual(status, 0);