Is JSONP safe to use?

SecurityJsonJsonp

Security Problem Overview


Are there any security issues that should be considered when using JSONP?

Security Solutions


Solution 1 - Security

Update: JSONP is a common hack to do cross-domain requests. Modern browsers now have Cross Origin Resource Sharing, and IE8+ have XDomainRequest which is similar. See http://enable-cors.org/ for more info.

JSONP is just a script include that allows you to use a callback. You should however be aware of Cross-site request forgery (CSRF).

As long as you control the script and the server, JSONP isn't anymore insecure than a script include. Unless you have a JSONP-service that returns sensitive data to logged in users. A malicious site can send a request to the service (hoping that the user is logged in on your site), and retreive the data. The service can check the referrer of the request, but it is possible to spoof the referrer using flash (thanks Chris Moschini).

Imagine this senario:

  • A user logs into his internet banking account. Storing a session cookie in the users browser. This site has a jsonp service with sensitive info about the user and his accounts.
  • Other sites won't know that the user is logged in, but they could do a wild guess and try to access the jsonp service. Since the user has a session cookie, the browser will get a response, and there's nothing stopping the site from doing an ajax post to save the sensitive data on their server.

Update June 28th 2012: If you want to protect against CSRF attacks you should read this in depth blog post by a security expert: http://erlend.oftedal.no/blog/?blogid=130

Solution 2 - Security

Yes, you need to be careful, but when used properly with trusted services it's relatively safe.

Here's a summary of the security issues with JSONP, as I understand it:

From the consumer's perspective:

  • You must trust the provider to not return malicious JavaScript instead of the expected JSON wrapped in the JSONP callback you specify.
  • The same is also true of any third party JavaScript embedded add-ons, such as Google Analytics.
  • It's only similar to XSS attacks in that it allows a 3rd party to execute arbitrary JavaScript in your application, however, you must first choose to trust that 3rd party by making the request in the first place.

From the provider's perspective:

  • You must not assume that even though the clients' cookie(s) are present in the request that the consumer is a webpage under your control. Check the Referer header against a whitelist of authorized URLs, and/or don't rely on cookie-based authentication.
  • Analogous to a CSRF / confused deputy attack.

Solution 3 - Security

There are security issues for both sides. The most serious one is for the site including JSONP.

If you are including a

Solution 4 - Security

JSONP is definitely not safe, as it's simply running whatever it gets cross-domain as JavaScript.

solution! solution!

Create an iframe, preferably a sandboxed one, and load JSONP there. Catch the result and pass it up via window.postMessage

And yes, somebody got this idea first, as usual :)

The blog post is no longer there, but I'm keeping the link here for credit: http://beebole.com/blog/general/sandbox-your-cross-domain-jsonp-to-improve-mashup-security/
edit: wayback machine link

It used the window.name hack for iframe communication, but that was for IE6 and 7.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondits59View Question on Stackoverflow
Solution 1 - SecuritygregersView Answer on Stackoverflow
Solution 2 - SecuritytlrobinsonView Answer on Stackoverflow
Solution 3 - SecurityGregory MagarshakView Answer on Stackoverflow
Solution 4 - SecuritynaugturView Answer on Stackoverflow