Skip to content
BENIBIX Learn

HttpOnly vs SameSite

People think HttpOnly stops forged requests, and it does not. HttpOnly hides a cookie from scripts on the page, so an XSS script cannot steal the login. The browser still sends the cookie everywhere it goes. SameSite decides where the cookie goes. It keeps the cookie off requests that start on other sites, which is what stops CSRF. A login cookie wants both.

Development🔐 Security

HttpOnly Cookie

A cookie setting. Scripts on the page can never read the cookie.

HttpOnly is a setting on a cookie that hides it from scripts on the page. The browser still sends the cookie with each request to its site. But code in the page cannot read it. So if an attacker gets a script onto the page through XSS, it cannot steal the login cookie. Login cookies should always carry it, along with Secure.

Example
A bad comment on Jack's site runs a script that tries to read each reader's cookies. The login cookie is HttpOnly, so the script gets nothing it can use.
Why it matters
It keeps the login safe even when a script gets in. The script can act on the page, but cannot carry the login away.
Full entry
Development🔐 Security

SameSite Cookie Attribute

A cookie setting. It keeps the cookie off requests that start on other sites.

SameSite is a setting on a cookie that says when the browser may send it. A request that starts on another site can leave the cookie behind. Lax keeps it off forms and hidden requests from other sites, but sends it when you click a link. Strict keeps it off every request that starts on another site. None always sends it, and then the cookie must be Secure.

Example
Emma sets her login cookie to SameSite=Lax. A bad page posts a hidden form to her app. The browser sends it with no cookie, so the request fails.
Why it matters
One word on the cookie stops most forged requests, even where an app forgot its form check.
Full entry

In one line

HttpOnly Cookie
A cookie setting. Scripts on the page can never read the cookie.
SameSite Cookie Attribute
A cookie setting. It keeps the cookie off requests that start on other sites.