Engineering 4 min read Updated
CSRF protection in plain terms
In short
Every state-changing request needs its own token; SameSite and an Origin check add layers of defence, but they do not replace the token.
A foreign page can make a visitor’s browser send a request the visitor never intended. If the browser still holds your site’s session cookie, the forged request looks genuine. Start with a token on every change, an explicit SameSite setting on cookies, and no data changes through GET.
The attack relies on cookies sent automatically
A browser attaches cookies to requests for a site automatically, including requests that a page on another site starts. An attacker can build a page that makes a visitor’s browser submit a form to your site, for example when the visitor opens it. Because the request carries the visitor’s cookies, your server may treat it as a normal action. The attacker usually cannot read the reply, so the usual goal is to change something, such as an email address, a password-reset setting or an order.
Put a token on every state-changing request
OWASP recommends the synchroniser token pattern for applications that keep state on the server. The server issues a random token, places it in each form, and rejects a state-changing request whose token is missing or wrong. Stateless applications can use double-submit cookies. OWASP prefers the signed variant, where the token is bound to the session with an HMAC. The naive variant is documented for reference only.
| Pattern | Suits | Note |
|---|---|---|
| Synchroniser token | Applications with session state on the server | The server stores the token and compares it with the submitted value |
| Signed double-submit cookie | Stateless applications | OWASP’s preferred variant, with a session-bound HMAC token |
| Naive double-submit cookie | Reference only | Weaker, and documented only for comparison |
SameSite is a second layer, not a replacement
The SameSite attribute tells the browser when to send a cookie with cross-site requests. Strict sends it only for requests from the same site. Lax also sends it for cross-site top-level navigations that use a safe method, but not for a cross-site POST. None sends the cookie with every request, and the Secure attribute must be set with it. Some browsers apply Lax when no value is given. In that case some browsers may still send the cookie with cross-site POST requests if it was set no more than two minutes earlier.
OWASP treats SameSite as a defence-in-depth layer rather than a complete CSRF defence, and recommends it for session cookies. Set the attribute explicitly, and do not scope cookies to the whole domain, because every subdomain would then receive them.
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Check the Origin header
For state-changing requests, OWASP suggests verifying the request origin with standard headers. Compare the full origin of the request with your own origin, not a prefix or a partial match. Treat a missing Origin header with caution, because a missing header and a value of null are not evidence that the request came from the same origin. Do not accept a state-changing request on the assumption that it is safe just because the header is absent.
Keep GET requests read-only
A GET request is easy to trigger from another page, from a link or from an image tag. For that reason OWASP says not to use GET for state-changing operations. Showing a list, a product page or a report can use GET. Changing a password, updating a profile, deleting a record or confirming an order should use POST, PUT or DELETE, and each of them should carry a token.
A practical checklist
- Give every state-changing form and API request a token that the server checks.
- Use POST, PUT or DELETE for changes, and keep GET read-only.
- Set SameSite explicitly on session cookies, and avoid scoping them to the whole domain.
- Check the Origin header on state-changing requests, and refuse requests whose origin cannot be verified.
- Test each form with a request from another origin, and confirm that it fails.
Session handling is the foundation of this protection; see Sessions and cookies done safely. Scripts injected into a page can forge requests as well, which is why a strict script policy matters: a Content Security Policy that holds. Header settings that support both are listed in HTTP security headers worth setting.
This article explains established guidance from OWASP and MDN. It is not a complete security review of any application, and the checklist is a summary written for this article.
Common questions
- Is SameSite enough to stop CSRF on its own?
- OWASP treats SameSite as a defence-in-depth layer rather than a complete defence, so keep a token on state-changing requests as well.
- Can data be changed with a GET request?
- OWASP says not to use GET requests for state-changing operations. Use POST, PUT or DELETE for changes and keep GET for reading.
- What should happen when the Origin header is missing?
- OWASP says a missing header, or a value of null, is not evidence that the request came from the same origin. Do not accept a state-changing request only because the header is absent.
Sources
- Cross-Site Request Forgery Prevention - OWASP Cheat Sheet Series, OWASP Foundation. Accessed 24 November 2026
- Set-Cookie header - HTTP | MDN, Mozilla Developer Network (MDN). Accessed 24 November 2026
Further reading
-
Sessions and cookies done safely
Protect session cookies with explicit attributes, a new ID at login and timeouts enforced on the server.
-
A Content Security Policy that holds in practice
A policy holds when inline scripts are gone, every nonce is fresh on each response, and the rules have been tested in report-only mode first.
-
API authentication: cookies, JWT or API keys?
Most web apps need stateful HttpOnly session cookies for browsers and hashed, scoped API keys for server-to-server integrations. Choose stateless JWTs only when multiple independent services must verify identity without calling a central store.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea