← Glossary

What Are HttpOnly Cookies?

HttpOnly cookies carry an attribute that stops JavaScript in the page from reading or writing them while the browser continues to attach them to matching HTTP requests. The flag narrows what an injected script can steal but does not by itself prevent cross-site scripting or request forgery.

Updated:

HttpOnly cookies are cookies set with the HttpOnly attribute, which tells the browser that the cookie may be used only in HTTP traffic and must not be exposed to scripts running in the page. Code that calls document.cookie returns accessible non-HttpOnly cookies whose scope matches the document, and cannot create or overwrite a cookie carrying the flag. The browser, however, keeps treating the cookie normally: whenever a request matches the cookie's domain, path and other attributes, the cookie goes out in the Cookie header. The attribute therefore changes one thing precisely, script visibility, and leaves everything else about the cookie's behaviour unchanged. Its purpose is to shrink the damage an injected script can do, chiefly to stop it from copying a session identifier and sending it elsewhere.

How HttpOnly Changes Script Access

A server sets the flag by appending it to the Set-Cookie header, usually together with the other security attributes:

Set-Cookie: sid=opaque-session-token; Path=/; Secure; HttpOnly; SameSite=Lax

From that point the cookie is invisible to page scripts. MDN's reference for the header describes the attribute as forbidding JavaScript access to the cookie, for example through the document.cookie property, and notes that this mitigates attacks against cross-site scripting. The newer, promise-based Cookie Store API follows the same rule and does not surface HttpOnly cookies either. There is also a defence-in-depth naming convention: a cookie whose name begins with __Http- must carry both the Secure and HttpOnly attributes, and browsers that support the prefix reject any attempt to set such a cookie from script, which provides an additional constraint in supporting browsers. It does not authenticate arbitrary clients sending a Cookie header.

Normal page code cannot apply the attribute; websites set it through an HTTP response. Script cannot add it to an existing cookie or set a new cookie with it, because the whole point is that script is excluded from that class of cookie. If an application needs a value both in script and on the server, it can expose a separate, non-sensitive value to the page. Duplicating a session secret in a readable cookie would undo the protection.

Cookies Still Sent with Requests

The most common misunderstanding is that HttpOnly keeps the cookie out of requests made by scripts. It does not. When page code calls fetch() or uses XMLHttpRequest with a credentials mode that permits cookies, the browser can attach an eligible HttpOnly cookie; MDN states this explicitly. The script never reads the value, but the server receives it. Domain, path, Secure, SameSite and browser privacy rules still determine eligibility, including for images and frames. Same-site does not necessarily mean same-origin.

This design is what makes the flag useful for session cookies without breaking applications. A single-page application can keep calling its own API, without reading its session cookie. An injected script loses the straightforward route of extracting that identifier through a cookie API. OWASP's session management guidance calls HttpOnly mandatory for session identifiers for this reason, while also noting that it protects only the cookie's confidentiality. A script that cannot read the cookie can still issue requests that carry it, so injected code may perform permitted actions during the active session. Sensitive operations can still require fresh authentication or other server checks.

HttpOnly vs Secure and SameSite

HttpOnly belongs to a family of cookie security attributes that each control a different axis. They are not alternatives and are routinely combined on the same cookie.

AttributeControlsThreat it addressesWhat it leaves open
HttpOnlyWhether page script can read or write the cookieTheft of the cookie value through injected scriptRequests made by that script still carry the cookie
SecureWhether the cookie is sent over plain HTTPInterception on an unencrypted connectionScript access and cross-site sending
SameSiteWhether the cookie accompanies cross-site requestsCross-site request forgery and some leakageSame-site script access and interception

The comparison usually phrased as HttpOnly vs Secure is really about two unrelated channels. Secure says the cookie travels only when the request uses the https scheme, subject to a localhost exception in browsers. Ordinary insecure websites cannot set Secure cookies. The flag does not prevent exposure at a compromised endpoint or after TLS termination. HttpOnly says nothing about transport. A cookie can be HttpOnly and still be sent in clear text over HTTP if Secure is missing, and a Secure cookie can be read by any script on the page if HttpOnly is missing. Session cookies that lack either flag have a gap.

SameSite governs a third axis: whether the browser includes the cookie when the request originates from a different site. With Strict the cookie goes only with same-site requests; with Lax it also accompanies top-level navigations using safe methods; with None it is eligible for cross-site sending and Secure is required; browser privacy controls may still block it. Some browsers treat a missing SameSite as Lax by default, but relying on that default is weaker than setting it. OWASP recommends Strict or Lax for session cookies.

Where HttpOnly Fits in Session Protection

Browsers enforce the attribute; servers still have to do their part. A session identifier should be long, random and meaningless, so that it cannot be guessed, and the server should bind it to a session record that it can expire and revoke. The identifier should be regenerated after login or any privilege change to defeat fixation, and both idle and absolute timeouts should be enforced on the server rather than trusted to the cookie's expiry. Logging out must invalidate the server-side session, because an HttpOnly cookie that the browser deletes may already have been copied by other means, such as malware with access to the cookie store or a misconfigured proxy.

In that framework, HttpOnly is the control that keeps the identifier away from the one adversary most likely to be present in a web page, injected script. It does not stop the script from being injected, which requires context-appropriate encoding, safe DOM handling and, as defence in depth, Content Security Policy. It does not stop cross-site request forgery, which is the job of SameSite and anti-forgery tokens. It does not protect the cookie in transit, which is the job of Secure and TLS. Treating the flag as a complete answer to any of those problems is the error most worth avoiding.

Frequently asked questions

Does HttpOnly stop all XSS attacks?

No. HttpOnly blocks direct access to the cookie through page cookie APIs, but injected code may still read page content or send authenticated requests. Preventing XSS requires safe handling of untrusted content. Keeping the session value hidden limits one consequence of a vulnerability; it does not remove that vulnerability.

Can the browser still send an HttpOnly cookie?

Yes, provided the request satisfies the cookie scope, credentials mode and browser policy. HttpOnly itself does not exclude fetch, XMLHttpRequest, navigations or resource loads. If an API call lacks the cookie, check those conditions before assuming the flag is responsible.

Can HttpOnly be combined with SameSite?

Yes, and session cookies usually should carry both, along with Secure. HttpOnly controls script access, SameSite controls whether the cookie joins cross-site requests, and Secure restricts it to encrypted connections. They address different threats and do not conflict with one another.

Can JavaScript set an HttpOnly cookie?

Ordinary page JavaScript cannot set the flag or directly overwrite a matching HttpOnly cookie through a cookie API. A server response can set it. Supporting browsers also enforce the __Http- prefix requirements, but a server must not treat a prefix alone as proof that a request came from a trusted client.

Does HttpOnly protect a cookie from interception on the network?

No. Transport protection comes from the Secure attribute and TLS. An HttpOnly cookie without Secure can still travel in clear text over plain HTTP, where anyone on the path can read it, so the two attributes are normally set together on sensitive cookies.