Skip to content

Instantly share code, notes, and snippets.

@hackvertor
Last active August 29, 2015 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hackvertor/c2fec87e60a0a5b751a7 to your computer and use it in GitHub Desktop.
Save hackvertor/c2fec87e60a0a5b751a7 to your computer and use it in GitHub Desktop.
function SOMEGuard(callbackStr) {
var o = [], flag = true, i, callbackObj;
callbackStr = callbackStr.replace(/[^\w.]/gi,'').split('.');
for(i=0;i<callbackStr.length;i++) {
o.push(callbackStr[i]);
try {
callbackObj = Function("return window."+o.join('.'))();
if(callbackObj === window.opener) {
flag = false;
break;
}
if(window.HTMLElement) {
if(callbackObj instanceof HTMLElement) {
flag = false;
break;
}
}
if(window.Node) {
if(callbackObj instanceof Node) {
flag = false;
break;
}
}
if(typeof callbackObj === 'object' && typeof callbackObj.nodeType === 'number' && typeof callbackObj.nodeName === 'string') {
flag = false;
break;
}
if(callbackStr[i] === 'returnValue') {
flag = false;
break;
}
} catch(e){
flag = false;
}
}
return flag;
}
@hackvertor
Copy link
Author

o={func:function(){alert('Called');}};
if(SOMEGuard('o.func')) {
  o.func();
  alert('Allowed');
} else {
  alert('Denied');
}   
if(SOMEGuard('document.body')) {
  alert('Allowed');
} else {
  alert('Denied');
}   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment