Safeguard
Security Guides•

Which cookie flags should a code review check on session cookies?

Check Secure, HttpOnly and SameSite on session cookies, the __Host- prefix, a narrow Domain scope and a fresh session ID at login. Based on MDN.

Safeguard Security Team
4 min read

On a session cookie, check Secure, HttpOnly and an intentional SameSite value. Prefer the __Host- prefix, avoid a broad Domain attribute, and confirm the session ID is regenerated when a user logs in. These come from the MDN cookie guide.

The flags

FlagWhat MDN says it doesReview note
SecureCookie is sent only over HTTPSExpected on every session cookie
HttpOnlyNot accessible to JavaScript, so it mitigates XSS cookie theftSession cookies should have it
SameSite StrictSent only for requests from the cookie's siteStrictest option. May disrupt links arriving from other sites
SameSite LaxAlso sent on navigation from another siteThe default when no attribute is set
SameSite NoneSent on cross-site requests, requires SecureNeeds a reason, such as an embedded service

Secure does not stop client side access: anyone with disk access, or script when HttpOnly is missing, can read the value.

Scope: Domain and Path

If Domain is omitted, the cookie goes only to the host that set it. Setting Domain, for example to a parent domain, includes subdomains and is less restrictive. In review, a new Domain attribute should prompt a question: does every subdomain deserve to see this cookie? A forgotten or compromised subdomain becomes a path to the session.

MDN notes that Path is not a security boundary and does not protect against reading from another path.

The __Host- prefix

A cookie named with the __Host- prefix must be set with Secure by an HTTPS page, must not have a Domain attribute, and must have Path set to a slash. The result is a cookie sent only to the host that set it. Browsers reject prefixed cookies that break the rules. MDN adds a subtle point: servers must check the full name including the prefix, since browsers do not strip it.

Set-Cookie: __Host-session=opaque-random-id; Secure; HttpOnly; SameSite=Lax; Path=/

Lifetime and fixation

Session cookies have no Max-Age or Expires and are deleted when the browser session ends, though session restore can make them last indefinitely. MDN advises regenerating and resending session cookies whenever a user authenticates, to prevent session fixation, keeping sensitive cookies short lived, and preferring an opaque session ID that the server looks up over storing sensitive data in the cookie.

Steps for a review

  1. Find where cookies are set: the framework session config and any manual header writes.
  2. Check each flag in the table.
  3. Check whether the login handler issues a new session ID.
  4. Check whether the cookie holds data, or only an opaque ID.
  5. If SameSite is None or Domain is broad, ask for the reason in the pull request.

Pair it with CSRF review

SameSite is a helpful layer, not a full defense against cross-site request forgery. The related OWASP CSRF guidance treats it as defense in depth, so review token or header protection on state changing routes at the same time.

Where Safeguard fits

Safeguard scans dependencies and builds SBOMs, including session and framework libraries. Try the free scan at gold.safeguard.sh/scan.

Quick check

What is the default SameSite value?

Per MDN, a cookie without a SameSite attribute is treated as Lax.

Does HttpOnly stop all cookie theft?

No. It blocks script access, which mitigates XSS cookie theft, but it does not stop other routes to the value, such as disk access.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.