Generating CRIME safe CSRF Tokens

Hanno's Blog

Monday, April 13. 2020

Generating CRIME safe CSRF Tokens


Trackbacks

No Trackbacks

Comments
Display comments as (Linear | Threaded)

Thanks for writing this up! I've been working on a little web app and wondering about whether the new SameSite header is enough to make CSRF tokens redundant.
#1 shane on 2020-04-14 02:42 (Reply)
You can use `hash_equals()` function to compare two hashes to provide time-attack safety.

I'm also working on a stateless CSRF project (https://github.com/Ayesh/StatelessCSRF) that can be used to generate CSRF tokens based on a server-stored secret, and with tokens bound to IP addresses, user-agent strings, etc.
#2 Ayesh (Homepage) on 2020-04-14 07:15 (Reply)
hash_equals() sounds like a good improvement, done that now.
#2.1 Hanno Böck (Homepage) on 2020-04-14 09:42 (Reply)
As Ayesh already mentioned, you should not rely on `==` (double-equals check) as it is vulnerable to type juggling attacks (see https://owasp.org/www-pdf-archive/PHPMagicTricks-TypeJuggling.pdf) and instead use `hash_equals` to mitigate timing side channel attacks.
Your generated CSRF tokens don't ever expire and old tokens remain valid. It would be quite trivial to find a valid token if timing information is accurate by sending different suffixes (updated from MSB to LSB) while the prefix remains static.
#3 Leon on 2020-04-14 09:30 (Reply)
The tokens expire with the session, which is outside the scope of this code to deal with how long they last.

I agree that safeguarding against timing is a good idea. But I am inclined to disagree that it "would be quite trivial" to perform a timing attack in any real world situation, but happy to be proven wrong here :-)
#3.1 Hanno Böck on 2020-04-14 09:34 (Reply)
Your CSRF tokens sound like they become closer to capabilities: tokens constrained to a specific resource/action. CSRF is a classic confused deputy so capabilities are a natural countermeasure.

I don’t like SameSite cookies because they are a very blunt instrument. Firstly, yes there are absolutely CSRF attacks that they don’t prevent: “site” means top-level registerable domain (eg example.com). This means that if somebody compromises wordpress.someforgottenproduct.example.com they can use that to launch CSRF attacks against payroll.example.com or whatever. SameSite will not prevent those because it’s considered the “same site”.

Secondly, there are legitimate reasons for wanting cross-origin cookies. For example, SameSite breaks several patterns in federation protocols like SAML and OIDC - eg when setting a state/nonce cookie and then using form_post response mode in OIDC. SAML Single-Logout with the HTTP POST binding is broken. And so on. If you ever use CORS with AC-Include-Credentials for genuine cross-site requests then that is also broken by SameSite. So people will probably disable SameSite cookies for these cases (until the browsers make it mandatory).
#4 Neil Madden (Homepage) on 2020-04-14 09:42 (Reply)
I hope you don't mind, I'd like to add a link to this blog and discussion to Chapter 4 of my book (API Security in Action, Manning), which covers cookies and CSRF. I think this is a really nice addition to the usual CSRF defences.
#4.1 Neil Madden (Homepage) on 2020-05-02 00:06 (Reply)
Sure, feel free. If you want you can add the blogpost in full :-)
(The texts on this blog are CC0 licensed.)
#4.1.1 Hanno (Homepage) on 2020-05-02 19:58 (Reply)

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 

About

This blog is written by Hanno Böck. Unless noted otherwise, its content is licensed as CC0.

You can find my web page with links to my work as a journalist here.

I am also publishing a newsletter about climate change and decarbonization technologies.

The blog uses the free software Serendipity and is hosted at schokokeks.org.

Hanno on Mastodon | Contact / Imprint | Privacy / Datenschutz