HTTP Cookies: A Deep Dive

The CyberSec Guru

Updated on:

HTTP Cookies: A Deep Dive

If you like this post, then please share it:

HTTP cookies (also known as web cookies or browser cookies) are small data files that websites send to your internet browser. These files are then stored within your browser and sent back to the server that set the cookie every time you make a subsequent request to the site. Cookies play a vital role in modern web development, enabling a variety of essential features. In this post, let’s take a deep dive into HTTP cookies.

Understanding Cookies

Understanding Cookies
Understanding Cookies

HTTP cookies, small text files sent to your browser by websites, are the unsung heroes of personalized web experiences. They function like mini memory chips, allowing websites to recall your past interactions and preferences. Let’s delve deeper into their purpose and the different types.

Purpose

  • Session Management: The Backbone of Smooth Online ExperiencesCookies are the backbone of features essential to our online lives. Imagine logging into your favorite site – it’s the cookie that tells the site, “Hey, I’m still logged in!” as you navigate across different pages. Online shopping? Cookies are the ones silently maintaining your cart’s contents. Complex games? Cookies likely help the site remember your progress and high scores.
  • Personalization: Tailoring the Web to Your PreferencesHave you ever noticed websites adapting to your likes? Cookies play a significant role. They remember your language settings, preferred themes (like dark mode), and other choices. This personalized touch saves you from repeatedly adjusting settings, making your web experience feel more bespoke.
  • Tracking: Insights into User BehaviorCookies offer valuable insights into how people use websites. They track things like pages visited, time spent, and even items clicked. Websites and advertisers use this data to tailor their content, target ads more effectively, and improve the overall website design based on how users interact with it.

Types

  • First-party cookies: These are the most common type. They’re set by the website you’re directly visiting. Their primary roles are session management and delivering personalized experiences within that specific website.
  • Third-party cookies: These are set by domains other than the website you’re currently on. They’re primarily used for cross-site tracking and targeted advertising. For instance, if you see an ad for a product you were browsing on a different website, it’s likely a third-party cookie at work.
Cookie Format
Cookie Format

HTTP cookies, those little text files sent by websites and stored in your browser, act like tiny memory cards holding essential information for a web server. While cookies themselves may appear simple, they have a structured format containing multiple components that dictate their behavior. Let’s dissect each element:

Core Components

  • CookieName=Value: This is the heart of the cookie.
    • CookieName: A unique, case-sensitive name assigned by the server to identify the specific piece of data being stored.
    • Value: The actual data associated with the cookie. This can be text strings, numbers, or encoded information. Keep in mind that there are limits to the size of a cookie, so values are normally kept concise.

Optional Attributes

  • Expires:
    • This attribute sets an expiry date for the cookie (formatted in GMT: Expires=Wed, 21 Oct 2023 07:28:00 GMT).
    • When a cookie has an Expires attribute, it becomes a “persistent cookie.” The browser will keep it stored on the user’s device and automatically delete it when the specified date and time are reached.
    • If Expires is omitted, the cookie becomes a “session cookie,” existing only in temporary memory. It’s deleted when the browser closes or the user navigates away from the website that set the cookie.
  • Max-Age:
    • An alternative to Expires, this attribute defines the cookie’s lifespan in seconds from the moment it’s set (e.g., Max-Age= 604800 means the cookie will last for a week).
    • It offers a more flexible way to manage cookie expiration.
  • Domain:
    • This attribute specifies the domain, or subdomains, where the cookie is permitted to be sent.
    • A domain must match or be a subdomain of the domain of the page setting the cookie for it to be included.
    • For instance, a cookie with Domain=example.com can be used on example.com itself, along with any subdomains like blog.example.com. A cookie domain can have a leading dot (.), ensuring it is sent to all the subdomains.
  • Path
    • The Path attribute narrows down the specific URL paths within a domain where a cookie should be sent.
    • The browser will only include the cookie in requests if the request URL matches the specified path or a sub-path within that path.
    • Example: A cookie with Path=/shop would be sent with requests to /shop/shop/items/shop/cart, but not with requests to /blog or /about.

Security-Centric Attributes

  • Secure:
    • This crucial flag mandates that the browser should only transmit the cookie over a secure HTTPS connection.
    • It’s essential for safeguarding confidential data, particularly when cookies hold login tokens or other sensitive information.
  • HttpOnly:
    • This attribute acts as a defense against Cross-Site Scripting (XSS) attacks.
    • When set, browsers block any attempts by client-side JavaScript code to access or modify the cookie. This adds an extra layer of protection since malicious scripts injected into a website won’t be able to steal cookie data.

Illustrative Example

Set-Cookie: sessionId=123456789; Expires=Wed, 15 Mar 2024 22:00:00 GMT; Path=/; Domain=.example.com; HttpOnly; Secure

This cookie:

  • Is named “sessionId”
  • Has a value of “123456789”
  • Will expire on March 15, 2024
  • Is valid for all pages within the example.com domain and its subdomains
  • Is only accessible by the web server, not client-side JavaScript
  • Will only be transmitted over HTTPS connections
Cookie Handling
Cookie Handling

HTTP cookies function through a seamless collaboration between web servers and web browsers. Let’s delve into the step-by-step process:

  • The Initiation: When a user visits a website for the first time or triggers an action that warrants the use of a cookie (logging in, adding items to a cart), the server generates the cookie.
  • The Set-Cookie Header: The server embeds the cookie information within the HTTP response headers using the Set-Cookie header. Here’s a sample Set-Cookie header:Set-Cookie: SessionID=abc123; Expires=Wed, 20 Mar 2024 10:15:30 GMT; Path=/; HttpOnly
  • Analyzing the Header:
    • SessionID=abc123: Assigns a unique identifier to track the user’s session.
    • Expires=Wed, 20 Mar 2024 10:15:30 GMT: Dictates the cookie’s expiry, after which it’ll be deleted by the browser.
    • Path=/: Restricts the cookie to the root path of the website.
    • HttpOnly: Mitigates XSS attacks by disallowing JavaScript access to the cookie.
  • Decoding the Instructions: Your web browser meticulously parses the Set-Cookie header, extracting all the critical details about the cookie.
  • Internal Storage: The browser designates a specific storage area for cookies. This storage is usually organized based on the domain that set the cookie.
  • Adhering to Restrictions: The browser respects the parameters outlined in the Set-Cookie header. These include:
    • Domain: Only sends the cookie back to the designated domain or its subdomains.
    • Path: Ensures the cookie is sent only for requests whose URL matches the specified path.
    • Secure: Transmits the cookie only over secure HTTPS connections if this flag is set.
    • Expiry/Max-Age: Automatically deletes the cookie when it expires or reaches its maximum age.
  • Subsequent Visits: Every time you re-access the same website, or a page within the path and domain restrictions, your browser performs a lookup.
  • Cookie Matching: The browser checks its cookie store to see if there are any cookies that match:
    • Domain: The cookie’s domain must correspond to the domain of the site you’re requesting.
    • Path: The URL you’re requesting must fall within the path specified by the cookie.
  • The Cookie Header: If matching cookies are found, the browser constructs a Cookie HTTP request header. For instance:Cookie: SessionID=abc123; UserPref=theme=dark
  • Sending Cookies to the Server: This Cookie header is automatically attached to your request, seamlessly providing the server with the stored cookie information.

Illustrative Example

  1. You visit an online store, https://www.example.com, and add a few products to your cart.
  2. The server creates a cookie named ShoppingCartID to store and track your cart contents. It sends this cookie to you with a Set-Cookie header.
  3. Your browser saves this cookie.
  4. As you browse different pages of the store, your browser includes the ShoppingCartID within the Cookie header of each subsequent request to https://www.example.com.
  5. The server uses ShoppingCartID to know which shopping cart belongs to you.
Cookie Domains
Cookie Domains

The Domain attribute in an HTTP cookie is a crucial parameter that governs the visibility and accessibility of a cookie across websites and their subdomains. It essentially determines which servers are permitted to receive a particular cookie, adding a layer of control and flexibility to how websites exchange information.

How the Domain Attribute Works

  1. Cookie Creation: When a server sets a cookie using the Set-Cookie header, it has the option to include the Domain attribute. Consider the following example:Set-Cookie: SessionID=abc123; Domain=example.com; Expires=Wed, 13 Mar 2024 22:23:01 GMT
  2. Browser Interpretation: Your web browser meticulously parses this instruction and understands it as follows:
    • “This cookie named ‘SessionID’ is valid for the domain ‘example.com’ and all of its subdomains.”
  3. Subsequent Requests: Every time you visit ‘example.com’ or its subdomains like ‘blog.example.com’ or ‘store.example.com’, your browser dutifully includes the ‘SessionID’ cookie in the HTTP request.

Scenarios for Using the Domain Attribute

  • Sharing Information Across Subdomains: A common website structure involves various subdomains serving different purposes. For example, you might have:
    • www.example.com: The main website
    • blog.example.com: The company’s blog
    • store.example.com: The online store By setting the Domain=example.com, a cookie created on one subdomain becomes accessible to others. This enables seamless functionality like maintaining a shopping cart across the entire site or keeping a user logged in while moving between subdomains.
  • Restricting Scope: Sometimes, you might want to limit cookie visibility to a specific subdomain or the exact domain that set it.
    • Example 1: If a cookie contains sensitive information pertinent only to store.example.com, setting the Domain=store.example.com ensures it’s not accidentally sent to other subdomains.
    • Example 2: Leaving the Domain attribute unset ties the cookie to the specific domain that created it, offering the strictest level of isolation.

Key Points and Caveats

  • Default Behavior (No Domain): If a server omits the Domain attribute entirely, the cookie defaults to the domain of the website that set it. This means it won’t be shared with any subdomains.
  • Inclusion of Subdomains: When you specify a Domain, it implicitly allows the cookie to be used by subdomains of that domain. Setting Domain=example.com includes all subdomains like blog.example.com, store.example.com, etc.
  • Public Suffixes: Browsers maintain an internal list of public suffixes (e.g., .com, .co.uk, .org). You cannot set a cookie Domain to a public suffix alone. This is a security measure to prevent malicious websites from potentially setting cookies too broadly.
  • Leading Dot Controversy (Outdated): In older specifications, a leading dot (e.g., .example.com) in the Domain attribute had special meaning. However, modern browsers generally ignore it. Best practice is to avoid the leading dot to ensure compatibility.

Let’s revisit the previous examples with additional explanations:

  • www.example.com:
    • The cookie is restricted solely to the exact domain ‘www.example.com’.
    • It won’t be accessible to blog.example.comstore.example.com, or even example.com (without the ‘www’).
  • example.com (no leading dot):
    • The cookie is available to ‘example.com’ and all its subdomains ( blog.example.comstore.example.com, and others).
  • No Domain Attribute:
    • The cookie is bound to the specific domain that created it. It behaves as though the Domain attribute was set to the exact domain itself.
Cookie Path
Cookie Path

The Path attribute, within the structure of an HTTP cookie, operates like a selective gatekeeper. It determines which specific URLs within a website’s domain are permitted to interact with a particular cookie. This attribute creates subdivisions on a website, enabling fine-grained control over how cookies are shared and utilized across different website sections.

  1. Organization: Cookie Path helps organize and compartmentalize cookies for different web application functions. For instance, a website might have separate cookies with different path values for:
    • /shoppingcart: To manage items added to the shopping cart.
    • /usersettings: To remember user preferences.
    • /blog: To track analytics specific to a blog section.
  2. Security: A carefully crafted Path attribute can enhance security by:
    • Limiting the scope of potentially sensitive cookies. A cookie containing authentication data might have a stricter path (e.g., /account) to prevent unintended exposure to less secure parts of the site.
    • Separating cookies to decrease the impact of cross-site scripting (XSS) vulnerabilities, should they occur.
  • Matching Mechanism: The browser compares the requested URL with a cookie’s Path value to determine if a match exists. The matching rules are as follows:
    • Exact Match: The request path and cookie path are identical.
    • Prefix Match: The cookie path is a prefix of the request path, and the last character of the cookie path is ‘/’.
    • Subdirectory Match: The cookie path is a prefix of the request path, and the character following the path prefix in the request path is a ‘/’.
  • Example: Consider a cookie set with Path=/shop. The browser would include this cookie for these request paths:
    • /shop (Exact match)
    • /shop/items (Prefix and subdirectory match)
    • /shop/checkout/payment (Prefix and subdirectory match)

Default Behavior (No Path Attribute):

When a cookie does not have a Path attribute specified:

  • Stricter Default: Newer browsers calculate the default value as the path of the URL that set the cookie.
  • Legacy Behavior (Older Browsers): The default value is ‘/’, making the cookie accessible throughout the entire domain.
Cookie Expires Attribute
Cookie Expires Attribute

The Expires attribute is a fundamental component within HTTP cookies that allows web developers to dictate a specific expiration date and time for a cookie. Once this designated expiration point is reached, the browser automatically eliminates the cookie from its storage. This attribute has two primary purposes:

  • Persistent Cookies: When an Expires attribute is included, the cookie is transformed into a persistent cookie. This means the cookie will survive beyond a single browser session and be available for future interactions with the website that set it. Persistent cookies are the mechanism behind features like “remember me” functionality on login forms or the preservation of your online shopping cart across visits.
  • Session Cookies: If the Expires attribute is absent, the cookie automatically becomes a session cookie. Session cookies are designed to be temporary and are deleted the moment you close your browser window or tab. These are typically employed to handle tasks that are confined to a single browsing session, such as managing items added to a shopping cart before checkout.

Setting the Expires Value

The Expires attribute takes a date and time value in the Greenwich Mean Time (GMT) format. Here’s an example of how to set a cookie that expires in one week:

Set-Cookie: username=JohnDoe; Expires=Wed, 20 Mar 2024 12:00:00 GMT;

Important Considerations

  • Time Zones: Always specify expiration dates and times in GMT to avoid discrepancies arising from different browser time zones.
  • Long Expiration: While you can set cookies to expire far into the future, be cautious with extremely long expiration periods. User preferences or website functionality might change over time, rendering excessively long-lived cookies obsolete or even problematic.
  • Explicit Deletion: You can forcefully delete a persistent cookie before its Expires date by setting its expiration to a past date and time.
  • Browser Variations: While the Expires attribute is widely supported, older browsers may have quirks or limitations in how they handle cookie expiration. Thorough testing across different browsers is recommended.
  • The 400-Day Limit: From approximately August 2022 (Chrome M104 onwards), browsers may cap the maximum expiration date allowed for cookies to approximately 400 days from the time it was set.

The Max-Age Attribute

As an alternative to the Expires attribute, you can use the Max-Age attribute. Max-Age specifies the cookie’s maximum lifespan in seconds from the moment it was created. For instance:

Set-Cookie: shoppingCartID=12345; Max-Age=604800; 

The above sets a cookie that will expire after one week (60 seconds * 60 minutes * 24 hours * 7 days = 604800 seconds).

Choosing Between Expires and Max-Age

Both attributes serve the same fundamental purpose. However, if both Expires and Max-Age are present, the Max-Age attribute takes precedence.

The HttpOnly Attribute

The HttpOnly Attribute
The HttpOnly Attribute

The HttpOnly attribute is a crucial security mechanism for HTTP cookies. By adding the word “HttpOnly” to a cookie’s Set-Cookie header, you instruct the browser to treat that specific cookie with enhanced protection. Let’s break down why this is so important:

Understanding Cross-Site Scripting (XSS)

  • XSS attacks occur when an attacker injects malicious JavaScript code into a vulnerable website. This code then executes in the victim’s browser in the context of that trusted website.
  • Consequences can be severe:
    • Session Hijacking: Stealing active user sessions (if cookies store authentication tokens).
    • Data Theft: Exfiltrating sensitive information from the vulnerable website.
    • Defacement: Modifying website content to inject scams or malware.

How HttpOnly Mitigates XSS

  • The Core Mechanism: The primary goal of the HttpOnly flag is to make a cookie inaccessible to client-side JavaScript. This means that even if an XSS attack succeeds in injecting malicious scripts, those scripts cannot directly read or manipulate the protected cookie.
  • Limiting Damage: While an XSS vulnerability might still exist, HttpOnly significantly reduces the risk of session hijacking, a common and highly damaging result of a successful XSS attack.

Key Scenarios where HttpOnly is Crucial

  • Session Cookies: Cookies that store session identifiers should always have the HttpOnly flag to prevent XSS-based session theft.
  • Sensitive Data: If any cookie contains personally identifiable information or other forms of sensitive data, the HttpOnly attribute provides an additional layer of security.
  • High-risk Websites: It’s a good practice to set HttpOnly on most cookies for websites dealing with financial or healthcare data to minimize the impact of potential XSS attacks.

Setting HttpOnly

HTTP/1.1 200 OK
Set-Cookie: SessionID=a89sdasd897; Expires=Wed, 13 Jan 2024 22:23:01 GMT; HttpOnly; Secure

The Secure attribute within an HTTP cookie is a crucial security mechanism designed to safeguard sensitive information transmitted between your browser and web servers. When a cookie is marked with the Secure flag, the browser takes strict measures to ensure its confidentiality and integrity.

Why the Secure Attribute Matters

  1. Man-in-the-middle (MitM) Attacks: In public networks or compromised Wi-Fi environments, attackers can eavesdrop on unencrypted HTTP traffic using techniques like packet sniffing. If cookies containing authentication tokens, session data, or other personal details don’t have the Secure attribute, this leaves them vulnerable to interception. An attacker could steal these cookies and hijack user sessions.
  2. Protection in Transit: The Secure attribute mandates that the cookie can only be sent to the server if the current connection uses HTTPS (HTTP over SSL/TLS). HTTPS provides encryption, meaning that even if an attacker intercepts the network traffic, the cookie data will be scrambled and unreadable.
  3. Defense Against Sensitive Data Exposure: Web applications frequently use cookies to store session identifiers, user preferences, login credentials, or even financial information. Without the Secure attribute, any of this sensitive data is potentially exposed during transmission over HTTP.

How the Secure Attribute Works

  • Server-Side: When a website sets a cookie with the Secure attribute, it instructs the web browser about the transmission requirements.
  • Browser-Side: The browser then enforces the following rule: If the current web page is not loaded over a secure HTTPS connection, the browser will refuse to send that cookie with the request. This restriction significantly reduces the attack surface for cookie theft.

Important Considerations

  • HTTPS is The Precursor: You can’t simply slap on a Secure flag to magically protect cookies sent over HTTP. The secure attribute works only when your website already has HTTPS properly configured.
  • Potential Incompatibilities: Be aware that older browsers, or those with strict security settings, might occasionally have issues dealing with Secure cookies. Ensure your web application gracefully handles scenarios where secure cookies cannot be transmitted.
  • Not a Silver Bullet: The Secure attribute does not safeguard against all possible cookie-based attacks. While crucial for confidentiality in transit, it doesn’t protect the cookie itself when stored in the browser. Other measures like the HttpOnly attribute are necessary for comprehensive security.

Here’s how you would set a cookie with the Secure attribute using a sample Set-Cookie HTTP header:

Set-Cookie: SessionID=123456789; Expires=Wed, 20 Mar 2024 20:00:00 UTC; Path=/; Secure
Cookie Content
Cookie Content

While it’s true that cookies are primarily designed for storing small pieces of text data, the variety and applications of this data are surprisingly extensive. Here’s a deeper look at the common types of information cookies hold, along with some more elaborate examples:

Core Identification and Session Management

  • User IDs: A unique identifier assigned to a registered user or a temporary visitor. This helps websites keep track of individual users.
  • Session Tokens: These are randomly generated strings of characters that act as a “key” to identify a user’s current session (interaction with a website over time). They are critical for maintaining logged-in status and other session-related data.
  • Last Login Timestamp: Stores the date and time of a user’s previous login, useful for welcome messages like “Welcome back!” or for security monitoring.

Personalization and User Preferences

  • Website Preferences: Cookies store choices users make on a website to improve their experience. These include:
    • Language: The website’s display language.
    • Themes: Dark mode vs. light mode, color schemes, and font choices.
    • Layout: Preferences for grid view vs. list view or adjusting the size of content elements.
    • Accessibility Settings: Text size, high-contrast mode, or screen reader preferences.
  • Content Personalization: Cookies track viewed items, search history, or other browsing patterns to suggest relevant content, products, or services.

E-commerce and Shopping Facilitation

  • Shopping Cart Items: Cookies preserve the contents of online shopping carts, even before a user logs in or creates an account.
  • Checkout Progress: For multi-step checkout processes, cookies store where a user is in the process to prevent data loss if they navigate away.
  • Wishlists or Saved Items: Cookies allow users to create lists of desired products for later consideration.
  • Recently Viewed Products: This information helps websites display relevant product recommendations or allow users to quickly return to a previous item.

Analytics and Tracking

  • User Behavior Data: Cookies record how users interact with a website:
    • Pages visited
    • Time spent on a page
    • Links clicked
    • Scrolling behavior
  • Advertising Metrics: Cookies help track:
    • Ad impressions (how often an ad is displayed)
    • Click-through rates (how often users interact with ads)
    • Conversion tracking (whether an ad leads to a desired action like a purchase)
  • Campaign Source and Attribution: Cookies can store information about how a user arrived at a website (e.g., search engine, referral site, or email campaign).

Security and Fraud Prevention

  • CSRF Tokens: Cross-Site Request Forgery (CSRF) tokens are unique, time-sensitive values stored in cookies. They help prevent unauthorized actions on behalf of a user.
  • Device Fingerprinting: Cookies can help create a “fingerprint” of a user’s device through a combination of data points (browser version, screen resolution, plugins, etc.). This can aid in detecting fraudulent login attempts.
  • Unusual Activity Detection: Cookies can track patterns like rapid login attempts from multiple locations or sudden changes in browsing habits to flag potential account compromise.

Absolutely! Here’s an expanded explanation of the cookie protocol focusing on the Set-Cookie and Cookie headers.

Cookie Protocol
Cookie Protocol

HTTP cookies function through a clear communication protocol involving two essential HTTP headers:

Set-Cookie: Server Directives

  • When a server wants to set a cookie on a user’s browser, it sends the Set-Cookie header in an HTTP response.
  • This header contains the cookie’s name, value, and various attributes that control the cookie’s behavior:
    • Expires/Max-Age: Set expiry date and time or maximum age of the cookie.
    • Domain: Limits the cookie’s scope to a specific domain and subdomains.
    • Path: Specifies a URL path within the domain where the cookie applies.
    • Secure: Requires transmission of the cookie only over HTTPS connections.
    • HttpOnly: Denies access to the cookie from client-side JavaScript (XSS mitigation).

Example Set-Cookie header

Set-Cookie: sessionID=12345; Expires=Wed, 20 Mar 2024 10:00:00 GMT; Domain=example.com; Path=/; Secure; HttpOnly 
  • When a user’s browser revisits a website that previously set a cookie, the browser checks for any stored cookies matching the domain and path of the current URL.
  • If matching cookies are found, the browser automatically includes them in the HTTP request using the Cookie header.
Cookie: sessionID=12345; username=JohnDoe 

How the Whole Process Works

  1. User visits a website: The server may set one or more cookies using the Set-Cookie header in the response.
  2. Browser stores cookies: The browser stores the cookies with their associated attributes.
  3. Subsequent visits: On each subsequent request to the same domain (and matching path), the browser sends the relevant cookies in the Cookie header.
  4. Server uses cookie data: The server uses the cookie information for personalization, session management, or other purposes.

Key Takeaways

  • HTTP cookies are small data files stored by websites in your browser.
  • They are used for session management, personalization, and tracking.
  • Cookies consist of a name-value pair and optional attributes like expiration, domain, path, secure, and HttpOnly.
  • The Set-Cookie header (server to client) sets the cookie on the browser.
  • The Cookie header (client to server) sends cookies back to the server on subsequent requests.
  • Secure and HttpOnly flags enhance cookie security.
  • Users have control over cookies in their browsers (view, delete, block).

In essence, cookies are a mechanism for websites to store and retrieve data on your browser. They play an important role in the modern web experience, but it’s important to be aware of how they work and how to manage them for privacy reasons.

If you like this post, then please share it:

Glossary

Newsletter Subscription

Sign up for the monthly newsletter today and stay ahead of the curve!

Subscription Form

Leave a Comment