-
-
Save aemkei/3847852 to your computer and use it in GitHub Desktop.
Is it possible to sandbox JS code
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 sandbox(script, context){ | |
context.window = {}; | |
for (var key in context){ | |
context.window[key] = context[key]; | |
} | |
context.global = context.window; | |
eval("with (context){~function(){'use strict';" + script + "}()}"); | |
} | |
// ~115 bytes: | |
// function(e,t,n,r){r={};for(n in t)r[n]=t[n];t.window=t.global=r,eval("with(t)~function(){'use strict';"+e+"}()")} | |
var NOT_ALLOWED = function(name){ | |
return function(){ | |
console.warn(name + "(); is not allowed."); | |
return function(){}; | |
}; | |
}; | |
var scope = { | |
"alert": function(message){ console.log(message); }, | |
"Function": NOT_ALLOWED("Function"), | |
"eval": NOT_ALLOWED("eval") | |
}; | |
function test(script){ | |
try { | |
sandbox(script, scope); | |
} catch (e) { | |
console.error(e); | |
} | |
} | |
var samples = [ | |
"alert('good try');", | |
"global.alert('1');", | |
"window.alert('2');", | |
"eval('alert(3)');", | |
"~new Function('alert(4)')();", | |
"~function(){this.alert(5)}()", | |
"(function(){this.eval('good try');}).apply(null)", | |
"(function(){return this;})().alert(6)" | |
]; | |
samples.forEach(test); |
test('(function*(){}).constructor("eval('alert(1337)')")().next()');
test('}}eval("alert(1337)");{+function(){');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This one break your jail:
'new (function(){}).constructor('alert("good try")')()'