Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JWT and CSRF differences

Tags:

I've been reading about JWT, and from what I understand, it's a token that the server sends after a user logs in. The user will have to send that token with all future HTTP requests. This creates a stateless way for the server to verify a user's request.

Now what I don't understand is that if the JWT is sent in the header and marked as HTTP only, why is a CSRF token also needed for prevention of CSRF attacks? My understanding is that both JWT and CSRF tokens are tied to a user, and that a JWT would serve both purposes.

I understand that CSRF tokens are used so HTTP requests from other sites won't be accepted. How does a JWT not accomplish that? What separates a CSRF token from a JWT token, and allows it to accomplish that difference?

I've been reading articles on JWT's and CSRF tokens as well as the double submit method, but there is just something I can't seem to understand or I'm missing.

like image 963
Caciano Avatar asked Aug 29 '17 18:08

Caciano


People also ask

What is the difference between CSRF and JWT token?

An authentication system based on tokens (JWT or random) stored in cookies is vulnerable to CSRF attacks, because cookies are sent automatically to server in each request and an attacker could build a harmful url link to your site.

Is JWT a CSRF?

To mitigate the known issues of this technique, the CSRF token is stored in a JWT. Additionally, the account identifier is included in this JWT as well for logged-in users. Storing the CSRF token in a JWT makes it possible for the back-end application to verify that it produced the token itself.

What is the difference between JWT and token?

JWT is a different approach which uses encryption and hashing techniques to validate the token instead of database checks. It starts the same as token auth, by sending the username and password and validating it against the database.

What is the difference between CSRF and CORS?

CSRF is a vulnerability and CORS is a method to relax the same-origin policy. CORS is something you might want to use (in certain circumstances) whereas CSRF is an undesirable design mistake. There are vulnerabilities associated with the CORS mechanism.


2 Answers

An authentication system based on tokens (JWT or random) stored in cookies is vulnerable to CSRF attacks, because cookies are sent automatically to server in each request and an attacker could build a harmful url link to your site.

https://yoursite.com/delete?something=1 

To protect your site it is needed to use a CSRF token that your application must send in the following request (not in a cookie).

Alternatively you could store the JWT in localStorage and send it in a Authorization header, then your site is protected against CSRF, but it could be vulnerable to XSS attacks. Take in account always the security considerations of the technical solution you choose

Update ¿why storing JWT in localstorage could be vulnerable to XSS attacks?

See this comparison between storing tokens in localstorage and http-only cookies https://academind.com/tutorials/localstorage-vs-cookies-xss

An attacker could inject javascript code to read the token from the localstorage and send it to his own server. However, this type of attack is not possible with an http-only cookie because it is not accessible from javascript

like image 132
pedrofb Avatar answered Oct 13 '22 05:10

pedrofb


All your questions are relative to the fact that a CSRF token in NEVER included in a cookie and that a JWT token MAY be sent in a cookie.

A JWT token can be sent:

1- in a cookie

2- in another type of header

3- outside the headers, in some POST attribute

4- outside the headers, in some GET parameter (not very common)

But for stateless authentication, you MUST use cookie (case 1).

So, for stateless authentication, you are prone to CSRF, with your JWT token. This is why you need to add some CSRF mitigation mechanism, based on some more information not included in a cookie, or not only included in a cookie.

All of this would not apply if you were accepting to implement a stateful authentication.

like image 45
Alexandre Fenyo Avatar answered Oct 13 '22 05:10

Alexandre Fenyo