Skip to main content
a promotional graphic telling you that PCI Compliance is no longer an annual exercise and that continuous monitory must be built in
Category: Tracking Technologies

HttpOnly Cookies

Also known as: HttpOnly flag, HttpOnly attribute
Simply put

An HttpOnly cookie is a cookie that carries a special setting telling the web browser it should only be sent to the server and not be readable by JavaScript running on the page. This helps protect sensitive cookies, such as those used to keep a user logged in, from being stolen if a malicious script manages to run in the browser. It is a technical security measure rather than a consent or privacy-preference setting.

Formal definition

HttpOnly is an attribute set on an HTTP cookie (typically via the Set-Cookie response header) that instructs the browser to withhold the cookie value from client-side scripting interfaces such as document.cookie, so the cookie is transmitted only in HTTP(S) requests to the server. Its primary purpose is to reduce the risk of session or cookie theft via cross-site scripting (XSS) by preventing browser-executed JavaScript from reading the cookie's value. HttpOnly is often applied to session cookies and is distinct from, and can be combined with, the Secure attribute, which restricts a cookie to encrypted HTTPS connections. Note that HttpOnly is a security control governing script access to cookie contents; it does not by itself determine whether a cookie is exempt from or subject to consent requirements under the ePrivacy Directive or its national implementations, nor whether associated processing complies with the GDPR or other applicable privacy laws, those obligations depend on the cookie's purpose and the data processed, which are outside the scope of this attribute.

Why it matters

HttpOnly cookies address one of the most common ways attackers attempt to hijack authenticated sessions. When a session cookie can be read by JavaScript running in the page, a successful cross-site scripting (XSS) attack can extract that cookie value and use it to impersonate the user. By marking a cookie HttpOnly, the browser withholds its value from client-side scripting interfaces, which makes it harder for many XSS attacks to steal session identifiers even if malicious script manages to execute. For teams responsible for protecting user accounts, this is a foundational hardening measure for session management.

For privacy and compliance professionals, it is important to keep HttpOnly in its proper lane. It is a technical security control that governs whether scripts can read a cookie's contents; it is not a consent mechanism or a privacy-preference setting. Whether a cookie must be disclosed, consented to, or logged depends on the cookie's purpose and the data processed, not on whether the HttpOnly attribute is set. A cookie can be HttpOnly and still require prior consent under the ePrivacy rules in most EU jurisdictions if it is used for non-essential purposes, and conversely a strictly necessary session cookie may be exempt from consent regardless of the attribute.

Understanding this distinction helps organizations avoid a common misconception that setting security attributes on cookies satisfies privacy obligations. Security controls such as HttpOnly and consent obligations under the ePrivacy Directive, GDPR, or US state privacy laws operate on different legal and technical planes, and each must be assessed on its own terms.

Who it's relevant to

Web developers and engineers
Developers implementing authentication and session management are the primary audience for HttpOnly. Applying the attribute to session cookies, typically via the Set-Cookie header, is a standard step in reducing the risk of session theft through XSS. Engineers should also consider combining HttpOnly with the Secure attribute where appropriate.
Security teams and application security professionals
Those responsible for defending against cross-site scripting and session hijacking treat HttpOnly as a well-established hardening measure. It makes it harder for many XSS attacks to read cookie values, though it is one layer among several and does not remove the underlying need to prevent script injection in the first place.
Privacy officers and data protection professionals
Privacy and compliance teams should recognize that HttpOnly is a security control and not a consent or privacy-preference setting. It does not by itself determine whether a cookie is exempt from or subject to consent under the ePrivacy Directive or its national implementations, nor whether associated processing complies with the GDPR or other applicable laws. Those obligations depend on the cookie's purpose and the data processed and must be assessed separately.
Legal counsel advising on cookie compliance
Counsel reviewing cookie practices should be aware that the presence of security attributes such as HttpOnly does not answer consent or lawfulness questions. Whether a cookie requires prior consent, and how associated processing is justified, turns on the cookie's function and applicable jurisdictional rules, which differ between the EU, the UK, and individual US states.

Inside HttpOnly Cookies

HttpOnly Attribute
A flag set on a cookie by the server (via the Set-Cookie response header) that instructs the browser to prevent access to the cookie through client-side scripting interfaces such as JavaScript's document.cookie. It is a security-oriented attribute rather than a consent or categorization mechanism.
Server-Side Accessibility
HttpOnly cookies remain readable by the server on subsequent requests because the browser still transmits them in HTTP request headers. The restriction applies only to client-side script access, not to transmission between browser and server.
Cross-Site Scripting (XSS) Mitigation
The primary purpose of the HttpOnly flag is to reduce the risk that a successful XSS attack can read sensitive cookie values, such as session identifiers, from within the page's scripting environment.
Relationship to Other Cookie Attributes
HttpOnly is typically used alongside other attributes such as Secure (restricting transmission to encrypted connections) and SameSite (restricting cross-site sending). These attributes address technical security and transmission behavior, and are distinct from a cookie's legal categorization (for example, strictly necessary versus analytics or advertising).
Independence from Consent Status
The HttpOnly flag governs how a cookie can be accessed technically; it does not determine whether the cookie requires consent. The applicable consent obligations depend on the cookie's purpose under the relevant legal regime, not on whether HttpOnly is set.

Common questions

Answers to the questions practitioners most commonly ask about HttpOnly Cookies.

Does setting the HttpOnly attribute make a cookie exempt from consent requirements?
No. HttpOnly is a security attribute that controls whether client-side JavaScript can access a cookie; it has no bearing on the legal classification of the cookie or whether consent is required. Consent obligations depend on the purpose of the cookie and the data processed, not on its technical flags. In most EU jurisdictions, a cookie used for analytics or advertising would still typically require prior consent under the ePrivacy rules regardless of whether it is marked HttpOnly, while a strictly necessary cookie may be exempt whether or not the attribute is set. The security benefit and the consent question should be assessed separately.
Does the HttpOnly attribute mean a cookie cannot track users or does not process personal data?
No. HttpOnly only prevents access by client-side scripts; the cookie is still transmitted to the server on each request and can carry identifiers used for tracking or that constitute personal data. A cookie can be both HttpOnly and used for purposes that engage the GDPR where personal data is processed. The attribute reduces certain risks, such as exposure through cross-site scripting, but it does not change what the cookie is used for or remove any applicable data protection obligations. Whether personal data is involved depends on the cookie's contents and purpose, which the HttpOnly flag does not address.
How do I set the HttpOnly attribute on a cookie?
The HttpOnly attribute is added to the Set-Cookie response header issued by the server, for example by appending the HttpOnly directive alongside the cookie name, value, and other attributes. Most web frameworks and server-side languages expose an option to enable it when creating a cookie. Because HttpOnly must be set server-side, cookies created purely in client-side JavaScript generally cannot carry it. The precise syntax and configuration method depend on your platform and framework, which are outside the scope of this entry.
Which cookies are good candidates for the HttpOnly attribute?
HttpOnly is commonly applied to cookies that do not need to be read by client-side scripts, such as certain session identifiers and authentication tokens, where restricting JavaScript access can reduce exposure to cross-site scripting attacks. Cookies that a legitimate script must read on the client cannot use HttpOnly without breaking that functionality. The appropriate choice depends on how each cookie is used in your application, and this security decision is distinct from any consent classification the cookie may require.
Should HttpOnly be combined with other cookie security attributes?
In practice, HttpOnly is often used together with other attributes such as Secure, which restricts transmission to encrypted connections, and SameSite, which governs cross-site sending behavior. These attributes address different aspects of cookie handling and are generally considered complementary rather than interchangeable. The suitable combination depends on your security requirements and application architecture, and the specific settings should be determined by your technical and security teams.
How does the HttpOnly attribute interact with a consent management platform (CMP)?
A CMP manages whether and when cookies are set based on the user's consent choices, whereas HttpOnly is a technical attribute on cookies that are set. The two operate at different layers: consent logic determines if a non-exempt cookie should be placed, and HttpOnly influences how an already-placed cookie can be accessed. Note that HttpOnly cookies cannot be read by client-side scripts, so a CMP relying on JavaScript to inspect or clear such cookies may need server-side coordination. Implementation details vary by CMP and platform, and a CMP supports but does not by itself establish the security or legal correctness of your cookie handling.

Common misconceptions

Setting HttpOnly on a cookie makes it a strictly necessary or essential cookie, so no consent is required.
HttpOnly is a technical security attribute and has no bearing on a cookie's legal classification. Whether consent is required generally depends on the cookie's purpose. In most EU jurisdictions the placing of and access to non-essential cookies typically requires prior consent under the ePrivacy rules, and any resulting processing of personal data is governed by the GDPR, regardless of whether the HttpOnly flag is set.
HttpOnly cookies cannot contain or relate to personal data, so data protection rules do not apply.
The HttpOnly flag only restricts client-side script access; it says nothing about the content of the cookie. An HttpOnly cookie such as a session identifier may still relate to an identifiable individual, in which case GDPR obligations may apply to the associated processing where the GDPR is in scope.
Marking a session cookie HttpOnly guarantees it is secure and compliant.
HttpOnly reduces one specific risk (script-based reading of the cookie) but does not by itself ensure security or legal compliance. It does not protect against all attack types, does not replace attributes such as Secure or SameSite, and does not address consent, transparency, or record-keeping obligations that may apply under EU, UK, or applicable US state frameworks.

Best practices

Set the HttpOnly flag on cookies holding sensitive values such as session identifiers, where client-side script access is not required, to reduce exposure in the event of an XSS vulnerability.
Combine HttpOnly with other appropriate attributes, such as Secure for transmission only over encrypted connections and a suitable SameSite value, since these controls address different technical risks.
Classify each cookie by its actual purpose to determine consent obligations separately from its technical attributes, because the HttpOnly flag does not affect whether a cookie is exempt or requires consent.
Where non-essential cookies are used, obtain valid prior consent in line with the applicable regime (for example, opt-in consent that is freely given, specific, informed, and unambiguous in most EU jurisdictions), regardless of whether HttpOnly is applied.
Document the security attributes and legal categorization of each cookie in your cookie inventory or consent records, keeping technical configuration and consent handling as distinct but linked records.
Treat HttpOnly as one element of a layered approach and involve security and legal or data protection colleagues, rather than relying on any single attribute or tool as evidence of compliance.
a promotional banner asking how ready are you for PCI DSS 4.0? With a call-to-action to get the checklist now.