HTTP Headers

Cookie Security Explained: HttpOnly, Secure, and SameSite

Test Admin October 20, 2026 7 min read 9 views

Three attributes on the Set-Cookie header decide whether a stolen cookie means a stolen account. Here is what HttpOnly, Secure, and SameSite actually protect against, how attackers bypass weak cookie configs, and how to audit your site in minutes.

Cookie Security Explained: HttpOnly, Secure, and SameSite
πŸ“‘ Table of contents

Log into a website and a small string of text becomes your identity: the session cookie. Every request after that carries it, and whoever holds it is you β€” no password required. That is why cookie theft is one of the oldest and still most reliable account-takeover techniques, and why three attributes on the Set-Cookie header decide whether a leaked cookie is a catastrophe or a dead end.

Authentication cookies are bearer tokens. The server issues one at login, then trusts whoever presents it. Common contents include:

  • πŸ”‘ Session identifiers β€” map the cookie to a server-side login state
  • 🎫 Signed auth tokens β€” stateless JWT-style credentials
  • 🧭 Preference and tracking IDs β€” lower value, but still profile data

The moment that string leaves the browser β€” via script access, an unencrypted request, or a cross-site submission β€” the account is effectively transferred to the attacker. The three security attributes each close one of those exit routes.

HttpOnly: the XSS damage limiter

The HttpOnly flag instructs the browser to hide the cookie from JavaScript entirely:

Set-Cookie: session=abc123; HttpOnly

Without it, any successful cross-site scripting injection can simply read document.cookie and exfiltrate the session. With it, the same XSS still fires β€” but the cookie jar is locked, and the attacker walks away with nothing durable. HttpOnly converts a full account takeover into a temporary in-page nuisance.

It costs nothing, works in every browser since 2006, and is still missing on a surprising share of production sites.

Secure: transport-only delivery

The Secure flag means the browser will only transmit the cookie over HTTPS:

  • ❌ No cookie on accidental http:// requests
  • ❌ No cookie on a misconfigured redirect chain that dips to plain HTTP
  • ❌ No cookie on mixed-content pages

Without the flag, one plain-HTTP request on a coffee-shop network leaks the credential in cleartext. With TLS everywhere the risk shrinks β€” but the flag is what guarantees the browser never even tries. The two defenses operate at different layers, which is why you need both. Our TLS analysis covers the transport side of that equation.

SameSite: the CSRF firewall

SameSite controls whether the cookie rides along on cross-site requests β€” the mechanism behind cross-site request forgery:

Value Cross-site GET Cross-site POST Practical effect
Strict blocked blocked Strongest; breaks cross-site link flows
Lax allowed on top-level navigation blocked Default; stops classic CSRF
None sent sent Required for legit cross-site embeds; must pair with Secure

An attacker's site can forge a request to your bank, your admin panel, or your profile page β€” but if the session cookie refuses to accompany it, the forged request arrives anonymous and dies. SameSite=Lax alone defuses the vast majority of CSRF scenarios. Reserve None for cookies that genuinely must work cross-site (embedded widgets, third-party SSO), and treat every None without Secure as a defect.

Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=3600

Every attribute does one job: HttpOnly blocks script theft, Secure blocks wire theft, **SameSiteblocks cross-site abuse. Deployed together they form the minimum bar for any authentication cookie β€” the same bar applied by the__Secure-and__Host-` cookie prefix conventions that modern browsers enforce.

How to audit your site

Manual checks work for a single page: open developer tools, watch each Set-Cookie header, note which flags appear. Real sites serve a dozen cookies across login flows, API subdomains, and legacy paths β€” and a cookie is only as safe as its weakest instance.

That mapping is exactly what a scanner automates. The 2MNY Cookie Security Audit flags every cookie missing any of the three attributes, and the Cookie Security Deep Analysis module goes further: over-scoped Domain= values, missing expiry on session cookies, and prefix-rule violations. Both run as part of a free security scan β€” no agent, no install, results in minutes.

Fixing the gaps

  • πŸ› οΈ Framework defaults first β€” Laravel, Django, and ASP.NET all set these flags via config; find the switch, not a regex
  • 🧨 Legacy cookies β€” anything set by old JavaScript cannot be HttpOnly; migrate it server-side
  • πŸ§ͺ Re-scan after changes β€” flag fixes are one-line edits that are easy to verify with the same security headers check methodology

Cookie hardening will never make a keynote talk β€” it is three keywords on one header. But those three keywords are the difference between "an XSS happened" and "we lost 40,000 sessions," which makes the audit one of the highest value-per-minute checks you can run on any website.

πŸ“’ SHARE 𝕏 in f
T
✍️ WRITTEN BY
Test Admin

Security research and engineering at 2MNY β€” the team behind 282 free website-security scanners and the open-source Website Vulnerability Scanner. We find exposed secrets, misconfigurations and real vulnerabilities, then explain how to fix them in plain language.

Frequently Asked Questions
1 What does the HttpOnly cookie flag do?

HttpOnly tells the browser to deny JavaScript access to the cookie. Even if an attacker injects a script into the page, document.cookie returns nothing, so the session token cannot be stolen client-side. It is set per-cookie in the Set-Cookie response header.

2 Is SameSite=Lax enough to stop CSRF?

Lax blocks cookies on cross-site POST submissions, which stops most classic CSRF attacks, while still allowing top-level navigation GETs so links from other sites keep working. Strict is stronger but breaks cross-site links. None disables the protection and now requires the Secure flag in modern browsers.

3 How do I check my website cookies for these flags?

Open browser developer tools, inspect each Set-Cookie response header, or run a free scan: 2MNY's Cookie Security Audit flags every cookie missing HttpOnly, Secure, or SameSite, and the Deep Analysis module additionally checks scope, expiry, and prefix rules.

4 Does HTTPS make the Secure flag unnecessary?

No. HTTPS protects transport, but the Secure flag is what prevents the browser from ever sending the cookie over an accidental plain-HTTP request, a misconfigured redirect, or a mixed-content page. They solve different layers of the same problem.

Is your website affected?

Find out in minutes β€” free scan, professional report, no credit card.

Start Free Scan