The Cookie
header that AngularJS is sending looks like:
_ga=redacted; sessionid=redacted; ; csrftoken=redacted
The extra semicolon may not be HTTP compliant.
There are several 'moving' parts to this problem.
Angular $http POST request fails with 403 error and "CSRF Failed: CSRF cookie not set."
The CSRF cookie and header are definitely set in the request as verified in the development tab in Chrome.
Chrome shows COOKIE header text in request as:
Cookie: _ga=redacted; sessionid=redacted; ; csrftoken=redacted
I have done enough stepping through Django code to see that the raw cookie string, above, comes into the Django app in the
HTTP_COOKIE
field of the request but thecsrftoken
cookie never makes it intorequest.COOKIE
. Django's cookie text parser is thrown off by the extra semicolon in the raw cookie text. I don't know if the parser is broken or AngularJS is broken since I don't know the official syntax requirements for raw cookie strings. But the easiest solution would be to figure out how to get Angular to drop that 'blank' cookie.Django 1.8.2, AngularJS 1.6.7 & Python 2.7.10
This happens in my dev environment on macOS but not on my staging system, which is a Linux box.
UPDATE:
Thanks to help from @xyres I worked around the problem by upgrading to Django 1.8.15. But AngularJS should not send this malformed cookie string, right? How do I fix my AngularJS client?