Skip to main content
Web DevAuth & SecurityDiagramIntermediate

CSRF (Cross-Site Request Forgery)

KO: CSRF (사이트 간 요청 위조)·EN: CSRF (Cross-Site Request Forgery)·JA: CSRF(クロスサイトリクエストフォージェリ)·Cross-Site Request Forgery

A web attack that tricks an authenticated user's browser into sending unintended state-changing requests to a trusted site using automatically attached cookies

Table of Contents

Structure Diagram

CSRF Attack Vector & Multi-Layer DefenseTap a node
Hidden POSCross-SiteMissing/InMalicious…Victim Br…SameSite …Forged Ac…
ROOTMalicious Site
1 link
Quick Answer

What is CSRF (Cross-Site Request Forgery)?

A web attack that tricks an authenticated user's browser into sending unintended state-changing requests to a trusted site using automatically attached cookies. It is a core concept in Auth & Security within Web Dev.

Core Principles

Cross-Site Request Forgery (CSRF) exploits the browser's habit of automatically attaching cookies to every request for a domain. If a logged-in user visits a malicious page, a hidden HTML form can silently submit POST https://bank.example/transfer—and the browser dutifully sends the victim's session cookie along with it.

Whereas XSS steals data by running code inside the trusted site, CSRF abuses the user's ambient cookie authority to trigger unwanted state changes without ever needing to read the response.

Practical Takeaways

  • Layer 1 — SameSite=Lax or Strict cookies: Blocks browsers from sending session cookies on cross-site POST requests.
  • Layer 2 — Anti-CSRF tokens & Fetch Metadata: Require an unpredictable per-session token (via hidden input or custom header like X-CSRF-Token) and validate Origin / Sec-Fetch-Site headers on state-changing routes.
  • Keep GET requests side-effect-free: Never mutate state via GET, since top-level navigations carry SameSite=Lax cookies.

Notable Examples

  • Built-in framework defenses: Django, Rails, Spring Security, and Next.js Server Actions automatically verify CSRF tokens or Origin headers.
  • Explicit Authorization header APIs: Because browsers never attach Bearer headers automatically, token-header APIs avoid CSRF (though they still require strong XSS defense).