Overview
This vulnerability occurs when an application sets a sensitive cookie (especially session identifiers or authentication tokens) without enabling theHttpOnly attribute. The HttpOnly flag instructs the browser not to allow client-side scripts (like JavaScript) to access the cookie via document.cookie. If this flag is missing, and an attacker successfully executes a Cross-Site Scripting (XSS) payload on the site, they can easily steal the sensitive cookie, leading to session hijacking. 🍪🚫📜➡️👤
Business Impact
Missing theHttpOnly flag on sensitive cookies significantly increases the impact of XSS vulnerabilities:
- Session Hijacking: An XSS attack can steal the session cookie, allowing the attacker to completely impersonate the user without needing their password.
- Token Theft: Other sensitive tokens stored in cookies (e.g., anti-CSRF tokens, API access tokens if stored this way) can be stolen and potentially misused.
HttpOnly is a crucial defense-in-depth measure against the consequences of XSS.
Reference Details
CWE ID: CWE-1004
OWASP Top 10 (2021): A05:2021 - Security Misconfiguration
Severity: Medium (Increases the impact of XSS)
Framework-Specific Analysis and Remediation
This is a configuration issue related to how cookies are set. Most modern web frameworks either enableHttpOnly by default for their session cookies or provide a clear configuration option to do so. The vulnerability typically arises when:
- The framework default is insecure and not overridden.
- Developers manually set sensitive cookies without including the
HttpOnlyflag.
- Configure Framework Defaults: Ensure the framework’s session cookie configuration explicitly sets
HttpOnlyto true. - Set Flag on Custom Cookies: When creating any cookie that contains sensitive data or is used for session management/authentication, always include the
HttpOnlyattribute. Cookies needed purely for client-side JavaScript manipulation should not contain sensitive data.
- Python
- Java
- .NET(C#)
- PHP
- Node.js
- Ruby
Framework Context
Controlled bySESSION_COOKIE_HTTPONLY and CSRF_COOKIE_HTTPONLY (though CSRF is often read by JS) in Django settings.py. Flask requires manual setting.Vulnerable Scenario 1: Django SESSION_COOKIE_HTTPONLY = False
Vulnerable Scenario 2: Flask Custom Cookie without httponly=True
Mitigation and Best Practices
- Django: Ensure
SESSION_COOKIE_HTTPONLY = True(this is the default and should generally not be changed). ConsiderCSRF_COOKIE_HTTPONLY = False(default) if your frontend JavaScript needs to read the CSRF token from the cookie, but ensure your session cookie is HttpOnly. - Flask: Always add
httponly=Truewhen callingresponse.set_cookiefor any sensitive cookie. Configure session extensions to use HttpOnly.
Secure Code Example
Testing Strategy
Access your site over HTTPS and log in. Use browser developer tools (Application/Storage tab) to inspect the session cookie (sessionid in Django, connect.sid often in Flask/Node) and any custom authentication cookies. Verify that the “HttpOnly” column/flag is checked/true. Try accessing document.cookie in the browser’s JavaScript console; sensitive cookies should not appear in the list.
