Is it safe to call jQuery's $.getJSON()
with a URL argument that came from an untrusted source, such as another user? In other words, is it safe to call $.getJSON()
with an untrusted URL? I will be careful not to trust the response and to handle the response safely, but does the call itself pose a security risk?
In other words, I'm talking about something like:
$.getJSON(url_from_user, function(...) { ... handle response safely ...});
or
$.getJSON('http://evil.com/foo.json', function(...) {...});
Could this allow code injection or XSS if some untrusted user provides a malicious value for url_from_user
or if someone malicious controls the evil.com
site? Again, assume that any JSON object returned will be treated safely.
More details and research I've done
The documentation for getJSON doesn't say anything either way about whether this scenario is secure. Logically, I would expect this scenario to be safe, as I would expect jQuery's implementation to download the text of the JSON object via XHR, parse this text using a safe JSON parser, and then return the JSON object.
However, after looking at the jQuery source code, I have some doubts about whether this is safe. Browsing through the source code for jQuery, it looks like this scenario might potentially allow XSS. The code for getJSON() is a bit elaborate (see src/ajax.js), but it seems to select a "transport" and then use it to send the AJAX request. I see that src/ajax/script.js registers a transport called the "script tag hack transport". This transport works roughly as follows: it adds a script tag to the document, e.g., <script src="http://evil.com/foo.json">
, and registers an onload handler that runs when the downloaded script has executed. In other words, the "script tag hack transport" is fundamentally insecure if the site is controlled by an attacker: it is including attacker-controlled script into the document and executing it. Besides the script tag hack transport, there's also a XHR transport that uses the browser's XMLHttpRequest() API. I am having a hard time following the twisty logic that determines under which conditions the "script tag hack" transport will be used.
So, coming back to my original question, is it safe to call $.getJSON()
with a user-provided URL? If it is potentially unsafe under some conditions, under what conditions (e.g., browser versions) is it safe/unsafe?
$.getJSON()
, and under the covers jQuery's implementation ofgetJSON()
is doing whatever it is doing (what it is doing is not under my control). More precisely, I'm reviewing code that calls$.getJSON()
. So while I appreciate your brainstorming, unfortunately ideas about what I could do instead of calling$.getJSON()
don't help answer my question. My question is whether$.getJSON()
is safe to call with an untrusted URL. Thank you for your engagement and feedback, though. – Dinorahdinosaur