Created
May 31, 2018 15:21
-
-
Save d0nutptr/12e0beaf4b84d19ca50f882d2a9305ef to your computer and use it in GitHub Desktop.
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
function jail(code) { | |
// quick string escape for inner strings | |
code = code.replace(/["'`\\]/g, function(v){ return `\\${v}`}); | |
var jail_script = "new Function("; | |
// Blacklist all global scope values | |
for(prop in window) { | |
jail_script += `"${prop}", `; | |
} | |
// Disable eval/Function | |
jail_script += `"eval", `; | |
jail_script += `"Function.prototype.constructor = null; Function = null; ${code}");`; | |
// Give us the Function object to make a call on. | |
var jail_internal = eval(jail_script); | |
jail_internal.call() | |
} | |
// jail("alert(1)") // This will error since alert doesn't exist anymore | |
// jail("console.log(1 + 1)") // Log output of '2' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment