Serksa
All Concepts
Security

CSRF Protection

1

What is it?

<strong>CSRF (Cross-Site Request Forgery)</strong> is an attack where a malicious site tricks your browser into making unwanted requests to a site you're logged into. CSRF tokens prevent this by validating that requests come from your own site.

2

Think of it like...

The Forged Signature Analogy

CSRF is like someone tricking you into signing a blank check. CSRF tokens are like watermarks that prove the signature is legitimate and not forged.

✍️

Your Signature (Session)

Proves it's you

😈

Forger (Attacker)

Tricks you into signing

🎫

CSRF Token (Watermark)

Proves it's legitimate

3

Visual Flow

🌐Your Site

Generates Token

🎫CSRF Token

Included in Form

Server

Validates Token

4

Where you see it

1

Server generates token

Random string for each session/form

2

Include in form

<input type='hidden' name='csrf' value='abc123'>

3

User submits form

Token sent with request

4

Server validates token

Checks if token matches session

5

Reject if invalid

Attacker's site can't get valid token

5

Common Mistake

Wrong

"HTTPS prevents CSRF attacks"

Correct

<strong>HTTPS doesn't prevent CSRF</strong>. HTTPS encrypts data in transit but doesn't validate request origin. You need CSRF tokens or SameSite cookies.

💡 Real-World Example

CSRF attack scenario:

1

You're logged into yourbank.com

2

You visit evil.com (attacker's site)

3

evil.com has: <form action='yourbank.com/transfer' method='POST'>

4

Without CSRF protection: Your browser sends your cookies, transfer succeeds!

5

With CSRF token: Server rejects (evil.com can't get valid token)