by 5vforest · 12y ago · 0 comments
There's a Rails design pattern where you send an AJAX request to your controller, and it returns a bunch of Javascript that gets eval'd by the page you're on. This is used for say, infinite scroll. You'll click the 'load more' button, the server will render a partial containing your new records, and return something like:
$('#itemsList').append('<li>My new item</li><li>another new item</li>')
So guess what? This is susceptible to XSS-ish attacks.Let's imagine that instead of a simple list item, this request was returning some sensitive information or a CSRF token. (Egor points to various examples of this.) Normally, executing a GET request from an unauthorized domain will not be allowed because of CORS. This is what prevents me from instructing the users of my website to make an AJAX request to gmail.com, allowing me to see all of their emails.
But "wait", you say, "the content type of this request is application/javascript."
So open developer tools and type this: (using jQuery for conciseness)
$.ajax({
url: 'https://basecamp.com/[BASECAMP_PROJECT_ID]/progress.js',
dataType: 'jsonp'
})
And boom, you have a bunch of sensitive information about your Basecamp project, accessible via JSONP request from any domain.I know it's only generally checked on posts, but turning it on for any xhr calls seems like it would solve any potential data leakage.
Also, very few web applications use basic auth, most use cookies.
He also linked to a post from six years ago[0] which had a good solution the problem at the time: pass data, not code (and process this data on the client side).
Is this RoR specific because RoR will always allow JSON responses to be turned into JSONP, or is there something else at work?
I'm suddenly pleased I've been holding off on doing JSONP as part of Django Slumber.
Having said that, it's worth pointing out that Django REST framework does not return JSONP by default, and although it does for historical reasons include a JSONP renderer, the documentation recommends the use of CORS instead.
you can't turn any JSON into JSONP. No, JSONP here is result of using RJS templates. It has nothing to do with original JSONP
1) the attacker knows of an authorized user, already logged in (most likely). So, any request sent by that browser to the target site will pass authentication checks.
2) they trick that user into loading a web page under their control by some means (e.g., phishing attack).
3) that web page, under their control, includes a <script> tag with a src attribute referencing target .rjs. It loads the target .rjs Javascript into a gimmicked environment in which the private data is captured. Since the web page is under the attacker's control, same-origin rules allow that captured private data to be sent back to the attacker directly.
To counter this, the target site needs some way to verify that its own Javascript is making the request, not some hostile web site. Thus the talk of CSRF tokens and checking .xhr?. (The latter counts on the browser's same-origin rules to prevent an AJAX request from the hostile web site --- the same-origin rule blocks that, but in most current browsers, there's no way to prevent the attacker from tossing in a <script> tag.)
It's not precisely JSONP. It turns out to be JSONP, unintendedly. This is where it becomes vulnerable
I believe this only a vulnerability if the server responds to jsonp requests in GET requests.
EDIT: ah, got it, invokation via GET