Close Look at CSRF Tokens
denvaar.github.io
denvaar.github.io
• Cross-Site Request Forgery is dead! (2017): https://scotthelme.co.uk/csrf-is-dead/
• CSRF is (really) dead (2019): https://scotthelme.co.uk/csrf-is-really-dead/
If your use of CSRF tokens is purely for preventing cross-site request forgery (and not things like form expiry or double-submit protection, which are commonly paired with it, for better or for worse) then CSRF tokens are genuinely obsolete and unnecessary, and you should set your SameSite attribute on your cookies to ensure you’re protected against CSRF in the most browsers (meaning all browsers from at least the last couple of years—including IE11 updated within that time, by the way—rather than probably just Chrome at present).
Older browsers: all browser releases from the last couple of years support it (including IE11); browsers from before then should not be being used on the public internet under any circumstances. It should be a negligible fraction of your user base that uses such browsers, and so long as you’re also happy to break IE11, a safe path (from a security perspective) is to actively break the site for old browsers by blacklisting them or checking for support of a comparatively recent feature. (Just don’t do a whitelist by user agent string, that approach is unmitigatably bad.)
You’re correct about some auth systems not playing nicely with strict same-site cookies, because they’ve been designed with certain assumptions in mind. I’m not particularly conversant with that space, never having had to interact with such a system.
If you want to protect against these attacks with basic cookie attributes you can use SameSite and register your site (and any intermediate sub-domains) on the public suffix list to effectively shun your subdomains as being completely separate sites.
Also, I seem to recall the PSL maintainers requesting at some point that people not do what you describe, because it wouldn’t scale at all.
Yes, adding to PSL is not at all scalable and will likely cause you a world of pain down the road. It was not intended as a serious suggestion. Use proper CSRF defences.
That's why I love if it's built into the framework like Django or Phoenix rather than bolted on or home-rolled.
That is, the protection will be on. But the way to be allowed in by the gate is not on by default. You have to remember to add the tag.
Which is why I'm so excited to just switch to SameSite once there's enough browser usage...
Whereas the hand-rolled CSRF scheme I've inherited will fail silently
Back in the day, the middleware scanned your response and injected it next to each </form> tag. A hack and dirty for sure, but removed that burden.
In the latest versions the framework sets the SameSite header properly, so you don't need the CSRF middleware at all (you can just remove it).
On the other hand, if you use some library to handle forms and validation like deform and colander, the CSRF handling is pretty transparent. Also, but validating CSRF on middleware level, you can't forget about it - features relying on POST requests won't work.
Does anyone know of a downside to that?
However I’m not so sure about this, as I still implement CSRF tokens in my SPAs. (Bit of a habit from my php days) I store the tokens in local storage and pass them through the request headers.
A lot of people have different ideas and methods when it comes to CSRF protection on SPAs. I’d love to hear other people’s opinion and tactics!
Feel free to correct me on anything.
Article: https://medium.com/tresorit-engineering/modern-csrf-mitigati...
If you don't care why, read the docs. If you want to understand HOW, read the code.