WIPIVERSE All articles

Secure cookie

A secure cookie (also known as an HTTPS cookie) is an HTTP cookie that includes the Secure attribute. This attribute instructs the user agent (typically a web browser) to transmit the cookie only over encrypted connections, specifically HTTPS (HTTP over TLS/SSL).

Mechanism When a server sets a cookie via the Set-Cookie HTTP response header, it may append the Secure flag. For example: Set-Cookie: session_id=abc123; Secure Upon receiving this header, a compliant user agent will store the cookie but will include it in the Cookie request header only if the request is made over a secure (HTTPS) channel. If the request is made over plain HTTP, the user agent must not send the cookie.

Purpose The primary purpose of the Secure attribute is to protect the confidentiality and integrity of the cookie data during transit. By restricting transmission to encrypted channels, it mitigates the risk of cookie theft via man-in-the-middle (MITM) attacks or network eavesdropping on unencrypted Wi-Fi or wired networks.

Limitations and Considerations

  • No Encryption at Rest: The Secure attribute does not encrypt the cookie data on the client's disk or in memory; it only governs transmission.
  • No Protection Against Subdomain Leakage: Unless combined with the Domain attribute configured restrictively or the __Host- prefix, a secure cookie set on a subdomain (e.g., sub.example.com) may be sent to other subdomains over HTTPS.
  • Dependency on HTTPS: The attribute is effective only if the entire site operates over HTTPS. If a site serves content over HTTP, the cookie will not be sent with those requests, potentially breaking session continuity.
  • Prefixes: Modern best practices recommend using cookie name prefixes (__Secure- or __Host-) which enforce the Secure attribute (and other security properties) at the browser level, preventing a cookie from being set without it.

Relationship to Other Attributes The Secure attribute is frequently used in conjunction with:

  • HttpOnly: Prevents client-side script (JavaScript) access to the cookie, mitigating cross-site scripting (XSS) risks.
  • SameSite: Controls whether the cookie is sent with cross-site requests, providing defense against cross-site request forgery (CSRF).

Standardization The Secure attribute is defined in RFC 6265 (HTTP State Management Mechanism) and its successor, RFC 6265bis.

Browse

More topics to explore

    Browse all articles