According to OWASP:
When a user authenticates to a site, the site should generate a (cryptographically strong) pseudorandom value and set it as a cookie on the user's machine separate from the session id.
(emphasis mine)
Why does the CSRF token need to be stored in a separate cookie if the session id is:
- a random value (a value the attacker cannot guess)
- stored in a cookie (a value the attacker cannot read)
- generated by the server (a value the attacker cannot write)
Why not simply use the session id as the CSRF token? You'd still submit the value twice (once in the cookie, once in the form) and compare the values, but wouldn't use a separate cookie for the CSRF token.
secureflag on all cookies: the session cookie, and also any cookies that are used for CSRF (and ideally use HSTS too). – D.W. Jun 27 '14 at 17:46secureonly helps if you change session id on login, otherwise a MiTM can delete the cookie, recreate it with the flag removed, wait for you to login, and hijack your credentials. – Gili Jun 27 '14 at 18:30secureflag set and if the site normally uses only HTTPS. Now consider a scenario where we use one cookie for the session ID and a separate cookie for deriving CSRF values (as you suggested), and imagine that the session cookie has thesecureflag set but the latter cookie doesn't. I realize this is a narrow case and David's answer would benefit from elaboration. 2. Agreed. – D.W. Jun 27 '14 at 18:44secure? 2. Actually, I think I was wrong. If an attacker uses SSL stripping, what prevents him from reading the new session id we return on login and stripping the secure flag while he's at it? – Gili Jun 27 '14 at 19:27