views:

1711

answers:

4

JSON allows you to retrieve data in multiple formats from an AJAX call. For example:

$.get(sourceUrl, data, callBack, 'json');

could be used to get and parse JSON code from sourceUrl.

JSON is the simply JavaScript code used to describe data. This could be evaled by a JavaScript interpreter to get a data structure back.

It's generally a bad idea to evaluate code from remote sources. I know the JSON spec doesn't specifically allow for function declarations, but there's no reason you couldn't include one in code and have an unsafe and naive consumer compile/execute the code.

How does jQuery handle the parsing? Does it evaluate this code? What safeguards are in place to stop someone from hacking sourceUrl and distributing malicious code?

A: 

All browsers I know of disable cross-site requests through Ajax. That is, if your page sits on my.example.com, you can't load anything using Ajax unless its URL is also at my.example.com.

This actually can be something of a nuisance, and there are ways for an attacker to inject source in other ways, but ostensibly this restriction is in place to address exactly the concern you mention.

Adam Bellaire
+1  A: 

$.getJSON() is used to execute (rather than using eval) javascript code from remote sources (using the JSONP idiom if a callback is specified). When using this method, it is totally up to you to trust the source, because they will have control to your entire page (they can even be sending cookies around).

From Douglas Crockford site about The Script Tag Hack (jsonp):

So the script can access and use its cookies. It can access the originating server using the user's authorization. It can inspect the DOM and the JavaScript global object, and send any information it finds anywhere in the world. The Script Tag Hack is not secure and should be avoided.

Luca Matteis
+6  A: 

The last time I looked (late 2008) the JQuery functions get() getJSON() etc internally eval the JSon string and so are exposed to the same security issue as eval.

Therefore it is a very good idea to use a parsing function that validates the JSON string to ensure it contains no dodgy non-JSON javascript code, before using eval() in any form.

You can find such a function at http://www.json.org/json2.js.

See JSON and Broswer Security for a good discussion of this area.

In summary, using JQuery's JSON functions without parsing the input JSON (using the above linked function or similar) is not 100% safe.

NB: If this sort of parsing is still missing from getJSON (might have recently been added) it is even more important to understand this risk due to the cross domain capability, from the JQuery reference docs:

As of jQuery 1.2, you can load JSON data located on another domain if you specify a JSONP callback, which can be done like so: "myurl?callback=?". jQuery automatically replaces the ? with the correct method name to call, calling your specified callback.

Ash
I believe oyu should use http://www.json.org/json2.js for this now -- i'm not sure of the exact updates that have occurred in json2, but i do know it will avoid accidentally overriding native browser implementations of the JSON API (maybe the original didn't prevent this?)
olliej
Thanks for the tip, I've updated the link.
Ash
I don't understand why it is "even more important when using jQuery.getJSON()". Both ways are executing javascript code, and both ways ways must be sanitized. When using getJSON() with a callback it's usually from another domain, therefore you have no control over the sanitization of the JSON.
Luca Matteis
A: 

Both IE 8 and Firefox 3.1 will have native JSON support, which will provide a safe alternative to eval(). I would expect other browsers to follow suit. I would also expect jQuery to change its implementation to use these native methods.

Chase Seibert