Course contentsShow
← Security
Lesson 1059 of 3,05222. Web Security - Client-Side and SPA SecurityFree lesson

Cookie Scoping and SameSite Attribute

How cookies respect origin boundaries through Domain and Path attributes, plus SameSite's CSRF protection mechanisms.

Cookie Scoping and SameSite Attribute

What you'll learn: How cookies respect origin boundaries through Domain and Path attributes, and how SameSite protects against cross-site attacks.

Understanding Cookie Scoping

While the Same-Origin Policy restricts JavaScript's access to cookies, cookies themselves have their own scoping rules that determine when browsers send them with requests. Two key attributes control this:

  • Domain attribute: Specifies which hosts can receive the cookie. If you set Domain=example.com, the cookie will be sent to example.com and all its subdomains like api.example.com. If omitted, the cookie only goes to the exact host that set it.

  • Path attribute: Limits cookies to specific URL paths. A cookie with Path=/admin only gets sent to requests under /admin/*, not to /profile or other paths.

Think of it like a mail delivery rule: Domain is the street address, Path is the apartment number. The cookie only gets delivered when both match.

The SameSite Attribute: CSRF Protection

The SameSite attribute is your defense against Cross-Site Request Forgery (CSRF). It controls whether cookies are sent with cross-site requests—when you click a link or submit a form from another site:

  • SameSite=Strict: Cookie never sent on cross-site requests. Most secure, but can break legitimate navigation.
  • SameSite=Lax (default in modern browsers): Cookie sent only on "safe" cross-site requests (like clicking a link), not on POST requests or image loads from other sites.
  • SameSite=None: Cookie sent everywhere, but requires Secure flag (HTTPS only).

Example: If evil.com tries to make your browser POST to bank.com/transfer, a SameSite=Lax or Strict cookie won't be included, blocking the CSRF attack.

Key Takeaway: Cookie scoping through Domain and Path controls where cookies travel, while SameSite prevents cookies from being weaponized in cross-site attacks—your primary defense against CSRF.