Cross-Site Request Forgery (CSRF)
Cross-Site Request Forgery (CSRF) is an attack that tricks an authenticated user's browser into unknowingly submitting a malicious request to a web application where the user is currently logged in.
Definition
Cross-Site Request Forgery (CSRF) is an attack that tricks an authenticated user's browser into unknowingly submitting a malicious request to a web application where the user is currently logged in.
Overview
CSRF exploits the fact that browsers automatically attach cookies (including session cookies) to any request sent to a site, regardless of where that request originated. If a victim is logged into a banking site and visits a malicious page containing a hidden form that auto-submits to the bank's "transfer funds" endpoint, the browser will include the victim's valid session cookie, and the bank server has no way to tell the request wasn't intentional — unless it has CSRF protections in place. Common defenses include CSRF tokens (a random, unpredictable value embedded in forms and validated server-side on submission), the SameSite cookie attribute (which restricts when cookies are sent on cross-site requests), and re-authentication for sensitive actions. CSRF is distinct from Cross-Site Scripting (XSS) — CSRF forges a request using the victim's existing session, while XSS injects and runs code in the victim's browser — but the two are often used together to escalate an attack. API-heavy applications need to think carefully about CSRF alongside broader Authentication and Authorization design, a topic covered in the API Security course.
Key Concepts
- Exploits automatic cookie/session inclusion in cross-origin requests
- Requires the victim to be authenticated and tricked into visiting a malicious page
- Mitigated with anti-CSRF tokens validated on every state-changing request
- SameSite cookie attribute limits when cookies are sent cross-site
- Distinct from but often paired with XSS for a stronger attack
- Primarily affects state-changing operations (POST/PUT/DELETE), not simple reads
Use Cases
Frequently Asked Questions
From the Blog
Cross-Site Scripting (XSS) Explained
Cross-site scripting lets attackers run malicious JavaScript in your users' browsers. Learn the three XSS types and how output encoding and CSP stop them.
Read More Cloud & CybersecurityWhat Is CSRF and How to Prevent It
CSRF tricks a logged-in user's browser into sending unwanted requests to a site. Learn how the attack works and how tokens and SameSite cookies stop it.
Read More Data ScienceCross-Validation: How to Trust Your Model
Cross-validation tests a model on multiple held-out splits so its score reflects real-world performance, not luck. Learn k-fold, its variants, and common pitfalls.
Read More Cloud & CybersecurityHow HTTPS and SSL Certificates Work
HTTPS encrypts traffic and proves a site's identity using TLS certificates. Learn how the handshake, public-key crypto, and certificate authorities work together.
Read More