Overview
This vulnerability occurs when an application sets a sensitive cookie (like a session identifier, authentication token, or CSRF token) over an HTTPS connection but fails to set theSecure attribute on that cookie. Without the Secure flag, the browser might transmit the cookie over subsequent unencrypted HTTP requests to the same domain (e.g., if the user manually types http:// or clicks an http:// link). An attacker eavesdropping on the network (e.g., on public Wi-Fi) could then intercept this cookie and potentially hijack the user’s session. 🍪🔓➡️👂
Business Impact
Failure to set theSecure flag on sensitive cookies transmitted over HTTPS undermines the protection offered by TLS/SSL:
- Session Hijacking: Attackers sniffing network traffic can steal session cookies sent over HTTP, allowing them to impersonate the user.
- Exposure of Sensitive Tokens: Other sensitive information stored in cookies (like CSRF tokens or user preferences linked to identity) can be exposed.
- Compliance Issues: Many security standards (like PCI-DSS) require the
Secureflag for cookies handling sensitive data.
Reference Details
CWE ID: CWE-614
OWASP Top 10 (2021): A05:2021 - Security Misconfiguration
Severity: Medium
Framework-Specific Analysis and Remediation
This is almost always a configuration issue. Modern web frameworks typically provide settings to enforce theSecure flag globally for session cookies, especially in production environments (often linked to HTTPS redirection settings). The vulnerability arises when these settings are disabled, misconfigured, or when developers set custom sensitive cookies manually without including the Secure flag.
Remediation:
- Enforce HTTPS: Ensure your entire application runs exclusively over HTTPS (see
CWE-319). - Configure Secure Cookies: Set the framework’s configuration option to always add the
Secureflag to session cookies in production. - Set Flag on Custom Cookies: When creating custom cookies containing sensitive data, explicitly add the
Secureattribute.
- Python
- Java
- .NET(C#)
- PHP
- Node.js
- Ruby
Framework Context
Controlled bySESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE in Django settings.py. Flask requires manual setting or configuration via extensions.Vulnerable Scenario 1: Django SESSION_COOKIE_SECURE = False
Vulnerable Scenario 2: Flask Custom Cookie without secure=True
Mitigation and Best Practices
- Django: Set
SESSION_COOKIE_SECURE = TrueandCSRF_COOKIE_SECURE = Truein your productionsettings.py. - Flask: When using
response.set_cookie, explicitly setsecure=True. Configure session extensions (like Flask-Session) to set the secure flag.
Secure Code Example
Testing Strategy
Access your site over HTTPS. Use browser developer tools (Application/Storage tab) to inspect the session cookie (sessionid, csrftoken) and any custom sensitive cookies. Verify that the “Secure” column/flag is checked/true. Try accessing an HTTP version of a page (if HTTPS redirection isn’t perfectly enforced) and see if the cookie is still sent (it shouldn’t be if Secure is set).
