Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

JWT_AUTH_COOKIE makes APIs CSRF vulnerable. #338

Open
mohsen-mahmoodi opened this issue Jun 5, 2017 · 15 comments
Open

JWT_AUTH_COOKIE makes APIs CSRF vulnerable. #338

mohsen-mahmoodi opened this issue Jun 5, 2017 · 15 comments

Comments

@mohsen-mahmoodi
Copy link

Using the JWT_AUTH_COOKIE is vulnerable to CSRF attacks and this is not mentioned in the documentation nor mitigated against in the code.

@angvp
Copy link
Contributor

angvp commented Jun 5, 2017

How is that? please explain.

@Radobilly
Copy link

Maybe by brute forcing the default hs256 JWT_ALGORITHM as mentioned here and here

@angvp
Copy link
Contributor

angvp commented Jul 24, 2017

@Mahmoodi ? input please.

@klis87
Copy link

klis87 commented Jul 24, 2017

@Mahmoodi is right. Because JWT is saved in a cookie, it will be automatically sent with every request, causing successful authentication without any need to add JWT to request header by the application. Because of this, it is generally vulterable to CSRF in exactly the same way as usual cookie session, so actually there is no need to provide any example here as general CSRF vulnarability examples could be applied here.

@angvp
Copy link
Contributor

angvp commented Jul 24, 2017

Hey @klis87 thanks for jump in, yeah I got that from @Radobilly's links and some googling .. I will write some docs and will update this ticket so you can proofread and add/delete more info to the main docs.

@AdamStelmaszczyk
Copy link

AdamStelmaszczyk commented Jul 28, 2017

Please correct me if I'm wrong, but seems to me that default Django CSRF protection, if used correctly, prevents problems:

https://docs.djangoproject.com/en/1.11/ref/csrf/

https://stormpath.com/blog/where-to-store-your-jwts-cookies-vs-html5-web-storage also says that:

Leveraging your web app framework’s CSRF protection makes cookies rock solid for storing a JWT.

Update:

Ah ok, I see one more thing now. Django's CSRF tokens will be generated if one has django.middleware.csrf.CsrfViewMiddleware, but they get ignored when using JSONWebTokenAuthentication. So yes, it looks like JWT cookie is vulnerable to CSRF.

@EduardoNogueira
Copy link

Any update on this?

@bmpenuelas
Copy link

I noticed this risk and created a pull request adding an option to handle csrf protection when using JWT in http-only cookie.

Now I see it's an open issue, for those interested, check out #434 and maybe we can close this.

@CapedHero
Copy link

Perhaps this post might be illuminating?

@bmpenuelas
Copy link

It is @CapedHero, that is the behaviour that you get when using #434.

@PaulDFPV
Copy link

PaulDFPV commented Oct 5, 2018

As well as requiring CSRF protection, shouldn't JWT_AUTH_COOKIE also be set with the Secure flag?

@bmpenuelas
Copy link

The Secure flag prevents the cookie from being sent over non-https connections, protecting it from eavesdropping among other things. Some users might want to protect their apps from CSRF and XSS while allowing them to work over http. In that case, the Secure flag would not be a suitable choice.

That said, using https and the Secure flag is of course advisable whenever possible.

@PaulDFPV
Copy link

PaulDFPV commented Oct 6, 2018

Perhaps use the Secure flag by default and have an option to turn it off then?

@pou426
Copy link

pou426 commented May 3, 2019

There is currently no way to customise the cookie settings. It would be really useful to have the choice to set them, especially the secure and samesite flags. Thanks!

@bmpenuelas
Copy link

bmpenuelas commented May 16, 2019

Yes @pou426 you can customize it the way you want using CSRF_COOKIE_SECURE and CSRF_COOKIE_SAMESITE respectively.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants