"CSRF Failed: CSRF token missing or incorrect" with Django #585
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Hi. I'm using django + django-rest-framework as backend and try to use ng2-file-upload to upload file, but failed with CSRF token missing.
I think I've correctly setup XSRFStrategy so other POST/PUT/DELETE requests work, and when uploading in request header there's
csrftokenin the cookie, but seems ng2-file-upload doesn't work with CSRF token very well.Here's request header:
Thank you very much
there has a authToken parameter in the file uploader for your need. I already tested it works with django
@grapemix:
Hi, thanks for the help. Sorry I'm pretty new to the front-end, I don't know what value I should pass to authToken parameter. Should I include the CSRF token in the string like this: DRF-TokenAuthentication, or just simply "Token" is sufficient?
I've tried both but still got the same message.
this.uploader.setOptions({headers: [{Authorization: 'Bearer ' + idToken}]});
I add the code to set the token, and the header also has the line
Authorization:Bearer <CSRFTOKEN>which matches the token in the cookie, but still got 403 with CSRF Failed. Guess I mess something else up, thanks the help anyway.I have the same issue, for some reason I can't send the CSRF token with this file uploader.
@TheAn4rch1st
I have some interesting finding in dango/csrf.py:275 (Django 1.10.5). I insert
pdb.set_trace()here and CSRF token is actually passed to the backend asrequest_csrf_token, but the token doesn't match for some reason.Can you also check the value to see if the token is really missing, or it's sent but doesn't match? Thanks.
Update:
Comparing to a successful uploading, I found ng2-file-upload missing salted csrf token in Request-Payload:
@TheAn4rch1st
Hi, I finally figure how to make it works. According to Django document: https://docs.djangoproject.com/en/1.10/ref/csrf/#ajax
You have to add 'X-CSRFTOKEN' to the header like this:
@keeper I've fixed the problem. The weird thing thing was that I was recieving the token in my backend correctly, but for some reason it still wasn't authenticating right.
This fixed it for me:
this._uploader = new FileUploader(
{
url: this._URL,
headers: [{name: 'X-CSRFToken', value: csrfToken.value}]
});
Haha lol
i used drf. There has few python oauth pkg.
To me, using header is overkill, but if it works for you, then it is cool. It seems you guys is likely to be blocked by the sever side oauth setup instead of the client side.
@keeper
While, how can i get the value of 'this.csrftoken'?
@lqm229026
I cannot find the exact StackOverflow page I referenced, however I get it from cookie using:
then:
Mind you I'm pretty new to front-end and JS, and I'm not sure if this the best practice or even the right way to do it. Wish it would help.
It is also possible to update the CSRF token per request. Eg. you can do in your component constructor:
@tarmo-randma which cookieService are you referring to?
It is just a service I have created myself for accessing Cookie values - that is where I store the server provided XSRF token. keeper provided an example implementation already above. In my case I am using:
In case you are using the HttpClientXsrfModule to add an XSRF Header, there is a simpler solution to get the XSRF Token.
Simply inject HttpXsrfTokenExtractor from '@angular/common/http'. To get the token call
const token = this.httpXsrfTokenExtractor.getToken();The full code could be :
Solved my problem: https://github.com/valor-software/ng2-file-upload/issues/1016#issuecomment-399444051
note the 'django.middleware.csrf.CsrfViewMiddleware' in the setting.py (MIDDLEWARE)