Skip to content

HttpOnly cookie

HttpOnly prevents direct cookie access by page scripts. Matching requests can still carry the cookie; session and permission checks remain necessary.

IT security

A cookie with the HttpOnly attribute is not accessible to page scripts through document.cookie. The server sets the attribute in the Set-Cookie response header. For a session identifier, this limits reading its value through injected JavaScript. It does not prevent that script from executing.

Separate the session identifier from display data

A fictional project portal stores its session identifier in the sessionId cookie. The interface only needs the display name for its greeting. After server-side session and permission checks, the proposed profile request returns displayName; the frontend does not need to read the session identifier for this.

The browser can still automatically attach an HttpOnly cookie to matching requests, including those initiated with fetch(). Other cookie conditions still apply. An injected script could therefore sometimes perform actions within the signed-in session without knowing the cookie value. XSS and CSRF protection and server-side permission checks remain necessary.

The separate Secure attribute restricts transmission to HTTPS; SameSite affects inclusion in cross-site requests. These properties have different jobs.

Sources