I recently learned that jQuery's $.getJSON()
is not safe to call on an untrusted URL. What about $.get()
? Is jQuery's $.get()
safe to call when the URL parameter comes from an untrusted source, or is this insecure?
This came up in a security code review I was doing, to check for XSS vulnerabilities. Example code pattern:
$.get(url, function (...) { ... })
Does this code pattern create a XSS vulnerability, if an attacker chooses url
maliciously?
Please assume that the function will handle the response from the AJAX request safely, and that url
comes from an untrusted source (e.g., some other user) and can be completely controlled by the adversary.
My concern: if url
is chosen by an attacker, can an attacker choose a malicious URL (e.g., containing callback=?
and pointing to their own site, or something clever like that) that causes jQuery to guess that the data type should be JSONP, enable JSONP, insert a script tag into the document, and introduce a XSS vulnerability in the same way that getJSON() does? (Since I'm not passing an explicit dataType
argument to $.get()
, jQuery will guess the data type, as described in the docs. I'm not sure what the security implications of that are.)
I ran across this code pattern in code review, and I'm trying to understand whether it is a potential vulnerability. I'm not looking for alternative ways this code could be written; instead, I want to know whether this kind of code pattern is secure as is.
Since the threat model is a bit tricky, let me give an example to help understand this better. Suppose Bob is a user of the service and he can provide a URL that's associated with his profile. Suppose that when Alice visits Bob's profile page in her browser, the Javascript code on the page takes the URL that Bob provided and passes it as an argument to $.get()
. The question is, is this safe? Could Bob use this to attack Alice? Could Bob trigger Alice's browser to execute arbitrary Javascript code, with all of Alice's power? As the linked question explains, $.getJSON()
is unsafe in this scenario -- but what about $.get()
? Is it unsafe too, or is it safe?
Since I got some requests for clarification, let me try explaining/asking the question a different way. Suppose I'm doing a code review to check whether some Javascript code contains any XSS vulnerabilities, and I see the following line of code:
$.get(url, function(resp) { /* do nothing */ });
Suppose I know that url
can be completely controlled by the attacker. Is this automatically a XSS vulnerability? Or is this always safe? Or if the answer is "it depends", what does it depend on?
Or, yet another way to think about this. Suppose I'm doing a code review and I see the following line of code:
$.get(url, f);
Suppose I know that url
can be completely controlled by the attacker. What do I need to check, to verify whether this is safe (free of XSS bugs)? I am aware that I need to check the code of f
to see whether it handles the response safely, because if f
is careless it could introduce a XSS bug. My question is: is that the only thing I need to check for? Or is this code pattern always an XSS vulnerability, regardless of how f
is coded?