CSRF
In one line
CSRF (Cross-Site Request Forgery) is an attack where a malicious website tricks a user's browser into making an unwanted request to a site where they are logged in.
In simple words
CSRF (Cross-Site Request Forgery) is a web security attack where a malicious site tricks the browser into making a request to a target site on behalf of the logged-in user. Because the browser automatically includes cookies, the request appears legitimate to the target server.
For example: a user is logged into their bank. They visit a malicious site that contains a hidden form that submits a transfer to the bank's URL. The browser sends the request with the user's session cookie, and the bank processes it as if the user intended it.
Prevention includes: anti-CSRF tokens (a random token embedded in forms that the server validates), SameSite cookie attribute (prevents cookies from being sent with cross-site requests), and checking the Origin or Referer header. Modern browsers' default SameSite=Lax behavior provides significant CSRF protection.
Example
<!-- Malicious site embeds: -->
<form action="https://bank.com/transfer" method="POST">
<input type="hidden" name="to" value="attacker">
<input type="hidden" name="amount" value="10000">
</form>
<script>document.forms[0].submit();</script>
<!-- Prevention: anti-CSRF token -->
<input type="hidden" name="csrf_token" value="random_token_from_server">A CSRF attack form and the defense — an anti-CSRF token that the attacker cannot guess or access.
Related terms
Related Resources
Related Lessons
- Web Security Basics
Web security including CSRF.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.