I need to execute javascript code that is written by users. Of course I must assume the javascript to be malicious. I have a global object in the page which the scripts must interact with, but I don't wan the script to be able to access anything else including the DOM, jQuery, and the window object.
Would it be possible to modify incoming javascript to strip out anything that I have not explicitly white listed?
For example:
function modField(){
if(!f.alpha.enabled){
f.main.enabled = /960/.test(f.productName.text);
f.name = document.getElementById('#username');
}
}
Would become after cleaning:
function modField(){
if(!f.alpha.enabled){
f.main.enabled = /960/.test(f.productName.text);
}
}
How do I do this?