views:

5191

answers:

9

Quick Question. Eval in JavaScript is unsafe is it not? I have a JSON object as a string and I need to turn it into an actual object so I can obtain the data:

function PopulateSeriesFields(result) 
{
    data = eval('(' + result + ')');
    var myFakeExample = data.exampleType
}

If it helps I am using the $.ajax method from jQuery.

Thanks

+4  A: 

Unsafe? That depends on if you can trust the data.

If you can trust that the string will be JSON (and won't include, for example, functions) then it is safe.

That said - if you are using jQuery, why are you doing this manually? Use the dataType option to specify that it is JSON and let the library take care of it for you.

David Dorward
jQuery uses eval...
J-P
@J-P: But only if JSON is not available. http://dev.jquery.com/browser/trunk/jquery/src/ajax.js#L504
Gumbo
I have the dataType thing but it still comes back as a string. Hang on, i'll try application/JSON
Damien
No good...How odd...
Damien
It seems that JSON is just available as a plugin and not a build-in component of jQuery for those that doesn’t support native JSON.
Gumbo
+3  A: 

Using JavaScript’s eval is unsafe. Because JSON is just a subset of JavaScript but JavaScript’s eval allows any valid JavaScript.

Use a real JSON parser like the JSON parser from json.org instead.

Gumbo
+2  A: 

The alternative to evaluating the code is to parse it manually. It's not as hard as it sounds but it's quite a lot heavier at runtime. You can read about it here.

The important part to note is evaluating JSON is not inherently insecure. As long as you trust the source not to balls things up. That includes making sure that things passed into the JSON encoder are properly escaped (to stop people 2 steps up the stream executing code on your users' machines).

Oli
+4  A: 

If you can't trust the source, then you're correct...eval is unsafe. It could be used to inject code into your pages.

Check out this link for a safer alternative:

JSON in Javascript

The page explains why eval is unsafe and provides a link to a JSON parser at the bottom of the page.

Justin Niessner
+15  A: 

Well, safe or not, when you are using jQuery, you're better to use the $.getJSON() method, not $.ajax():

$.getJSON(url, function(data){
    alert(data.exampleType);
});

eval() is usually considered safe for JSON parsing when you are only communicating with your own server and especially when you use a good JSON library on server side that guarantees that generated JSON will not contain anything nasty.

Even Douglas Crockford, the author of JSON, said that you shouldn't use eval() anywhere in your code, except for parsing JSON. See the corresponding section in his book JavaScript: The Good Parts

Rene Saarsoo
I was writing the exact same thing +1
Pablo Fernandez
and now I see this..
Damien
"getJSON" is EXACTLY the same as using $.ajax with a dataType of 'json' ...
J-P
Sure, bug getJSON() is shorter to write.
Rene Saarsoo
@Rene - except that $.getJSON() doesn't provide nearly the flexibility that $.ajax() does. Particularly in the handling of errors, success/complete events, etc...
Goyuix
+2  A: 

I asked a similar question here:

http://stackoverflow.com/questions/646597/eval-is-evil-so-what-should-i-use-instead

m_oLogin
+6  A: 

You should use JSON and write JSON.parse.

"Manual" parsing is too slow, so JSON.parse implementation from the library checks stuff and then ends up using eval, so it is still unsafe. But, if you are using a newer browser (IE8 or Firefox), the library code is not actually executed. Instead, native browser support kicks in, and then you are safe.

Read more here and here.

buti-oxa
http://code.google.com/p/json-sans-eval/ is a manual parser that aims to be fast and secure. You are right though that the native support should be preferred where available.
Mike Samuel
A: 

Create a dynamic script tag and place the javascript/JSON string inside it.

Referance:

http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html

Jineesh
This has the same security problems as eval.
Mike Samuel
A: 

If you are using jQuery, as of version 1.4.1 you can use jQuery.parseJSON()

See this answer: http://stackoverflow.com/questions/3016236/safe-json-parsing-with-jquery/3016292#3016292

thomh