Catalog My learning Blog
Application Security for Developers: Secure Coding Course · Phase 2 · Lesson 10 · 4 min read

CSRF Attacks and SameSite Cookies Explained

On this page
  1. What is CSRF? A forged request from your own browser
  2. What makes a site vulnerable to CSRF
  3. SameSite cookies: Strict vs Lax vs None
  4. Why SameSite alone isn't enough
  5. A complete CSRF defense: tokens and Origin checks
  6. Check yourself

What is CSRF? A forged request from your own browser

You're logged in to your bank in one tab. In another, you open a page with a harmless-looking article. Hidden in it is a form that posts to the bank. Your browser attaches your bank cookie, as it always has, and the bank sees a valid, authenticated request to transfer money. That's cross-site request forgery (CSRF).

Code
<!-- On the attacker's page; the victim only has to visit it while logged in -->
<form action="https://bank.example.com/transfer" method="POST" id="f">
  <input type="hidden" name="to" value="attacker-account">
  <input type="hidden" name="amount" value="1000">
</form>
<script>document.getElementById('f').submit();</script>

The attacker never sees the cookie or the response. They don't need to: the damage is the request itself.

What makes a site vulnerable to CSRF

  1. Authentication is automatic: cookies (or HTTP basic auth, or client certificates) the browser sends by itself.
  2. The action can be triggered with a request the attacker can construct: a form post, an image tag for a GET, or a simple fetch.
  3. Nothing in the request proves it came from your own pages.

APIs authenticated only with an Authorization: Bearer header that your JavaScript adds aren't vulnerable to classic CSRF: another site can't make the browser add that header.

SameSite cookies: Strict vs Lax vs None

The SameSite cookie attribute tells the browser when to send a cookie on requests initiated by other sites:

ValueCookie sent on cross-site requests?Effect
StrictNeverStrongest; but following a link from email to your site arrives logged out
LaxOnly on top-level navigations with safe methods (clicking a link, a GET)Blocks cross-site form POSTs, fetches and iframes; the usual choice
NoneAlways (requires Secure)For deliberate cross-site use, such as embedded widgets; no CSRF protection

Chrome and Edge treat cookies without a SameSite attribute as Lax; set it yourself anyway, since other browsers differ. With SameSite=Lax, the hidden form above arrives without the bank cookie, so the request is anonymous and fails.

The attacker's page submits a form to the target site; with SameSite=Lax the browser omits the session cookie, and a CSRF token or Origin check rejects anything that still gets through.
The attacker's page submits a form to the target site; with SameSite=Lax the browser omits the session cookie, and a CSRF token or Origin check rejects anything that still gets through.

Why SameSite alone isn't enough

  • GET requests that change state are still sent on top-level navigation under Lax. Never change anything with GET.
  • "Same site" isn't "same origin": evil.example.com is the same site as app.example.com. A compromised or user-controlled subdomain can still forge requests.
  • Older browsers and embedded webviews may not enforce it.

A complete CSRF defense: tokens and Origin checks

  1. SameSite=Lax (or Strict) on session cookies, plus Secure and HttpOnly (Day 4).
  2. CSRF tokens for cookie-authenticated, state-changing requests: a random value tied to the session, included in forms or a custom header, and checked by the server. The attacker's page can't read it. Most frameworks provide this (Django, Rails, Spring Security, ASP.NET antiforgery, csrf-csrf for Express).
  3. Check Origin (or Sec-Fetch-Site) on state-changing requests and reject other origins. Modern browsers send these headers on cross-site requests. In Express:
JavaScript
app.use((req, res, next) => {
  if (['GET', 'HEAD', 'OPTIONS'].includes(req.method)) return next();
  const site = req.get('Sec-Fetch-Site');
  if (site && site !== 'same-origin' && site !== 'none')
    return res.status(403).json({ title: 'Cross-site request blocked' });
  next();
});
Code
POST /transfer  Sec-Fetch-Site: same-origin   200
POST /transfer  Sec-Fetch-Site: cross-site    403
POST /transfer  Sec-Fetch-Site: same-site     403
POST /transfer  (header absent)               200

none means the user typed the URL or used a bookmark. Requests without the header (older browsers, server-to-server calls) are let through here and must be covered by tokens or SameSite; reject same-site too unless you trust every subdomain.

  1. No state changes on GET, and re-authentication for very sensitive actions (changing email, wiring money).

Check yourself

The same-origin policy stops the attacker's page from reading responses from your site. The attack works only by causing a state-changing request, like a transfer or email change, that the browser sends with the victim's cookies.

No. Lax still sends cookies on top-level GET navigations, so an attacker's link or redirect to the delete URL works. State-changing actions must never use GET.

The browser doesn't attach those tokens automatically; only your own JavaScript adds them, and other sites can't read or add them. CSRF relies on credentials the browser sends by itself, like cookies.

The server issues a random token tied to the session, the page includes it in forms or headers, and the server rejects state-changing requests without a matching token. Attacker pages can't read it.

Browsers send the origin of cross-site requests, so the server can reject state-changing requests from other origins as an extra layer.