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.
setTimeoutandsetIntervalas the first param can be a string, which will beeval'd.Object,Number, especiallyFunction, and basically all the properties of the global object (window). If you 'isolate' the user scripts in an IFrame, make sure they can't access the frames collection in any way.(function(){}).constructoror similar anyway?