HttpOnly, Secure, and SameSite Cookies Explained

Learn what the HttpOnly, Secure, and SameSite cookie attributes do, how they help protect sessions, and what they do not protect against.

HttpOnly, Secure, and SameSite Cookies Explained

What do HttpOnly, Secure, and SameSite mean on a Cookie?

Cookies can carry a session identifier between a browser and a server. Three attributes help control how a cookie is used: HttpOnly, Secure, and SameSite. They address different risks, so it helps to understand each one separately.

HttpOnly: keep the cookie value out of page JavaScript

When a cookie has the HttpOnly attribute, browser JavaScript cannot read or change its value through APIs such as document.cookie. The browser can still attach the cookie to matching HTTP requests, and the server can read it there.

Set-Cookie: __Host-session=opaque-session-id; Path=/; HttpOnly

This helps protect a session identifier from being copied out by an XSS script. It does not remove the XSS vulnerability: malicious JavaScript running in the page may still make authenticated requests using the browser's session. Keep preventing XSS and enforce authorization on the server. MDN: Using HTTP cookies · OWASP: Session Management

Only the server can set the HttpOnly attribute in a Set-Cookie response header. Client-side JavaScript cannot turn an ordinary cookie into an HttpOnly cookie.

Secure: send the cookie over HTTPS

The Secure attribute tells the browser to send the cookie only over secure HTTPS connections (with a localhost exception in some browsers). It helps prevent the cookie from being exposed over unencrypted HTTP. It does not encrypt the cookie on the user's device or prevent JavaScript from reading it; use HttpOnly for that separate control. MDN: Secure cookie configuration

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

Production sites should serve authentication over HTTPS and mark session cookies Secure.

SameSite: control sending in cross-site contexts

The SameSite attribute controls whether the browser includes a cookie with requests initiated from a different site. It helps reduce some cross-site request forgery (CSRF) attacks. The common values are:

  • Strict: send the cookie only in same-site contexts. This is restrictive and may mean a user arriving from an external link is not signed in on that first navigation.
  • Lax: allow the cookie on same-site requests and some top-level cross-site navigations using safe methods, such as following a link. This is a common balance for session cookies.
  • None: allow cross-site sending. It must be paired with Secure.

SameSite is a useful layer of CSRF defense, not a complete replacement for a CSRF token or other defenses when the application needs them. Choose the value based on your app's navigation, embedding, and authentication flows; don't leave the behavior to browser defaults. MDN: Set-Cookie reference · OWASP: CSRF Prevention

Put them together

A server-managed session cookie might look like this:

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

The __Host- prefix asks supporting browsers to enforce additional scope rules: the cookie must be Secure, must use Path=/, and must not specify a Domain. This can reduce risks from less-trusted subdomains. Use a framework's session handling where possible, and confirm its cookie settings match your application's needs. MDN: Cookie prefixes

AttributeMain effectIt does not…
HttpOnlyPrevent page JavaScript from reading the cookie valueStop injected scripts from making requests with the active session
SecureRestrict sending to HTTPSEncrypt data stored on the device or make an insecure site secure
SameSiteLimit cookie sending in cross-site contextsReplace every CSRF defense or authorization check

Try it yourself

In your development environment, inspect a server-set session cookie in the browser's developer tools. Find its HttpOnly, Secure, and SameSite values. Then compare those settings with the Set-Cookie response header. Do not paste a real session cookie into a tutorial, screenshot, or public bug report.

Recap

  • HttpOnly prevents page JavaScript from reading the cookie value.
  • Secure restricts cookie sending to HTTPS.
  • SameSite influences sending in cross-site contexts and helps with CSRF defense.
  • These attributes complement one another, but none makes XSS, CSRF, or server authorization concerns disappear.

What's next?

Next, we’ll learn what JSON is and how it differs from a JavaScript object.

Written by

Dev Likhon

Dev Likhon

Full Stack Developer & Tech Educator

6+ years of professional development experience.

About Me
Share:

Comments

Share what you learned or ask a question.

Loading comments...