-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added missing {% csrf_token %} to some forms #3854
Conversation
No output has been received in the last 10 minutes, this potentially indicates a stalled build or something wrong with the build itself. The above error is generated for some of the environments, but most of the builds are passing. Can somebody trigger a rebuild? |
@atombrella I restarted the travis build. |
Strange for the base template as it's used successfully for the browsable API. |
To be honest, I inserted the tokens thinking it was weird that the forms didn't have them, and haven't worked with a setup of DRF. |
The CSRF tokens should be being sent in the AJAX request headers. Happy to reconsider this if someone verifies that there's an actual issue here. |
I am running into this problem as well: If you enable CSRF_COOKIE_HTTPONLY, javascript does not have access to the CSRF token, and the csrfmiddlewaretoken is also missing from the form, so it fails verification. I created a simple django app to demonstrate: https://github.com/matteosimone/drf-testing. The only modifications made to this app are adding CSRF_COOKIE_HTTP_ONLY and enforcing authentication for rest framework. Clone and go into the testing directory.
Go into http://localhost:8000/users/, log in, click on "Raw Data Form", and then click "Submit". It will say CSRF Verification failed. |
Somehow this is an issue. According to https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-CSRF_COOKIE_HTTPONLY you shouldn't get the CSRF token from a cookie but from a hidden form input: "If you enable this and need to send the value of the CSRF token with Ajax requests, your JavaScript will need to pull the value from a hidden CSRF token form input on the page instead of from the cookie.". Combined with the following advise from What works - but is not tested to be secure, I'm no expert on this - is: base.html
csrf.js
This solves the problem. I haven't made a PR because I'm not sure this is a valid, secure solution. See also: |
Workaround while this isn't fixed: Create a local template ([your-project-template-root]/rest_framework/api.html) and add this code:
|
The docs for
Don't set |
Another pull request (#3703) pointed this out.