CSRF Works Because Browsers Attach Cookies to Requests Other Sites Trigger
A cookie is a small piece of data a site asks the browser to store and send back with every later request to that site: it keeps a cart between pages or a user logged in. The browser sends it even when another site caused the request. Cross-site request forgery (CSRF) abuses exactly that: a page on evil.example submits a hidden form to bank.example/transfer, the browser adds the victim's session cookie, and the bank sees an authenticated request it never meant to accept.
Why the Server Can't Tell by Default
The forged request has the right URL, the right method and a valid session cookie. Nothing in a cookie says which site started the request. The defence must add something the attacker's page can't know or can't send.
Defences
- A CSRF token in every state-changing form: a secret value tied to the user's session that the server checks on
POST. The attacker's page can make the browser send the cookie but can't read the token from another origin. SameSitecookies:SameSite=Lax(the default in modern browsers when the attribute is missing) stops the cookie from being sent on cross-sitePOSTrequests;Strictblocks it on every cross-site request.- Safe methods stay safe:
GETmust never change state, since links and images can trigger it from anywhere.
In Django
CsrfViewMiddleware is enabled by default and rejects unsafe requests without a valid token. Templates add it to forms:
<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">Create</button>
</form>
JavaScript clients read the csrftoken cookie and send it in the X-CSRFToken header.
References
- Web Application and Django Security MOC - hub for web security notes.
- Non-Root Container Filesystem Ownership and Persistent Media Volumes -
CSRF_TRUSTED_ORIGINSwith a host-mapped port. - Django documentation: Cross Site Request Forgery protection - the token, AJAX use and trusted origins.
- OWASP Cross-Site Request Forgery Prevention Cheat Sheet - tokens, SameSite and other defences.
- MDN: Using HTTP cookies - cookie attributes,
SameSiteincluded.
Review cards
Flashcard Why can't a server tell a forged cross-site request from a real one by default?
The forged request has the right URL, method and a valid session cookie, which the browser attaches whichever site triggered it. Nothing in a cookie says which site started the request.
Flashcard Why does a CSRF token stop a forged form post?
It's a secret tied to the session, checked on unsafe requests. The attacker's page can make the browser send the cookie, but can't read the token from another origin.
Quiz How does a Django template add the CSRF token to a POST form?
-
{% csrf_token %}inside the<form> -
{{ csrf_token }}as plain text in the form -
@csrf_exempton the view - Nothing:
CsrfViewMiddlewareadds it to forms
The tag renders a hidden input with the token. The middleware only checks it, and csrf_exempt turns the protection off. JavaScript clients send it in the X-CSRFToken header.
Flashcard What does `SameSite=Lax` change for a session cookie?
The browser doesn't send it on cross-site POST requests, which stops forged form posts. Lax is the default in modern browsers when the attribute is missing; Strict blocks every cross-site request.