# 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.

- Type: term
- Domain: Engineering & architecture
- Level: Intermediate
- Last reviewed: 2026-09-23
- Original: [Soroush’s Product Wiki](<https://wiki.soroush.uk/topic/cross-site-request-forgery/>)

## What 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.

## Why 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.

## When 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.

## How it works

1. An authenticated user visits or loads attacker-controlled content.
2. The content submits a request to a target origin using a form, navigation, image, or script-capable mechanism.
3. The browser automatically includes qualifying target cookies.
4. The target accepts the request if it lacks an unforgeable intent check.
5. Defenses reject it using a CSRF token, SameSite policy, origin validation, or required custom header as appropriate.

## A 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.

## Where 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.

## Visualization

[View the visualization on the website](<https://wiki.soroush.uk/topic/cross-site-request-forgery/#visualization-cross-site-request-forgery>)

## Sources

1. [Cross-Site Request Forgery Prevention Cheat Sheet](<https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html>) — OWASP Foundation · Security practice guidance
2. [Using HTTP Cookies](<https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Cookies>) — MDN Web Docs · Official technical documentation

## Related topics

- [Cookie](<https://wiki.soroush.uk/topic/cookie/>)
- [Same-Origin Policy](<https://wiki.soroush.uk/topic/same-origin-policy/>)
- [Cross-Site Scripting \(XSS\)](<https://wiki.soroush.uk/topic/cross-site-scripting/>)
