What Is CSRF (Cross-Site Request Forgery)?

CSRF tricks a logged-in user's browser into unknowingly submitting a request to a site they're authenticated with, performing an action the user didn't inten...

Published November 16, 2026

Cross-site request forgery (CSRF) tricks a logged-in user's browser into sending an unwanted request to a site where they are authenticated, because browsers attach cookies automatically. Defend with CSRF tokens and SameSite cookies.

The concept

If bank.com relies only on a session cookie, a malicious page can submit a hidden form to bank.com/transfer and the browser will include the cookie.

Defences: a secret per-session token required on state-changing requests, SameSite=Lax/Strict cookies, and checking Origin headers. Never change state with GET.

The forged request

auto-submitPOST /transfer + cookieevil.comhidden formVictim browserhas cookiebank.comtrusts cookie
The browser sends the victim's cookies automatically.

Practical examples

Attack page

<form action="https://bank.com/transfer" method="POST" id="f"><input name="to" value="attacker"></form>
<script>f.submit()</script>

Laravel token

<form method="POST">@csrf ...</form>

SameSite cookie

Set-Cookie: sid=abc; HttpOnly; Secure; SameSite=Lax

SPA with bearer tokens

Authorization headers are not sent automatically, so classic CSRF does not apply

Common mistakes

  • State-changing GET endpoints.
  • Disabling CSRF protection to 'fix' a form.
  • Tokens that are predictable or not tied to the session.

Key takeaways

  • Browsers add cookies automatically.
  • Require unguessable tokens on writes.
  • Use SameSite cookies.

FAQ

CSRF vs XSS?

CSRF abuses the user's authority to send requests; XSS runs attacker code in the page.

Do JWT headers prevent CSRF?

Header-based auth is not attached automatically, so yes, but beware XSS.

More General articles