Cross-Site Request Forgery (CSRF) Explained
What you'll learn: How attackers can trick authenticated users into unknowingly performing unwanted actions on websites they trust.
What Is CSRF?
Cross-Site Request Forgery (CSRF, pronounced "sea-surf") is an attack where a malicious website tricks your browser into making unwanted requests to a site where you're already authenticated. Unlike XSS where the attacker injects code into the trusted site, CSRF exploits the fact that your browser automatically sends credentials (cookies, session tokens) with every request to a domain.
How CSRF Works
Imagine you're logged into your bank at bank.com. Your browser has an authenticated session cookie. An attacker tricks you into visiting evil.com, which contains hidden HTML that submits a form to your bank:
<form action="https://bank.com/transfer" method="POST">
<input type="hidden" name="to" value="attacker-account">
<input type="hidden" name="amount" value="1000">
</form>
<script>document.forms[0].submit();</script>
When this form submits, your browser automatically includes your bank.com cookies. The bank sees a legitimate authenticated request—but you never intended to make it.
Real-World Analogy
Think of CSRF like someone forging your signature on a check while you're still holding your pen. The bank teller (server) recognizes your authentic signature (session cookie), but you didn't actually authorize that check to be written.
Why It's Dangerous
CSRF attacks can:
- Transfer money or change account settings
- Post content as the victim
- Modify passwords or email addresses
- Execute any state-changing action the victim is authorized to perform
Key Takeaway: CSRF exploits the automatic inclusion of credentials in browser requests, allowing attackers to perform unauthorized actions on behalf of authenticated users without their knowledge or consent.