Sensitive cookies without HttpOnly

Prevent script access to sensitive cookies with HttpOnly

Description

Without HttpOnly, browser JavaScript can read an authentication token or session ID from a cookie.

Potential impact

If XSS exposes a valid token or session ID, someone else may gain access to the user’s account.

Remediation

Set HttpOnly=true and Secure=true by default in the shared code that creates sensitive cookies. Choose a SameSite policy that fits your cross-site requests.

Examples

Before

java
Cookie cookie = new Cookie("sid", token);
response.addCookie(cookie);

After

java
Cookie cookie = new Cookie("sid", token);
cookie.setHttpOnly(true);
cookie.setSecure(true);
response.addCookie(cookie);

The example calls setHttpOnly(true) and setSecure(true) before adding sid to the response.

References