Javascript 大厦评估及维修&;新功能

Javascript 大厦评估及维修&;新功能,javascript,node.js,Javascript,Node.js,我正在codewars.com上写一些随机拼图,我很好奇是否有人能想出一种在运行以下代码后评估代码的方法: eval = function(){}; delete Function.prototype.constructor; Function = undefined; // the following are to block require('vm') -- if anyone wants to run this // in production it may be better to b

我正在codewars.com上写一些随机拼图,我很好奇是否有人能想出一种在运行以下代码后评估代码的方法:

eval = function(){};
delete Function.prototype.constructor;
Function = undefined;

// the following are to block require('vm') -- if anyone wants to run this
// in production it may be better to block that one module (others?)
require = undefined;
module.__proto__.require = undefined; // added this due to alexpod's answer, modified due to Fabrício Matté's :)
module.constructor = undefined; // added this due to alexpod's answer

这在node.js中,因此
setTimeout(“string”)
不起作用。

嗯,在
node
中还有
module
变量。因此,您可以要求使用
vm
包,并使用其
require
方法运行代码:

var vm = module.require('vm');
vm.runInThisContext(' console.log("hello") ');
UPD 你更新了问题,但我们可以再次破解:

var vm = module.constructor.prototype.require('vm');
vm.runInThisContext(' console.log("hello") ');
UPD2 另一种变体:

var vm = module.constructor._load('vm');
vm.runInThisContext(' console.log("hello") ');
UPD3 再次更改条件,以便下一个变体:

module.constructor.prototype._compile(' console.log("again hacked") ');
// or
module.__proto__._compile(' console.log("again hacked") ');
// or
Object.getPrototypeOf(module)._compile(' console.log("again hacked") ');
我认为最好设置
module=undefined
使问题更复杂:)

UPD4 还有另一个没有
模块的变体
:)

但它只在CLI(“repl”)中工作

UPD5 同样,在版本>=0.11.x的
iojs
节点中,您可以使用
contextify
绑定:

var contextify = process.binding('contextify');
var script = new contextify.ContextifyScript(' console.log("im here, buddy") ');
script.runInThisContext();
var evals = process.binding('evals');
var script = new evals.NodeScript(' console.log("here I am") ')
script.runInThisContext();
在版本为<0.11.x的
节点中,可以使用
evals
绑定:

var contextify = process.binding('contextify');
var script = new contextify.ContextifyScript(' console.log("im here, buddy") ');
script.runInThisContext();
var evals = process.binding('evals');
var script = new evals.NodeScript(' console.log("here I am") ')
script.runInThisContext();

module.require=未定义是不够的,因为
require
继承自模块原型:

module.require = undefined;

var vm = module.__proto__.require('vm');
vm.runInThisContext('console.log(1)');
相反,你应该:

module.__proto__.require = undefined;
// now this fails and you can't use the __proto__ trick:
var vm = module.require('vm');
使用构造函数:

(function*(){}).constructor('console.log(1);')().next().value;

您可能会更成功地将此发布在编码挑战网站上。我想这是为了安全措施?在这种情况下,正如你的问题所暗示的那样,这是一个黑名单。@Barmar我以为这是一个编码挑战网站?@guest271314——这与什么有关系?你有权访问
文档吗?如果您这样做了,那么您可以使用
var script=document.createElement(“脚本”);script.innerHTML=“您的代码”;document.body.appendChild(脚本)
.Upvoting,稍后将接受,因为这是第一个正确答案:)但仍在等待更多答案:D@zyklus你改变了问题的条件:)是的,因为我想要尽可能多的答案。你的回答是正确的,我不反对:)@zyklus在问题中添加
module=undefined
,做得更多complex@zyklus哦,没注意到法布里西奥的回答