--allow-child-process


稳定性: 1.1 - 积极开发

¥Stability: 1.1 - Active development

使用 权限模型 时,默认情况下该进程将无法生成任何子进程。尝试这样做将抛出 ERR_ACCESS_DENIED,除非用户在启动 Node.js 时明确传递 --allow-child-process 标志。

¥When using the Permission Model, the process will not be able to spawn any child process by default. Attempts to do so will throw an ERR_ACCESS_DENIED unless the user explicitly passes the --allow-child-process flag when starting Node.js.

示例:

¥Example:

const childProcess = require('node:child_process');
// Attempt to bypass the permission
childProcess.spawn('node', ['-e', 'require("fs").writeFileSync("/new-file", "example")']); 
$ node --permission --allow-fs-read=* index.js
node:internal/child_process:388
  const err = this._handle.spawn(options);
                           ^
Error: Access to this API has been restricted
    at ChildProcess.spawn (node:internal/child_process:388:28)
    at node:internal/main/run_main_module:17:47 {
  code: 'ERR_ACCESS_DENIED',
  permission: 'ChildProcess'
} 

child_process.spawn 不同,child_process.fork API 从父进程复制执行参数。这意味着,如果你在启用权限模型的情况下启动 Node.js 并包含 --allow-child-process 标志,则调用 child_process.fork() 会将所有权限模型标志传播到子进程。

¥Unlike child_process.spawn, the child_process.fork API copies the execution arguments from the parent process. This means that if you start Node.js with the Permission Model enabled and include the --allow-child-process flag, calling child_process.fork() will propagate all Permission Model flags to the child process.