liveBook · Manning

SameSite cookie

As explained in Grokking Web Application Security

The SameSite attribute is a cookie attribute that controls whether a browser sends the cookie with cross-site requests.

A SameSite cookie controls whether the browser includes a cookie when another site sends a request to your application. This limits cross-site request forgery (CSRF): an attacker may otherwise trick a logged-in user into submitting an unwanted action, such as posting content or deleting an account. section 2.14

Choose the policy according to the balance between protection and sign-in convenience:

  • SameSite=Strict strips cookies from cross-site requests, including a user’s click through an external link. It provides stronger isolation but may require the user to log in again. section 2.14 section 6.11
  • SameSite=Lax permits cookies on cross-site GET requests but strips them from POST, PUT, and DELETE requests. This preserves ordinary link navigation while protecting state-changing requests. section 2.14
  • Add SameSite to sensitive cookies, including session and anti-CSRF cookies. Treat it as an additional defense rather than a replacement for anti-CSRF tokens. section 6.11

Cookie configuration

A practical session-cookie header is: Set-Cookie: session_id=2308797c-348a-4939-9049; SameSite=Lax. For a stricter policy, use SameSite=Strict alongside Secure and HttpOnly.

Modern browsers use Lax when SameSite is omitted, but explicitly adding the attribute supports users of older browsers. Also design GET requests without side effects and keep anti-CSRF protections for stronger coverage. section 2.14 section 6.18

How Grokking Web Application Security covers it

How deep it goes
Explains Strict and Lax behavior by request type, browser defaults, usability tradeoffs, and defense-in-depth limits.
Examples
Uses Set-Cookie examples with SameSite=Lax and SameSite=Strict, including Secure and HttpOnly flags.
What it assumes
Assumes basic HTTP cookies and request methods while explaining CSRF attacks and cross-site cookie behavior.
What you can do after
Apply SameSite policies to sensitive cookies and combine them with anti-CSRF tokens and side-effect-free GET requests.
How it connects
Connects SameSite cookies with CSRF, session cookies, anti-CSRF tokens, Secure, HttpOnly, and safe HTTP methods.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Web Application Security. The section links point to the parts of the book it draws on.