Cookies normally remain readable by JavaScript in the browser. If an attacker injects JavaScript through cross-site scripting (XSS), that access can expose session cookies. The HttpOnly attribute tells the browser to keep a cookie value inaccessible to client-side scripts. section 2.13 section 9.7
Protecting a session cookie
Set the attribute in the response header: Set-Cookie: session_id=4b44bd3f-5186; Secure; HttpOnly. Use Secure with HttpOnly so the cookie is also restricted to a secure channel. section 9.7 section 2.13
Apply HttpOnly where cookies carry authentication or session state:
- Mark session cookies HttpOnly so browser JavaScript cannot access session identifiers. section 9.11
- Use the framework's httpOnly configuration option, which is often enabled by default in modern frameworks. section 9.7
- Store JWT access tokens in HttpOnly cookies when avoiding direct exposure to client-side JavaScript. section 9.4
- Treat HttpOnly as one layer of defense; content security policies and escaping still help protect sessions from XSS. section 9.7
HTTP/2.0 200 OK Content-Type: text/html Set-Cookie: session_id=9264be3c7df125; Secure; HttpOnly #1
Inspect a complete Set-Cookie header that combines Secure and HttpOnly. section 2.13
