For the complete documentation index, see llms.txt. This page is also available as Markdown.

CSRF - Cross Site Request Forgery Attacks

CSRF (Cross-Site Request Forgery) - Overview

1. What it is

  • Attacker tricks the victim's browser into sending a request the victim never intended

  • Victim is already logged in β†’ browser auto-attaches their session cookie β†’ server treats it as legit

  • Partly breaks the same-origin policy's whole point (sites shouldn't be able to act on each other's behalf)

2. Three conditions needed for CSRF to work

3. How the attack actually looks

  • Auto-submitting hidden form on attacker's page:

html

  <form action="https://site.com/email/change" method="POST">
    <input type="hidden" name="email" value="pwned@evil.net" />
  </form>
  <script>document.forms[0].submit();</script>
  • If the action supports GET, even simpler - just an image tag:

html

  • Delivery = same as reflected XSS: link via email/social media, or planted in a comment on a popular site

  • Not cookie-only either - HTTP Basic auth and client cert auth can be CSRF'd too, since browser auto-attaches those too

4. Common defenses (and how each fails)

CSRF tokens

  • Random unpredictable value tied to session, required on every sensitive request

  • Bypass angles:

SameSite cookies

  • Browser withholds cookie on cross-site requests, based on restriction level:

    • Strict - never sent cross-site

    • Lax - sent cross-site only on GET + top-level navigation (default in Chrome since 2021 if unset)

    • None - sent everywhere, needs Secure flag

  • Bypass angles:

Referer-based validation

  • Checks that Referer header matches the app's own domain

  • Bypass angles:

5. Testing checklist

6. Prevention (context for writeups)

  • Token generated with a real CSRF-random source, tied to the session, validated on every state-changing request - not just when present

  • Prefer SameSite=Strict (or Lax at minimum) on session cookies

  • Don't assume same-site = safe - a same-site XSS/gadget bug anywhere on the site can undo SameSite protection entirely

Last updated