Skip to main content
All Terms Glossary

HttpOnly Cookies

Definition

HttpOnly cookies are cookies that cannot be accessed through client-side JavaScript, providing protection against XSS attacks attempting to steal session tokens. This attribute ensures that even if an attacker manages to execute malicious scripts, they cannot directly access these cookies. It's a crucial security measure for session management and authentication.

What HttpOnly cookies are

An HttpOnly cookie is one the server marks with the HttpOnly attribute in its Set-Cookie header. That single flag tells the browser to keep the cookie out of reach of client-side scripts: it is still attached automatically to matching HTTP requests, so the application works normally, but JavaScript running on the page cannot read or write it through document.cookie or the Cookie Store API. The attribute is aimed squarely at session and authentication cookies, the ones an attacker most wants. It does not change how the cookie is transmitted or scoped; that is the job of the separate Secure, SameSite, Domain, and Path attributes.

Why HttpOnly matters

The most direct way to abuse a cross-site scripting flaw is to read the victim's session cookie and ship it to an attacker-controlled server, letting them replay the session and impersonate the user. HttpOnly removes that primitive: even code that successfully executes on the page cannot see the cookie value, so it cannot exfiltrate it by simple reads. That raises the bar meaningfully for session hijacking. It is important to be precise about the limit, though: HttpOnly stops the cookie from being read, not from being used. An injected script can still issue authenticated requests in the browser, riding the cookie the browser attaches automatically.

Using HttpOnly effectively

Set HttpOnly on every cookie that JavaScript has no legitimate reason to touch, especially session and auth tokens, and pair it with Secure and an appropriate SameSite value. Do not treat it as an anti-XSS control in itself: the real fix for injection is input handling, output encoding, and a strong Content Security Policy. HttpOnly limits the damage if those fail. Because an injected payload can still act within the session without ever reading the cookie, detecting the malicious script itself matters. cside analyses third-party script payloads through a Script method and can block anomalous behaviour in real time, closing the gap cookie flags leave behind.

Definition

If cookies are HttpOnly, is my site safe from XSS?

No. HttpOnly stops a script from reading the cookie, but the injected script still runs with the user's privileges and can send authenticated requests, alter the page, capture keystrokes, or steal data from forms. HttpOnly reduces one consequence of XSS; it does not prevent the vulnerability or its other abuses.

Definition

Should every cookie be HttpOnly?

Set it on any cookie JavaScript does not need, which covers most session and authentication cookies. The exception is a cookie your own client-side code must read, for example some CSRF-token or UI-state patterns. For those, keep the value non-sensitive and rely on other protections rather than dropping HttpOnly on the session cookie.

Got more questions

Talk to a security expert

We answer client-side security questions every day. Bring yours.

Book a demo

Want to walk through this with an engineer?

Thirty minutes, on your own site. Not a slide deck.

We'll show you:

Which third-party scripts are running on your site right now
Where you stand on PCI DSS 6.4.3 and 11.6.1
How much of your traffic is bots and AI agents

Rather just send a question?

Finding open slots…

Real humans only. We'd know.

Having trouble booking? Open scheduler in a new tab

What are you trying to solve?

Tell us in a line and we'll come back with something useful, not a generic pitch.

We usually help with:

Seeing which third-party scripts run on your site
PCI DSS 6.4.3 and 11.6.1 evidence
Bots, AI agents and account takeover

Prefer to just book a time? Pick a slot instead