HttpOnly, Secure and SameSite on a Session Cookie

HttpOnly blocks script, Secure blocks the unencrypted hop, SameSite blocks a request from another site. Three flags close three different attacks.

HttpOnly, Secure and SameSite on a Session Cookie

A session cookie is a token the browser sends back to the server on each request. If the token is stolen, the session is stolen. The theft is not one path. It is three. Each path is closed by a different flag.

HttpOnly stops script in the page from reading the cookie. A script injected into the page cannot take it through document.cookie. The flag does not remove script injection. The injection remains; only reading the cookie from script is closed. OWASP ASVS 4.0.3 asks for this attribute as a verification item in the session section.

Secure asks that the cookie be sent only on an encrypted connection. RFC 6265 defines the attribute. If the site still answers on the unencrypted address, a token without the flag travels on the plain request as well. HSTS narrows that gap separately. It does not replace the Secure flag.

SameSite says whether the cookie goes on a request that another site started. Lax sends the cookie on a top-level navigation and withholds it on a silent request from another site. Strict withholds it on cross-site navigation too. None, together with Secure, allows the cookie to cross sites on purpose. Which one is chosen depends on whether the session is needed on a return that started elsewhere. The three values are not all switched on at once.

When FenergyTech sets a session cookie, the three flags are written separately on the response header. If one flag is missing, the session is not treated as finished.

The three flags are not all marked "on" at once. SameSite=None without Secure is dropped by a current browser. The 2011 text of RFC 6265 does not carry None in today's sense. Current behaviour treats a cookie with no SameSite as Lax. Relying on that and not writing Lax means that on a browser from the day before, the cookie still goes on every request. The value is written out.

If the token sits in the cookie and the cookie is not HttpOnly, a script that entered the page takes it through document.cookie. HttpOnly cuts that. If the CSRF token is in the same cookie and script can read it, the token goes too. The token lives in the body, or in a separate cookie the script cannot read. The two are not placed in the same cookie.

If Secure is set and the site still answers on plain HTTP, the session is not created on that request. If it is created, the flag is not there. The test looks at whether the plain address sends the cookie.

Sources: RFC 6265, HTTP State Management Mechanism, IETF, 2011. OWASP Application Security Verification Standard 4.0.3, 2021, V3 Session Management.

Share:

Planning a similar project?

We can talk through how the topic in this article applies to your product.

More articles

Browse our other technical articles