Course contentsShow
← Computer Science
Lesson 2468 of 2,87255. Web SecurityFree lesson

SameSite Cookie Attribute

Leveraging SameSite cookie flags (Strict, Lax, None) to prevent browsers from sending cookies with cross-site requests.

SameSite Cookie Attribute

You'll learn how browsers can automatically block cookies from being sent with cross-site requests using the SameSite attribute, providing robust CSRF protection.

What is the SameSite Attribute?

The SameSite cookie attribute is a browser-level defense mechanism that controls when cookies are sent with requests. It tells the browser: "Only send this cookie under these specific circumstances." This prevents attackers from leveraging your cookies when making requests from malicious sites—cutting CSRF attacks off at the source.

Think of it like a security guard at a building entrance. SameSite acts as instructions: "strict" mode means "only let someone in if they entered through our main door," while "lax" means "main door preferred, but links from trusted sources are okay too."

The Three SameSite Values

Strict

Set-Cookie: sessionId=abc123; SameSite=Strict

The most restrictive setting. Cookies are never sent with cross-site requests—even if you click a link from another site. Maximum CSRF protection, but can disrupt legitimate navigation (e.g., clicking a link from email won't include your session cookie).

Lax

Set-Cookie: sessionId=abc123; SameSite=Lax

A balanced approach (and the modern browser default). Cookies are sent with top-level navigation (clicking links) but not with cross-site subrequests like images, iframes, or AJAX calls. Blocks most CSRF attacks while maintaining usability.

None

Set-Cookie: tracking=xyz789; SameSite=None; Secure

Allows cookies to be sent with all cross-site requests (old behavior). Must be paired with the Secure flag (HTTPS only). Used for legitimate cross-site scenarios like embedded widgets or third-party authentication.

Why It Matters for CSRF

SameSite provides defense-in-depth alongside CSRF tokens. Even if developers forget tokens on certain endpoints, SameSite=Lax or Strict prevents the browser from sending authentication cookies with malicious cross-site requests.

Key Takeaway: The SameSite cookie attribute (Strict, Lax, or None) instructs browsers when to send cookies with requests, providing automatic CSRF protection by preventing cookies from crossing site boundaries under dangerous conditions.