CodePark/ questions / Cookies & state
START PRACTICINGPRACTICE
Backend question · HTTP & API design › Cookies & state

Explain each attribute in this Set-Cookie header.

Set-Cookie: sid=8f2k1; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=3600

Each attribute sets one rule for the cookie:

  • sid=8f2k1 is the cookie's name and value.
  • HttpOnly hides the cookie from JavaScript. document.cookie cannot read it. This limits damage from XSS (cross-site scripting: an attacker's script running on your page), because the script cannot steal the cookie.
  • Secure means the browser only sends the cookie over HTTPS.
  • SameSite=Lax controls when the cookie goes with requests from other sites. Details below.
  • Path=/ makes the cookie apply to every path on the host.
  • Max-Age=3600 makes it a persistent cookie that expires in one hour (3600 seconds).

SameSite=Lax in detail. The browser sends the cookie:

  • with same-site requests.
  • with top-level GET navigations from other sites, like clicking a link to your site.

It does not send it with cross-site subrequests (images, iframes, fetch calls) or cross-site POSTs.

Max-Age in detail. Without Max-Age or Expires, it would be a session cookie. The browser removes a session cookie when the browser session ends. If both Max-Age and Expires are set, Max-Age wins.

There is no Domain attribute. So the cookie is host-only: the browser sends it only to the exact host that set it, not to subdomains.

  • HttpOnly means JavaScript cannot read the cookie.
  • Secure means the cookie is sent over HTTPS only.
  • SameSite controls whether the cookie is sent with cross-site requests.
  • Max-Age or Expires makes a persistent cookie; without them it is a session cookie. If both are set, Max-Age wins.
  • No Domain attribute means the cookie is host-only.