Skip to content

Instantly share code, notes, and snippets.

@d0nutptr
Created May 31, 2018 15:20
Show Gist options
  • Save d0nutptr/19470942836ea8836f15175722e95990 to your computer and use it in GitHub Desktop.
Save d0nutptr/19470942836ea8836f15175722e95990 to your computer and use it in GitHub Desktop.
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