是否可以通过覆盖诸如alert
、window.location
和eval
之类的各种函数来沙盒用户提交的Javascript?
我不是在寻找完美的解决方案。我相信有些人仍然会找到一种方法来重新排列div以拼出脏话或其他恶意内容,但如果我能够可靠地100%禁用页面重定向,我就会大部分满意。
我在Chrome中尝试过,做了一些像
context={}; //use this to prevent `this` from being `window`
context.f=function(){
var window=null,location=null,eval=function(){};
console.log(window); //also the other two
};
context.f();
看起来很有前途。如果我用用户提交的代码替换console
行(检查括号平衡),这是非常糟糕的想法还是比较糟糕的想法?在Chrome中,通过访问this
到函数并重新定义内容仍然可以破坏事情,但对我来说这是可以接受的。
Object
、Number
,特别是Function
,以及全局对象(window
)的所有属性。如果您在IFrame中“隔离”用户脚本,请确保它们无法以任何方式访问框架集合。 - Martijn(function(){}).constructor
或类似的地方访问它们吗? - Loyal Tingley