我如何 运行 在 VM 中编写代码,同时限制对除指定全局变量之外的所有全局变量的访问

How do I run code in a VM while restricting access to all globals except the ones specified

如果我在字符串中有一些代码,我如何运行它而不暴露任何全局变量。

vm 模块似乎公开了内置函数:

const vm = require("vm");

vm.runInNewContext("console", {}); // No error, outputs an object
                                   // Expected it to error, since we are passing an empty context

不是 this question 的副本,其解决方案对我不起作用(在 Node v10.5.0 中)。

看起来像console

如果您尝试其他全局变量,如 requireprocess__dirname__filename 等,它们都会导致抛出错误。