我有一个包含iframe(相同来源)的窗口,因此来自此iframe的脚本可以通过简单引用top.foo
来访问顶层窗口的属性。我想授予对这些属性的访问权限,并通过黑名单隐藏其他属性。从iframe中的脚本隐藏对象的某些属性
这是我到目前为止有:
(function(){
var private = PrivateObject;
Object.defineProperty(window, 'PrivateObject', {
get: function getter() {
if (!(getter.caller instanceof Function)) {
throw 'You can\'t access PrivateObject from the iframe';
}
return private;
},
set: function setter(x) {
if (!(setter.caller instanceof Function)) {
throw 'You can\'t access PrivateObject from the iframe';
}
private = x;
},
});
})();
这背后的基本理念是,f.caller instanceof Function
应检测外来窗口对象的调用,因为window1.Function !== window2.Function
。
但是这does not work如果访问器是从顶级代码调用,其中f.caller === null
。任何解决方案
对不起,如果是天真的,但如果.caller为null不会从顶级代码工作?异常不会被抛出? – dave 2012-03-12 19:31:10
问题是,用这种方法你会得到误报和误报。所以我正在寻找一种解决方案,允许从父级的顶级代码访问,同时阻止访问iframe的顶级代码。 – user123444555621 2012-03-12 20:50:03