Skip to content
ScanAnt

What is CSRF (cross-site request forgery)?

Cross-site request forgery (CSRF) is an attack where another website makes a visitor's browser send a request to your site while they are logged in. Because the browser attaches the session cookie automatically, your server may treat the forged request as a real action by that user.

How it works

Suppose your bank-style app changes an e-mail address with a POST to /account/email and trusts the session cookie. A malicious page can contain a hidden form pointing at that URL and submit it as soon as a logged-in user opens the page. The browser sends the cookie, the server sees a valid session and applies the change. The attacker never sees your data; they only need your server to accept a state-changing request without proof that it came from your own page.

<!-- On evil.example, auto-submitted -->
<form action="https://your-app.example/account/email" method="POST">
  <input name="email" value="[email protected]">
</form>
<script>document.forms[0].submit()</script>

How to tell if your site is affected

  • Login uses cookies, and actions such as changing e-mail, password or settings work from a plain form POST.
  • Your session cookie has no SameSite attribute, or SameSite=None.
  • State-changing actions are exposed as GET links, such as /delete?id=5.
  • There is no CSRF token, no Origin check and no custom header requirement on write endpoints.
  • APIs that use an Authorization header kept in memory are mostly not affected, because the browser does not attach that header by itself.

How to fix it

  1. Set the session cookie to SameSite=Lax or Strict, plus Secure and HttpOnly. Modern browsers then withhold it from most cross-site POSTs.

  2. Add anti-CSRF tokens to every state-changing form and verify them on the server. Frameworks such as Laravel, Django and Rails do this by default; do not switch it off.

  3. In Next.js route handlers and server actions, check that the Origin or Host header matches your site for write requests. Server actions do this check for you.

  4. Never change state on GET requests. Use POST, PUT, PATCH or DELETE.

  5. Do not rely on CORS to prevent CSRF: a cross-site form post is not blocked by CORS.

How ScanAnt checks for it

Quick scanDeep scan

The quick scan checks whether your session cookie is missing SameSite, Secure or HttpOnly, which is the most common cause of CSRF exposure. In the deep scan, with the separate level 3 opt-in, ScanAnt sends preflight requests and one POST without cookies or with a foreign Origin to endpoints that appear to change state without login, and records only status codes and headers. It does not submit your real forms, and CSRF token logic inside logged-in areas is not tested.

Questions

What is the difference between CSRF and XSS?

CSRF abuses the browser's trust in a cookie from outside your site. XSS runs attacker code inside your site. XSS can defeat CSRF tokens, so fix both.

Does SameSite cookie stop CSRF completely?

It stops most cross-site requests, and Lax is the browser default today. It is a strong layer but not the only one: same-site subdomains and older browsers still call for tokens or Origin checks.

Do JSON APIs need CSRF protection?

If authentication is a cookie, yes, even if the body is JSON. If authentication is an Authorization bearer header that scripts add themselves, CSRF is not a practical risk.

Does Supabase or Firebase need CSRF tokens?

Their client libraries send the token in a request header rather than a cookie by default, so classic CSRF does not apply. If you store their session in cookies for server rendering, add SameSite and Origin checks.

See if your site is affected

Free for any public site, no signup. Need the verified or deep scan? Choose the level on the scan page.

Choose a scan level →