I have a window containing an iframe (same origin), so scripts from this iframe can access the top window’s attributes by simply referencing top.foo. I want to grant access to some of these attributes, and hide others via blacklist.
This is what I have so far:
(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;
},
});
})();
The basic idea behind this is that f.caller instanceof Function should detect calls from foreign window objects, since window1.Function !== window2.Function.
But this does not work if the accessors are called from top-level code, where f.caller === null. Any solutions?
For now, I’ve decided to go with the following approach, since I don’t think it’s possible to detect top-level calls:
If anyone comes up with a better solution, please let me know!