idnits 2.17.1 draft-ietf-httpstate-cookie-21.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- ** There is 1 instance of too long lines in the document, the longest one being 7 characters in excess of 72. -- The draft header indicates that this document obsoletes RFC2965, but the abstract doesn't seem to mention this, which it should. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year == The document seems to contain a disclaimer for pre-RFC5378 work, but was first submitted on or after 10 November 2008. The disclaimer is usually necessary only for documents that revise or obsolete older RFCs, and that take significant amounts of text from those RFCs. If you can contact all authors of the source material and they are willing to grant the BCP78 rights to the IETF Trust, you can and should remove the disclaimer. Otherwise, the disclaimer is needed and you can ignore this comment. (See the Legal Provisions document at https://trustee.ietf.org/license-info for more information.) -- The document date (January 20, 2011) is 4817 days in the past. Is this intentional? Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) == Unused Reference: 'RFC5246' is defined on line 1611, but no explicit reference was found in the text ** Obsolete normative reference: RFC 2616 (Obsoleted by RFC 7230, RFC 7231, RFC 7232, RFC 7233, RFC 7234, RFC 7235) ** Obsolete normative reference: RFC 3490 (Obsoleted by RFC 5890, RFC 5891) ** Obsolete normative reference: RFC 5246 (Obsoleted by RFC 8446) -- Obsolete informational reference (is this intentional?): RFC 2109 (Obsoleted by RFC 2965) -- Obsolete informational reference (is this intentional?): RFC 2965 (Obsoleted by RFC 6265) -- Obsolete informational reference (is this intentional?): RFC 2818 (Obsoleted by RFC 9110) Summary: 4 errors (**), 0 flaws (~~), 3 warnings (==), 5 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 httpstate A. Barth 3 Internet-Draft U.C. Berkeley 4 Obsoletes: 2965 (if approved) January 20, 2011 5 Intended status: Standards Track 6 Expires: July 24, 2011 8 HTTP State Management Mechanism 9 draft-ietf-httpstate-cookie-21 11 Abstract 13 This document defines the HTTP Cookie and Set-Cookie header fields. 14 These header fields can be used by HTTP servers to store state 15 (called cookies) at HTTP user agents, letting the servers maintain a 16 stateful session over the mostly stateless HTTP protocol. Although 17 cookies have many historical infelicities that degrade their security 18 and privacy, the Cookie and Set-Cookie header fields are widely used 19 on the Internet. 21 Editorial Note (To be removed by RFC Editor) 23 If you have suggestions for improving this document, please send 24 email to . Suggestions with test cases 25 are especially appreciated. Further Working Group information is 26 available from . 28 Status of this Memo 30 This Internet-Draft is submitted in full conformance with the 31 provisions of BCP 78 and BCP 79. 33 Internet-Drafts are working documents of the Internet Engineering 34 Task Force (IETF). Note that other groups may also distribute 35 working documents as Internet-Drafts. The list of current Internet- 36 Drafts is at http://datatracker.ietf.org/drafts/current/. 38 Internet-Drafts are draft documents valid for a maximum of six months 39 and may be updated, replaced, or obsoleted by other documents at any 40 time. It is inappropriate to use Internet-Drafts as reference 41 material or to cite them other than as "work in progress." 43 This Internet-Draft will expire on July 24, 2011. 45 Copyright Notice 47 Copyright (c) 2011 IETF Trust and the persons identified as the 48 document authors. All rights reserved. 50 This document is subject to BCP 78 and the IETF Trust's Legal 51 Provisions Relating to IETF Documents 52 (http://trustee.ietf.org/license-info) in effect on the date of 53 publication of this document. Please review these documents 54 carefully, as they describe your rights and restrictions with respect 55 to this document. Code Components extracted from this document must 56 include Simplified BSD License text as described in Section 4.e of 57 the Trust Legal Provisions and are provided without warranty as 58 described in the Simplified BSD License. 60 This document may contain material from IETF Documents or IETF 61 Contributions published or made publicly available before November 62 10, 2008. The person(s) controlling the copyright in some of this 63 material may not have granted the IETF Trust the right to allow 64 modifications of such material outside the IETF Standards Process. 65 Without obtaining an adequate license from the person(s) controlling 66 the copyright in such materials, this document may not be modified 67 outside the IETF Standards Process, and derivative works of it may 68 not be created outside the IETF Standards Process, except to format 69 it for publication as an RFC or to translate it into languages other 70 than English. 72 Table of Contents 74 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 5 75 2. Conventions . . . . . . . . . . . . . . . . . . . . . . . . . 7 76 2.1. Conformance Criteria . . . . . . . . . . . . . . . . . . . 7 77 2.2. Syntax Notation . . . . . . . . . . . . . . . . . . . . . 7 78 2.3. Terminology . . . . . . . . . . . . . . . . . . . . . . . 7 79 3. Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 80 3.1. Examples . . . . . . . . . . . . . . . . . . . . . . . . . 9 81 4. Server Requirements . . . . . . . . . . . . . . . . . . . . . 12 82 4.1. Set-Cookie . . . . . . . . . . . . . . . . . . . . . . . . 12 83 4.1.1. Syntax . . . . . . . . . . . . . . . . . . . . . . . . 12 84 4.1.2. Semantics (Non-Normative) . . . . . . . . . . . . . . 13 85 4.2. Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . 16 86 4.2.1. Syntax . . . . . . . . . . . . . . . . . . . . . . . . 16 87 4.2.2. Semantics . . . . . . . . . . . . . . . . . . . . . . 16 88 5. User Agent Requirements . . . . . . . . . . . . . . . . . . . 17 89 5.1. Subcomponent Algorithms . . . . . . . . . . . . . . . . . 17 90 5.1.1. Dates . . . . . . . . . . . . . . . . . . . . . . . . 17 91 5.1.2. Canonicalized host names . . . . . . . . . . . . . . . 19 92 5.1.3. Domain matching . . . . . . . . . . . . . . . . . . . 19 93 5.1.4. Paths and path-match . . . . . . . . . . . . . . . . . 19 94 5.2. The Set-Cookie Header . . . . . . . . . . . . . . . . . . 20 95 5.2.1. The Expires Attribute . . . . . . . . . . . . . . . . 22 96 5.2.2. The Max-Age Attribute . . . . . . . . . . . . . . . . 23 97 5.2.3. The Domain Attribute . . . . . . . . . . . . . . . . . 23 98 5.2.4. The Path Attribute . . . . . . . . . . . . . . . . . . 23 99 5.2.5. The Secure Attribute . . . . . . . . . . . . . . . . . 24 100 5.2.6. The HttpOnly Attribute . . . . . . . . . . . . . . . . 24 101 5.3. Storage Model . . . . . . . . . . . . . . . . . . . . . . 24 102 5.4. The Cookie Header . . . . . . . . . . . . . . . . . . . . 28 103 6. Implementation Considerations . . . . . . . . . . . . . . . . 30 104 6.1. Limits . . . . . . . . . . . . . . . . . . . . . . . . . . 30 105 6.2. Application Programming Interfaces . . . . . . . . . . . . 30 106 6.3. IDNA dependency and migration . . . . . . . . . . . . . . 30 107 7. Privacy Considerations . . . . . . . . . . . . . . . . . . . . 32 108 7.1. Third-Party Cookies . . . . . . . . . . . . . . . . . . . 32 109 7.2. User Controls . . . . . . . . . . . . . . . . . . . . . . 32 110 7.3. Expiration Dates . . . . . . . . . . . . . . . . . . . . . 33 111 8. Security Considerations . . . . . . . . . . . . . . . . . . . 34 112 8.1. Overview . . . . . . . . . . . . . . . . . . . . . . . . . 34 113 8.2. Ambient Authority . . . . . . . . . . . . . . . . . . . . 34 114 8.3. Clear Text . . . . . . . . . . . . . . . . . . . . . . . . 35 115 8.4. Session Identifiers . . . . . . . . . . . . . . . . . . . 35 116 8.5. Weak Confidentiality . . . . . . . . . . . . . . . . . . . 36 117 8.6. Weak Integrity . . . . . . . . . . . . . . . . . . . . . . 37 118 8.7. Reliance on DNS . . . . . . . . . . . . . . . . . . . . . 37 119 9. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 38 120 9.1. Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . 38 121 9.2. Set-Cookie . . . . . . . . . . . . . . . . . . . . . . . . 38 122 9.3. Cookie2 . . . . . . . . . . . . . . . . . . . . . . . . . 38 123 9.4. Set-Cookie2 . . . . . . . . . . . . . . . . . . . . . . . 38 124 10. References . . . . . . . . . . . . . . . . . . . . . . . . . . 40 125 10.1. Normative References . . . . . . . . . . . . . . . . . . . 40 126 10.2. Informative References . . . . . . . . . . . . . . . . . . 40 127 Appendix A. Acknowledgements . . . . . . . . . . . . . . . . . . 42 128 Author's Address . . . . . . . . . . . . . . . . . . . . . . . . . 43 130 1. Introduction 132 This document defines the HTTP Cookie and Set-Cookie header fields. 133 Using the Set-Cookie header field, an HTTP server can pass name/value 134 pairs and associated metadata (called cookies) to a user agent. When 135 the user agent makes subsequent requests to the server, the user 136 agent uses the metadata and other information to determine whether to 137 return the name/value pairs in the Cookie header. 139 Although simple on their surface, cookies have a number of 140 complexities. For example, the server indicates a scope for each 141 cookie when sending it to the user agent. The scope indicates the 142 maximum amount of time the user agent should return the cookie, the 143 servers to which the user agent should return the cookie, and the URI 144 schemes for which the cookie is applicable. 146 For historical reasons, cookies contain a number of security and 147 privacy infelicities. For example, a server can indicate that a 148 given cookie is intended for "secure" connections, but the Secure 149 attribute does not provide integrity in the presence of an active 150 network attacker. Similarly, cookies for a given host are shared 151 across all the ports on that host, even though the usual "same-origin 152 policy" used by web browsers isolates content retrieved via different 153 ports. 155 There are two audiences for this specifications: developers of 156 cookie-generating servers and developers of cookie-consuming user 157 agents. 159 To maximize interoperability with user agents, servers SHOULD limit 160 themselves to the well-behaved profile defined in Section 4 when 161 generating cookies. 163 User agents MUST implement the more liberal processing rules defined 164 in Section 5, in order to maximize interoperability with existing 165 servers that do not conform to the well-behaved profile defined in 166 Section 4. 168 This document specifies the syntax and semantics of these headers as 169 they are actually used on the Internet. In particular, this document 170 does not create new syntax or semantics beyond those in use today. 171 The recommendations for cookie generation provided in 172 Section 4represent a preferred subset of current server behavior, and 173 even the more liberal cookie processing algorithm provided in 174 Section 5 does not recommend all of the syntactic and semantic 175 variations in use today. Where some existing software differs from 176 the recommended protocol in significant ways, the document contains a 177 note explaining the difference. 179 Prior to this document, there were at least three descriptions of 180 cookies: the so-called "Netscape cookie specification" [Netscape], 181 RFC 2109 [RFC2109], and RFC 2965 [RFC2965]. However, none of these 182 documents describe how the Cookie and Set-Cookie headers are actually 183 used on the Internet (see [Kri2001] for historical context). In 184 relation to previous IETF specifications of HTTP state management 185 mechanisms, this document requests the following actions: 187 1. Change the status of [RFC2109] to Historic (it has already been 188 obsoleted by [RFC2965]). 190 2. Change the status of [RFC2965] to Historic. 192 3. Indicate that [RFC2965] is obsoleted by this document. 194 In particular, in moving RFC 2965 to Historic and obsoleting it, this 195 document deprecates the use of the Cookie2 and Set-Cookie2 header 196 fields. 198 2. Conventions 200 2.1. Conformance Criteria 202 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 203 "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this 204 document are to be interpreted as described in [RFC2119]. 206 Requirements phrased in the imperative as part of algorithms (such as 207 "strip any leading space characters" or "return false and abort these 208 steps") are to be interpreted with the meaning of the key word 209 ("MUST", "SHOULD", "MAY", etc) used in introducing the algorithm. 211 Conformance requirements phrased as algorithms or specific steps can 212 be implemented in any manner, so long as the end result is 213 equivalent. In particular, the algorithms defined in this 214 specification are intended to be easy to understand and are not 215 intended to be performant. 217 2.2. Syntax Notation 219 This specification uses the Augmented Backus-Naur Form (ABNF) 220 notation of [RFC5234]. 222 The following core rules are included by reference, as defined in 223 [RFC5234], Appendix B.1: ALPHA (letters), CR (carriage return), CRLF 224 (CR LF), CTL (controls), DIGIT (decimal 0-9), DQUOTE (double quote), 225 HEXDIG (hexadecimal 0-9/A-F/a-f), LF (line feed), NUL (null octet), 226 OCTET (any 8-bit sequence of data except NUL), SP (space), HTAB 227 (horizontal tab), CHAR (any US-ASCII character), VCHAR (any visible 228 US-ASCII character), and WSP (whitespace). 230 The OWS (optional whitespace) rule is used where zero or more linear 231 whitespace characters MAY appear: 233 OWS = *( [ obs-fold ] WSP ) 234 ; "optional" whitespace 235 obs-fold = CRLF 237 OWS SHOULD either not be produced or be produced as a single SP 238 character. 240 2.3. Terminology 242 The terms user agent, client, server, proxy, and origin server have 243 the same meaning as in the HTTP/1.1 specification ([RFC2616], Section 244 1.3). 246 The request-host is the name of the host, as known by the user agent, 247 to which the user agent is sending an HTTP request or is receiving an 248 HTTP response from (i.e., the name of the host to which it sent the 249 corresponding HTTP request). 251 The term request-uri is defined in Section 5.1.2 of [RFC2616]. 253 Two sequences of octets are said to case-insensitively match each 254 other if and only if they are equivalent under the i;ascii-casemap 255 collation defined in [RFC4790]. 257 The term string means a sequence of non-NUL octets. 259 3. Overview 261 This section outlines a way for an origin server to send state 262 information to a user agent and for the user agent to return the 263 state information to the origin server. 265 To store state, the origin server includes a Set-Cookie header in an 266 HTTP response. In subsequent requests, the user agent returns a 267 Cookie request header to the origin server. The Cookie header 268 contains cookies the user agent received in previous Set-Cookie 269 headers. The origin server is free to ignore the Cookie header or 270 use its contents for an application-defined purpose. 272 Origin servers can send a Set-Cookie response header with any 273 response. An origin server can include multiple Set-Cookie header 274 fields in a single response. The presence of a Cookie or a Set- 275 Cookie header field does not preclude HTTP caches from storing and 276 reusing a response. 278 Origin servers SHOULD NOT fold multiple Set-Cookie header fields into 279 a single header field. The usual mechanism for folding HTTP headers 280 fields (i.e., as defined in [RFC2616]) might change the semantics of 281 the Set-Cookie header field because the %x2C (",") character is used 282 by Set-Cookie in a way that conflicts with such folding. 284 3.1. Examples 286 Using the Set-Cookie header, a server can send the user agent a short 287 string in an HTTP response that the user agent will return in future 288 HTTP requests that are within the scope of the cookie. For example, 289 the server can send the user agent a "session identifier" named SID 290 with the value 31d4d96e407aad42. The user agent then returns the 291 session identifier in subsequent requests. 293 == Server -> User Agent == 295 Set-Cookie: SID=31d4d96e407aad42 297 == User Agent -> Server == 299 Cookie: SID=31d4d96e407aad42 301 The server can alter the default scope of the cookie using the Path 302 and Domain attributes. For example, the server can instruct the user 303 agent to return the cookie to every path and every subdomain of 304 example.com. 306 == Server -> User Agent == 308 Set-Cookie: SID=31d4d96e407aad42; Path=/; Domain=example.com 310 == User Agent -> Server == 312 Cookie: SID=31d4d96e407aad42 314 As shown in the next example, the server can store multiple cookies 315 at the user agent. For example, the server can store a session 316 identifier as well as the user's preferred language by returning two 317 Set-Cookie header fields. Notice that the server uses the Secure and 318 HttpOnly attributes to provide additional security protections for 319 the more-sensitive session identifier (see Section 4.1.2.) 321 == Server -> User Agent == 323 Set-Cookie: SID=31d4d96e407aad42; Path=/; Secure; HttpOnly 324 Set-Cookie: lang=en-US; Path=/; Domain=example.com 326 == User Agent -> Server == 328 Cookie: SID=31d4d96e407aad42; lang=en-US 330 Notice that the Cookie header above contains two cookies, one named 331 SID and one named lang. If the server wishes the user agent to 332 persist the cookie over multiple "sessions" (e.g., user agent 333 restarts), the server can specify an expiration date in the Expires 334 attribute. Note that the user agent might delete the cookie before 335 the expiration date if the user agent's cookie store exceeds its 336 quota or if the user manually deletes the server's cookie. 338 == Server -> User Agent == 340 Set-Cookie: lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT 342 == User Agent -> Server == 344 Cookie: SID=31d4d96e407aad42; lang=en-US 346 Finally, to remove a cookie, the server returns a Set-Cookie header 347 with an expiration date in the past. The server will be successful 348 in removing the cookie only if the Path and the Domain attribute in 349 the Set-Cookie header match the values used when the cookie was 350 created. 352 == Server -> User Agent == 354 Set-Cookie: lang=; Expires=Sun, 06 Nov 1994 08:49:37 GMT 356 == User Agent -> Server == 358 Cookie: SID=31d4d96e407aad42 360 4. Server Requirements 362 This section describes the syntax and semantics of a well-behaved 363 profile of the Cookie and Set-Cookie headers. 365 4.1. Set-Cookie 367 The Set-Cookie HTTP response header is used to send cookies from the 368 server to the user agent. 370 4.1.1. Syntax 372 Informally, the Set-Cookie response header contains the header name 373 "Set-Cookie" followed by a ":" and a cookie. Each cookie begins with 374 a name-value pair, followed by zero or more attribute-value pairs. 375 Servers SHOULD NOT send Set-Cookie headers that fail to conform to 376 the following grammar: 378 set-cookie-header = "Set-Cookie:" SP set-cookie-string 379 set-cookie-string = cookie-pair *( ";" SP cookie-av ) 380 cookie-pair = cookie-name "=" cookie-value 381 cookie-name = token 382 cookie-value = token / "" 383 token = 385 cookie-av = expires-av / max-age-av / domain-av / 386 path-av / secure-av / httponly-av / 387 extension-av 388 expires-av = "Expires=" sane-cookie-date 389 sane-cookie-date = 390 max-age-av = "Max-Age=" non-zero-digit *DIGIT 391 ; In practice, both expires-av and max-age-av are limited 392 ; to dates representable by the user agent. 393 non-zero-digit = %x31-39 394 ; digits 1 through 9 395 domain-av = "Domain=" domain-value 396 domain-value = 397 ; defined in [RFC1034], Section 3.5, as 398 ; enhanced by [RFC1123], Section 2.1 399 path-av = "Path=" path-value 400 path-value = 401 secure-av = "Secure" 402 httponly-av = "HttpOnly" 403 extension-av = 405 Note that some of the grammatical terms above reference documents 406 that use different grammatical notations than this document (which 407 uses ABNF from [RFC5234]). 409 The semantics of the cookie-value are not defined by this document. 411 To maximize compatibility with user agents, servers that wish to 412 store arbitrary data in a cookie-value SHOULD encode that data, for 413 example, using Base 16 [RFC4648]. 415 The portions of the set-cookie-string produced by the cookie-av term 416 are known as attributes. To maximize compatibility with user agents, 417 servers SHOULD NOT produce two attributes with the same name in the 418 same set-cookie-string. (See Section 5.3 for how user agents handle 419 this case.) 421 Servers SHOULD NOT include more than one Set-Cookie header field in 422 the same response with the same cookie-name. (See Section 5.2 for 423 how user agents handle this case.) 425 If a server sends multiple responses containing Set-Cookie headers 426 concurrently to the user agent (e.g., when communicating with the 427 user agent over multiple sockets), these responses create a "race 428 condition" that can lead to unpredictable behavior. 430 NOTE: Some existing user agents differ on their interpretation of 431 two-digit years. To avoid compatibility issues, servers SHOULD use 432 the rfc1123-date format, which requires a four-digit year. 434 NOTE: Some user agents store and process dates in cookies as 32-bit 435 UNIX time_t values. Implementation bugs in the libraries supporting 436 time_t processing on some systems might cause such user agents to 437 process dates after the year 2038 incorrectly. 439 4.1.2. Semantics (Non-Normative) 441 This section describes a simplified semantics of the Set-Cookie 442 header. These semantics are detailed enough to be useful for 443 understanding the most common uses of cookies by servers. The full 444 semantics are described in Section 5. 446 When the user agent receives a Set-Cookie header, the user agent 447 stores the cookie together with its attributes. Subsequently, when 448 the user agent makes an HTTP request, the user agent includes the 449 applicable, non-expired cookies in the Cookie header. 451 If the user agent receives a new cookie with the same cookie-name, 452 domain-value, and path-value as a cookie that it has already stored, 453 the existing cookie is evicted and replaced with the new cookie. 455 Notice that servers can delete cookies by sending the user agent a 456 new cookie with an Expires attribute with a value in the past. 458 Unless the cookie's attributes indicate otherwise, the cookie is 459 returned only to the origin server (and not, e.g., to any 460 subdomains), and it expires at the end of the current session (as 461 defined by the user agent). User agents ignore unrecognized cookie 462 attributes (but not the entire cookie). 464 4.1.2.1. The Expires Attribute 466 The Expires attribute indicates the maximum lifetime of the cookie, 467 represented as the date and time at which the cookie expires. The 468 user agent is not required to retain the cookie until the specified 469 date has passed. In fact, user agents often evict cookies due to 470 memory pressure or privacy concerns. 472 4.1.2.2. The Max-Age Attribute 474 The Max-Age attribute indicates the maximum lifetime of the cookie, 475 represented as the number of seconds until the cookie expires. The 476 user agent is not required to retain the cookie for the specified 477 duration. In fact, user agents often evict cookies from due to 478 memory pressure or privacy concerns. 480 NOTE: Some existing user agents do not support the Max-Age 481 attribute. User agents that do not support the Max-Age attribute 482 ignore the attribute. 484 If a cookie has both the Max-Age and the Expires attribute, the Max- 485 Age attribute has precedence and controls the expiration date of the 486 cookie. If a cookie has neither the Max-Age nor the Expires 487 attribute, the user agent will retain the cookie until "the current 488 session is over" (as defined by the user agent). 490 4.1.2.3. The Domain Attribute 492 The Domain attribute specifies those hosts to which the cookie will 493 be sent. For example, if the value of the Domain attribute is 494 "example.com", the user agent will include the cookie in the Cookie 495 header when making HTTP requests to example.com, www.example.com, and 496 www.corp.example.com. (Note that a leading %x2E ("."), if present, 497 is ignored even though that character is not permitted, but a 498 trailing %x2E ("."), if present, will cause the user agent to ignore 499 the attribute.) If the server omits the Domain attribute, the user 500 agent will return the cookie only to the origin server. 502 WARNING: Some existing user agents treat an absent Domain 503 attribute as if the Domain attribute were present and contained 504 the current host name. For example, if example.com returns a Set- 505 Cookie header without a Domain attribute, these user agents will 506 erroneously send the cookie to www.example.com as well. 508 The user agent will reject cookies unless the Domain attribute 509 specifies a scope for the cookie that would include the origin 510 server. For example, the user agent will accept a cookie with a 511 Domain attribute of "example.com" or of "foo.example.com" from 512 foo.example.com, but the user agent will not accept a cookie with a 513 Domain attribute of "bar.example.com" or of "baz.foo.example.com". 515 NOTE: For security reasons, many user agents are configured to reject 516 Domain attributes that correspond to "public suffixes". For example, 517 some user agents will reject Domain attributes of "com" or "co.uk". 518 (See Section 5.3 for more information.) 520 4.1.2.4. The Path Attribute 522 The scope of each cookie is limited to a set of paths, controlled by 523 the Path attribute. If the server omits the Path attribute, the user 524 agent will use the "directory" of the request-uri's path component as 525 the default value. (See Section 5.1.4 for more details.) 527 The user agent will include the cookie in an HTTP request only if the 528 path portion of the request-uri matches (or is a subdirectory of) the 529 cookie's Path attribute, where the %x2F ("/") character is 530 interpreted as a directory separator. 532 Although seemingly useful for isolating cookies between different 533 paths within a given host, the Path attribute cannot be relied upon 534 for security (see Section 8). 536 4.1.2.5. The Secure Attribute 538 The Secure attribute limits the scope of the cookie to "secure" 539 channels (where "secure" is defined by the user agent). When a 540 cookie has the Secure attribute, the user agent will include the 541 cookie in an HTTP request only if the request is transmitted over a 542 secure channel (typically HTTP over Transport Layer Security (TLS) 543 [RFC2818]). 545 Although seemingly useful for protecting cookies from active network 546 attackers, the Secure attribute protects only the cookie's 547 confidentiality. An active network attacker can overwrite Secure 548 cookies from an insecure channel, disrupting their integrity (see 549 Section 8.6 for more details). 551 4.1.2.6. The HttpOnly Attribute 553 The HttpOnly attribute limits the scope of the cookie to HTTP 554 requests. In particular, the attribute instructs the user agent to 555 omit the cookie when providing access to cookies via "non-HTTP" APIs 556 (such as a web browser API that exposes cookies to scripts). 558 Note that the HttpOnly attribute is independent of the Secure 559 attribute: a cookie can have both the HttpOnly and the Secure 560 attribute. 562 4.2. Cookie 564 4.2.1. Syntax 566 The user agent sends stored cookies to the origin server in the 567 Cookie header. If the server conforms to the requirements in 568 Section 4.1 (and the user agent conforms to the requirements in 569 Section 5), the user agent will send a Cookie header that conforms to 570 the following grammar: 572 cookie-header = "Cookie:" OWS cookie-string OWS 573 cookie-string = cookie-pair *( ";" SP cookie-pair ) 575 4.2.2. Semantics 577 Each cookie-pair represents a cookie stored by the user agent. The 578 cookie-pair contains the cookie-name and cookie-value the user agent 579 received in the Set-Cookie header. 581 Notice that the cookie attributes are not returned. In particular, 582 the server cannot determine from the Cookie header alone when a 583 cookie will expire, for which hosts the cookie is valid, for which 584 paths the cookie is valid, or whether the cookie was set with the 585 Secure or HttpOnly attributes. 587 The semantics of individual cookies in the Cookie header are not 588 defined by this document. Servers are expected to imbue these 589 cookies with application-specific semantics. 591 Although cookies are serialized linearly in the Cookie header, 592 servers SHOULD NOT rely upon the serialization order. In particular, 593 if the Cookie header contains two cookies with the same name (e.g., 594 that were set with different Path or Domain attributes), servers 595 SHOULD NOT rely upon the order in which these cookies appear in the 596 header. 598 5. User Agent Requirements 600 This section specifies the Cookie and Set-Cookie headers in 601 sufficient detail that a user agent implementing these requirements 602 precisely can interoperate with existing servers (even those that do 603 not conform to the well-behaved profile described in Section 4. 605 5.1. Subcomponent Algorithms 607 This section defines some algorithms used by user agents to process 608 specific subcomponents of the Cookie and Set-Cookie headers. 610 5.1.1. Dates 612 The user agent MUST use an algorithm equivalent to the following 613 algorithm to parse a cookie-date. Note that the various boolean 614 flags defined as a part of the algorithm (i.e., found-time, found- 615 day-of-month, found-month, found-year) are initially "not set". 617 1. Using the grammar below, divide the cookie-date into date-tokens. 619 cookie-date = *delimiter date-token-list *delimiter 620 date-token-list = date-token *( 1*delimiter date-token ) 621 date-token = 1*non-delimiter 623 delimiter = %x09 / %x20-2F / %x3B-40 / %x5B-60 / %x7B-7E 624 non-delimiter = %x00-08 / %x0A-1F / DIGIT / ":" / ALPHA / %x7F-FF 625 non-digit = %x00-2F / %x3A-FF 627 day-of-month = 1*2DIGIT ( non-digit *OCTET ) 628 month = ( "jan" / "feb" / "mar" / "apr" / 629 "may" / "jun" / "jul" / "aug" / 630 "sep" / "oct" / "nov" / "dec" ) *OCTET 631 year = 2*4DIGIT ( non-digit *OCTET ) 632 time = hms-time ( non-digit *OCTET ) 633 hms-time = time-field ":" time-field ":" time-field 634 time-field = 1*2DIGIT 636 2. Process each date-token sequentially in the order the date-tokens 637 appear in the cookie-date: 639 1. If the found-time flag is not set and the token matches the 640 time production, set the found-time flag and set the hour- 641 value, minute-value, and second-value to the numbers denoted 642 by the digits in the date-token, respectively. Skip the 643 remaining sub-steps and continue to the next date-token. 645 2. If the found-day-of-month flag is not set and the date-token 646 matches the day-of-month production, set the found-day-of- 647 month flag and set the day-of-month-value to the number 648 denoted by the date-token. Skip the remaining sub-steps and 649 continue to the next date-token. 651 3. If the found-month flag is not set and the date-token matches 652 the month production, set the found-month flag and set the 653 month-value to the month denoted by the date-token. Skip the 654 remaining sub-steps and continue to the next date-token. 656 4. If the found-year flag is not set and the date-token matches 657 the year production, set the found-year flag and set the 658 year-value to the number denoted by the date-token. Skip the 659 remaining sub-steps and continue to the next date-token. 661 3. If the year-value is greater than or equal to 70 and less than or 662 equal to 99, increment the year-value by 1900. 664 4. If the year-value is greater than or equal to 0 and less than or 665 equal to 69, increment the year-value by 2000. 667 1. NOTE: Some existing user agents interpret two-digit years 668 differently. 670 5. Abort these steps and fail to parse the cookie-date if 672 * at least one of the found-day-of-month, found-month, found- 673 year, or found-time flags is not set, 675 * the day-of-month-value is less than 1 or greater than 31, 677 * the year-value is less than 1601, 679 * the hour-value is greater than 23, 681 * the minute-value is greater than 59, or 683 * the second-value is greater than 59. 685 (Note that leap seconds cannot be represented in this syntax.) 687 6. Let the parsed-cookie-date be the date whose day-of-month, month, 688 year, hour, minute, and second (in UTC) are the day-of-month- 689 value, the month-value, the year-value, the hour-value, the 690 minute-value, and the second-value, respectively. If no such 691 date exists, abort these steps and fail to parse the cookie-date. 693 7. Return the parsed-cookie-date as the result of this algorithm. 695 5.1.2. Canonicalized host names 697 A canonicalized host name is the string generated by the following 698 algorithm: 700 1. Convert the host name to a sequence of individual domain name 701 labels. 703 2. Convert each label that is not a NR-LDH label, to a A-label (see 704 Section 2.3.2.1 of [RFC5890] for the fomer and latter), or to a 705 "punycode label" (a label resulting from the "ToASCII" conversion 706 in Section 4 of [RFC3490]), as appropriate (see Section 6.3 of 707 this specification). 709 3. Concatentate the resulting labels, separated by a %x2E (".") 710 character. 712 5.1.3. Domain matching 714 A string domain-matches a given domain string if at least one of the 715 following conditions hold: 717 o The domain string and the string are identical. (Note that both 718 the domain string and the string will have been canonicalized to 719 lower case at this point.) 721 o All of the following conditions hold: 723 * The domain string is a suffix of the string. 725 * The last character of the string that is not included in the 726 domain string is a %x2E (".") character. 728 * The string is a host name (i.e., not an IP address). 730 5.1.4. Paths and path-match 732 The user agent MUST use an algorithm equivalent to the following 733 algorithm to compute the default-path of a cookie: 735 1. Let uri-path be the path portion of the request-uri if such a 736 portion exists (and empty otherwise). For example, if the 737 request-uri contains just a path (and optional query string), 738 then the uri-path is that path (without the %x3F ("?") character 739 or query string), and if the request-uri contains a full 740 absoluteURI, the uri-path is the path component of that URI. 742 2. If the uri-path is empty or if the first character of the uri- 743 path is not a %x2F ("/") character, output %x2F ("/") and skip 744 the remaining steps. 746 3. If the uri-path contains only a single %x2F ("/") character, 747 output %x2F ("/") and skip the remaining steps. 749 4. Output the characters of the uri-path from the first character up 750 to, but not including, the right-most %x2F ("/"). 752 A request-path path-matches a given cookie-path if at least one of 753 the following conditions hold: 755 o The cookie-path and the request-path are identical. 757 o The cookie-path is a prefix of the request-path and the last 758 character of the cookie-path is %x2F ("/"). 760 o The cookie-path is a prefix of the request-path and the first 761 character of the request-path that is not included in the cookie- 762 path is a %x2F ("/") character. 764 5.2. The Set-Cookie Header 766 When a user agent receives a Set-Cookie header field in an HTTP 767 response, the user agent MAY ignore the Set-Cookie header field in 768 its entirety. For example, the user agent might wish to block 769 responses to "third-party" requests from setting cookies (See 770 Section 7.1). 772 If the user agent does not ignore the Set-Cookie header field in its 773 entirety, the user agent MUST parse the field-value of the Set-Cookie 774 header field as a set-cookie-string (defined below). 776 NOTE: The algorithm below is more permissive than the grammar in 777 Section 4.1. For example, the algorithm strips leading and trailing 778 whitespace from the cookie name and value (but maintains internal 779 whitespace), whereas the grammar in Section 4.1 forbids whitespace in 780 these positions. User agents use this algorithm so as to 781 interoperate with servers that do not follow the recommendations in 782 Section 4. 784 A user agent MUST use an algorithm equivalent to the following 785 algorithm to parse a "set-cookie-string": 787 1. If the set-cookie-string contains a %x3B (";") character: 789 The name-value-pair string consists of the characters up to, 790 but not including, the first %x3B (";"), and the unparsed- 791 attributes consist of the remainder of the set-cookie-string 792 (including the %x3B (";") in question). 794 Otherwise: 796 The name-value-pair string consists of all the characters 797 contained in the set-cookie-string, and the unparsed- 798 attributes is the empty string. 800 2. If the name-value-pair string lacks a %x3D ("=") character, 801 ignore the set-cookie-string entirely. 803 3. The (possibly empty) name string consists of the characters up 804 to, but not including, the first %x3D ("=") character, and the 805 (possibly empty) value string consists of the characters after 806 the first %x3D ("=") character. 808 4. Remove any leading or trailing WSP characters from the name 809 string and the value string. 811 5. If the name string is empty, ignore the set-cookie-string 812 entirely. 814 6. The cookie-name is the name string, and the cookie-value is the 815 value string. 817 The user agent MUST use an algorithm equivalent to the following 818 algorithm to parse the unparsed-attributes: 820 1. If the unparsed-attributes string is empty, skip the rest of 821 these steps. 823 2. Discard the first character of the unparsed-attributes (which 824 will be a %x3B (";") character). 826 3. If the remaining unparsed-attributes contains a %x3B (";") 827 character: 829 Consume the characters of the unparsed-attributes up to, but 830 not including, the first %x3B (";") character. 832 Otherwise: 834 Consume the remainder of the unparsed-attributes. 836 Let the cookie-av string be the characters consumed in this step. 838 4. If the cookie-av string contains a %x3D ("=") character: 840 The (possibly empty) attribute-name string consists of the 841 characters up to, but not including, the first %x3D ("=") 842 character, and the (possibly empty) attribute-value string 843 consists of the characters after the first %x3D ("=") 844 character. 846 Otherwise: 848 The attribute-name string consists of the entire cookie-av 849 string, and the attribute-value string is empty. 851 5. Remove any leading or trailing WSP characters from the attribute- 852 name string and the attribute-value string. 854 6. Process the attribute-name and attribute-value according to the 855 requirements in the following subsections. (Notice that 856 attributes with unrecognized attribute-names are ignored.) 858 7. Return to Step 1 of this algorithm. 860 When the user agent finishes parsing the set-cookie-string, the user 861 agent is said to "receive a cookie" from the request-uri with name 862 cookie-name, value cookie-value, and attributes cookie-attribute- 863 list. (See Section 5.3 for additional requirements triggered by 864 receiving a cookie.) 866 5.2.1. The Expires Attribute 868 If the attribute-name case-insensitively matches the string 869 "Expires", the user agent MUST process the cookie-av as follows. 871 Let the expiry-time be the result of parsing the attribute-value as 872 cookie-date (see Section 5.1.1). 874 If the attribute-value failed to parse as a cookie date, ignore the 875 cookie-av. 877 If the expiry-time is later than the last date the user agent can 878 represent, the user agent MAY replace the expiry-time with the last 879 representable date. 881 If the expiry-time is earlier than the earliest date the user agent 882 can represent, the user agent MAY replace the expiry-time with the 883 earliest representable date. 885 Append an attribute to the cookie-attribute-list with an attribute- 886 name of Expires and an attribute-value of expiry-time. 888 5.2.2. The Max-Age Attribute 890 If the attribute-name case-insensitively matches the string "Max- 891 Age", the user agent MUST process the cookie-av as follows. 893 If the first character of the attribute-value is not a DIGIT or a "-" 894 character, ignore the cookie-av. 896 If the remainder of attribute-value contains a non-DIGIT character, 897 ignore the cookie-av. 899 Let delta-seconds be the attribute-value converted to an integer. 901 If delta-seconds is less than or equal to zero (0), let expiry-time 902 be the earliest representable date and time. Otherwise, let the 903 expiry-time be the current date and time plus delta-seconds seconds. 905 Append an attribute to the cookie-attribute-list with an attribute- 906 name of Max-Age and an attribute-value of expiry-time. 908 5.2.3. The Domain Attribute 910 If the attribute-name case-insensitively matches the string "Domain", 911 the user agent MUST process the cookie-av as follows. 913 If the attribute-value is empty, the behavior is undefined. However, 914 user agent SHOULD ignore the cookie-av entirely. 916 If the first character of the attribute-value string is %x2E ("."): 918 Let cookie-domain be the attribute-value without the leading %x2E 919 (".") character. 921 Otherwise: 923 Let cookie-domain be the entire attribute-value. 925 Convert the cookie-domain to lower case. 927 Append an attribute to the cookie-attribute-list with an attribute- 928 name of Domain and an attribute-value of cookie-domain. 930 5.2.4. The Path Attribute 932 If the attribute-name case-insensitively matches the string "Path", 933 the user agent MUST process the cookie-av as follows. 935 If the attribute-value is empty or if the first character of the 936 attribute-value is not %x2F ("/"): 938 Let cookie-path be the default-path. 940 Otherwise: 942 Let cookie-path be the attribute-value. 944 Append an attribute to the cookie-attribute-list with an attribute- 945 name of Path and an attribute-value of cookie-path. 947 5.2.5. The Secure Attribute 949 If the attribute-name case-insensitively matches the string "Secure", 950 the user agent MUST append an attribute to the cookie-attribute-list 951 with an attribute-name of Secure and an empty attribute-value. 953 5.2.6. The HttpOnly Attribute 955 If the attribute-name case-insensitively matches the string 956 "HttpOnly", the user agent MUST append an attribute to the cookie- 957 attribute-list with an attribute-name of HttpOnly and an empty 958 attribute-value. 960 5.3. Storage Model 962 The user agent stores the following fields about each cookie: name, 963 value, expiry-time, domain, path, creation-time, last-access-time, 964 persistent-flag, host-only-flag, secure-only-flag, and http-only- 965 flag. 967 When the user agent "receives a cookie" from a request-uri with name 968 cookie-name, value cookie-value, and attributes cookie-attribute- 969 list, the user agent MUST process the cookie as follows: 971 1. A user agent MAY ignore a received cookie in its entirety. For 972 example, the user agent might wish to block receiving cookies 973 from "third-party" responses or the user agent might not wish to 974 store cookies that exceed some size. 976 2. Create a new cookie with name cookie-name, value cookie-value. 977 Set the creation-time and the last-access-time to the current 978 date and time. 980 3. If the cookie-attribute-list contains an attribute with an 981 attribute-name of "Max-Age": 983 Set the cookie's persistent-flag to true. 985 Set the cookie's expiry-time to attribute-value of the last 986 attribute in the cookie-attribute-list with an attribute-name 987 of "Max-Age". 989 Otherwise, if the cookie-attribute-list contains an attribute 990 with an attribute-name of "Expires" (and does not contain an 991 attribute with an attribute-name of "Max-Age"): 993 Set the cookie's persistent-flag to true. 995 Set the cookie's expiry-time to attribute-value of the last 996 attribute in the cookie-attribute-list with an attribute-name 997 of "Expires". 999 Otherwise: 1001 Set the cookie's persistent-flag to false. 1003 Set the cookie's expiry-time to the latest representable 1004 date. 1006 4. If the cookie-attribute-list contains an attribute with an 1007 attribute-name of "Domain": 1009 Let the domain-attribute be the attribute-value of the last 1010 attribute in the cookie-attribute-list with an attribute-name 1011 of "Domain". 1013 Otherwise: 1015 Let the domain-attribute be the empty string. 1017 5. If the user agent is configured to reject "public suffixes" and 1018 the domain-attribute is a public suffix: 1020 If the domain-attribute is identical to the canonicalized 1021 request-host: 1023 Let the domain-attribute be the empty string. 1025 Otherwise: 1027 Ignore the cookie entirely and abort these steps. 1029 NOTE: A "public suffix" is a domain that is controlled by a 1030 public registry, such as "com", "co.uk", and "pvt.k12.wy.us". 1031 This step is essential for preventing attacker.com from 1032 disrupting the integrity of example.com by setting a cookie 1033 with a Domain attribute of "com". Unfortunately, the set of 1034 public suffixes (also known as "registry controlled domains") 1035 changes over time. If feasible, user agents SHOULD use an 1036 up-to-date public suffix list, such as the one maintained by 1037 the Mozilla project at . 1039 6. If the domain-attribute is non-empty: 1041 If the canonicalized request-host does not domain-match the 1042 domain-attribute: 1044 Ignore the cookie entirely and abort these steps. 1046 Otherwise: 1048 Set the cookie's host-only-flag to false. 1050 Set the cookie's domain to the domain-attribute. 1052 Otherwise: 1054 Set the cookie's host-only-flag to true. 1056 Set the cookie's domain to the canonicalized request-host. 1058 7. If the cookie-attribute-list contains an attribute with an 1059 attribute-name of "Path", set the cookie's path to attribute- 1060 value of the last attribute in the cookie-attribute-list with an 1061 attribute-name of "Path". Otherwise, set cookie's path to the 1062 default-path of the request-uri. 1064 8. If the cookie-attribute-list contains an attribute with an 1065 attribute-name of "Secure", set the cookie's secure-only-flag to 1066 true. Otherwise, set cookie's secure-only-flag to false. 1068 9. If the cookie-attribute-list contains an attribute with an 1069 attribute-name of "HttpOnly", set the cookie's http-only-flag to 1070 true. Otherwise, set cookie's http-only-flag to false. 1072 10. If the cookie was received from a "non-HTTP" API and the 1073 cookie's http-only-flag is set, abort these steps and ignore the 1074 cookie entirely. 1076 11. If the cookie store contains a cookie with the same name, 1077 domain, and path as the newly created cookie: 1079 1. Let old-cookie be the existing cookie with the same name, 1080 domain, and path as the newly created cookie. (Notice that 1081 this algorithm maintains the invariant that there is at most 1082 one such cookie.) 1084 2. If the newly created cookie was received from a "non-HTTP" 1085 API and the old-cookie's http-only-flag is set, abort these 1086 steps and ignore the newly created cookie entirely. 1088 3. Update the creation-time of the newly created cookie to 1089 match the creation-time of the old-cookie. 1091 4. Remove the old-cookie from the cookie store. 1093 12. Insert the newly created cookie into the cookie store. 1095 A cookie is "expired" if the cookie has an expiry date in the past. 1097 The user agent MUST evict all expired cookies from the cookie store 1098 if, at any time, an expired cookie exists in the cookie store. 1100 At any time, the user agent MAY "remove excess cookies" from the 1101 cookie store if the number of cookies sharing a domain field exceeds 1102 some implementation defined upper bound (such as 50 cookies). 1104 At any time, the user agent MAY "remove excess cookies" from the 1105 cookie store if the cookie store exceeds some predetermined upper 1106 bound (such as 3000 cookies). 1108 When the user agent removes excess cookies from the cookie store, the 1109 user agent MUST evict cookies in the following priority order: 1111 1. Expired cookies. 1113 2. Cookies that share a domain field with more than a predetermined 1114 number of other cookies. 1116 3. All cookies. 1118 If two cookies have the same removal priority, the user agent MUST 1119 evict the cookie with the earliest last-access date first. 1121 When "the current session is over" (as defined by the user agent), 1122 the user agent MUST remove from the cookie store all cookies with the 1123 persistent-flag set to false. 1125 5.4. The Cookie Header 1127 The user agent includes stored cookies in the Cookie HTTP request 1128 header. 1130 When the user agent generates an HTTP request, the user agent MUST 1131 NOT attach more than one Cookie header field. 1133 A user agent MAY omit the Cookie header in its entirety. For 1134 example, the user agent might wish to block sending cookies during 1135 "third-party" requests from setting cookies (See Section 7.1). 1137 If the user agent does attach a Cookie header field to an HTTP 1138 request, the user agent MUST send the cookie-string (defined below) 1139 as the value of the header field. 1141 The user agent MUST use an algorithm equivalent to the following 1142 algorithm to compute the "cookie-string" from a cookie store and a 1143 request-uri: 1145 1. Let cookie-list be the set of cookies from the cookie store that 1146 meet all of the following requirements: 1148 * Either: 1150 The cookie's host-only-flag is true and the canonicalized 1151 request-host is identical to the cookie's domain. 1153 Or: 1155 The cookie's host-only-flag is false and the canonicalized 1156 request-host domain-matches cookie's domain. 1158 * The request-uri's path path-matches cookie's path. 1160 * If the cookie's secure-only-flag is true, then the request- 1161 uri's scheme must denote a "secure" protocol (as defined by 1162 the user agent). 1164 NOTE: The notion of a "secure" protocol is not defined by 1165 this document. Typically, user agents consider a protocol 1166 secure if the protocol makes use of transport-layer 1167 security, such as SSL or TLS. For example, most user 1168 agents consider "https" to be a scheme that denotes a 1169 secure protocol. 1171 * If the cookie's http-only-flag is true, then exclude the 1172 cookie if the cookie-string is being generated for a "non- 1173 HTTP" API (as defined by the user agent). 1175 2. The user agent SHOULD sort the cookie-list in the following 1176 order: 1178 * Cookies with longer paths are listed before cookies with 1179 shorter paths. 1181 * Among cookies that have equal length path fields, cookies with 1182 earlier creation-times are listed before cookies with later 1183 creation-times. 1185 NOTE: Not all user agents sort the cookie-list in this order, but 1186 this order reflects common practice when this document was 1187 written, and, historically, there have been servers that 1188 (erroneously) depended on this order. 1190 3. Update the last-access-time of each cookie in the cookie-list to 1191 the current date and time. 1193 4. Serialize the cookie-list into a cookie-string by processing each 1194 cookie in the cookie-list in order: 1196 1. Output the cookie's name, the %x3D ("=") character, and the 1197 cookie's value. 1199 2. If there is an unprocessed cookie in the cookie-list, output 1200 the characters %x3B and %x20 ("; "). 1202 NOTE: Despite its name, the cookie-string is actually a sequence of 1203 octets, not a sequence of characters. To convert the cookie-string 1204 (or components thereof) into a sequence of characters (e.g., for 1205 presentation to the user), the user agent might wish to try using the 1206 UTF-8 character encoding [RFC3629] to decode the octet sequence. 1207 This decoding might fail, however, because not every sequence of 1208 octets is valid UTF-8. 1210 6. Implementation Considerations 1212 6.1. Limits 1214 Practical user agent implementations have limits on the number and 1215 size of cookies that they can store. General-use user agents SHOULD 1216 provide each of the following minimum capabilities: 1218 o At least 4096 bytes per cookie (as measured by the sum of the 1219 length of the cookie's name, value, and attributes). 1221 o At least 50 cookies per domain. 1223 o At least 3000 cookies total. 1225 Servers SHOULD use as few and as small cookies as possible to avoid 1226 reaching these implementation limits and to minimize network 1227 bandwidth due to the Cookie header being included in every request. 1229 Servers SHOULD gracefully degrade if the user agent fails to return 1230 one or more cookies in the Cookie header because the user agent might 1231 evict any cookie at any time on orders from the user. 1233 6.2. Application Programming Interfaces 1235 One reason the Cookie and Set-Cookie headers uses such esoteric 1236 syntax is because many platforms (both in servers and user agents) 1237 provide a string-based application programing interface (API) to 1238 cookies, requiring application-layer programmers to generate and 1239 parse the syntax used by the Cookie and Set-Cookie headers, which 1240 many programmers have done incorrectly, resulting in interoperability 1241 problems. 1243 Instead of providing string-based APIs to cookies, platforms would be 1244 well-served by providing more semantic APIs. It is beyond the scope 1245 of this document to recommend specific API designs, but there are 1246 clear benefits to accepting an abstract "Date" object instead of a 1247 serialized date string. 1249 6.3. IDNA dependency and migration 1251 IDNA2008 [RFC5890] supersedes IDNA2003 [RFC3490]. However, there are 1252 differences between the two specifications, and thus there can be 1253 differences in processing (e.g. converting) domain name labels that 1254 have been registered under one from those registered under the other. 1255 There will be a transition period of some time during which IDNA2003- 1256 based domain name labels will exist in the wild. User agents SHOULD 1257 implement IDNA2008 [RFC5890] and MAY implement [UTS46] or [RFC5895] 1258 in order to facilitate their IDNA transition. If a user agent does 1259 not implement IDNA2008, the user agent MUST implement IDNA2003 1260 [RFC3490]. 1262 7. Privacy Considerations 1264 Cookies are often criticized for letting servers track users. For 1265 example, a number of "web analytics" companies use cookies to 1266 recognize when a user returns to a web site or visits another web 1267 site. Although cookies are not the only mechanism servers can use to 1268 track users across HTTP requests, cookies facilitate tracking because 1269 they are persistent across user agent sessions and can be shared 1270 between hosts. 1272 7.1. Third-Party Cookies 1274 Particularly worrisome are so-called "third-party" cookies. In 1275 rendering an HTML document, a user agent often requests resources 1276 from other servers (such as advertising networks). These third-party 1277 servers can use cookies to track the user even if the user never 1278 visits the server directly. For example, if a user visits a site 1279 that contains content from a third party and then later visits 1280 another site that contains content from the same third party, the 1281 third party can track the user between the two sites. 1283 Some user agents restrict how third-party cookies behave. For 1284 example, some of these user agents refuse to send the Cookie header 1285 in third-party requests. Others refuse to process the Set-Cookie 1286 header in responses to third-party requests. User agents vary widely 1287 in their third-party cookie policies. This document grants user 1288 agents wide latitude to experiment with third-party cookie policies 1289 that balance the privacy and compatibility needs of their users. 1290 However, this document does not endorse any particular third-party 1291 cookie policy. 1293 Third-party cookie blocking policies are often ineffective at 1294 achieving their privacy goals if servers attempt to work around their 1295 restrictions to track users. In particular, two collaborating 1296 servers can often track users without using cookies at all by 1297 injecting identifying information into dynamic URLs. 1299 7.2. User Controls 1301 User agents SHOULD provide users with a mechanism for managing the 1302 cookies stored in the cookie store. For example, a user agent might 1303 let users delete all cookies received during a specified time period 1304 or all the cookies related to a particular domain. In addition, many 1305 user agents include a user interface element that lets users examine 1306 the cookies stored in their cookie store. 1308 User agents SHOULD provide users with a mechanism for disabling 1309 cookies. When cookies are disabled, the user agent MUST NOT include 1310 a Cookie header in outbound HTTP requests and the user agent MUST NOT 1311 process Set-Cookie headers in inbound HTTP responses. 1313 Some user agents provide users the option of preventing persistent 1314 storage of cookies across sessions. When configured thusly, user 1315 agents MUST treat all received cookies as if the persistent-flag were 1316 set to false. Some popular user agents expose this functionality via 1317 "private browsing" mode [Aggarwal2010] 1319 Some user agents provide users with the ability to approve individual 1320 writes to the cookie store. In many common usage scenarios, these 1321 controls generate a large number of prompts. However, some privacy- 1322 conscious users find these controls useful nonetheless. 1324 7.3. Expiration Dates 1326 Although servers can set the expiration date for cookies to the 1327 distant future, most user agents do not actually retain cookies for 1328 multiple decades. Rather than chosing gratiously long expiration 1329 periods, servers SHOULD promote user privacy by selecting reasonable 1330 cookie expiration periods based on the purpose of the cookie. For 1331 example, a typical session identifier might reasonably be set to 1332 expire in two weeks. 1334 8. Security Considerations 1336 8.1. Overview 1338 Cookies have a number of security pitfalls. This section overviews a 1339 few of the more salient issues. 1341 In particular, cookies encourage developers to rely on ambient 1342 authority for authentication, often becoming vulnerable to attacks 1343 such as cross-site request forgery [CSRF]. Also, when storing 1344 session identifiers in cookies, developers often create session 1345 fixation vulnerabilities. 1347 Transport-layer encryption, such as that employed in HTTPS, is 1348 insufficient to prevent a network attacker from obtaining or altering 1349 a victim's cookies because the cookie protocol itself has various 1350 vulnerabilities (see "Weak Confidentiality" and "Weak Integrity", 1351 below). In addition, by default, cookies do not provide 1352 confidentiality or integrity from network attackers, even when used 1353 in conjunction with HTTPS. 1355 8.2. Ambient Authority 1357 A server that uses cookies to authenticate users can suffer security 1358 vulnerabilities because some user agents let remote parties issue 1359 HTTP requests from the user agent (e.g., via HTTP redirects or HTML 1360 forms). When issuing those requests, user agents attach cookies even 1361 if the remote party does not know the contents of the cookies, 1362 potentially letting the remote party exercise authority at an unwary 1363 server. 1365 Although this security concern goes by a number of names (e.g., 1366 cross-site request forgery, confused deputy), the issue stems from 1367 cookies being a form of ambient authority. Cookies encourage server 1368 operators to separate designation (in the form of URLs) from 1369 authorization (in the form of cookies). Consequently, the user agent 1370 might supply the authorization for a resource designated by the 1371 attacker, possibly causing the server or its clients to undertake 1372 actions designated by the attacker as though they were authorized by 1373 the user. 1375 Instead of using cookies for authorization, server operators might 1376 wish to consider entangling designation and authorization by treating 1377 URLs as capabilities. Instead of storing secrets in cookies, this 1378 approach stores secrets in URLs, requiring the remote entity to 1379 supply the secret itself. Although this approach is not a panacea, 1380 judicious application of these principles can lead to more robust 1381 security. 1383 8.3. Clear Text 1385 Unless sent over a secure channel (such as TLS), the information in 1386 the Cookie and Set-Cookie headers is transmitted in the clear. 1388 1. All sensitive information conveyed in these headers is exposed to 1389 an eavesdropper. 1391 2. A malicious intermediary could alter the headers as they travel 1392 in either direction, with unpredictable results. 1394 3. A malicious client could alter the Cookie header before 1395 transmission, with unpredictable results. 1397 Servers SHOULD encrypt and sign the contents of cookies (using 1398 whatever format the server desires) when transmitting them to the 1399 user agent (even when sending the cookies over a secure channel). 1400 However, encrypting and signing cookie contents does not prevent an 1401 attacker from transplanting a cookie from one user agent to another 1402 or from replaying the cookie at a later time. 1404 In addition to encrypting and signing the contents of every cookie, 1405 servers that require a higher level of security SHOULD use the Cookie 1406 and Set-Cookie headers only over a secure channel. When using 1407 cookies over a secure channel, servers SHOULD set the Secure 1408 attribute (see Section 4.1.2.5) for every cookie. If a server does 1409 not set the Secure attribute, the protection provided by the secure 1410 channel will be largely moot. 1412 For example, consider a webmail server that stores a session 1413 identifier in a cookie and is typically accessed over HTTPS. If the 1414 server does not set the Secure attribute on its cookies, an active 1415 network attacker can intercept any outbound HTTP request from the 1416 user agent and redirect that request to the webmail server over HTTP. 1417 Even if the webmail server is not listening for HTTP connections, the 1418 user agent will still include cookies in the request. The active 1419 network attacker can intercept these cookies, reply them against the 1420 server, and learn the contents of the user's email. If, instead, the 1421 server had set the Secure attribute on its cookies, the user agent 1422 would not have included the cookies in the clear-text request. 1424 8.4. Session Identifiers 1426 Instead of storing session information directly in a cookie (where it 1427 might be exposed to or replayed by an attacker), servers commonly 1428 store a nonce (or "session identifier") in a cookie. When the server 1429 receives an HTTP request with a nonce, the server can look up state 1430 information associated with the cookie using the nonce as a key. 1432 Using session identifier cookies limits the damage an attacker can 1433 cause if the attacker learns the contents of a cookie because the 1434 nonce is useful only for interacting with the server (unlike non- 1435 nonce cookie content, which might itself be sensitive). Furthermore, 1436 using a single nonce prevents an attacker from "splicing" together 1437 cookie content from two interactions with the server, which could 1438 cause the server to behave unexpectedly. 1440 Using session identifiers is not without risk. For example, the 1441 server SHOULD take care to avoid "session fixation" vulnerabilities. 1442 A session fixation attack proceeds in three steps. First, the 1443 attacker transplants a session identifier from his or her user agent 1444 to the victim's user agent. Second, the victim uses that session 1445 identifier to interact with the server, possibly imbuing the session 1446 identifier with the user's credentials or confidential information. 1447 Third, the attacker uses the session identifier to interact with 1448 server directly, possibly obtaining the user's authority or 1449 confidential information. 1451 8.5. Weak Confidentiality 1453 Cookies do not provide isolation by port. If a cookie is readable by 1454 a service running on one port, the cookie is also readable by a 1455 service running on another port of the same server. If a cookie is 1456 writable by a service on one port, the cookie is also writable by a 1457 service running on another port of the same server. For this reason, 1458 servers SHOULD NOT both run mutually distrusting services on 1459 different ports of the same host and use cookies to store security- 1460 sensitive information. 1462 Cookies do not provide isolation by scheme. Although most commonly 1463 used with the http and https schemes, the cookies for a given host 1464 might also be available to other schemes, such as ftp and gopher. 1465 Although this lack of isolation by scheme is most apparent in non- 1466 HTTP APIs that permit access to cookies (e.g., HTML's document.cookie 1467 API), the lack of isolation by scheme is actually present in 1468 requirements for processing cookies themselves (e.g., consider 1469 retrieving a URI with the gopher scheme via HTTP). 1471 Cookies do not always provide isolation by path. Although the 1472 network-level protocol does not send cookies stored for one path to 1473 another, some user agents expose cookies via non-HTTP APIs, such as 1474 HTML's document.cookie API. Because some of these user agents (e.g., 1475 web browsers) do not isolate resources received from different paths, 1476 a resource retrieved from one path might be able to access cookies 1477 stored for another path. 1479 8.6. Weak Integrity 1481 Cookies do not provide integrity guarantees for sibling domains (and 1482 their subdomains). For example, consider foo.example.com and 1483 bar.example.com. The foo.example.com server can set a cookie with a 1484 Domain attribute of "example.com" (possibly overwriting an existing 1485 "example.com" cookie set by bar.example.com), and the user agent will 1486 include that cookie in HTTP requests to bar.example.com. In the 1487 worst case, bar.example.com will be unable to distinguish this cookie 1488 from a cookie it set itself. The foo.example.com server might be 1489 able to leverage this ability to mount an attack against 1490 bar.example.com. 1492 Even though the Set-Cookie header supports the Path attribute, the 1493 Path attribute does not provide any integrity protection because the 1494 user agent will accept an arbitrary Path attribute in a Set-Cookie 1495 header. For example, an HTTP response to a request for 1496 http://example.com/foo/bar can set a cookie with a Path attribute of 1497 "/qux". Consequently, servers SHOULD NOT both run mutually 1498 distrusting services on different paths of the same host and use 1499 cookies to store security-sensitive information. 1501 An active network attacker can also inject cookies into the Cookie 1502 header sent to https://example.com/ by impersonating a response from 1503 http://example.com/ and injecting a Set-Cookie header. The HTTPS 1504 server at example.com will be unable to distinguish these cookies 1505 from cookies that it set itself in an HTTPS response. An active 1506 network attacker might be able to leverage this ability to mount an 1507 attack against example.com even if example.com uses HTTPS 1508 exclusively. 1510 Servers can partially mitigate these attacks by encrypting and 1511 signing the contents of their cookies. However, using cryptography 1512 does not mitigate the issue completely because an attacker can replay 1513 a cookie he or she received from the authentic example.com server in 1514 the user's session, with unpredictable results. 1516 Finally, an attacker might be able to force the user agent to delete 1517 cookies by storing a large number of cookies. Once the user agent 1518 reaches its storage limit, the user agent will be forced to evict 1519 some cookies. Servers SHOULD NOT rely upon user agents retaining 1520 cookies. 1522 8.7. Reliance on DNS 1524 Cookies rely upon the Domain Name System (DNS) for security. If the 1525 DNS is partially or fully compromised, the cookie protocol might fail 1526 to provide the security properties required by applications. 1528 9. IANA Considerations 1530 The permanent message header field registry (see [RFC3864]) should be 1531 updated with the following registrations: 1533 9.1. Cookie 1535 Header field name: Cookie 1537 Applicable protocol: http 1539 Status: standard 1541 Author/Change controller: IETF 1543 Specification document: this specification (Section 5.4) 1545 9.2. Set-Cookie 1547 Header field name: Set-Cookie 1549 Applicable protocol: http 1551 Status: standard 1553 Author/Change controller: IETF 1555 Specification document: this specification (Section 5.2) 1557 9.3. Cookie2 1559 Header field name: Cookie2 1561 Applicable protocol: http 1563 Status: obsoleted 1565 Author/Change controller: IETF 1567 Specification document: [RFC2965] 1569 9.4. Set-Cookie2 1571 Header field name: Set-Cookie2 1573 Applicable protocol: http 1575 Status: obsoleted 1576 Author/Change controller: IETF 1578 Specification document: [RFC2965] 1580 10. References 1582 10.1. Normative References 1584 [RFC1034] Mockapetris, P., "Domain names - concepts and facilities", 1585 STD 13, RFC 1034, November 1987. 1587 [RFC1123] Braden, R., "Requirements for Internet Hosts - Application 1588 and Support", STD 3, RFC 1123, October 1989. 1590 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1591 Requirement Levels", BCP 14, RFC 2119, March 1997. 1593 [RFC2616] Fielding, R., Gettys, J., Mogul, J., Frystyk, H., 1594 Masinter, L., Leach, P., and T. Berners-Lee, "Hypertext 1595 Transfer Protocol -- HTTP/1.1", RFC 2616, June 1999. 1597 [RFC3490] Faltstrom, P., Hoffman, P., and A. Costello, 1598 "Internationalizing Domain Names in Applications (IDNA)", 1599 RFC 3490, March 2003. 1601 See Section 6.3 for an explanation why the normative 1602 reference to an obsoleted specification is needed. 1604 [RFC4790] Newman, C., Duerst, M., and A. Gulbrandsen, "Internet 1605 Application Protocol Collation Registry", RFC 4790, 1606 March 2007. 1608 [RFC5234] Crocker, D., Ed. and P. Overell, "Augmented BNF for Syntax 1609 Specifications: ABNF", STD 68, RFC 5234, January 2008. 1611 [RFC5246] Dierks, T. and E. Rescorla, "The Transport Layer Security 1612 (TLS) Protocol Version 1.2", RFC 5246, August 2008. 1614 [RFC5890] Klensin, J., "Internationalized Domain Names for 1615 Applications (IDNA): Definitions and Document Framework", 1616 RFC 5890, August 2010. 1618 10.2. Informative References 1620 [RFC2109] Kristol, D. and L. Montulli, "HTTP State Management 1621 Mechanism", RFC 2109, February 1997. 1623 [RFC2965] Kristol, D. and L. Montulli, "HTTP State Management 1624 Mechanism", RFC 2965, October 2000. 1626 [RFC2818] Rescorla, E., "HTTP Over TLS", RFC 2818, May 2000. 1628 [Netscape] 1629 Netscape Communications Corp., "Persistent Client State -- 1630 HTTP Cookies", 1999, . 1634 [Kri2001] Kristol, D., "HTTP Cookies: Standards, Privacy, and 1635 Politics", ACM Transactions on Internet Technology Vol. 1, 1636 #2, November 2001, . 1638 [RFC3629] Yergeau, F., "UTF-8, a transformation format of ISO 1639 10646", STD 63, RFC 3629, November 2003. 1641 [RFC4648] Josefsson, S., "The Base16, Base32, and Base64 Data 1642 Encodings", RFC 4648, October 2006. 1644 [RFC3864] Klyne, G., Nottingham, M., and J. Mogul, "Registration 1645 Procedures for Message Header Fields", BCP 90, RFC 3864, 1646 September 2004. 1648 [RFC5895] Resnick, P. and P. Hoffman, "Mapping Characters for 1649 Internationalized Domain Names in Applications (IDNA) 1650 2008", RFC 5895, September 2010. 1652 [UTS46] Davis, M. and M. Suignard, "Unicode IDNA Compatibility 1653 Processing", Unicode Technical Standards # 46, 2010, 1654 . 1656 [CSRF] Barth, A., Jackson, C., and J. Mitchell, "Robust Defenses 1657 for Cross-Site Request Forgery", 2008, . 1661 [Aggarwal2010] 1662 Aggarwal, G., Burzstein, E., Jackson, C., and D. Boneh, 1663 "An Analysis of Private Browsing Modes in Modern 1664 Browsers", 2010, . 1667 Appendix A. Acknowledgements 1669 This document borrows heavily from RFC 2109 [RFC2109]. We are 1670 indebted to David M. Kristol and Lou Montulli for their efforts to 1671 specify cookies. David M. Kristol, in particular, provided 1672 invaluable advice on navigating the IETF process. We would also like 1673 to thank Thomas Broyer, Tyler Close, Alissa Cooper, Bil Corry, 1674 corvid, Lisa Dusseault, Roy T. Fielding, Blake Frantz, Anne van 1675 Kesteren, Eran Hammer-Lahav, Jeff Hodges, Bjoern Hoehrmann, Achim 1676 Hoffmann, Georg Koppen, Dean McNamee, Alexey Melnikov, Mark Miller, 1677 Mark Pauley, Yngve N. Pettersen, Julian Reschke, Peter Saint-Andre, 1678 Mark Seaborn, Maciej Stachowiak, Daniel Stenberg, Tatsuhiro 1679 Tsujikawa, David Wagner, Dan Winship, and Dan Witte for their 1680 valuable feedback on this document. 1682 Author's Address 1684 Adam Barth 1685 University of California, Berkeley 1687 Email: abarth@eecs.berkeley.edu 1688 URI: http://www.adambarth.com/