Anti-CSRF Tokens
CSRF tokens prevent attackers from tricking users into performing unwanted actions.
Results in 1–3 minutes · Free · No signup required
Why It Matters
Without CSRF protection, attackers can craft malicious pages that submit forms to your site using the victim's authenticated session.
How We Check
We analyze forms for CSRF token presence and check for proper SameSite cookie attributes as an additional defense.
Good vs Bad Values
| Value | Verdict | Why |
|---|---|---|
<form method="post"><input type="hidden" name="csrf_token" value="…"> | Good | A POST form with a hidden field whose name contains csrf. The server must also verify it. |
name="_token" (Laravel), "authenticity_token" (Rails), "__RequestVerificationToken" (ASP.NET) | Good | The default field names of three major frameworks. All are recognised. |
name="csrfmiddlewaretoken" (Django) | Good | Contains csrf, so it matches. Django inserts it when the template uses the csrf_token tag. |
<form method="get" action="/search"> | Good | GET forms are skipped. A GET request must not change state, so there is nothing to forge. |
<form method="post" action="/subscribe"> with no hidden token | Bad | Any other site can make a logged-in visitor's browser submit this request. The check flags it. |
A form with no method attribute and a delete or update action | Weak | Treated as GET and skipped. If the handler on the server accepts it as a state change, the check cannot see the problem. |
SPA form that sends the token in an X-CSRF-Token header, no hidden input | Weak | Protected in practice, but the check only looks for a hidden field in the form, so it reports a false positive. Document it and rely on tests. |
name="token" used for something else, such as a newsletter id | Weak | Any input whose name contains token is accepted. The check can pass a form that has no real CSRF protection. |
How to Fix
Implement CSRF tokens in all state-changing forms. Use SameSite=Lax or Strict cookies. Verify token on the server for each request.
Cookies first — the existing advice recommends SameSite. Add it at the proxy if the application does not set it, so the session cookie is not sent on cross-site POSTs:
proxy_cookie_flags ~ httponly secure samesite=lax;
Header edit Set-Cookie ^(.*)$ "$1; SameSite=Lax"
Per framework:
<form method="post">
{% csrf_token %}
…
</form>
<form method="POST" action="/profile">
@csrf
…
</form>
<%= form_with url: profile_path do |f| %>
<%# authenticity_token is added automatically %>
<% end %>
<form method="post">
@Html.AntiForgeryToken()
…
</form>
Express — the csurf package is deprecated, so use a maintained implementation such as csrf-csrf (double-submit cookie) or lusca, and make sure the middleware runs before your routes. Next.js — Server Actions compare the Origin and Host headers for you; for custom route handlers that change state, check the Origin header yourself and reject anything that is not your own site. Spring — CSRF protection is enabled by default in Spring Security; keep it enabled for browser-facing routes.
Verify with a forged request from another origin:
curl -i -X POST https://example.com/profile \
-H 'Origin: https://evil.example' \
-H 'Cookie: session=YOUR_SESSION' -d 'name=test'
The server should answer with 403 (or 400), not 200 or a redirect to success. Test it with a valid session cookie; without one the request would fail for the wrong reason.
Common Mistakes
- Generating a token but never verifying it on the server. A field that is not checked is decoration.
- Using one static token for every user and every session. A CSRF token must be unpredictable and tied to the session or user.
- Exempting the login or API route from CSRF protection for convenience, then reusing the exemption for routes that do change state.
- Relying only on SameSite=Lax cookies. It is a good second layer, but it does not protect same-site requests or browsers that ignore it, and it does not replace the token.
- Accepting the token from a GET parameter. A token in a URL leaks through logs, history and the Referer header.
- Caching a page that contains a per-user token. A shared cache then hands one user's token to others, or serves an expired one.
Frequently Asked Questions
What is Anti-CSRF Tokens?
CSRF tokens prevent attackers from tricking users into performing unwanted actions.
Why does Anti-CSRF Tokens matter for website security?
Without CSRF protection, attackers can craft malicious pages that submit forms to your site using the victim's authenticated session.
How do I fix Anti-CSRF Tokens issues?
Implement CSRF tokens in all state-changing forms. Use SameSite=Lax or Strict cookies. Verify token on the server for each request.
Related Security Checks
Check Your Website Now
Run a free security scan to check for Anti-CSRF Tokens issues and 61 other security checks.
Scan Your Website Free