views:

65

answers:

7

I have an html page with a textfield for the user to put expressions like these:

(x+23)*2
((x-y)*(x+y))
x*2/z+y

The user enters them and I use the 'eval' method to execute them. Before calling eval I make a check that there is nothing dodgy (like attempting to define a function or similar).

It seems to me that I should be fine. But am I introducing a security hole because I call 'eval' on user's string. what's the risk ?

A: 

All the user can do is to evaluate stuff in the scope of this page. If the user types in horrible script, what can it do to anyone except the user him/herself?

There is nothing the user can eval in that field that could not also be put in the location bar as a bookmarklet - all is run in the scope of the client browser.

mplungjan
so am I right ? nothing to worry about ?
Lx1
Unless you send the code somewhere else or someone else can give a url to your page where the code in the url is eval'd then I would not worry at all here. As soon as you save the code for example on a webpage with examples, the code could execute on the browser of the person who loads the page next.
mplungjan
+1  A: 

The client can call javascript on its client anyway with the help of browser plugins and javascript debugging tools. It would another thing if you'd attempt to run userdefined code on the server, that would be very risky.

codymanix
nothing on the serverside. I am just evaling his textfield.is there any risk ?
Lx1
As I said, the user is able to do anything on his machine anyway so you are not adding additional risk.
codymanix
A: 

If you happen to have jQuery installed something like this may happen if not checked:

$.getScript("test.js");

http://api.jquery.com/jQuery.getScript/

Im0rtality
same asjavscript:void($.getScript("test.js")) from the location bar
mplungjan
and so what ? what'd happen ?
Lx1
same as if the user had pasted it into your textarea and had it eval'd. jQuery would load the script
mplungjan
A: 

Presumably you are filtering the string the user provides. However there is a risk that there is a sneaky way to accomplish harm that you have overlooked.

gnibbler
like what ? assume I let any dodgy script... so what ?
Lx1
+1  A: 

The main thing to worry about is if they can form a URL and send it to someone and then have the eval be performed on another machine by clicking the URL. This would be possible if your form uses GET or even if you don't distinguish between GET/POST when you evaluate the form.

There are other things you can do to be even more sure.

Lou Franco
+1  A: 

If you are only evaling a user's code to that user on that page then you are fine. You start to get security problems when take user entered strings and eval them on other user's visits. If you aren't doing this, then there is no security hole at all. Anyone can Eval Javascript on a page they are visiting, you can't stop them.

Jake
you said:Anyone can Eval Javascript on a page they are visiting, you can't stop them.How do you mean ?
Lx1
javascript:window.alert(document.location)
tc.
sorry I don't get you. what's that ?
Lx1
It is a bookmarklet. Paste it into the location bar and the script will execute in the scope of your web page
mplungjan
A: 

What do you mean by "I make a check that there is nothing dodgy"? Blacklisting certain keywords doesn't work. For example,

eval("func"+"tion () { window.alert('haha'); }()");

As Lou said, you have to be worried when you add functionality to the page. If you add a "share this" button which makes a link to http://example.com/mypage?expr=x-1, it wouldn't be difficult to trick an unsuspecting user to click a link which stole cookies.

I'm pretty sure you can find JavaScript sandboxing out there somewhere.

tc.