Should I remove CsrfViewMiddleware from MIDDLEWARE when using TokenAuthentication in DEFAULT_AUTHENTICATION_CLASSES? #8792
Unanswered
luiscastillocr
asked this question in
Question & Answer
Replies: 1 comment 5 replies
-
Are you sure the view is not using the |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello ladies and gentlemen,
I recently started to upgrade an old Django stack from 1.11.X to 2.2, the thing is there is this API(token authenticated) that suddently started to fail with
CSRF Failed: CSRF cookie not set.
on every non secure request(PUT, PATH, POST, etc) so tracking the error i found out theCsrfViewMiddleware
middleware class is the culprit of the problem, so basically it checks if the CSRF cookie exists on theprocess_request
method failing the request since there is nothing that has previounsly set the cookie to make it exists during the request. so since the middleware runs before the authentication classes in DRF, there is no way you can validate the user and bypass the cookie validation from the authenticator so, the most obvious solution is to disable CsrfViewMiddleware from the middleware and let the authenticator to handle the user session but i know disabling the CsrfViewMiddleware is a potential security risk, so my question is, what are the correct django middleware classes to use when you authenticate the user using a token?Here are some aditional details:
All the API views are Class based views
Thanks
Beta Was this translation helpful? Give feedback.
All reactions