Skip to content

Commit 240a86d

Browse files
davidje13aduh95
authored andcommitted
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 f2a05ee commit 240a86d

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
@@ -681,20 +681,18 @@ function initializePermission() {
681681
ObjectFreeze(require('path'));
682682
const { has, drop } = require('internal/process/permission');
683683
const warnFlags = [
684-
'--allow-addons',
685-
'--allow-child-process',
686-
'--allow-inspector',
687-
'--allow-wasi',
688-
'--allow-worker',
684+
{ flag: '--allow-addons', enabled: true, code: 'PERM0001' },
685+
{ flag: '--allow-child-process', enabled: true, code: 'PERM0002' },
686+
{ flag: '--allow-ffi', enabled: process.config.variables.node_use_ffi, code: 'PERM0003' },
687+
{ flag: '--allow-inspector', enabled: true, code: 'PERM0004' },
688+
{ flag: '--allow-wasi', enabled: true, code: 'PERM0005' },
689+
{ flag: '--allow-worker', enabled: true, code: 'PERM0006' },
689690
];
690-
if (process.config.variables.node_use_ffi) {
691-
warnFlags.splice(2, 0, '--allow-ffi');
692-
}
693-
for (const flag of warnFlags) {
694-
if (getOptionValue(flag)) {
691+
for (const { flag, enabled, code } of warnFlags) {
692+
if (enabled && getOptionValue(flag)) {
695693
process.emitWarning(
696694
`The flag ${flag} must be used with extreme caution. ` +
697-
'It could invalidate the permission model.', 'SecurityWarning');
695+
'It could invalidate the permission model.', 'SecurityWarning', code);
698696
}
699697
}
700698
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)