r/ProgrammerTIL • u/Zatara7 • Nov 10 '19
Javascript That using JWTs for sessions auth is less secure than cookies
Reference: https://www.rdegges.com/2018/please-stop-using-local-storage/
CTRL-F "JWT"
Basically since we can mark cookies as http-only, making them inaccessible to xss attackers.
I knew both things, didn't connect the dots until I read that post.
Found the post while trying to learn why do people need things such as Redux while we have React sessions.. But I found this instead.
38
Upvotes
89
u/WalkingPacifist Nov 10 '19
This post is misleading, using localStorage to store session information is insecure. If you use JWT in cookies like normal implementations and make it HTTP-Only you're fine. Nothing inherently bad with using JWT like the title makes it seem. The article states the same.
29
u/vampiire Nov 10 '19 edited Nov 11 '19
This is a false analogy. JWT is transport mechanism for communicating information in a trusted (signed) manner. Most often to have stateless sessions by transporting a user identity.
A cookie a client storage mechanism. Local storage is a client storage mechanism. Both storage mechanisms have their flaws.
Cookies are vulnerable to XSS and CSRF attacks. They are Inherently vulnerable to CSRF because they are attached to every request sent to the domain they originated from.
Both can be mitigated with the httpOnly (XSS mitigation) and same-site (CSRF mitigation) flags. But until all browsers respect both of these flags and you can be assured all your users are using browser versions that support them then the risks remain.
Local storage is inherently vulnerable to XSS because JavaScript has to be able to access it to be used. Its inherently invulnerable to CSRF because of the same origin policy.
How you store and send (from the client) JWT is where the risks are presented. Nothing about JWTs themselves is inherently vulnerable.
What sucks is that JWT get a bad rap because of lazy / uninformed developers storing and sending them improperly. And it gets perpetuated from this article which has correct but incomplete information. It’s the go to source for “JWTs are bad”. But that’s because while he does a great job describing how local storage is bad and how to use cookie sessions he doesn’t highlight how to use JWTs properly or their benefits over cookie sessions.
EDIT: see response below for “how to use properly / benefits”
4
u/BleLLL Nov 10 '19
So how would you store a JWT on a SPA without a backend? say an angular app that is hosted as a static file ? I dont think his back-end session suggestion applies here, does it?
2
u/kimbizo Jan 08 '20
Encryption at rest for all storage should be non-negotiable at this point.
-4
u/Kyeana Nov 10 '19
I totally agree with this, but one thing to note is that if you are using sessions you will need to make sure you have some form of CSRF protection in place, which you don’t need if you are using JWTs passed in via headers.