Cookies are small bits of data that websites store on your computer so they can remember who you are and what you like. They’re name/value pairs sent by the server when you visit a website, which are then stored in your browser until their expiration date or until you delete them manually. When you return to that same site later on, your browser sends those cookies back to the server so it knows who you are and what you prefer.
However, not all servers follow these guidelines exactly. RFC 6265 (and now RFC 7690) recommends encrypting and signing cookie contents when transmitting them over a secure channel for added security. Servers that require higher levels of security should use the Cookie and Set-Cookie headers only over a secure channel, setting the Secure attribute on every cookie to prevent an active network attacker from intercepting or replaying cookies in clear text.
Using session identifier cookies is another common technique for storing state information associated with a user’s session. Instead of storing sensitive data directly in a cookie (where it might be exposed to or replayed by an attacker), servers commonly store a nonce (or “session identifier”) in a cookie. When the server receives an HTTP request with a nonce, the server can look up state information associated with the cookie using the nonce as a key.
Using session identifiers also presents risks, such as “session fixation” vulnerabilities that allow an attacker to transplant a session identifier from their own user agent onto yours. To avoid these vulnerabilities, servers should take care when implementing session management mechanisms and ensure they are properly configured for maximum security.
Cookies do not provide isolation by port or scheme, which can lead to data leakage between different services running on the same server. For example, a cookie set for one domain might be accessible from another domain if both domains share the same IP address. Similarly, cookies set over HTTPS might be accessible via non-HTTP APIs like HTML’s document.cookie API or through other protocols like FTP and Gopher.
Cookies also do not provide integrity guarantees for sibling domains (and their subdomains). For example, a cookie set by foo.example.com with a Domain attribute of “example.com” might be accessible from bar.example.com, potentially allowing an attacker to mount an attack against bar.example.com using the cookie data.
Finally, cookies rely on DNS for security, which can be problematic if the DNS is partially or fully compromised. In such cases, the cookie protocol might fail to provide the security properties required by applications.
However, it’s important to note that cookies are not the only mechanism for state management. Other techniques like server-side sessions or client-side storage mechanisms like localStorage and sessionStorage can also be used in conjunction with or instead of cookies depending on the application requirements.