Created
May 31, 2018 15:20
-
-
Save d0nutptr/19470942836ea8836f15175722e95990 to your computer and use it in GitHub Desktop.
This file contains 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", "Function", `; | |
jail_script += `"${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