|
| 1 | +// Flags: --permission-audit --allow-child-process --allow-fs-read=* --allow-fs-write=* |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +const { isMainThread } = require('worker_threads'); |
| 6 | + |
| 7 | +if (!isMainThread) { |
| 8 | + common.skip('This test only works on a main thread'); |
| 9 | +} |
| 10 | +if (process.config.variables.node_without_node_options) { |
| 11 | + common.skip('missing NODE_OPTIONS support'); |
| 12 | +} |
| 13 | + |
| 14 | +const assert = require('assert'); |
| 15 | +const childProcess = require('child_process'); |
| 16 | + |
| 17 | +// Verify that the parent is running in audit mode |
| 18 | +assert.strictEqual(typeof process.permission.has, 'function'); |
| 19 | + |
| 20 | +{ |
| 21 | + assert.strictEqual(process.env.NODE_OPTIONS, undefined); |
| 22 | +} |
| 23 | + |
| 24 | +// Child should inherit --permission-audit and the allow-flags via NODE_OPTIONS |
| 25 | +{ |
| 26 | + const { status, stdout, stderr } = childProcess.spawnSync(process.execPath, |
| 27 | + [ |
| 28 | + '-e', |
| 29 | + ` |
| 30 | + console.log(typeof process.permission); |
| 31 | + console.log(process.permission.has("fs.write")); |
| 32 | + console.log(process.permission.has("fs.read")); |
| 33 | + console.log(process.permission.has("child")); |
| 34 | + `, |
| 35 | + ] |
| 36 | + ); |
| 37 | + assert.strictEqual(status, 0, `stderr: ${stderr}`); |
| 38 | + const [permType, fsWrite, fsRead, child] = stdout.toString().split('\n'); |
| 39 | + assert.strictEqual(permType, 'object', 'child should have process.permission defined'); |
| 40 | + assert.strictEqual(fsWrite, 'true'); |
| 41 | + assert.strictEqual(fsRead, 'true'); |
| 42 | + assert.strictEqual(child, 'true'); |
| 43 | +} |
| 44 | + |
| 45 | +// Child spawned with explicit --permission should use its own flags, not inherit parent's |
| 46 | +{ |
| 47 | + const { status, stdout, stderr } = childProcess.spawnSync( |
| 48 | + process.execPath, |
| 49 | + [ |
| 50 | + '--permission', |
| 51 | + '--allow-fs-write=*', |
| 52 | + '-e', |
| 53 | + ` |
| 54 | + console.log(typeof process.permission); |
| 55 | + console.log(process.permission.has("fs.write")); |
| 56 | + console.log(process.permission.has("fs.read")); |
| 57 | + console.log(process.permission.has("child")); |
| 58 | + `, |
| 59 | + ] |
| 60 | + ); |
| 61 | + assert.strictEqual(status, 0, `stderr: ${stderr}`); |
| 62 | + const [permType, fsWrite, fsRead, child] = stdout.toString().split('\n'); |
| 63 | + assert.strictEqual(permType, 'object'); |
| 64 | + assert.strictEqual(fsWrite, 'true'); |
| 65 | + assert.strictEqual(fsRead, 'false'); |
| 66 | + assert.strictEqual(child, 'false'); |
| 67 | +} |
| 68 | + |
| 69 | +// Child spawned with explicit --permission-audit should use its own flags |
| 70 | +{ |
| 71 | + const { status, stdout, stderr } = childProcess.spawnSync( |
| 72 | + process.execPath, |
| 73 | + [ |
| 74 | + '--permission-audit', |
| 75 | + '--allow-fs-write=*', |
| 76 | + '-e', |
| 77 | + ` |
| 78 | + console.log(typeof process.permission); |
| 79 | + console.log(process.permission.has("fs.write")); |
| 80 | + console.log(process.permission.has("fs.read")); |
| 81 | + `, |
| 82 | + ] |
| 83 | + ); |
| 84 | + assert.strictEqual(status, 0, `stderr: ${stderr}`); |
| 85 | + const [permType, fsWrite, fsRead] = stdout.toString().split('\n'); |
| 86 | + assert.strictEqual(permType, 'object'); |
| 87 | + assert.strictEqual(fsWrite, 'true'); |
| 88 | + // fs.read was not allowed in the explicit child flags |
| 89 | + assert.strictEqual(fsRead, 'false'); |
| 90 | +} |
| 91 | + |
| 92 | +{ |
| 93 | + assert.strictEqual(process.env.NODE_OPTIONS, undefined); |
| 94 | +} |
0 commit comments