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?
eval
to get your app's cookies (like user session tokens, which would allow them to impersonate the logged-in user), any state held in memory on your client and make requests to get further state from your server. It could post any data to another server. In short, they could own your application. – Donaufiddle.jshell.net
instead ofjsfiddle.net
), so it has no access to the jsfiddle application. – Orrin