|
| 1 | +// Flags: --permission --allow-child-process --allow-fs-read=* --allow-worker |
| 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 | + |
| 11 | +const assert = require('assert'); |
| 12 | +const childProcess = require('child_process'); |
| 13 | + |
| 14 | +{ |
| 15 | + assert.ok(process.permission.has('child')); |
| 16 | +} |
| 17 | + |
| 18 | +{ |
| 19 | + assert.strictEqual(process.env.NODE_OPTIONS, undefined); |
| 20 | +} |
| 21 | + |
| 22 | +{ |
| 23 | + const { status, stdout } = childProcess.spawnSync(process.execPath, |
| 24 | + [ |
| 25 | + '-e', |
| 26 | + ` |
| 27 | + console.log(process.permission.has("fs.write")); |
| 28 | + console.log(process.permission.has("fs.read")); |
| 29 | + console.log(process.permission.has("child")); |
| 30 | + console.log(process.permission.has("net")); |
| 31 | + console.log(process.permission.has("worker")); |
| 32 | + `, |
| 33 | + ] |
| 34 | + ); |
| 35 | + const [fsWrite, fsRead, child, net, worker] = stdout.toString().split('\n'); |
| 36 | + assert.strictEqual(status, 0); |
| 37 | + assert.strictEqual(fsWrite, 'false'); |
| 38 | + assert.strictEqual(fsRead, 'true'); |
| 39 | + assert.strictEqual(child, 'true'); |
| 40 | + assert.strictEqual(net, 'false'); |
| 41 | + assert.strictEqual(worker, 'true'); |
| 42 | +} |
| 43 | + |
| 44 | +// It should not override when --permission is passed |
| 45 | +{ |
| 46 | + const { status, stdout } = childProcess.spawnSync( |
| 47 | + process.execPath, |
| 48 | + [ |
| 49 | + '--permission', |
| 50 | + '--allow-fs-write=*', |
| 51 | + '-e', |
| 52 | + ` |
| 53 | + console.log(process.permission.has("fs.write")); |
| 54 | + console.log(process.permission.has("fs.read")); |
| 55 | + console.log(process.permission.has("child")); |
| 56 | + console.log(process.permission.has("net")); |
| 57 | + console.log(process.permission.has("worker")); |
| 58 | + `, |
| 59 | + ] |
| 60 | + ); |
| 61 | + const [fsWrite, fsRead, child, net, worker] = stdout.toString().split('\n'); |
| 62 | + assert.strictEqual(status, 0); |
| 63 | + assert.strictEqual(fsWrite, 'true'); |
| 64 | + assert.strictEqual(fsRead, 'false'); |
| 65 | + assert.strictEqual(child, 'false'); |
| 66 | + assert.strictEqual(net, 'false'); |
| 67 | + assert.strictEqual(worker, 'false'); |
| 68 | +} |
| 69 | + |
| 70 | +// It should not override when NODE_OPTIONS with --permission is passed |
| 71 | +{ |
| 72 | + const { status, stdout } = childProcess.spawnSync( |
| 73 | + process.execPath, |
| 74 | + [ |
| 75 | + '-e', |
| 76 | + ` |
| 77 | + console.log(process.permission.has("fs.write")); |
| 78 | + console.log(process.permission.has("fs.read")); |
| 79 | + console.log(process.permission.has("child")); |
| 80 | + console.log(process.permission.has("net")); |
| 81 | + console.log(process.permission.has("worker")); |
| 82 | + `, |
| 83 | + ], |
| 84 | + { |
| 85 | + env: { |
| 86 | + ...process.env, |
| 87 | + 'NODE_OPTIONS': '--permission --allow-fs-write=*', |
| 88 | + } |
| 89 | + } |
| 90 | + ); |
| 91 | + const [fsWrite, fsRead, child, net, worker] = stdout.toString().split('\n'); |
| 92 | + assert.strictEqual(status, 0); |
| 93 | + assert.strictEqual(fsWrite, 'true'); |
| 94 | + assert.strictEqual(fsRead, 'false'); |
| 95 | + assert.strictEqual(child, 'false'); |
| 96 | + assert.strictEqual(net, 'false'); |
| 97 | + assert.strictEqual(worker, 'false'); |
| 98 | +} |
| 99 | + |
| 100 | +{ |
| 101 | + assert.strictEqual(process.env.NODE_OPTIONS, undefined); |
| 102 | +} |
0 commit comments