SameSite Cookie Attribute
What you'll learn: How to use the SameSite cookie flag to control when browsers send cookies with cross-site requests, blocking many CSRF attacks automatically.
The Core Problem
Browsers traditionally send cookies with every request to a domain, even when that request originates from a different site. This behavior enables CSRF attacks—malicious sites can trigger actions on your behalf because your authentication cookies go along for the ride.
What is SameSite?
The SameSite attribute is a cookie flag that tells browsers: "only send this cookie under certain conditions." It's like putting a return address restriction on a letter—you control whether it can be delivered from anywhere, or only from trusted locations.
The Three Values
SameSite=Strict
The most restrictive. Cookies are never sent with cross-site requests. If you click a link from another site to example.com, your cookies won't be included—even for top-level navigation. This prevents CSRF completely but may disrupt user experience (users appear "logged out" when arriving from external links).
SameSite=Lax (default in modern browsers)
A balanced approach. Cookies are sent with top-level navigation (clicking links) but not with cross-site subrequests (images, iframes, AJAX, forms using POST). This blocks most CSRF attacks while maintaining reasonable usability.
SameSite=None
No restrictions—cookies sent with all requests, like the old behavior. Requires the Secure flag (HTTPS only). Use only when you specifically need cross-site cookie transmission (embedded widgets, SSO scenarios).
Setting the Attribute
Set-Cookie: sessionId=abc123; SameSite=Strict; Secure; HttpOnly
Set-Cookie: sessionId=abc123; SameSite=Lax; Secure
Set-Cookie: tracking=xyz; SameSite=None; Secure
Key Takeaway: Use SameSite=Strict or Lax on authentication cookies to prevent browsers from sending them with cross-site requests, providing automatic CSRF protection with no server-side token logic required.