process.permission.drop(scope[, reference])
从当前进程中移除指定的权限。这个操作是不可逆的——一旦权限被移除,就不能通过任何 Node.js API 恢复。
🌐 Drops the specified permission from the current process. This operation is irreversible — once a permission is dropped, it cannot be restored through any Node.js API.
在审计模式(--permission-audit)下,撤销权限会生效,但由于被拒绝的操作不会抛出异常,其影响仅限于改变 permission.has() 的返回值。
🌐 In audit mode (--permission-audit), dropping a permission takes effect,
but since denied operations do not throw, the impact is limited to changing the
return value of permission.has().
如果没有提供参考,整个范围都会被撤销。例如,process.permission.drop('fs.read') 会撤销所有文件系统的读取权限。
🌐 If no reference is provided, the entire scope is dropped. For example,
process.permission.drop('fs.read') will revoke ALL file system read
permissions.
当提供引用时,只有那个特定资源的权限会被取消。例如,process.permission.drop('fs.read', '/etc/myapp') 会撤销对该目录的读取权限,同时保持其他读取权限不变。
🌐 When a reference is provided, only the permission for that specific resource
is dropped. For example, process.permission.drop('fs.read', '/etc/myapp')
will revoke read access to that directory while keeping other read
permissions intact.
重要提示: 你只能释放明确授予的确切资源。传给 drop() 的引用必须与原始授予匹配:
- 如果使用通配符(
*)授予了权限,比如--allow-fs-read=*,单独的路径无法被移除——只能整个范围一起移除(通过在没有引用的情况下调用drop())。 - 如果某个目录被授予访问权限(例如
--allow-fs-read=/my/folder),你无法取消对其中单个文件的访问权限。你必须取消同样被授予的目录权限。其他剩余的授权仍然有效。
可用的范围和process.permission.has()一样:
🌐 The available scopes are the same as process.permission.has():
fs- 所有文件系统(同时丢弃读写)fs.read- 文件系统读取操作fs.write- 文件系统写入操作child- 子进程生成操作worker- 工作线程生成操作inspector- 检查器操作wasi- WASI 操作addon- 本地插件操作
const fs = require('node:fs');
// Read configuration during startup
const config = fs.readFileSync('/etc/myapp/config.json', 'utf8');
// Drop read access to the config directory after initialization
process.permission.drop('fs.read', '/etc/myapp');
// This will now throw ERR_ACCESS_DENIED
fs.readFileSync('/etc/myapp/config.json');