Skip to content
BENIBIX Learn

HttpOnly Cookie

Development🔐 SecurityDefences

Also known as HttpOnly, HttpOnly Flag, HttpOnly Attribute

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

Think of it like

Think of it like a sealed letter. The postman can carry it, but he cannot read it.

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.

Definition

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 code

http
Set-Cookie: session=7f3a9c; HttpOnly; Secure
HttpOnly hides the cookie from page scripts. Secure sends it only over HTTPS.
The third panel: a script steals the cookie. HttpOnly is the fix it names.

Common mistake

Thinking HttpOnly stops XSS. The script still runs, and it can act as the user while the page is open.

Key takeaways

  1. 1
    Hidden from scripts. Page code cannot read it.
  2. 2
    Still sent. The browser adds it to requests.
  3. 3
    Not a cure. XSS still needs output encoding.

Compare