Live data from Hacker News

JSON users: Avoid CSRFs by not using top-level arrays

flask.pocoo.org

71–80 of 85 posts

Re: JSON users: Avoid CSRFs by not using top-level arrays

#71

Title is incorrect, Array usage is about Javascript/JSON Hijacking not protection against CSRF.

Correct, the technical term for it is 'XSSI', or Cross Site Script Inclusion.

I haven't heard of that term before now, but I would argue that XSSI is a way of doing CSRF rather than CSRF, or something entirely different itself. (From Wikipedia: "Unlike cross-site scripting (XSS), which exploits the trust a user has for a particular site, CSRF exploits the trust that a site has in a user's browser.")

Apologies if the title made it seem like not using javascript arrays was a magic bullet to preventing all CSRF.

Re: JSON users: Avoid CSRFs by not using top-level arrays

#72

Earlier quoted context omitted.

Haacked has a step-by-step description of how this attack works: http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-js... The Microsoft asp.net web stack avoids this by automatically wrapping json responses with an object "{d:...}". The comments describe this as only affecting FF2.0 although testing was informal. (You should, of course, still protect your services.)

I think it's just the ASP.Net AJAX Toolkit not plain ASP.Net or ASP.Net MVC.

Thanks, I should have clarified. Here is the breakdown according to Phil's post linked below:

ASP.NET ASMX Web Services, WCF Web Services, and the now-defunct ASP.NET AJAX automatically wrap with {'d':...}.

ASP.NET MVC does not.

Phil explains that the reason there is a difference is that with MVC there is no common client library that automatically strips the {'d':...} wrapper. So they felt it would be too confusing for users. http://haacked.com/archive/2009/06/25/json-hijacking.aspx

That was an old article so maybe they have changed the default behavior since then. In any case, you can manually wrap the response with {'d':...}.

Re: JSON users: Avoid CSRFs by not using top-level arrays

#73
post #6

I'm not familiar with CSRF so had to look this up: [Cross Site Request Forgery] vulnerabilities occur when a website allows an authenticated user to perform a sensitive action but does not verify that the user herself is invoking that action. The key to understanding CSRF attacks is to recognize that websites typically don't verify that a request came from an authorized user. Instead they verify only that the request…

Haacked has a step-by-step description of how this attack works: http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-js... The Microsoft asp.net web stack avoids this by automatically wrapping json responses with an object "{d:...}". The comments describe this as only affecting FF2.0 although testing was informal. (You should, of course, still protect your services.)

ASP.Net also requires POST for web service calls, so this sort of include doesn't work.

Re: JSON users: Avoid CSRFs by not using top-level arrays

#74
post #32

Earlier quoted context omitted.

Because [] is not syntactic sugar for new Array. The code the article wrote about doesn't work in any implementation that I know of. I think the ESv3 spec wasn't very clear: "Create a new array as if by the expression new Array()." But the implementations (always?) did the right thing, and the ESv5 spec is more clear: it adds "where Array is the standard built-in constructor with that name."

> Because [] is not syntactic sugar for new Array In Firefox 4 and other new browsers this is no longer the case. But right now it's still an issue as many people are using older browsers.

I just tested with Firefox portable 3.0.0 and it looks like it was already fixed then (June 2008).

Re: JSON users: Avoid CSRFs by not using top-level arrays

#76
post #71

Earlier quoted context omitted.

Correct, the technical term for it is 'XSSI', or Cross Site Script Inclusion.

I haven't heard of that term before now, but I would argue that XSSI is a way of doing CSRF rather than CSRF, or something entirely different itself. (From Wikipedia: "Unlike cross-site scripting (XSS), which exploits the trust a user has for a particular site, CSRF exploits the trust that a site has in a user's browser.") Apologies if the title made it seem like not using javascript arrays was a magic bullet to prev…

They are definitely similar, but I wouldn't say XSSI is a type of CSRF. CSRF refers specifically to a few methods of attack, distinct from what is used in XSSI.

Regardless, it seems CSRF is much more widely known than XSSI, so you could say worrying about the distinction is just pedantry. I was very surprised when I searched earlier and could not so much as find an OWASP mention of XSSI. Still very important to know though.

Re: JSON users: Avoid CSRFs by not using top-level arrays

#77

An idea: when using session-based authentication and returning JSON, check that the Referer [sic] matches your domain. I think I'm missing something major, though.

One elegant solution mentioned at a DNSSEC talk was to simply include your crsf token in the header of all get/post requests and have the server reject anything without said token. Assuming you'd be using some js library to do your ajax, you could make that modification in there so that you'd transparently use it without needing to modify any existing code. Only the urls that send html will not need the crsf header token.

Re: JSON users: Avoid CSRFs by not using top-level arrays

#78
post #63
post #50

Earlier quoted context omitted.

The Django "{% csrftoken %}" that you put into forms (and similar things in other frameworks), is used when posting form responses. It turns into a hidden form field ( ). This helps protects against someone creating a form on their own malicious site, that posts some data to yours. This attack mentioned in the OP is effectively completely different. It is off a GET request. Imagine you were running a social network s…

Thanks a lot for the excellent explanation. I tend to do my Ajax requests with post just to get the token in. Is there a reason not doing so, like savings in bandwidth or something like that? Might that be a gain Facebook is trying to achieve?

  > Is there a reason not doing so, like savings in bandwidth or something
  > like that?
GETs may perform slightly better, see http://developer.yahoo.com/performance/rules.html#ajax_get

Re: JSON users: Avoid CSRFs by not using top-level arrays

#79

Title is incorrect, Array usage is about Javascript/JSON Hijacking not protection against CSRF.

Correct, the technical term for it is 'XSSI', or Cross Site Script Inclusion.

Based on what? Never heard of XSSI in this context, isn't XSSI used for extended server side includes?

Re: JSON users: Avoid CSRFs by not using top-level arrays

#80
post #37
post #6

I'm not familiar with CSRF so had to look this up: [Cross Site Request Forgery] vulnerabilities occur when a website allows an authenticated user to perform a sensitive action but does not verify that the user herself is invoking that action. The key to understanding CSRF attacks is to recognize that websites typically don't verify that a request came from an authorized user. Instead they verify only that the request…

A few days ago I asked, "Ask HN: Best Approaches to Prevent Session Hijacking?" ( http://news.ycombinator.com/item?id=2663293 ) and presented a technique I'm experimenting with that uses session timestamps as tokens to validate that each request is authentic and an attacker hasn't stolen the session key. It works by updating the token (timestamp) on the server and in the cookie on each request and they both have to m…

How much does running everything over HTTPS mitigate this cross-site stuff?
Post reply on HN