termEngineering & architecture

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)

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]

Forged authenticated action Attacker-controlled content makes the victim browser send ambient credentials
Attacker siteVictim browserTarget service
  1. 01Target service Victim browserExisting authenticated sessionQualifying cookies are stored
  2. 02Attacker site Victim browserInduce cross-site requestForm, navigation, image, or other mechanism
  3. 03Victim browser Target serviceRequest with ambient cookiesBrowser attaches qualifying credentials
  4. 04Target service Victim browserReject without intent proofToken, SameSite, origin, or custom-header defense

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

  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.

05A practical example

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

  1. Cross-Site Request Forgery Prevention Cheat SheetOWASP Foundation · Security practice guidanceCross-Site Request Forgery Prevention Cheat Sheet
  2. Using HTTP CookiesMDN Web Docs · Official technical documentationUsing HTTP Cookies
View saved topics

Start typing to search titles, definitions, categories, and frameworks.