I need to run a Javascript function that is completely written by the user. I provide him a skeleton, but the details are for him to specify, e.g.
function main(model, console) {
// the user can enter anything here
// ideally, he would only be allowed to
// use the methods that "model" and "console" provide, e.g.
var item = model.getItems();
console.log("Found " + item.length + " items.");
}
For the application to work, the user only needs to access methods and properties of the parameters (he explicitely doesn't require document or window access or send XMLHttpRequests
).
I have already read several articles about the eval() function and how you can use it to run code. I also read other articles on StackOverflow (how jsFiddle runs code, eval in general, etc.), but I'm still not sure how to do it properly.
First of all: what are the real issues of eval()
? What can an attacker do and how can you prevent it (with whitelists, blacklists or user input sanitizing libraries)?
Can anyone explain in depth how jsFiddle and such websites execute user input?