Skip to main content

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 the Secure 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 the Secure 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 Secure flag 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 the Secure 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:
  1. Enforce HTTPS: Ensure your entire application runs exclusively over HTTPS (see CWE-319).
  2. Configure Secure Cookies: Set the framework’s configuration option to always add the Secure flag to session cookies in production.
  3. Set Flag on Custom Cookies: When creating custom cookies containing sensitive data, explicitly add the Secure attribute.

Framework Context

Controlled by SESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE in Django settings.py. Flask requires manual setting or configuration via extensions.

Mitigation and Best Practices

  • Django: Set SESSION_COOKIE_SECURE = True and CSRF_COOKIE_SECURE = True in your production settings.py.
  • Flask: When using response.set_cookie, explicitly set secure=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).