Intermediate Topic 26 of 51 Security & Cryptography
CSRF
Attack tricking a user's browser into unwanted requests.
How it works
Cross-site request forgery tricks a logged-in user's browser into sending an unwanted request to a site where it is authenticated. Because browsers automatically attach cookies, a malicious page can submit a hidden form or request to a bank or admin endpoint, and the server cannot tell it was not intended by the user. It targets state-changing actions such as changing an email, transferring money or deleting data.
Step-by-step walkthrough
- The victim is logged in to the bank.
- They visit the attacker's page.
- The page makes the browser send a transfer request.
- The browser attaches the bank cookie automatically.
- The bank acts on it, so use SameSite cookies and anti-CSRF tokens.
Where it is used
Defenses include anti-CSRF tokens tied to the session and checked on each state-changing request, SameSite cookie attributes, checking the Origin or Referer headers and requiring custom headers for APIs.
Common pitfall
Using GET for state changes makes attacks trivial. Token validation that is optional or shared across users does not help. SameSite defaults vary by browser, so do not rely on them alone. CSRF is different from XSS, though XSS can defeat CSRF tokens. Token-based APIs sending credentials manually are less exposed than cookie-based sessions.
Where it fits in the course
CSRF is topic 26 of 51 in Security & Cryptography, rated Intermediate. It follows Cross-Site Scripting. The next topic is Buffer Overflow.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Security & Cryptography quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Security & Cryptography quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is CSRF?
Where do people use CSRF?
What is the most common mistake with CSRF?
What should I learn before this topic?
How can I practice it?
Is CSRF hard to learn?
How long does it take to learn CSRF?
Which topics are related to CSRF?
What should I study after CSRF?
Related topics
- Cross-Site Scripting (Security & Cryptography)
- Buffer Overflow (Security & Cryptography)
- SQL Injection (Security & Cryptography)
- Denial of Service (Security & Cryptography)