Cookie Secure and HttpOnly meaning, in plain words

The Secure attribute limits cookies to encrypted traffic; HttpOnly hides them from scripts. What RFC 6265, OWASP and IPA say and what they do not cover.

Written by: Mio Kanzaki(Security Analyst (AI))Reviewed by: 千葉圭介Updated

The Secure attribute of a cookie makes the cookie travel over encrypted traffic only. A cookie is a small piece of data that lets a browser remember things such as a login state. With the Secure attribute, the browser sends that cookie only over an encrypted connection. The HttpOnly attribute, often mentioned alongside it, hides the cookie from scripts inside the page.

How do the Secure and HttpOnly attributes work?

It is like telling the addressee to accept letters only in registered envelopes. An ordinary envelope, meaning unencrypted traffic, is no longer used to carry the cookie.

RFC 6265 says a cookie with the Secure attribute is sent only when the request goes over a secure channel, normally HTTP over TLS. The same RFC warns that this attribute protects confidentiality only. An attacker who can interfere with the connection can overwrite a Secure cookie from an insecure channel. The HttpOnly attribute tells the browser not to hand the cookie to anything other than HTTP, such as a script.

IPA lists adding the secure attribute to cookies used over HTTPS as a "fundamental solution". It explains that without it, a cookie issued over HTTPS may also be sent over unencrypted HTTP and be captured by eavesdropping. In its section on XSS, IPA calls the HttpOnly attribute a "precautionary measure", noting that browser support varies and that it does not remove all the threats of XSS.

How to protect login state is summarized in the page on session management flaws. For the outside view of your pages, the free URL check is available.

Why do the Secure and HttpOnly attributes matter?

The OWASP Session Management Cheat Sheet calls the Secure attribute mandatory to prevent session ID leaks through a man-in-the-middle attack. It also says that redirecting to HTTPS alone is not enough, because without the Secure attribute a browser may still send the cookie over plain HTTP.

The HttpOnly attribute has the job of preventing theft of the session ID through XSS. As IPA says, other threats remain, though. For example, it cannot stop the requests that a script on the page sends after XSS has occurred.

To check, open the browser's developer tools and look at the attributes of the cookie used for login. If they are missing, ask your production company to add them.

Related terms

  • SameSite attribute: a setting that limits cookie sending from other sites
  • HSTS: a setting that makes the browser always use encrypted traffic
  • Man-in-the-middle attack: an attack that enters the middle of a connection to read or rewrite it
  • XSS: an attack in which a script planted in a site runs in a visitor's browser

Frequently asked questions

If I add the Secure attribute, can the cookie no longer be overwritten?

Overwriting is not prevented. According to RFC 6265, the Secure attribute protects only the confidentiality of the cookie. An attacker who can interfere with the connection can overwrite a Secure cookie from an insecure channel.

If I add HttpOnly, is XSS no longer a problem?

No. IPA calls HttpOnly a precautionary measure and says it does not remove every threat from XSS. The fundamental defense against XSS, such as proper handling of output, has to be done separately.

Next step

The overall approach to protecting login state is in the page on session management flaws. To have someone confirm your cookie configuration, contact us.

Frequently asked questions

If I add the Secure attribute, can the cookie no longer be overwritten?

Overwriting is not prevented. According to RFC 6265, the Secure attribute protects only the confidentiality of the cookie. An attacker who can interfere with the connection can overwrite a Secure cookie from an insecure channel.

If I add HttpOnly, is XSS no longer a problem?

No. IPA calls HttpOnly a precautionary measure and says it does not remove every threat from XSS. The fundamental defense against XSS, such as proper handling of output, has to be done separately.

See what an attacker can see, today

The free URL check returns a grade in under a minute. For costs and next steps, book a free consultation.