http-state Working Group A. Barth
Internet-Draft U.C. Berkeley
Expires: January 31, 2010 August 2009

HTTP State Management Mechanism
draft-abarth-cookie-00

Abstract

This document defines the HTTP Cookie and Set-Cookie headers.

NOTE:

Status of This Memo

This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79.

Internet-Drafts are working documents of the Internet Engineering Task Force (IETF). Note that other groups may also distribute working documents as Internet-Drafts. The list of current Internet- Drafts is at http:/⁠/⁠datatracker.ietf.org/⁠drafts/⁠current/⁠.

Internet-Drafts are draft documents valid for a maximum of six months and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use Internet-Drafts as reference material or to cite them other than as "work in progress."

This Internet-Draft will expire on January 31, 2010.

Copyright Notice

Copyright (c) 2009 IETF Trust and the persons identified as the document authors. All rights reserved.

This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (http:/⁠/⁠trustee.ietf.org/⁠license-⁠info) in effect on the date of publication of this document. Please review these documents carefully, as they describe your rights and restrictions with respect to this document. Code Components extracted from this document must include Simplified BSD License text as described in Section 4.e of the Trust Legal Provisions and are provided without warranty as described in the Simplified BSD License.


Table of Contents

1. Introduction

This document defines the HTTP Cookie and Set-Cookie header.

2. Terminology

The terms user agent, client, server, proxy, and origin server have the same meaning as in the HTTP/1.0 specification.

Fully-qualified host name (FQHN) means either the fully-qualified domain name (FQDN) of a host (i.e., a completely specified domain name ending in a top-level domain such as .com or .uk), or the numeric Internet Protocol (IP) address of a host. The fully qualified domain name is preferred; use of numeric IP addresses is strongly discouraged. [TODO: What does "strongly discouraged" mean?]

The terms request-host and request-URI refer to the values the client would send to the server as, respectively, the host (but not port) and abs_path portions of the absoluteURI (http_URL) of the HTTP request line. Note that request-host must be a FQHN. Hosts names can be specified either as an IP address or a FQHN string. Sometimes we compare one host name with another. Host A's name domain-matches host B's if

Note that domain-match is not a commutative operation: a.b.c.com domain-matches .c.com, but not the reverse.

Because it was used in Netscape's original implementation of state management, we will use the term cookie to refer to the state information that passes between an origin server and user agent, and that gets stored by the user agent.

3. State and Sessions

This document describes a way to create stateful sessions with HTTP requests and responses. HTTP servers respond to each client request without relating that request to previous or subsequent requests; the technique allows clients and servers that wish to exchange state information to place HTTP requests and responses within a larger context, which we term a "session". This context might be used to create, for example, a "shopping cart", in which user selections can be aggregated before purchase, or a magazine browsing system, in which a user's previous reading affects which offerings are presented.

There are, of course, many different potential contexts and thus many different potential types of session. The designers' paradigm for sessions created by the exchange of cookies has these key attributes:

  1. Each session has a beginning and an end.
  2. Each session is relatively short-lived.
  3. Either the user agent or the origin server may terminate a session.
  4. The session is implicit in the exchange of state information.

4. Outline

We outline here a way for an origin server to send state information to the user agent, and for the user agent to return the state information to the origin server.

4.1. Syntax: General

The two state management headers, Set-Cookie and Cookie, have common syntactic properties involving attribute-value pairs. The following grammar uses the notation, and tokens DIGIT (decimal digits) and token (informally, a sequence of non-special, non-white space characters) from the HTTP/1.1 specification [RFC 2068] to describe their syntax.

[TODO: Test this grammar. I think there are many, many issue with this grammer. For example, this grammar seems to permit whitespace around the "=", but I don't think that actually works.]

   av-pairs        =       av-pair *(";" av-pair)
   av-pair         =       attr ["=" value]        ; optional value
   attr            =       token
   value           =       word
   word            =       token | quoted-string
          

Attributes (names) (attr) are case-insensitive. White space is permitted between tokens. Note that while the above syntax description shows value as optional, most attrs require them.

NOTE: The syntax above allows whitespace between the attribute and the = sign. [TODO: This is probably wrong, however.]

4.2. Origin Server Role

4.2.1. General

The origin server initiates a session, if it so desires. (Note that "session" here does not refer to a persistent network connection but to a logical session created from HTTP requests and responses. The presence or absence of a persistent connection should have no effect on the use of cookie-derived sessions). To initiate a session, the origin server returns an extra response header to the client, Set-Cookie. (The details follow later.)

A user agent returns a Cookie request header (see below) to the origin server if it chooses to continue a session. The origin server may ignore it or use it to determine the current state of the session. It may send the client a Set-Cookie response header with the same or different information, or it may send no Set-Cookie header at all. The origin server effectively ends a session by sending the client a Set-Cookie header with Max-Age=0. [TODO: Need to say something about Expires here.]

Servers may return a Set-Cookie response headers with any response. User agents should send Cookie request headers, subject to other rules detailed below, with every request.

An origin server may include multiple Set-Cookie headers in a response. Note that an intervening gateway could fold multiple such headers into a single header. [TODO: Investigate how UAs cope with such folded headers.]

4.2.2. Set-Cookie Syntax

The syntax for the Set-Cookie response header is

[TODO: Valdiate this syntax.]

   set-cookie      =       "Set-Cookie:" cookies
   cookies         =       1#cookie
   cookie          =       NAME "=" VALUE *(";" cookie-av)
   NAME            =       attr
   VALUE           =       value
   cookie-av       =       "Comment" "=" value
                   |       "Domain" "=" value
                   |       "Max-Age" "=" value
                           [TODO: Expires is clearly missing.]
                   |       "Path" "=" value
                   |       "Secure"
                           [TODO: HTTPOnly is also missing.]
                   |       "Version" "=" 1*DIGIT
                           [TODO: Version is likely a fantasy.]
            

Informally, the Set-Cookie response header comprises the token Set-Cookie:, followed by a comma-separated list of one or more cookies. Each cookie begins with a NAME=VALUE pair, followed by zero or more semi-colon-separated attribute-value pairs. The specific attributes and the semantics of their values follows. The NAME=VALUE attribute-value pair must come first in each cookie. The others, if present, can occur in any order. If an attribute appears more than once in a cookie, the behavior is undefined. [TODO: Test what happens when attributes are multiply defined.]

4.2.3. Controlling Caching

[TODO: Should we go into this much detail here? This seems redudant with the HTTP specs.]

An origin server must be cognizant of the effect of possible caching of both the returned resource and the Set-Cookie header. Caching "public" documents is desirable. For example, if the origin server wants to use a public document such as a "front door" page as a sentinel to indicate the beginning of a session for which a Set-Cookie response header must be generated, the page should be stored in caches "pre-expired" so that the origin server will see further requests. "Private documents", for example those that contain information strictly private to a session, should not be cached in shared caches.

If the cookie is intended for use by a single user, the Set-Cookie header should not be cached. A Set-Cookie header that is intended to be shared by multiple users may be cached.

The origin server should send the following additional HTTP/1.1 response headers, depending on circumstances: [TODO: Is this good advice?]

and one of the following:

HTTP/1.1 servers must send Expires: old-date (where old-date is a date long in the past) on responses containing Set-Cookie response headers unless they know for certain (by out of band means) that there are no downsteam HTTP/1.0 proxies. HTTP/1.1 servers may send other Cache-Control directives that permit caching by HTTP/1.1 proxies in addition to the Expires: old-date directive; the Cache-Control directive will override the Expires: old-date for HTTP/1.1 proxies.

4.3. User Agent Role

4.3.1. Interpreting Set-Cookie

The user agent keeps separate track of state information that arrives via Set-Cookie response headers from each origin server (as distinguished by name or IP address and port). The user agent applies these defaults for optional attributes that are missing:

Version
Defaults to "old cookie" behavior as originally specified by Netscape. See the HISTORICAL section. [TODO: Unlikely.]
Domain
Defaults to the request-host. (Note that there is no dot at the beginning of request-host.) [TODO: This is important to test!]
Max-Age
The default behavior is to discard the cookie when the user agent exits. [TODO: Interaction with Expires.]
Expires
The default behavior is to discard the cookie when the user agent exits. [TODO: Interaction with Max-Age.]
Path
Defaults to the path of the request URL that generated the Set-Cookie response, up to, but not including, the right-most /. [TODO: Test! This seems wrong for paths that are just a single slash]
Secure
If absent, the user agent may send the cookie over an insecure channel.

4.3.2. Rejecting Cookies

To prevent possible security or privacy violations, a user agent must reject a cookie (shall not store its information) if any of the following is true:

Examples:

4.3.3. Cookie Management

If a user agent receives a Set-Cookie response header whose NAME is the same as a pre-existing cookie, and whose Domain and Path attribute values exactly (string) match those of a pre-existing cookie, the new cookie supersedes the old. However, if the Set-Cookie has a value for Max-Age of zero, the (old and new) cookie is discarded. Otherwise cookies accumulate until they expire (resources permitting), at which time they are discarded. [TODO: Do cookies really accumulate like this? Also, need to talk about Expires]

Because user agents have finite space in which to store cookies, they may also discard older cookies to make space for newer ones, using, for example, a least-recently-used algorithm, along with constraints on the maximum number of cookies that each origin server may set. [TODO: Consider recommending a cookie eviction strategy that works in practice.]

If a Set-Cookie response header includes a Comment attribute, the user agent should store that information in a human-readable form with the cookie and should display the comment text as part of a cookie inspection user interface. [TODO: I think the Comment attribute is a fantasy.]

User agents should allow the user to control cookie destruction. An infrequently-used cookie may function as a "preferences file" for network applications, and a user may wish to keep it even if it is the least-recently-used cookie. One possible implementation would be an interface that allows the permanent storage of a cookie through a checkbox (or, conversely, its immediate destruction). [TODO: Remove?]

Privacy considerations dictate that the user have considerable control over cookie management. The PRIVACY section contains more information.

4.3.4. Sending Cookies to the Origin Server

When it sends a request to an origin server, the user agent sends a Cookie request header to the origin server if it has cookies that are applicable to the request, based on

The syntax for the header is:


   cookie          =       "Cookie:" cookie-version
                           1*((";" | ",") cookie-value)
   cookie-value    =       NAME "=" VALUE [";" path] [";" domain]
   cookie-version  =       "$Version" "=" value
   NAME            =       attr
   VALUE           =       value
   path            =       "$Path" "=" value
   domain          =       "$Domain" "=" value

            

[TODO: This syntax is entirely wrong.]

The following rules apply to choosing applicable cookie-values from among all the cookies the user agent has.

If multiple cookies satisfy the criteria above, they are ordered in the Cookie header such that those with more specific Path attributes precede those with less specific. Ordering with respect to other attributes (e.g., Domain) is unspecified. [TODO: Figure out the correct ordering.]

Note: For backward compatibility, the separator in the Cookie header is semi-colon (;) everywhere. A server should also accept comma (,) as the separator between cookie-values for future compatibility. [TODO: Test whether servers actually do this.]

4.3.5. Sending Cookies in Unverifiable Transactions

[TODO: This entire section seems like a fantasy.]

[TODO: Consider explaining how third-party cookie blocking works.]

4.4. How an Origin Server Interprets the Cookie Header

[TODO: This section appears to be nonsense.]

4.5. Caching Proxy Role

One reason for separating state information from both a URL and document content is to facilitate the scaling that caching permits. To support cookies, a caching proxy must obey these rules already in the HTTP specification [TODO: If they're already in the HTTP specification, aren't they redundant here?]:

Proxies must not introduce Set-Cookie (Cookie) headers of their own in proxy responses (requests).

5. Examples

5.1. Example 1

    POST /acme/login HTTP/1.1
    [form data]
              

Most detail of request and response headers has been omitted. Assume the user agent has no stored cookies.

User identifies self via a form.

    HTTP/1.1 200 OK
    Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
              

Cookie reflects user's identity. [TODO: This is insecure.]

    POST /acme/pickitem HTTP/1.1
    Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"
    [form data]
              

User selects an item for "shopping basket."

    HTTP/1.1 200 OK
    Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"
              

Shopping basket contains an item.

    POST /acme/shipping HTTP/1.1
    Cookie: $Version="1";
            Customer="WILE_E_COYOTE"; $Path="/acme";
            Part_Number="Rocket_Launcher_0001"; $Path="/acme"
    [form data]
              

User selects shipping method from form.

    HTTP/1.1 200 OK
    Set-Cookie: Shipping="FedEx"; Version="1"; Path="/acme"
              

New cookie reflects shipping method.

    POST /acme/process HTTP/1.1
    Cookie: $Version="1";
            Customer="WILE_E_COYOTE"; $Path="/acme";
            Part_Number="Rocket_Launcher_0001"; $Path="/acme";
            Shipping="FedEx"; $Path="/acme"
    [form data]
              

User chooses to process order.

    HTTP/1.1 200 OK
              

Transaction is complete.

  1. User Agent -> Server
  2. Server -> User Agent
  3. User Agent -> Server
  4. Server -> User Agent
  5. User Agent -> Server
  6. Server -> User Agent
  7. User Agent -> Server
  8. Server -> User Agent

[TODO: This example is really silly. We shouldn't be recommending this at all.]

The user agent makes a series of requests on the origin server, after each of which it receives a new cookie. All the cookies have the same Path attribute and (default) domain. Because the request URLs all have /acme as a prefix, and that matches the Path attribute, each request contains all the cookies received so far.

5.2. Example 2

This example illustrates the effect of the Path attribute. All detail of request and response headers has been omitted. Assume the user agent has no stored cookies.

Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";
        Path="/acme"
            
Set-Cookie: Part_Number="Riding_Rocket_0023"; Version="1";
        Path="/acme/ammo"
            

Imagine the user agent has received, in response to earlier requests, the response headers

Cookie: $Version="1";
        Part_Number="Riding_Rocket_0023"; $Path="/acme/ammo";
        Part_Number="Rocket_Launcher_0001"; $Path="/acme"
            

A subsequent request by the user agent to the (same) server for URLs of the form /acme/ammo/... would include the following request header:

Note that the NAME=VALUE pair for the cookie with the more specific Path attribute, /acme/ammo, comes before the one with the less specific Path attribute, /acme. Further note that the same cookie name appears more than once.

Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"
            

A subsequent request by the user agent to the (same) server for a URL of the form /acme/parts/ would include the following request header:

Here, the second cookie's Path attribute /acme/ammo is not a prefix of the request URL, /acme/parts/, so the cookie does not get forwarded to the server.

6. Implementation Considerations

Here we speculate on likely or desirable details for an origin server that implements state management.

6.1. Set-Cookie Content

An origin server's content should probably be divided into disjoint application areas, some of which require the use of state information. The application areas can be distinguished by their request URLs. The Set-Cookie header can incorporate information about the application areas by setting the Path attribute for each one.

The session information can obviously be clear or encoded text that describes state. However, if it grows too large, it can become unwieldy. Therefore, an implementor might choose for the session information to be a key to a server-side resource. [TODO: Describe briefly how to generate a decent session key.]

[TODO: We could recommend that servers encrypt and mac their cookie data.]

[TODO: Mention issues that arise from having multiple concurrent sessions.]

6.2. Implementation Limits

Practical user agent implementations have limits on the number and size of cookies that they can store. In general, user agents' cookie support should have no fixed limits. [TODO: Why not?] They should strive to store as many frequently-used cookies as possible. Furthermore, general-use user agents should provide each of the following minimum capabilities individually, although not necessarily simultaneously: [TODO: Where do these numbers come from?]

User agents created for specific purposes or for limited-capacity devices should provide at least 20 cookies of 4096 bytes, to ensure that the user can interact with a session-based origin server.

The information in a Set-Cookie response header must be retained in its entirety. If for some reason there is inadequate space to store the cookie, it must be discarded, not truncated.

Applications should use as few and as small cookies as possible, and they should cope gracefully with the loss of a cookie. [TODO: Could mention latency issues that arise from having tons of cookies.]

6.2.1. Denial of Service Attacks

User agents may choose to set an upper bound on the number of cookies to be stored from a given host or domain name or on the size of the cookie information. Otherwise, a malicious server could attempt to flood a user agent with many cookies, or large cookies, on successive responses, which would force out cookies the user agent had received from other servers. However, the minima specified above should still be supported. [TODO: These minima still let an attacker exhaust the entire cookie store. There's not much we can do about it though.]

7. Privacy

7.1. User Agent Control

An origin server could create a Set-Cookie header to track the path of a user through the server. Users may object to this behavior as an intrusive accumulation of information, even if their identity is not evident. (Identity might become evident if a user subsequently fills out a form that contains identifying information.) This state management specification therefore requires that a user agent give the user control over such a possible intrusion, although the interface through which the user is given this control is left unspecified. However, the control mechanisms provided shall at least allow the user

Such control could be provided by, for example, mechanisms

A user agent usually begins execution with no remembered state information. It should be possible to configure a user agent never to send Cookie headers, in which case it can never sustain state with an origin server. (The user agent would then behave like one that is unaware of how to handle Set-Cookie response headers.)

When the user agent terminates execution, it should let the user discard all state information. Alternatively, the user agent may ask the user whether state information should be retained. If the user chooses to retain state information, it would be restored the next time the user agent runs.

7.2. Protocol Design

The restrictions on the value of the Domain attribute are meant to reduce the ways that cookies can "leak" to the "wrong" site. The intent is to restrict cookies to one, or a closely related set of hosts. Therefore a request-host is limited as to what values it can set for Domain.

8. Security Considerations

8.1. Clear Text

The information in the Set-Cookie and Cookie headers is transmitted in the clear. Three consequences are:

  1. Any sensitive information that is conveyed in in the headers is exposed to an easedropper.
  2. A malicious intermediary could alter the headers as they travel in either direction, with unpredictable results.
  3. A malicious client could alter the Cookie header before transmission, with unpredictable results.

These facts imply that information of a personal and/or financial nature should be sent over a secure channel. For less sensitive information, or when the content of the header is a database key, an origin server should be vigilant to prevent a bad Cookie value from causing failures.

8.2. Cookie Spoofing

[TODO: Mention integrity issue where a sibling domain can inject cookies.]

[TODO: Mention integrity issue where a HTTP can inject cookies into HTTPS.]

8.3. Unexpected Cookie Sharing

A user agent should make every attempt to prevent the sharing of session information between hosts that are in different domains. Embedded or inlined objects may cause particularly severe privacy problems if they can be used to share cookies between disparate hosts. For example, a malicious server could embed cookie information for host a.com in a URI for host b.com. User agent implementors are strongly encouraged to prevent this sort of exchange whenever possible. [TODO: How are they supposed to do this? This section makes little sense.]

9. Other, Similar, Proposals

[TODO: Describe relation to the Netscape Cookie Spec, RFC 2109, RFC 2629, and cookie-v2.]

10. References

Appendix A. Acknowledgements

This document borrows heavily from RFC 2109. [TODO: Figure out the proper way to credit the authors of RFC 2109.]

Author's Address

Adam Barth University of California, Berkeley EMail: abarth@eecs.berkeley.edu URI: http://www.adambarth.com/