-
-
Save hongymagic/2862221 to your computer and use it in GitHub Desktop.
Masking eval, extracting FunctionBody – not really sure where it would be used.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extract FunctionBody as eval expects FunctionBody | |
Function.prototype.getBody = function() { | |
var m = this.toString().match(/\{([\s\S]*)\}/m)[1]; | |
return m.replace(/^\s*\/\/.*$/mg,''); | |
}; | |
var GLOBAL = "GLOBAL"; | |
function foo () { | |
console.log(GLOBAL, this); | |
} | |
function bar () { | |
setTimeout(function () { | |
console.log(GLOBAL, this); | |
}, 0); | |
} | |
function hack () { | |
console.log(window); | |
} | |
foo(); // "GLOBAL" | |
bar(); // "GLOBAL" | |
// This function does not restrict access to `window` | |
function mask (func) { | |
"use strict"; | |
var GLOBAL = 5; | |
eval(func.getBody()); | |
} | |
mask(foo); // 5 | |
mask(bar); // 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment