审核模式
🌐 Audit Mode
--permission-audit 标志启用了权限模型的审核模式。在审核模式下,会执行权限检查,但访问不会被拒绝——也不会抛出 ERR_ACCESS_DENIED 错误。相反,每个权限违规都会通过 node:diagnostics_channel 模块发布,允许应用观察并记录在执行强制模式下哪些操作会被拒绝。执行会照常继续。
🌐 The --permission-audit flag enables audit mode for the Permission Model.
In audit mode, permission checks are performed but access is not denied —
no ERR_ACCESS_DENIED error is thrown. Instead, each permission violation is
published through the node:diagnostics_channel module, allowing the
application to observe and log which operations would be denied under enforce
mode. Execution continues normally.
审核模式对于在使用 --permission 部署之前发现你的应用需要哪些权限非常有用。它还可以与 --allow-fs-read、--allow-fs-write、--allow-child-process、--allow-worker、--allow-addons 和 --allow-wasi 标志结合使用,以在授予其他权限的同时审计部分权限。
🌐 Audit mode is useful for discovering what permissions your application
requires before deploying with --permission. It can also be combined
with the --allow-fs-read, --allow-fs-write,
--allow-child-process, --allow-worker, --allow-addons, and
--allow-wasi flags to audit a subset of permissions while granting
others.
当审计模式下权限检查失败时,会向对应被拒绝范围的诊断通道发布一条消息。通道名称如下:
🌐 When a permission check fails in audit mode, a message is published to the diagnostics channel corresponding to the denied scope. The channel names are:
node:permission-model:fs— 文件系统(读写)node:permission-model:child— 子进程node:permission-model:worker— 工作线程node:permission-model:inspector— 检查器node:permission-model:wasi— WASInode:permission-model:addon— 原生插件
每条消息都是一个具有以下属性的对象:
🌐 Each message is an object with the following properties:
const diagnostics_channel = require('node:diagnostics_channel');
diagnostics_channel.channel('node:permission-model:fs').subscribe((msg) => {
console.log(`Permission denied: ${msg.permission} on ${msg.resource}`);
});
// Running with --permission-audit, this publishes a diagnostics channel
// message but does not throw
const fs = require('node:fs');
fs.readFileSync('/etc/passwd'); 如果同时指定了 --permission 和 --permission-audit,--permission 优先,权限模型将以强制模式运行。
🌐 If both --permission and --permission-audit are specified,
--permission takes precedence and the Permission Model runs in enforce mode.