SameSite Cookie Attribute
What You'll Learn
You'll learn how the SameSite cookie attribute restricts when cookies are sent with cross-site requests, reducing CSRF attacks and limiting XSS damage.
The Problem: Cookies Travel Everywhere
By default, browsers automatically attach cookies to every request to a domain—even when that request originates from a completely different site. This is how attackers execute CSRF attacks (tricking your browser into making authenticated requests) and how XSS can steal cookies across contexts.
What SameSite Does
The SameSite attribute tells the browser: "Only send this cookie under specific circumstances." It has three values:
SameSite=Strict
The cookie is never sent with cross-site requests. If you're on evil.com and click a link to bank.com, the browser won't send bank.com's cookies until the page fully loads.
Analogy: Your apartment key only works when you approach from inside the building—never from outside.
SameSite=Lax (default in modern browsers)
Cookies are sent only with "safe" cross-site requests like top-level navigation (clicking links), but not with embedded images, iframes, or AJAX calls from other sites.
Analogy: Your key works when you walk through the front door, but not when someone tries to reach through a window.
SameSite=None; Secure
Cookies are sent with all cross-site requests (old behavior), but must use HTTPS. Required for legitimate cross-site integrations.
Security Impact
Set-Cookie: sessionid=abc123; SameSite=Strict; HttpOnly; Secure
This configuration means:
- CSRF: Blocked—attackers can't force authenticated actions
- XSS Cookie Theft: Reduced—even if XSS injects code, cross-origin exfiltration is harder
Key Takeaway: Use SameSite=Strict or Lax on session cookies to prevent browsers from sending them with attacker-initiated cross-site requests, dramatically reducing CSRF risk and limiting XSS attack surface.