Cross-Site Request Forgery (CSRF)
An attack that causes a user's browser to send an unwanted state-changing request carrying ambient credentials to another site.
Download Markdown (.md)Soroush’s Product Wiki · https://wiki.soroush.uk/topic/cross-site-request-forgery/
01What it means
Cross-Site Request Forgery (CSRF) exploits credentials, especially cookies, that browsers attach automatically to requests. An attacker induces a victim's browser to perform an action on a target service; if the service cannot distinguish the forged request from an intended one, it acts with the victim's authority.[1][2]
The target must require evidence of intent that another site cannot supply, such as a CSRF token or validated origin.
02Why it matters
CSRF can change account data, initiate transactions, or alter permissions without stealing a password or reading the response. Defenses must bind state-changing requests to the intended site and user interaction.
03When to use it
Apply CSRF defenses to state-changing browser requests authenticated by cookies or other ambient credentials. APIs using explicit authorization headers are generally less exposed to classic CSRF, but XSS, CORS, and token-storage risks still require separate analysis.
04How it works
- An authenticated user visits or loads attacker-controlled content.
- The content submits a request to a target origin using a form, navigation, image, or script-capable mechanism.
- The browser automatically includes qualifying target cookies.
- The target accepts the request if it lacks an unforgeable intent check.
- Defenses reject it using a CSRF token, SameSite policy, origin validation, or required custom header as appropriate.
05A practical example
A profile form requires a per-session CSRF token in addition to the session cookie and verifies the request origin. An attacker's cross-site form can send the cookie but cannot supply the valid token.
06Where it breaks
SameSite reduces but does not eliminate every cross-site interaction risk, and CSRF tokens do not stop same-origin malicious scripts. XSS can often bypass CSRF controls by acting within the trusted origin.
Common mistakes
- Using GET for state-changing operations.
- Relying only on a secret cookie that the browser sends automatically.
- Accepting missing or invalid CSRF tokens for compatibility.
- Assuming CORS blocks form submissions or prevents all CSRF.
07Sources
- Cross-Site Request Forgery Prevention Cheat SheetOWASP Foundation · Security practice guidanceCross-Site Request Forgery Prevention Cheat Sheet
- Using HTTP CookiesMDN Web Docs · Official technical documentationUsing HTTP Cookies