title: Why is it common to put CSRF prevention tokens in cookies?
body: <p>I'm trying to understand the whole issue with CSRF and appropriate ways to prevent it. (Resources I've read, understand, and agree with: <a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html" rel="noreferrer">OWASP CSRF Prevention Cheat Sheet</a>, <a href="https://stackoverflow.com/questions/9632262/questions-about-csrf.">Questions about CSRF</a>)</p>
  <p>As I understand it, the vulnerability around CSRF is introduced by the assumption that (from the webserver's point of view) a valid session cookie in an incoming HTTP request reflects the wishes of an authenticated user. But all cookies for the origin domain are magically attached to the request by the browser, so really all the server can infer from the presence of a valid session cookie in a request is that the request comes from a browser which has an authenticated session; it cannot further assume anything about the <em>code</em> running in that browser, or whether it really reflects user wishes. The way to prevent this is to include additional authentication information (the &quot;CSRF token&quot;) in the request, carried by some means other than the browser's automatic cookie handling. Loosely speaking, then, the session cookie authenticates the user/browser and the CSRF token authenticates the code running in the browser.</p>
  <p>So in a nutshell, if you're using a session cookie to authenticate users of your web application, you should also add a CSRF token to each response, and require a matching CSRF token in each (mutating) request. The CSRF token then makes a roundtrip from server to browser back to server, proving to the server that the page making the request is approved by (generated by, even) that server.</p>
  <p>On to my question, which is about the specific transport method used for that CSRF token on that roundtrip.</p>
  <p>It seems common (e.g. in <a href="http://docs.angularjs.org/api/ng.$http" rel="noreferrer">AngularJS</a>, <a href="https://docs.djangoproject.com/en/dev/ref/contrib/csrf/" rel="noreferrer">Django</a>, <a href="http://guides.rubyonrails.org/security.html#csrf-countermeasures" rel="noreferrer">Rails</a>) to send the CSRF token from server to client as a cookie (i.e. in a Set-Cookie header), and then have Javascript in the client scrape it out of the cookie and attach it as a separate XSRF-TOKEN header to send back to the server.</p>
  <p>(An alternate method is the one recommended by e.g. <a href="http://dailyjs.com/2012/09/13/express-3-csrf-tutorial/" rel="noreferrer">Express</a>, where the CSRF token generated by the server is included in the response body via server-side template expansion, attached directly to the code/markup that will supply it back to the server, e.g. as a hidden form input. That example is a more web 1.0-ish way of doing things, but would generalize fine to a more JS-heavy client.)</p>
  <p>Why is it so common to use Set-Cookie as the downstream transport for the CSRF token / why is this a good idea? I imagine the authors of all these frameworks considered their options carefully and didn't get this wrong. But at first glance, using cookies to work around what's essentially a design limitation on cookies seems daft. In fact, if you used cookies as the roundtrip transport (Set-Cookie: header downstream for the server to tell the browser the CSRF token, and Cookie: header upstream for the browser to return it to the server) you would reintroduce the vulnerability you are trying to fix.</p>
  <p>I realize that the frameworks above don't use cookies for the whole roundtrip for the CSRF token; they use Set-Cookie downstream, then something else (e.g. a X-CSRF-Token header) upstream, and this does close off the vulnerability. But even using Set-Cookie as the downstream transport is potentially misleading and dangerous; the browser will now attach the CSRF token to every request including genuine malicious XSRF requests; at best that makes the request bigger than it needs to be and at worst some well-meaning but misguided piece of server code might actually try to use it, which would be really bad. And further, since the actual intended recipient of the CSRF token is client-side Javascript, that means this cookie can't be protected with http-only. So sending the CSRF token downstream in a Set-Cookie header seems pretty suboptimal to me.</p>