← Glossary

What Are Secure Cookies?

Secure cookies are HTTP cookies carrying the Secure attribute, which instructs the browser to send them only over encrypted HTTPS connections and to refuse them from insecure origins. The attribute governs transport, not the secrecy of the stored value.

Updated:

Secure cookies are cookies marked with the Secure attribute in a Set-Cookie header or through an eligible client-side cookie API. The attribute is a single flag with one job: it tells the browser to include the cookie only in requests made over an encrypted connection, which in practice means the https scheme. Without the flag, an otherwise eligible cookie may be sent over plain HTTP as well as HTTPS. On the unencrypted path, an intermediary may read or alter it. With the flag, the browser withholds it from unencrypted requests entirely. The name promises more than it delivers. The flag itself neither encrypts the cookie on disk nor hides it from scripts, and it does not prevent theft through other channels. It is a transport restriction, and understanding it as exactly that avoids a family of mistakes.

How the Secure Attribute Is Set and Applied

A server sets the attribute by appending it to the cookie's definition, for example Set-Cookie: session=abc123; Secure; HttpOnly; SameSite=Lax; Path=/. From that point the browser enforces two rules described in the MDN cookie documentation. First, the cookie is sent to the server only when the request uses the https scheme. Second, insecure sites, meaning pages loaded over http, cannot set a cookie with the Secure attribute at all; the browser discards such a Set-Cookie header. That second rule blocks setting a Secure-marked cookie through an ordinary insecure response. It does not establish the authenticity of every cookie name received by the application.

Secure cookie transmission is decided per request, not per session. If a request actually travels over plain HTTP, a Secure cookie is withheld, apart from supported local-development exceptions. Modern browsers may upgrade or block mixed-content requests before sending them, which is one more reason modern sites serve everything over HTTPS and redirect plain requests early.

Transport Restrictions and the Localhost Exception

The https requirement has a documented carve-out for local development. The Set-Cookie reference states that the https requirements are ignored when the Secure attribute is set by localhost, and the cookie guide notes that Secure cookies are never sent over unsecured HTTP except on localhost, adding that this exception is not supported by Safari. Developers running a site on a plain http localhost address can therefore test Secure cookies in Chrome and Firefox but will see them dropped in Safari. The behaviour exists because browsers treat localhost as a potentially trustworthy origin, and it should not be relied on for any address other than the local loopback.

Two naming conventions build further rules on the flag. A cookie whose name begins with __Secure- is accepted only if it carries the Secure attribute and was set from a secure page. A cookie beginning with __Host- must additionally omit the Domain attribute and set Path to the root, binding it to a single host. Browsers that understand these prefixes reject non-conforming cookies, which provides additional browser-enforced constraints. A server cannot assume every client sending a Cookie header enforced those rules. Browsers that do not support prefixes accept the cookies regardless, so the prefixes strengthen rather than replace the attribute.

Secure Compared With HttpOnly and SameSite

The three attributes are often set together because each addresses a different channel through which a cookie might be misused.

AttributeWhat it restrictsThreat it reducesWhat it does not do
SecureTransmission over unencrypted connectionsInterception on the network pathHide the cookie from scripts or from the device
HttpOnlyAccess from JavaScript via document.cookieTheft through cross-site scriptingStop the cookie being sent with script-initiated requests
SameSiteInclusion in cross-site requestsCross-site request forgeryEncrypt or hide anything

Secure vs HttpOnly is the pairing people most often confuse. A Secure cookie that lacks HttpOnly is readable by scripts in an eligible page context, including injected scripts, subject to the cookie's domain and path scope. An HttpOnly cookie that lacks Secure cannot be read by scripts but can be captured by anyone observing a plain HTTP request. Session identifiers need both. The MDN guide also records a dependency between Secure and SameSite: a cookie with SameSite=None, which opts into being sent on cross-site requests, must also carry Secure or the browser rejects it. The same requirement applies to the newer Partitioned attribute. Secure has thus become a precondition for several other cookie features, which is why discussions of SameSite so frequently mention it.

Where Secure Cookies Are Expected

Session identifiers and remember-me tokens need protected transport because their disclosure can enable impersonation. Other cookies, including CSRF tokens and consent preferences, also benefit from Secure on an HTTPS site, although their sensitivity and need for script access differ. Public sites served entirely over HTTPS gain nothing by omitting the flag and lose a safety net if a single http link or a misconfigured redirect ever appears. Security guidance such as the OWASP session management cheat sheet lists Secure among the attributes a session cookie should always carry. Development and staging environments are the usual place where the flag is dropped for convenience and then forgotten, which is exactly the situation the localhost exception is meant to cover without removing the attribute from code.

What the Attribute Cannot Protect

The word Secure describes the channel and nothing else. The MDN guide is explicit that the attribute does not prevent all access to sensitive information: someone with access to the client's disk, or a script when HttpOnly is absent, can read and modify the value. The cookie is stored in the browser's cookie database in whatever form the browser uses, which is not necessarily encrypted. Session hijacking remains possible if the token leaks through a script, a malicious extension, a compromised device or a server-side log. A Secure cookie can also carry a worthless or guessable value; the attribute says nothing about how the value was generated.

Secure cookies address the plain-text network path. A sensitive session still needs controls on how its token is generated, accepted and revoked. HTTPS cookies set with Secure, HttpOnly, an appropriate SameSite value, a short lifetime and a high-entropy value are the combination that makes a session cookie reasonably safe, and the Secure flag is the piece that keeps the rest from being undone by a single unencrypted request.

Frequently asked questions

Does Secure encrypt the stored cookie?

No. Secure controls transport, not the cookie store's encryption at rest. Browsers and operating systems may separately protect stored cookies. The flag itself does not determine whether an attacker with disk or process access can recover a value.

Can JavaScript read a Secure cookie?

Yes, unless the cookie also carries HttpOnly. Secure and HttpOnly address different channels: Secure blocks unencrypted transport, HttpOnly blocks script access. A session cookie should normally have both.

Why are Secure and SameSite often discussed together?

Because browsers require the Secure attribute whenever SameSite=None is used, and also for the Partitioned attribute. Secure has become a precondition for opting a cookie into cross-site or partitioned use, so the two are set in the same header.

Can a Secure cookie be set over plain HTTP?

Ordinary insecure HTTP responses cannot set a Secure-marked cookie; supported localhost exceptions exist for development. This protects that specific setting path, but does not authenticate arbitrary Cookie headers or replace server-side validation of session identifiers.

Does the Secure attribute prevent session hijacking?

Only the variant that relies on sniffing unencrypted traffic. A token can still leak through injected scripts, malicious extensions, a compromised device or server logs. Secure is one layer; HttpOnly, SameSite, short lifetimes and high-entropy values are the others.