Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Sandbox?

Tags:

javascript

Would it be possible to sandbox user-submitted Javascript by overriding various functions such as alert, window.location, and eval?

I'm not looking for a perfect solution. I'm sure some people would still find a way to rearrange divs to spell out swear words or something malicious, but if I could disable page redirects 100% reliably I would be mostly happy.

I tried in Chrome, and doing something like

context={}; //use this to prevent `this` from being `window`
context.f=function(){
  var window=null,location=null,eval=function(){};
  console.log(window); //also the other two
};
context.f();

seems promising. If I replace the console line with user-submitted code (checking for paren balancing), would that be an absurdly bad idea or a mildly bad idea? On Chrome I can still break things by going through this to Function and redefining things, but that would be acceptable to me.

like image 716
Loyal Tingley Avatar asked Feb 18 '11 17:02

Loyal Tingley


1 Answers

You can use Microsoft Web Sandbox or Google Caja.

like image 193
SLaks Avatar answered Oct 20 '22 05:10

SLaks