I am writing down my understanding of the CSRF protection mechanism in Django. Please correct me if it is faulty.
The csrfViewMiddleware
creates a unique string and stores it in a hidden field 'csrfmiddlewaretoken'
of a form originating from the host. Since a malicious website mimicking this form will not know about the value of this field, it cannot use it.
When someone tries to POST the form, the website checks the 'csrfmiddlewaretoken'
field and its value. If it is wrong or not set, then a csrf attack is detected.
But then, what exactly is the CSRFCookie
? The doc says the unique value is set in CSRFCookie
and also in the hidden field
.This is where I am confused. Does a cookie get sent to the browser with the unique string embedded?