pillarjs / understanding-csrf

What are CSRF tokens and how do they work?
1.4k stars 120 forks source link

Passing CSRF token to client #3

Closed bitinn closed 9 years ago

bitinn commented 9 years ago

Thx to recent update, my question in #2 are addressed.

I do have 1 more question: is it worth discussing ways of passing CSRF token to client?

I guess both are valid options? though they both appear to have a drawback:

As a side-note: does it mean CSRF token really isn't designed for SPA, but for a Progressive Enhancement model? I mean if you are SPA, you should always use JSON body instead?

dougwilson commented 9 years ago

If your backend accepts only application/json and not the other types, then CSRF is not necessary. This is the best way to construct your SPA unless you designed your SPA to fully work in web browsers that have JavaScript disabled.

bitinn commented 9 years ago

So for SPA, use JSON body for API interaction when possible; otherwise use an extra Set-Cookie to expose token to client JS on each request?

dougwilson commented 9 years ago

Yes, that sounds right to me; if you have to expose a CSRF token to a SPA, a Set-Cookie is probably the best method, because I don't believe even a CORS response will expose the Set-Cookie header, which makes it safe even if there is mis-configured CORS on your server :)

bitinn commented 9 years ago

Cool, I will close this, @jonathanong if you feel that's a good addition, please add it so no more debate about how to set token properly with AJAX-based service.