Illustration of a browser cookie protected by a padlock and shield on a dark navy background

Cookie security flags are attributes you add to the Set-Cookie header to control how a browser stores and sends a cookie. Three of them carry almost all the weight: Secure stops the cookie travelling over plain HTTP, HttpOnly hides it from JavaScript, and SameSite limits when the browser attaches it to cross-site requests. For any cookie that keeps a user logged in, the safe default is Secure; HttpOnly; SameSite=Lax.

Why this matters for your website

A session cookie is the equivalent of a door key handed to a visitor. If an attacker copies it, they do not need the password, the email account or the second factor. They simply replay the cookie and become that user until it expires.

For a WooCommerce store or a small SaaS, the stolen session is usually an administrator or a customer with saved payment details. The flags cost nothing, break almost nothing when set correctly, and remove the three easiest ways to steal or abuse that key.

What a session cookie actually is

When you log in, the server generates a random session identifier and sends it back in a Set-Cookie response header. The browser stores it and attaches it to every subsequent request to that site. The server looks up the identifier and knows who you are.

That means the cookie value is the authentication. The OWASP Session Management Cheat Sheet treats protecting it in transit, in the browser and across sites as three separate jobs, which is exactly what the three flags do.

The three attacks the flags defend against

Interception over HTTP, blocked by Secure

Without the Secure attribute, a browser will send the cookie over an unencrypted HTTP connection. Anyone on the same public Wi-Fi, or anywhere on the network path, can read it. A single HTTP image request to your domain is enough to leak a session.

The fix: add Secure to every cookie, and pair it with HSTS so browsers never try HTTP in the first place. We cover that header in our security headers explained guide.

Theft through cross-site scripting, blocked by HttpOnly

If an attacker gets JavaScript onto your page, through a vulnerable plugin, a comment field or a compromised third-party script, that script can read document.cookie and post the contents to a server they control. The user sees nothing.

The fix: HttpOnly makes the cookie invisible to document.cookie. The XSS flaw still needs fixing, but the session survives it.

Cross-site request forgery, limited by SameSite

Browsers historically attached your cookies to requests triggered by any site. A hidden form on an attacker's page could make your logged-in browser change an email address or place an order. SameSite controls this.

  • Strict — the cookie is never sent from another site, including normal links. Users arriving from Google or email appear logged out on the first click.
  • Lax — sent on top-level navigations that use GET, not on cross-site POST requests or hidden frames. This is the practical default for session cookies.
  • None — always sent, and only accepted by browsers when combined with Secure. Required for cookies used inside third-party iframes or embedded widgets.

SameSite reduces CSRF exposure, it does not remove the need for anti-CSRF tokens on state-changing requests.

Diagram mapping three attacks to the three cookie flags that block them
Each flag blocks a different attack path; only all three together protect the session.

An annotated Set-Cookie header

Set-Cookie: __Host-sid=8f2c...e1;   # name (with prefix)
  Path=/;                           # scope inside the site
  Secure;                           # HTTPS only
  HttpOnly;                         # invisible to JavaScript
  SameSite=Lax;                     # not sent on cross-site POSTs
  Max-Age=3600                      # expires after one hour

Note what is absent: no Domain attribute, so the cookie is not shared with subdomains. The full attribute list is documented on MDN's Set-Cookie reference.

How to set cookie flags in your stack

PHP sessions — set the parameters before session_start():

session_set_cookie_params([
  'path' => '/', 'secure' => true,
  'httponly' => true, 'samesite' => 'Lax',
]);
session_start();

WordPress — set both the WordPress Address and Site Address to https://, and add define('FORCE_SSL_ADMIN', true); to wp-config.php. WordPress then marks its authentication cookies Secure and HttpOnly. Cookies set by plugins are not covered, so enforce the flags at the server as well.

Express (Node.js):

res.cookie('sid', value, {
  httpOnly: true, secure: true,
  sameSite: 'lax', path: '/'
});

Django — in settings.py:

SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = "Lax"
CSRF_COOKIE_SECURE = True

Rails — in an initializer:

Rails.application.config.session_store :cookie_store,
  key: "_app_session", secure: true,
  httponly: true, same_site: :lax

nginx as a reverse proxy — a safety net for cookies set by code you do not control (nginx 1.19.3 and later):

proxy_cookie_flags ~ secure httponly samesite=lax;

Apply the proxy rule last and retest logins, checkout and any embedded widget, because a blanket HttpOnly will break cookies a front-end script genuinely needs to read.

Decision tree for choosing between SameSite Lax, Strict and None
Use this decision tree to pick the SameSite value for each cookie you set.

Cookie prefixes: __Host- and __Secure-

Prefixes are a naming convention browsers enforce. A cookie named __Secure-sid is only accepted if it carries Secure and came from an HTTPS page. A cookie named __Host-sid must additionally have Path=/ and no Domain attribute, which stops a compromised subdomain from overwriting it.

Use __Host- for session cookies whenever your application does not need to share them across subdomains.

How to check your own site

Open your browser developer tools, go to the Application or Storage tab, and read the Cookies table. It shows the Secure, HttpOnly and SameSite columns for every cookie on the page. Log in first, because session cookies only appear after authentication.

For an outside view, Scantinel (scantinel.ai) is an automated website security assessment platform that runs OSINT, network, and OWASP Top 10 web vulnerability tests against ownership-verified assets and delivers actionable, business-owner-legible reports. The Essential scan audits the flags on every cookie your site sets, alongside TLS configuration and security headers, and finishes in minutes. It is an automated assessment, not a penetration test, and it will not see cookies that only appear behind a login unless you run an authenticated Premium AI scan.

Start with the free website security check, or see plans and pricing. For the wider context, read our website security guide.

Key takeaways

  • Set Secure; HttpOnly; SameSite=Lax on every session and authentication cookie.
  • Secure defends against interception, HttpOnly against XSS theft, SameSite against CSRF; you need all three.
  • SameSite=None is only valid with Secure, and only belongs on cookies used in third-party contexts.
  • Name session cookies with the __Host- prefix when subdomain sharing is not required.
  • Enforce flags at the proxy as a safety net, then retest login, checkout and embedded widgets.

Frequently asked questions

What does the HttpOnly flag do?

HttpOnly tells the browser that the cookie must not be readable by JavaScript through document.cookie. The cookie is still sent with every HTTP request as normal, so logins keep working. Its purpose is to contain the damage if a cross-site scripting flaw ever reaches your pages.

Should all cookies be SameSite Strict?

No. Strict means a visitor following a link from Google, a newsletter or a payment provider arrives without their session cookie and appears logged out. Lax is the right default for session cookies; reserve Strict for high-value cookies such as an admin session where the extra friction is acceptable.

Do cookie flags affect analytics cookies?

They can. Analytics and consent scripts usually set their cookies from JavaScript and need to read them back, so a blanket HttpOnly rule at the proxy will break them. Apply Secure to everything, but scope HttpOnly to cookies your server sets, and verify tracking still reports after any change.

Published June 2025. Cookie behaviour changes as browsers update, so check primary sources such as OWASP, MDN, NVD and WPScan before acting on any security guidance.