Skip to content

Commit d1f3d0a

Browse files
authored
permission: add unique warning codes
Adds unique warning codes of the form PERM0000 for all permissions related SecurityWarnings, so that they can be individually silenced if required. Fixes: #59818 Signed-off-by: David Evans <davidje13@users.noreply.github.com> PR-URL: #64414 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 4b8cee0 commit d1f3d0a

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

lib/internal/process/pre_execution.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -691,20 +691,18 @@ function initializePermission() {
691691
ObjectFreeze(require('path'));
692692
const { has, drop } = require('internal/process/permission');
693693
const warnFlags = [
694-
'--allow-addons',
695-
'--allow-child-process',
696-
'--allow-inspector',
697-
'--allow-wasi',
698-
'--allow-worker',
694+
{ flag: '--allow-addons', enabled: true, code: 'PERM0001' },
695+
{ flag: '--allow-child-process', enabled: true, code: 'PERM0002' },
696+
{ flag: '--allow-ffi', enabled: process.config.variables.node_use_ffi, code: 'PERM0003' },
697+
{ flag: '--allow-inspector', enabled: true, code: 'PERM0004' },
698+
{ flag: '--allow-wasi', enabled: true, code: 'PERM0005' },
699+
{ flag: '--allow-worker', enabled: true, code: 'PERM0006' },
699700
];
700-
if (process.config.variables.node_use_ffi) {
701-
warnFlags.splice(2, 0, '--allow-ffi');
702-
}
703-
for (const flag of warnFlags) {
704-
if (getOptionValue(flag)) {
701+
for (const { flag, enabled, code } of warnFlags) {
702+
if (enabled && getOptionValue(flag)) {
705703
process.emitWarning(
706704
`The flag ${flag} must be used with extreme caution. ` +
707-
'It could invalidate the permission model.', 'SecurityWarning');
705+
'It could invalidate the permission model.', 'SecurityWarning', code);
708706
}
709707
}
710708
const warnCommaFlags = [

test/parallel/test-permission-warning-flags.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ for (const flag of warnFlags) {
2525
]
2626
);
2727

28-
assert.match(stderr.toString(), new RegExp(`SecurityWarning: The flag ${RegExp.escape(flag)} must be used with extreme caution`));
28+
assert.match(stderr.toString(), new RegExp(`\\[PERM\\d{4}\\] SecurityWarning: The flag ${RegExp.escape(flag)} must be used with extreme caution`));
2929
assert.strictEqual(status, 0);
3030
}
31+
32+
const { status, stderr } = spawnSync(
33+
process.execPath,
34+
[
35+
'--permission', '--allow-child-process', '--allow-wasi', '--disable-warning=PERM0002', '-e',
36+
'setTimeout(() => {}, 1)',
37+
]
38+
);
39+
40+
// Disabled warning does not appear
41+
assert.doesNotMatch(stderr.toString(), new RegExp(`The flag --allow-child-process must be used with extreme caution`));
42+
// But non-disabled warnings still appear
43+
assert.match(stderr.toString(), new RegExp(`The flag --allow-wasi must be used with extreme caution`));
44+
assert.strictEqual(status, 0);

0 commit comments

Comments
 (0)