Skip to content

How does CSRF work when the attacker cannot read cookies?

IntermediateAsked very oftenConcept roundConceptWeb Security
Asked atPayPalStripeGoogle
#csrf#samesite#cookies#csrf-tokens#same-origin-policy

Company tags are compiled from public interview reports and are signals, not guarantees.

What interviewers are testing

CSRF separates people who understand the browser credential model from people who repeat "use a CSRF token". Interviewers probe why the attacker never needs to read the cookie, why the same-origin policy does not help, and whether you know that POST-only APIs and default-Lax cookies change the risk surface without eliminating it. The best answers connect browser policy, server validation, and API design into one story.

Mental model

The browser chooses which cookies to attach by looking at the request destination, not at the page that initiated it. So evil.com can make the victim browser send an authenticated request to bank.com while never reading the cookie or the response. Defenses either stop the browser from attaching credentials (SameSite) or make the server verify intent independently of ambient cookies (CSRF tokens, Origin checks) — and state-changing GET endpoints break both stories.

Step-by-step solution

  1. Step 1: Cookies travel by destination

    Cookies are not attached by the page that initiates a request; they are attached by the browser according to the request destination origin. That single fact is the foundation of CSRF. When the victim is logged into bank.com, the session cookie lives in the browser cookie store. Any page, including evil.com, can cause the browser to submit a form, load an image, or fire a no-cors fetch whose destination is bank.com, and the browser will attach the bank cookie because the destination matches. The attacker never sees that cookie and does not need to. The animation follows the request: the evil site triggers a transfer POST, the browser reaches into the cookie store, attaches the session cookie, and the bank server processes the request as the logged-in user. The response is unreadable cross-origin, but the money already moved.

  2. Step 2: Riding, not stealing

    Candidates often assume CSRF requires stealing a session token, so they answer with HTTPS or token storage and miss the point. CSRF never reads the cookie: the attacker page only causes the browser to send it, and the side effect on the server is the payload. That is why the same-origin policy does not save you — it blocks reading responses, not sending requests. The animation shows this explicitly: on the forged request the cookie store hands the session cookie to the browser, which forwards it to the bank, while the evil site learns nothing back. Mechanisms that trigger a credentialed cross-site request include auto-submitted forms, image and link tags, and fetch with mode no-cors. Because the response body is irrelevant, blocking script access changes nothing. The defense must live either in the browser cookie policy or in server-side request validation.

    html
    <!-- evil.com: no script needed, no cookie access needed -->
    <form action="https://bank.com/transfer" method="POST" id="f">
      <input type="hidden" name="to" value="attacker" />
      <input type="hidden" name="amount" value="5000" />
    </form>
    <script>document.getElementById('f').submit();</script>
  3. Step 3: CSRF tokens prove intent

    If the browser attaches cookies automatically, the server needs a second signal that the request came from the application's own page and not from a forged one. The standard answer is a CSRF token: a random, per-session value embedded in forms or sent as a custom header, which the server compares on every state-changing request. The attacker cannot read the token because the same-origin policy prevents cross-origin reads, and cannot guess it because it is unpredictable. The animation shows both paths: the legitimate submit carries the cookie plus a matching token and is accepted; the forged submit carries the cookie but no valid token and is rejected. Implementation details matter — bind the token to the session, compare in constant time, and rotate it on login. A double-submit cookie is an acceptable stateless variant when SameSite is also enforced.

  4. Step 4: SameSite: the browser default

    SameSite gives the browser a veto before the cookie is ever attached. SameSite=Strict never sends the cookie on cross-site requests; SameSite=Lax allows it only on top-level GET navigations and withholds it from cross-site POSTs, iframes, and scripted requests; SameSite=None requires Secure and sends everywhere, which is the setting to audit carefully. Modern browsers default unset cookies to Lax, which quietly removed a large share of the CSRF surface. The animation sends the same forged POST twice: first the Lax cookie is withheld by the browser, then the request arrives at the bank with no session and fails closed. The caveat is the GET exception — a top-level link still carries a Lax cookie, so any GET endpoint that changes state remains exploitable. Never change state on GET.

  5. Step 5: POST-only is not enough

    Some engineers conclude that accepting only POST blocks CSRF because an attacker cannot forge a POST. They can: a hidden HTML form with method post, submitted automatically by script or by an onload handler, sends a simple cross-site request with no preflight, and cookies ride along. The misconception comes from confusing POST with non-simple CORS requests. Requiring Content-Type application/json or a custom header raises the bar because those trigger a preflight that a cross-site form cannot satisfy, but that is a side effect of CORS rather than of the method itself. Meanwhile GET endpoints with side effects are the worst case: links, images, and prefetchers all trigger them. The animation shows a form-based cross-site POST succeeding while a JSON request is stopped by preflight. Keep state changes on POST and still require tokens or SameSite.

    javascript
    // a simple form POST needs no preflight — CSRF works
    form.method = 'POST';
    form.action = 'https://bank.com/transfer';
    
    // a JSON fetch is non-simple: the browser preflights it,
    // and without CORS approval the request never reaches the bank
    fetch('https://bank.com/transfer', {
      method: 'POST',
      credentials: 'include',
      headers: { 'Content-Type': 'application/json' },
    });

Edge cases & traps

  • Assuming POST-only is safe: HTML forms POST cross-site as simple requests without preflight — require a CSRF token or SameSite instead of trusting the method.
  • Forgetting GET side effects: links, images, and prefetchers trigger top-level or subresource GETs that still carry Lax cookies — never change state on GET.
  • Using SameSite=None without Secure: browsers reject insecure None cookies, and None sends them on every cross-site request — pair None with Secure and a CSRF token.
  • Trusting the Origin header when it is absent: some clients omit it — fall back to Referer, validate against an allowlist, and fail closed for state-changing requests.
  • Storing SPA tokens in localStorage to dodge CSRF: the vulnerability disappears but XSS theft takes over — pick the threat you can mitigate and layer defenses.

Follow-up questions

Go deeper: Explore the Web Security visualizer