idnits 2.17.1 draft-ietf-httpstate-cookie-22.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 (February 18, 2011) is 4816 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 1618, 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) February 18, 2011 5 Intended status: Standards Track 6 Expires: August 22, 2011 8 HTTP State Management Mechanism 9 draft-ietf-httpstate-cookie-22 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 August 22, 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 . . . . . . . . . . . . . . . . . . 24 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 Section 4 172 represent a preferred subset of current server behavior, and even the 173 more liberal cookie processing algorithm provided in Section 5 does 174 not recommend all of the syntactic and semantic variations in use 175 today. Where some existing software differs from the recommended 176 protocol in significant ways, the document contains a note explaining 177 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 MAY send a Set-Cookie response header with any 273 response. User agents MAY ignore Set-Cookie headers contained in 274 responses with 100-level status codes but MUST process Set-Cookie 275 headers contained in other responses (including responses with 400- 276 and 500-level status codes). An origin server can include multiple 277 Set-Cookie header fields in a single response. The presence of a 278 Cookie or a Set-Cookie header field does not preclude HTTP caches 279 from storing and reusing a response. 281 Origin servers SHOULD NOT fold multiple Set-Cookie header fields into 282 a single header field. The usual mechanism for folding HTTP headers 283 fields (i.e., as defined in [RFC2616]) might change the semantics of 284 the Set-Cookie header field because the %x2C (",") character is used 285 by Set-Cookie in a way that conflicts with such folding. 287 3.1. Examples 289 Using the Set-Cookie header, a server can send the user agent a short 290 string in an HTTP response that the user agent will return in future 291 HTTP requests that are within the scope of the cookie. For example, 292 the server can send the user agent a "session identifier" named SID 293 with the value 31d4d96e407aad42. The user agent then returns the 294 session identifier in subsequent requests. 296 == Server -> User Agent == 298 Set-Cookie: SID=31d4d96e407aad42 300 == User Agent -> Server == 302 Cookie: SID=31d4d96e407aad42 304 The server can alter the default scope of the cookie using the Path 305 and Domain attributes. For example, the server can instruct the user 306 agent to return the cookie to every path and every subdomain of 307 example.com. 309 == Server -> User Agent == 311 Set-Cookie: SID=31d4d96e407aad42; Path=/; Domain=example.com 313 == User Agent -> Server == 315 Cookie: SID=31d4d96e407aad42 317 As shown in the next example, the server can store multiple cookies 318 at the user agent. For example, the server can store a session 319 identifier as well as the user's preferred language by returning two 320 Set-Cookie header fields. Notice that the server uses the Secure and 321 HttpOnly attributes to provide additional security protections for 322 the more-sensitive session identifier (see Section 4.1.2.) 324 == Server -> User Agent == 326 Set-Cookie: SID=31d4d96e407aad42; Path=/; Secure; HttpOnly 327 Set-Cookie: lang=en-US; Path=/; Domain=example.com 329 == User Agent -> Server == 331 Cookie: SID=31d4d96e407aad42; lang=en-US 333 Notice that the Cookie header above contains two cookies, one named 334 SID and one named lang. If the server wishes the user agent to 335 persist the cookie over multiple "sessions" (e.g., user agent 336 restarts), the server can specify an expiration date in the Expires 337 attribute. Note that the user agent might delete the cookie before 338 the expiration date if the user agent's cookie store exceeds its 339 quota or if the user manually deletes the server's cookie. 341 == Server -> User Agent == 343 Set-Cookie: lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT 345 == User Agent -> Server == 347 Cookie: SID=31d4d96e407aad42; lang=en-US 349 Finally, to remove a cookie, the server returns a Set-Cookie header 350 with an expiration date in the past. The server will be successful 351 in removing the cookie only if the Path and the Domain attribute in 352 the Set-Cookie header match the values used when the cookie was 353 created. 355 == Server -> User Agent == 357 Set-Cookie: lang=; Expires=Sun, 06 Nov 1994 08:49:37 GMT 359 == User Agent -> Server == 361 Cookie: SID=31d4d96e407aad42 363 4. Server Requirements 365 This section describes the syntax and semantics of a well-behaved 366 profile of the Cookie and Set-Cookie headers. 368 4.1. Set-Cookie 370 The Set-Cookie HTTP response header is used to send cookies from the 371 server to the user agent. 373 4.1.1. Syntax 375 Informally, the Set-Cookie response header contains the header name 376 "Set-Cookie" followed by a ":" and a cookie. Each cookie begins with 377 a name-value pair, followed by zero or more attribute-value pairs. 378 Servers SHOULD NOT send Set-Cookie headers that fail to conform to 379 the following grammar: 381 set-cookie-header = "Set-Cookie:" SP set-cookie-string 382 set-cookie-string = cookie-pair *( ";" SP cookie-av ) 383 cookie-pair = cookie-name "=" cookie-value 384 cookie-name = token 385 cookie-value = token / *base64-character 386 base64-character = ALPHA / DIGIT / "+" / "/" / "=" 387 token = 389 cookie-av = expires-av / max-age-av / domain-av / 390 path-av / secure-av / httponly-av / 391 extension-av 392 expires-av = "Expires=" sane-cookie-date 393 sane-cookie-date = 394 max-age-av = "Max-Age=" non-zero-digit *DIGIT 395 ; In practice, both expires-av and max-age-av are limited 396 ; to dates representable by the user agent. 397 non-zero-digit = %x31-39 398 ; digits 1 through 9 399 domain-av = "Domain=" domain-value 400 domain-value = 401 ; defined in [RFC1034], Section 3.5, as 402 ; enhanced by [RFC1123], Section 2.1 403 path-av = "Path=" path-value 404 path-value = 405 secure-av = "Secure" 406 httponly-av = "HttpOnly" 407 extension-av = 408 Note that some of the grammatical terms above reference documents 409 that use different grammatical notations than this document (which 410 uses ABNF from [RFC5234]). 412 The semantics of the cookie-value are not defined by this document. 414 To maximize compatibility with user agents, servers that wish to 415 store arbitrary data in a cookie-value SHOULD encode that data, for 416 example, using Base64 [RFC4648]. 418 The portions of the set-cookie-string produced by the cookie-av term 419 are known as attributes. To maximize compatibility with user agents, 420 servers SHOULD NOT produce two attributes with the same name in the 421 same set-cookie-string. (See Section 5.3 for how user agents handle 422 this case.) 424 Servers SHOULD NOT include more than one Set-Cookie header field in 425 the same response with the same cookie-name. (See Section 5.2 for 426 how user agents handle this case.) 428 If a server sends multiple responses containing Set-Cookie headers 429 concurrently to the user agent (e.g., when communicating with the 430 user agent over multiple sockets), these responses create a "race 431 condition" that can lead to unpredictable behavior. 433 NOTE: Some existing user agents differ on their interpretation of 434 two-digit years. To avoid compatibility issues, servers SHOULD use 435 the rfc1123-date format, which requires a four-digit year. 437 NOTE: Some user agents store and process dates in cookies as 32-bit 438 UNIX time_t values. Implementation bugs in the libraries supporting 439 time_t processing on some systems might cause such user agents to 440 process dates after the year 2038 incorrectly. 442 4.1.2. Semantics (Non-Normative) 444 This section describes a simplified semantics of the Set-Cookie 445 header. These semantics are detailed enough to be useful for 446 understanding the most common uses of cookies by servers. The full 447 semantics are described in Section 5. 449 When the user agent receives a Set-Cookie header, the user agent 450 stores the cookie together with its attributes. Subsequently, when 451 the user agent makes an HTTP request, the user agent includes the 452 applicable, non-expired cookies in the Cookie header. 454 If the user agent receives a new cookie with the same cookie-name, 455 domain-value, and path-value as a cookie that it has already stored, 456 the existing cookie is evicted and replaced with the new cookie. 457 Notice that servers can delete cookies by sending the user agent a 458 new cookie with an Expires attribute with a value in the past. 460 Unless the cookie's attributes indicate otherwise, the cookie is 461 returned only to the origin server (and not, e.g., to any 462 subdomains), and it expires at the end of the current session (as 463 defined by the user agent). User agents ignore unrecognized cookie 464 attributes (but not the entire cookie). 466 4.1.2.1. The Expires Attribute 468 The Expires attribute indicates the maximum lifetime of the cookie, 469 represented as the date and time at which the cookie expires. The 470 user agent is not required to retain the cookie until the specified 471 date has passed. In fact, user agents often evict cookies due to 472 memory pressure or privacy concerns. 474 4.1.2.2. The Max-Age Attribute 476 The Max-Age attribute indicates the maximum lifetime of the cookie, 477 represented as the number of seconds until the cookie expires. The 478 user agent is not required to retain the cookie for the specified 479 duration. In fact, user agents often evict cookies from due to 480 memory pressure or privacy concerns. 482 NOTE: Some existing user agents do not support the Max-Age 483 attribute. User agents that do not support the Max-Age attribute 484 ignore the attribute. 486 If a cookie has both the Max-Age and the Expires attribute, the Max- 487 Age attribute has precedence and controls the expiration date of the 488 cookie. If a cookie has neither the Max-Age nor the Expires 489 attribute, the user agent will retain the cookie until "the current 490 session is over" (as defined by the user agent). 492 4.1.2.3. The Domain Attribute 494 The Domain attribute specifies those hosts to which the cookie will 495 be sent. For example, if the value of the Domain attribute is 496 "example.com", the user agent will include the cookie in the Cookie 497 header when making HTTP requests to example.com, www.example.com, and 498 www.corp.example.com. (Note that a leading %x2E ("."), if present, 499 is ignored even though that character is not permitted, but a 500 trailing %x2E ("."), if present, will cause the user agent to ignore 501 the attribute.) If the server omits the Domain attribute, the user 502 agent will return the cookie only to the origin server. 504 WARNING: Some existing user agents treat an absent Domain 505 attribute as if the Domain attribute were present and contained 506 the current host name. For example, if example.com returns a Set- 507 Cookie header without a Domain attribute, these user agents will 508 erroneously send the cookie to www.example.com as well. 510 The user agent will reject cookies unless the Domain attribute 511 specifies a scope for the cookie that would include the origin 512 server. For example, the user agent will accept a cookie with a 513 Domain attribute of "example.com" or of "foo.example.com" from 514 foo.example.com, but the user agent will not accept a cookie with a 515 Domain attribute of "bar.example.com" or of "baz.foo.example.com". 517 NOTE: For security reasons, many user agents are configured to reject 518 Domain attributes that correspond to "public suffixes". For example, 519 some user agents will reject Domain attributes of "com" or "co.uk". 520 (See Section 5.3 for more information.) 522 4.1.2.4. The Path Attribute 524 The scope of each cookie is limited to a set of paths, controlled by 525 the Path attribute. If the server omits the Path attribute, the user 526 agent will use the "directory" of the request-uri's path component as 527 the default value. (See Section 5.1.4 for more details.) 529 The user agent will include the cookie in an HTTP request only if the 530 path portion of the request-uri matches (or is a subdirectory of) the 531 cookie's Path attribute, where the %x2F ("/") character is 532 interpreted as a directory separator. 534 Although seemingly useful for isolating cookies between different 535 paths within a given host, the Path attribute cannot be relied upon 536 for security (see Section 8). 538 4.1.2.5. The Secure Attribute 540 The Secure attribute limits the scope of the cookie to "secure" 541 channels (where "secure" is defined by the user agent). When a 542 cookie has the Secure attribute, the user agent will include the 543 cookie in an HTTP request only if the request is transmitted over a 544 secure channel (typically HTTP over Transport Layer Security (TLS) 545 [RFC2818]). 547 Although seemingly useful for protecting cookies from active network 548 attackers, the Secure attribute protects only the cookie's 549 confidentiality. An active network attacker can overwrite Secure 550 cookies from an insecure channel, disrupting their integrity (see 551 Section 8.6 for more details). 553 4.1.2.6. The HttpOnly Attribute 555 The HttpOnly attribute limits the scope of the cookie to HTTP 556 requests. In particular, the attribute instructs the user agent to 557 omit the cookie when providing access to cookies via "non-HTTP" APIs 558 (such as a web browser API that exposes cookies to scripts). 560 Note that the HttpOnly attribute is independent of the Secure 561 attribute: a cookie can have both the HttpOnly and the Secure 562 attribute. 564 4.2. Cookie 566 4.2.1. Syntax 568 The user agent sends stored cookies to the origin server in the 569 Cookie header. If the server conforms to the requirements in 570 Section 4.1 (and the user agent conforms to the requirements in 571 Section 5), the user agent will send a Cookie header that conforms to 572 the following grammar: 574 cookie-header = "Cookie:" OWS cookie-string OWS 575 cookie-string = cookie-pair *( ";" SP cookie-pair ) 577 4.2.2. Semantics 579 Each cookie-pair represents a cookie stored by the user agent. The 580 cookie-pair contains the cookie-name and cookie-value the user agent 581 received in the Set-Cookie header. 583 Notice that the cookie attributes are not returned. In particular, 584 the server cannot determine from the Cookie header alone when a 585 cookie will expire, for which hosts the cookie is valid, for which 586 paths the cookie is valid, or whether the cookie was set with the 587 Secure or HttpOnly attributes. 589 The semantics of individual cookies in the Cookie header are not 590 defined by this document. Servers are expected to imbue these 591 cookies with application-specific semantics. 593 Although cookies are serialized linearly in the Cookie header, 594 servers SHOULD NOT rely upon the serialization order. In particular, 595 if the Cookie header contains two cookies with the same name (e.g., 596 that were set with different Path or Domain attributes), servers 597 SHOULD NOT rely upon the order in which these cookies appear in the 598 header. 600 5. User Agent Requirements 602 This section specifies the Cookie and Set-Cookie headers in 603 sufficient detail that a user agent implementing these requirements 604 precisely can interoperate with existing servers (even those that do 605 not conform to the well-behaved profile described in Section 4). 607 A user agent could enforce more restrictions than those specified 608 herein (e.g., for the sake of improved security); however, 609 experiments have shown that such strictness reduces the likelihood 610 that a user agent will be able to interoperate with existing servers. 612 5.1. Subcomponent Algorithms 614 This section defines some algorithms used by user agents to process 615 specific subcomponents of the Cookie and Set-Cookie headers. 617 5.1.1. Dates 619 The user agent MUST use an algorithm equivalent to the following 620 algorithm to parse a cookie-date. Note that the various boolean 621 flags defined as a part of the algorithm (i.e., found-time, found- 622 day-of-month, found-month, found-year) are initially "not set". 624 1. Using the grammar below, divide the cookie-date into date-tokens. 626 cookie-date = *delimiter date-token-list *delimiter 627 date-token-list = date-token *( 1*delimiter date-token ) 628 date-token = 1*non-delimiter 630 delimiter = %x09 / %x20-2F / %x3B-40 / %x5B-60 / %x7B-7E 631 non-delimiter = %x00-08 / %x0A-1F / DIGIT / ":" / ALPHA / %x7F-FF 632 non-digit = %x00-2F / %x3A-FF 634 day-of-month = 1*2DIGIT ( non-digit *OCTET ) 635 month = ( "jan" / "feb" / "mar" / "apr" / 636 "may" / "jun" / "jul" / "aug" / 637 "sep" / "oct" / "nov" / "dec" ) *OCTET 638 year = 2*4DIGIT ( non-digit *OCTET ) 639 time = hms-time ( non-digit *OCTET ) 640 hms-time = time-field ":" time-field ":" time-field 641 time-field = 1*2DIGIT 643 2. Process each date-token sequentially in the order the date-tokens 644 appear in the cookie-date: 646 1. If the found-time flag is not set and the token matches the 647 time production, set the found-time flag and set the hour- 648 value, minute-value, and second-value to the numbers denoted 649 by the digits in the date-token, respectively. Skip the 650 remaining sub-steps and continue to the next date-token. 652 2. If the found-day-of-month flag is not set and the date-token 653 matches the day-of-month production, set the found-day-of- 654 month flag and set the day-of-month-value to the number 655 denoted by the date-token. Skip the remaining sub-steps and 656 continue to the next date-token. 658 3. If the found-month flag is not set and the date-token matches 659 the month production, set the found-month flag and set the 660 month-value to the month denoted by the date-token. Skip the 661 remaining sub-steps and continue to the next date-token. 663 4. If the found-year flag is not set and the date-token matches 664 the year production, set the found-year flag and set the 665 year-value to the number denoted by the date-token. Skip the 666 remaining sub-steps and continue to the next date-token. 668 3. If the year-value is greater than or equal to 70 and less than or 669 equal to 99, increment the year-value by 1900. 671 4. If the year-value is greater than or equal to 0 and less than or 672 equal to 69, increment the year-value by 2000. 674 1. NOTE: Some existing user agents interpret two-digit years 675 differently. 677 5. Abort these steps and fail to parse the cookie-date if 679 * at least one of the found-day-of-month, found-month, found- 680 year, or found-time flags is not set, 682 * the day-of-month-value is less than 1 or greater than 31, 684 * the year-value is less than 1601, 686 * the hour-value is greater than 23, 688 * the minute-value is greater than 59, or 690 * the second-value is greater than 59. 692 (Note that leap seconds cannot be represented in this syntax.) 694 6. Let the parsed-cookie-date be the date whose day-of-month, month, 695 year, hour, minute, and second (in UTC) are the day-of-month- 696 value, the month-value, the year-value, the hour-value, the 697 minute-value, and the second-value, respectively. If no such 698 date exists, abort these steps and fail to parse the cookie-date. 700 7. Return the parsed-cookie-date as the result of this algorithm. 702 5.1.2. Canonicalized host names 704 A canonicalized host name is the string generated by the following 705 algorithm: 707 1. Convert the host name to a sequence of individual domain name 708 labels. 710 2. Convert each label that is not a NR-LDH label, to a A-label (see 711 Section 2.3.2.1 of [RFC5890] for the fomer and latter), or to a 712 "punycode label" (a label resulting from the "ToASCII" conversion 713 in Section 4 of [RFC3490]), as appropriate (see Section 6.3 of 714 this specification). 716 3. Concatentate the resulting labels, separated by a %x2E (".") 717 character. 719 5.1.3. Domain matching 721 A string domain-matches a given domain string if at least one of the 722 following conditions hold: 724 o The domain string and the string are identical. (Note that both 725 the domain string and the string will have been canonicalized to 726 lower case at this point.) 728 o All of the following conditions hold: 730 * The domain string is a suffix of the string. 732 * The last character of the string that is not included in the 733 domain string is a %x2E (".") character. 735 * The string is a host name (i.e., not an IP address). 737 5.1.4. Paths and path-match 739 The user agent MUST use an algorithm equivalent to the following 740 algorithm to compute the default-path of a cookie: 742 1. Let uri-path be the path portion of the request-uri if such a 743 portion exists (and empty otherwise). For example, if the 744 request-uri contains just a path (and optional query string), 745 then the uri-path is that path (without the %x3F ("?") character 746 or query string), and if the request-uri contains a full 747 absoluteURI, the uri-path is the path component of that URI. 749 2. If the uri-path is empty or if the first character of the uri- 750 path is not a %x2F ("/") character, output %x2F ("/") and skip 751 the remaining steps. 753 3. If the uri-path contains only a single %x2F ("/") character, 754 output %x2F ("/") and skip the remaining steps. 756 4. Output the characters of the uri-path from the first character up 757 to, but not including, the right-most %x2F ("/"). 759 A request-path path-matches a given cookie-path if at least one of 760 the following conditions hold: 762 o The cookie-path and the request-path are identical. 764 o The cookie-path is a prefix of the request-path and the last 765 character of the cookie-path is %x2F ("/"). 767 o The cookie-path is a prefix of the request-path and the first 768 character of the request-path that is not included in the cookie- 769 path is a %x2F ("/") character. 771 5.2. The Set-Cookie Header 773 When a user agent receives a Set-Cookie header field in an HTTP 774 response, the user agent MAY ignore the Set-Cookie header field in 775 its entirety. For example, the user agent might wish to block 776 responses to "third-party" requests from setting cookies (See 777 Section 7.1). 779 If the user agent does not ignore the Set-Cookie header field in its 780 entirety, the user agent MUST parse the field-value of the Set-Cookie 781 header field as a set-cookie-string (defined below). 783 NOTE: The algorithm below is more permissive than the grammar in 784 Section 4.1. For example, the algorithm strips leading and trailing 785 whitespace from the cookie name and value (but maintains internal 786 whitespace), whereas the grammar in Section 4.1 forbids whitespace in 787 these positions. User agents use this algorithm so as to 788 interoperate with servers that do not follow the recommendations in 789 Section 4. 791 A user agent MUST use an algorithm equivalent to the following 792 algorithm to parse a "set-cookie-string": 794 1. If the set-cookie-string contains a %x3B (";") character: 796 The name-value-pair string consists of the characters up to, 797 but not including, the first %x3B (";"), and the unparsed- 798 attributes consist of the remainder of the set-cookie-string 799 (including the %x3B (";") in question). 801 Otherwise: 803 The name-value-pair string consists of all the characters 804 contained in the set-cookie-string, and the unparsed- 805 attributes is the empty string. 807 2. If the name-value-pair string lacks a %x3D ("=") character, 808 ignore the set-cookie-string entirely. 810 3. The (possibly empty) name string consists of the characters up 811 to, but not including, the first %x3D ("=") character, and the 812 (possibly empty) value string consists of the characters after 813 the first %x3D ("=") character. 815 4. Remove any leading or trailing WSP characters from the name 816 string and the value string. 818 5. If the name string is empty, ignore the set-cookie-string 819 entirely. 821 6. The cookie-name is the name string, and the cookie-value is the 822 value string. 824 The user agent MUST use an algorithm equivalent to the following 825 algorithm to parse the unparsed-attributes: 827 1. If the unparsed-attributes string is empty, skip the rest of 828 these steps. 830 2. Discard the first character of the unparsed-attributes (which 831 will be a %x3B (";") character). 833 3. If the remaining unparsed-attributes contains a %x3B (";") 834 character: 836 Consume the characters of the unparsed-attributes up to, but 837 not including, the first %x3B (";") character. 839 Otherwise: 841 Consume the remainder of the unparsed-attributes. 843 Let the cookie-av string be the characters consumed in this step. 845 4. If the cookie-av string contains a %x3D ("=") character: 847 The (possibly empty) attribute-name string consists of the 848 characters up to, but not including, the first %x3D ("=") 849 character, and the (possibly empty) attribute-value string 850 consists of the characters after the first %x3D ("=") 851 character. 853 Otherwise: 855 The attribute-name string consists of the entire cookie-av 856 string, and the attribute-value string is empty. 858 5. Remove any leading or trailing WSP characters from the attribute- 859 name string and the attribute-value string. 861 6. Process the attribute-name and attribute-value according to the 862 requirements in the following subsections. (Notice that 863 attributes with unrecognized attribute-names are ignored.) 865 7. Return to Step 1 of this algorithm. 867 When the user agent finishes parsing the set-cookie-string, the user 868 agent is said to "receive a cookie" from the request-uri with name 869 cookie-name, value cookie-value, and attributes cookie-attribute- 870 list. (See Section 5.3 for additional requirements triggered by 871 receiving a cookie.) 873 5.2.1. The Expires Attribute 875 If the attribute-name case-insensitively matches the string 876 "Expires", the user agent MUST process the cookie-av as follows. 878 Let the expiry-time be the result of parsing the attribute-value as 879 cookie-date (see Section 5.1.1). 881 If the attribute-value failed to parse as a cookie date, ignore the 882 cookie-av. 884 If the expiry-time is later than the last date the user agent can 885 represent, the user agent MAY replace the expiry-time with the last 886 representable date. 888 If the expiry-time is earlier than the earliest date the user agent 889 can represent, the user agent MAY replace the expiry-time with the 890 earliest representable date. 892 Append an attribute to the cookie-attribute-list with an attribute- 893 name of Expires and an attribute-value of expiry-time. 895 5.2.2. The Max-Age Attribute 897 If the attribute-name case-insensitively matches the string "Max- 898 Age", the user agent MUST process the cookie-av as follows. 900 If the first character of the attribute-value is not a DIGIT or a "-" 901 character, ignore the cookie-av. 903 If the remainder of attribute-value contains a non-DIGIT character, 904 ignore the cookie-av. 906 Let delta-seconds be the attribute-value converted to an integer. 908 If delta-seconds is less than or equal to zero (0), let expiry-time 909 be the earliest representable date and time. Otherwise, let the 910 expiry-time be the current date and time plus delta-seconds seconds. 912 Append an attribute to the cookie-attribute-list with an attribute- 913 name of Max-Age and an attribute-value of expiry-time. 915 5.2.3. The Domain Attribute 917 If the attribute-name case-insensitively matches the string "Domain", 918 the user agent MUST process the cookie-av as follows. 920 If the attribute-value is empty, the behavior is undefined. However, 921 user agent SHOULD ignore the cookie-av entirely. 923 If the first character of the attribute-value string is %x2E ("."): 925 Let cookie-domain be the attribute-value without the leading %x2E 926 (".") character. 928 Otherwise: 930 Let cookie-domain be the entire attribute-value. 932 Convert the cookie-domain to lower case. 934 Append an attribute to the cookie-attribute-list with an attribute- 935 name of Domain and an attribute-value of cookie-domain. 937 5.2.4. The Path Attribute 939 If the attribute-name case-insensitively matches the string "Path", 940 the user agent MUST process the cookie-av as follows. 942 If the attribute-value is empty or if the first character of the 943 attribute-value is not %x2F ("/"): 945 Let cookie-path be the default-path. 947 Otherwise: 949 Let cookie-path be the attribute-value. 951 Append an attribute to the cookie-attribute-list with an attribute- 952 name of Path and an attribute-value of cookie-path. 954 5.2.5. The Secure Attribute 956 If the attribute-name case-insensitively matches the string "Secure", 957 the user agent MUST append an attribute to the cookie-attribute-list 958 with an attribute-name of Secure and an empty attribute-value. 960 5.2.6. The HttpOnly Attribute 962 If the attribute-name case-insensitively matches the string 963 "HttpOnly", the user agent MUST append an attribute to the cookie- 964 attribute-list with an attribute-name of HttpOnly and an empty 965 attribute-value. 967 5.3. Storage Model 969 The user agent stores the following fields about each cookie: name, 970 value, expiry-time, domain, path, creation-time, last-access-time, 971 persistent-flag, host-only-flag, secure-only-flag, and http-only- 972 flag. 974 When the user agent "receives a cookie" from a request-uri with name 975 cookie-name, value cookie-value, and attributes cookie-attribute- 976 list, the user agent MUST process the cookie as follows: 978 1. A user agent MAY ignore a received cookie in its entirety. For 979 example, the user agent might wish to block receiving cookies 980 from "third-party" responses or the user agent might not wish to 981 store cookies that exceed some size. 983 2. Create a new cookie with name cookie-name, value cookie-value. 984 Set the creation-time and the last-access-time to the current 985 date and time. 987 3. If the cookie-attribute-list contains an attribute with an 988 attribute-name of "Max-Age": 990 Set the cookie's persistent-flag to true. 992 Set the cookie's expiry-time to attribute-value of the last 993 attribute in the cookie-attribute-list with an attribute-name 994 of "Max-Age". 996 Otherwise, if the cookie-attribute-list contains an attribute 997 with an attribute-name of "Expires" (and does not contain an 998 attribute with an attribute-name of "Max-Age"): 1000 Set the cookie's persistent-flag to true. 1002 Set the cookie's expiry-time to attribute-value of the last 1003 attribute in the cookie-attribute-list with an attribute-name 1004 of "Expires". 1006 Otherwise: 1008 Set the cookie's persistent-flag to false. 1010 Set the cookie's expiry-time to the latest representable 1011 date. 1013 4. If the cookie-attribute-list contains an attribute with an 1014 attribute-name of "Domain": 1016 Let the domain-attribute be the attribute-value of the last 1017 attribute in the cookie-attribute-list with an attribute-name 1018 of "Domain". 1020 Otherwise: 1022 Let the domain-attribute be the empty string. 1024 5. If the user agent is configured to reject "public suffixes" and 1025 the domain-attribute is a public suffix: 1027 If the domain-attribute is identical to the canonicalized 1028 request-host: 1030 Let the domain-attribute be the empty string. 1032 Otherwise: 1034 Ignore the cookie entirely and abort these steps. 1036 NOTE: A "public suffix" is a domain that is controlled by a 1037 public registry, such as "com", "co.uk", and "pvt.k12.wy.us". 1038 This step is essential for preventing attacker.com from 1039 disrupting the integrity of example.com by setting a cookie 1040 with a Domain attribute of "com". Unfortunately, the set of 1041 public suffixes (also known as "registry controlled domains") 1042 changes over time. If feasible, user agents SHOULD use an 1043 up-to-date public suffix list, such as the one maintained by 1044 the Mozilla project at . 1046 6. If the domain-attribute is non-empty: 1048 If the canonicalized request-host does not domain-match the 1049 domain-attribute: 1051 Ignore the cookie entirely and abort these steps. 1053 Otherwise: 1055 Set the cookie's host-only-flag to false. 1057 Set the cookie's domain to the domain-attribute. 1059 Otherwise: 1061 Set the cookie's host-only-flag to true. 1063 Set the cookie's domain to the canonicalized request-host. 1065 7. If the cookie-attribute-list contains an attribute with an 1066 attribute-name of "Path", set the cookie's path to attribute- 1067 value of the last attribute in the cookie-attribute-list with an 1068 attribute-name of "Path". Otherwise, set cookie's path to the 1069 default-path of the request-uri. 1071 8. If the cookie-attribute-list contains an attribute with an 1072 attribute-name of "Secure", set the cookie's secure-only-flag to 1073 true. Otherwise, set cookie's secure-only-flag to false. 1075 9. If the cookie-attribute-list contains an attribute with an 1076 attribute-name of "HttpOnly", set the cookie's http-only-flag to 1077 true. Otherwise, set cookie's http-only-flag to false. 1079 10. If the cookie was received from a "non-HTTP" API and the 1080 cookie's http-only-flag is set, abort these steps and ignore the 1081 cookie entirely. 1083 11. If the cookie store contains a cookie with the same name, 1084 domain, and path as the newly created cookie: 1086 1. Let old-cookie be the existing cookie with the same name, 1087 domain, and path as the newly created cookie. (Notice that 1088 this algorithm maintains the invariant that there is at most 1089 one such cookie.) 1091 2. If the newly created cookie was received from a "non-HTTP" 1092 API and the old-cookie's http-only-flag is set, abort these 1093 steps and ignore the newly created cookie entirely. 1095 3. Update the creation-time of the newly created cookie to 1096 match the creation-time of the old-cookie. 1098 4. Remove the old-cookie from the cookie store. 1100 12. Insert the newly created cookie into the cookie store. 1102 A cookie is "expired" if the cookie has an expiry date in the past. 1104 The user agent MUST evict all expired cookies from the cookie store 1105 if, at any time, an expired cookie exists in the cookie store. 1107 At any time, the user agent MAY "remove excess cookies" from the 1108 cookie store if the number of cookies sharing a domain field exceeds 1109 some implementation defined upper bound (such as 50 cookies). 1111 At any time, the user agent MAY "remove excess cookies" from the 1112 cookie store if the cookie store exceeds some predetermined upper 1113 bound (such as 3000 cookies). 1115 When the user agent removes excess cookies from the cookie store, the 1116 user agent MUST evict cookies in the following priority order: 1118 1. Expired cookies. 1120 2. Cookies that share a domain field with more than a predetermined 1121 number of other cookies. 1123 3. All cookies. 1125 If two cookies have the same removal priority, the user agent MUST 1126 evict the cookie with the earliest last-access date first. 1128 When "the current session is over" (as defined by the user agent), 1129 the user agent MUST remove from the cookie store all cookies with the 1130 persistent-flag set to false. 1132 5.4. The Cookie Header 1134 The user agent includes stored cookies in the Cookie HTTP request 1135 header. 1137 When the user agent generates an HTTP request, the user agent MUST 1138 NOT attach more than one Cookie header field. 1140 A user agent MAY omit the Cookie header in its entirety. For 1141 example, the user agent might wish to block sending cookies during 1142 "third-party" requests from setting cookies (See Section 7.1). 1144 If the user agent does attach a Cookie header field to an HTTP 1145 request, the user agent MUST send the cookie-string (defined below) 1146 as the value of the header field. 1148 The user agent MUST use an algorithm equivalent to the following 1149 algorithm to compute the "cookie-string" from a cookie store and a 1150 request-uri: 1152 1. Let cookie-list be the set of cookies from the cookie store that 1153 meet all of the following requirements: 1155 * Either: 1157 The cookie's host-only-flag is true and the canonicalized 1158 request-host is identical to the cookie's domain. 1160 Or: 1162 The cookie's host-only-flag is false and the canonicalized 1163 request-host domain-matches cookie's domain. 1165 * The request-uri's path path-matches cookie's path. 1167 * If the cookie's secure-only-flag is true, then the request- 1168 uri's scheme must denote a "secure" protocol (as defined by 1169 the user agent). 1171 NOTE: The notion of a "secure" protocol is not defined by 1172 this document. Typically, user agents consider a protocol 1173 secure if the protocol makes use of transport-layer 1174 security, such as SSL or TLS. For example, most user 1175 agents consider "https" to be a scheme that denotes a 1176 secure protocol. 1178 * If the cookie's http-only-flag is true, then exclude the 1179 cookie if the cookie-string is being generated for a "non- 1180 HTTP" API (as defined by the user agent). 1182 2. The user agent SHOULD sort the cookie-list in the following 1183 order: 1185 * Cookies with longer paths are listed before cookies with 1186 shorter paths. 1188 * Among cookies that have equal length path fields, cookies with 1189 earlier creation-times are listed before cookies with later 1190 creation-times. 1192 NOTE: Not all user agents sort the cookie-list in this order, but 1193 this order reflects common practice when this document was 1194 written, and, historically, there have been servers that 1195 (erroneously) depended on this order. 1197 3. Update the last-access-time of each cookie in the cookie-list to 1198 the current date and time. 1200 4. Serialize the cookie-list into a cookie-string by processing each 1201 cookie in the cookie-list in order: 1203 1. Output the cookie's name, the %x3D ("=") character, and the 1204 cookie's value. 1206 2. If there is an unprocessed cookie in the cookie-list, output 1207 the characters %x3B and %x20 ("; "). 1209 NOTE: Despite its name, the cookie-string is actually a sequence of 1210 octets, not a sequence of characters. To convert the cookie-string 1211 (or components thereof) into a sequence of characters (e.g., for 1212 presentation to the user), the user agent might wish to try using the 1213 UTF-8 character encoding [RFC3629] to decode the octet sequence. 1214 This decoding might fail, however, because not every sequence of 1215 octets is valid UTF-8. 1217 6. Implementation Considerations 1219 6.1. Limits 1221 Practical user agent implementations have limits on the number and 1222 size of cookies that they can store. General-use user agents SHOULD 1223 provide each of the following minimum capabilities: 1225 o At least 4096 bytes per cookie (as measured by the sum of the 1226 length of the cookie's name, value, and attributes). 1228 o At least 50 cookies per domain. 1230 o At least 3000 cookies total. 1232 Servers SHOULD use as few and as small cookies as possible to avoid 1233 reaching these implementation limits and to minimize network 1234 bandwidth due to the Cookie header being included in every request. 1236 Servers SHOULD gracefully degrade if the user agent fails to return 1237 one or more cookies in the Cookie header because the user agent might 1238 evict any cookie at any time on orders from the user. 1240 6.2. Application Programming Interfaces 1242 One reason the Cookie and Set-Cookie headers uses such esoteric 1243 syntax is because many platforms (both in servers and user agents) 1244 provide a string-based application programing interface (API) to 1245 cookies, requiring application-layer programmers to generate and 1246 parse the syntax used by the Cookie and Set-Cookie headers, which 1247 many programmers have done incorrectly, resulting in interoperability 1248 problems. 1250 Instead of providing string-based APIs to cookies, platforms would be 1251 well-served by providing more semantic APIs. It is beyond the scope 1252 of this document to recommend specific API designs, but there are 1253 clear benefits to accepting an abstract "Date" object instead of a 1254 serialized date string. 1256 6.3. IDNA dependency and migration 1258 IDNA2008 [RFC5890] supersedes IDNA2003 [RFC3490]. However, there are 1259 differences between the two specifications, and thus there can be 1260 differences in processing (e.g. converting) domain name labels that 1261 have been registered under one from those registered under the other. 1262 There will be a transition period of some time during which IDNA2003- 1263 based domain name labels will exist in the wild. User agents SHOULD 1264 implement IDNA2008 [RFC5890] and MAY implement [UTS46] or [RFC5895] 1265 in order to facilitate their IDNA transition. If a user agent does 1266 not implement IDNA2008, the user agent MUST implement IDNA2003 1267 [RFC3490]. 1269 7. Privacy Considerations 1271 Cookies are often criticized for letting servers track users. For 1272 example, a number of "web analytics" companies use cookies to 1273 recognize when a user returns to a web site or visits another web 1274 site. Although cookies are not the only mechanism servers can use to 1275 track users across HTTP requests, cookies facilitate tracking because 1276 they are persistent across user agent sessions and can be shared 1277 between hosts. 1279 7.1. Third-Party Cookies 1281 Particularly worrisome are so-called "third-party" cookies. In 1282 rendering an HTML document, a user agent often requests resources 1283 from other servers (such as advertising networks). These third-party 1284 servers can use cookies to track the user even if the user never 1285 visits the server directly. For example, if a user visits a site 1286 that contains content from a third party and then later visits 1287 another site that contains content from the same third party, the 1288 third party can track the user between the two sites. 1290 Some user agents restrict how third-party cookies behave. For 1291 example, some of these user agents refuse to send the Cookie header 1292 in third-party requests. Others refuse to process the Set-Cookie 1293 header in responses to third-party requests. User agents vary widely 1294 in their third-party cookie policies. This document grants user 1295 agents wide latitude to experiment with third-party cookie policies 1296 that balance the privacy and compatibility needs of their users. 1297 However, this document does not endorse any particular third-party 1298 cookie policy. 1300 Third-party cookie blocking policies are often ineffective at 1301 achieving their privacy goals if servers attempt to work around their 1302 restrictions to track users. In particular, two collaborating 1303 servers can often track users without using cookies at all by 1304 injecting identifying information into dynamic URLs. 1306 7.2. User Controls 1308 User agents SHOULD provide users with a mechanism for managing the 1309 cookies stored in the cookie store. For example, a user agent might 1310 let users delete all cookies received during a specified time period 1311 or all the cookies related to a particular domain. In addition, many 1312 user agents include a user interface element that lets users examine 1313 the cookies stored in their cookie store. 1315 User agents SHOULD provide users with a mechanism for disabling 1316 cookies. When cookies are disabled, the user agent MUST NOT include 1317 a Cookie header in outbound HTTP requests and the user agent MUST NOT 1318 process Set-Cookie headers in inbound HTTP responses. 1320 Some user agents provide users the option of preventing persistent 1321 storage of cookies across sessions. When configured thusly, user 1322 agents MUST treat all received cookies as if the persistent-flag were 1323 set to false. Some popular user agents expose this functionality via 1324 "private browsing" mode [Aggarwal2010] 1326 Some user agents provide users with the ability to approve individual 1327 writes to the cookie store. In many common usage scenarios, these 1328 controls generate a large number of prompts. However, some privacy- 1329 conscious users find these controls useful nonetheless. 1331 7.3. Expiration Dates 1333 Although servers can set the expiration date for cookies to the 1334 distant future, most user agents do not actually retain cookies for 1335 multiple decades. Rather than chosing gratiously long expiration 1336 periods, servers SHOULD promote user privacy by selecting reasonable 1337 cookie expiration periods based on the purpose of the cookie. For 1338 example, a typical session identifier might reasonably be set to 1339 expire in two weeks. 1341 8. Security Considerations 1343 8.1. Overview 1345 Cookies have a number of security pitfalls. This section overviews a 1346 few of the more salient issues. 1348 In particular, cookies encourage developers to rely on ambient 1349 authority for authentication, often becoming vulnerable to attacks 1350 such as cross-site request forgery [CSRF]. Also, when storing 1351 session identifiers in cookies, developers often create session 1352 fixation vulnerabilities. 1354 Transport-layer encryption, such as that employed in HTTPS, is 1355 insufficient to prevent a network attacker from obtaining or altering 1356 a victim's cookies because the cookie protocol itself has various 1357 vulnerabilities (see "Weak Confidentiality" and "Weak Integrity", 1358 below). In addition, by default, cookies do not provide 1359 confidentiality or integrity from network attackers, even when used 1360 in conjunction with HTTPS. 1362 8.2. Ambient Authority 1364 A server that uses cookies to authenticate users can suffer security 1365 vulnerabilities because some user agents let remote parties issue 1366 HTTP requests from the user agent (e.g., via HTTP redirects or HTML 1367 forms). When issuing those requests, user agents attach cookies even 1368 if the remote party does not know the contents of the cookies, 1369 potentially letting the remote party exercise authority at an unwary 1370 server. 1372 Although this security concern goes by a number of names (e.g., 1373 cross-site request forgery, confused deputy), the issue stems from 1374 cookies being a form of ambient authority. Cookies encourage server 1375 operators to separate designation (in the form of URLs) from 1376 authorization (in the form of cookies). Consequently, the user agent 1377 might supply the authorization for a resource designated by the 1378 attacker, possibly causing the server or its clients to undertake 1379 actions designated by the attacker as though they were authorized by 1380 the user. 1382 Instead of using cookies for authorization, server operators might 1383 wish to consider entangling designation and authorization by treating 1384 URLs as capabilities. Instead of storing secrets in cookies, this 1385 approach stores secrets in URLs, requiring the remote entity to 1386 supply the secret itself. Although this approach is not a panacea, 1387 judicious application of these principles can lead to more robust 1388 security. 1390 8.3. Clear Text 1392 Unless sent over a secure channel (such as TLS), the information in 1393 the Cookie and Set-Cookie headers is transmitted in the clear. 1395 1. All sensitive information conveyed in these headers is exposed to 1396 an eavesdropper. 1398 2. A malicious intermediary could alter the headers as they travel 1399 in either direction, with unpredictable results. 1401 3. A malicious client could alter the Cookie header before 1402 transmission, with unpredictable results. 1404 Servers SHOULD encrypt and sign the contents of cookies (using 1405 whatever format the server desires) when transmitting them to the 1406 user agent (even when sending the cookies over a secure channel). 1407 However, encrypting and signing cookie contents does not prevent an 1408 attacker from transplanting a cookie from one user agent to another 1409 or from replaying the cookie at a later time. 1411 In addition to encrypting and signing the contents of every cookie, 1412 servers that require a higher level of security SHOULD use the Cookie 1413 and Set-Cookie headers only over a secure channel. When using 1414 cookies over a secure channel, servers SHOULD set the Secure 1415 attribute (see Section 4.1.2.5) for every cookie. If a server does 1416 not set the Secure attribute, the protection provided by the secure 1417 channel will be largely moot. 1419 For example, consider a webmail server that stores a session 1420 identifier in a cookie and is typically accessed over HTTPS. If the 1421 server does not set the Secure attribute on its cookies, an active 1422 network attacker can intercept any outbound HTTP request from the 1423 user agent and redirect that request to the webmail server over HTTP. 1424 Even if the webmail server is not listening for HTTP connections, the 1425 user agent will still include cookies in the request. The active 1426 network attacker can intercept these cookies, replay them against the 1427 server, and learn the contents of the user's email. If, instead, the 1428 server had set the Secure attribute on its cookies, the user agent 1429 would not have included the cookies in the clear-text request. 1431 8.4. Session Identifiers 1433 Instead of storing session information directly in a cookie (where it 1434 might be exposed to or replayed by an attacker), servers commonly 1435 store a nonce (or "session identifier") in a cookie. When the server 1436 receives an HTTP request with a nonce, the server can look up state 1437 information associated with the cookie using the nonce as a key. 1439 Using session identifier cookies limits the damage an attacker can 1440 cause if the attacker learns the contents of a cookie because the 1441 nonce is useful only for interacting with the server (unlike non- 1442 nonce cookie content, which might itself be sensitive). Furthermore, 1443 using a single nonce prevents an attacker from "splicing" together 1444 cookie content from two interactions with the server, which could 1445 cause the server to behave unexpectedly. 1447 Using session identifiers is not without risk. For example, the 1448 server SHOULD take care to avoid "session fixation" vulnerabilities. 1449 A session fixation attack proceeds in three steps. First, the 1450 attacker transplants a session identifier from his or her user agent 1451 to the victim's user agent. Second, the victim uses that session 1452 identifier to interact with the server, possibly imbuing the session 1453 identifier with the user's credentials or confidential information. 1454 Third, the attacker uses the session identifier to interact with 1455 server directly, possibly obtaining the user's authority or 1456 confidential information. 1458 8.5. Weak Confidentiality 1460 Cookies do not provide isolation by port. If a cookie is readable by 1461 a service running on one port, the cookie is also readable by a 1462 service running on another port of the same server. If a cookie is 1463 writable by a service on one port, the cookie is also writable by a 1464 service running on another port of the same server. For this reason, 1465 servers SHOULD NOT both run mutually distrusting services on 1466 different ports of the same host and use cookies to store security- 1467 sensitive information. 1469 Cookies do not provide isolation by scheme. Although most commonly 1470 used with the http and https schemes, the cookies for a given host 1471 might also be available to other schemes, such as ftp and gopher. 1472 Although this lack of isolation by scheme is most apparent in non- 1473 HTTP APIs that permit access to cookies (e.g., HTML's document.cookie 1474 API), the lack of isolation by scheme is actually present in 1475 requirements for processing cookies themselves (e.g., consider 1476 retrieving a URI with the gopher scheme via HTTP). 1478 Cookies do not always provide isolation by path. Although the 1479 network-level protocol does not send cookies stored for one path to 1480 another, some user agents expose cookies via non-HTTP APIs, such as 1481 HTML's document.cookie API. Because some of these user agents (e.g., 1482 web browsers) do not isolate resources received from different paths, 1483 a resource retrieved from one path might be able to access cookies 1484 stored for another path. 1486 8.6. Weak Integrity 1488 Cookies do not provide integrity guarantees for sibling domains (and 1489 their subdomains). For example, consider foo.example.com and 1490 bar.example.com. The foo.example.com server can set a cookie with a 1491 Domain attribute of "example.com" (possibly overwriting an existing 1492 "example.com" cookie set by bar.example.com), and the user agent will 1493 include that cookie in HTTP requests to bar.example.com. In the 1494 worst case, bar.example.com will be unable to distinguish this cookie 1495 from a cookie it set itself. The foo.example.com server might be 1496 able to leverage this ability to mount an attack against 1497 bar.example.com. 1499 Even though the Set-Cookie header supports the Path attribute, the 1500 Path attribute does not provide any integrity protection because the 1501 user agent will accept an arbitrary Path attribute in a Set-Cookie 1502 header. For example, an HTTP response to a request for 1503 http://example.com/foo/bar can set a cookie with a Path attribute of 1504 "/qux". Consequently, servers SHOULD NOT both run mutually 1505 distrusting services on different paths of the same host and use 1506 cookies to store security-sensitive information. 1508 An active network attacker can also inject cookies into the Cookie 1509 header sent to https://example.com/ by impersonating a response from 1510 http://example.com/ and injecting a Set-Cookie header. The HTTPS 1511 server at example.com will be unable to distinguish these cookies 1512 from cookies that it set itself in an HTTPS response. An active 1513 network attacker might be able to leverage this ability to mount an 1514 attack against example.com even if example.com uses HTTPS 1515 exclusively. 1517 Servers can partially mitigate these attacks by encrypting and 1518 signing the contents of their cookies. However, using cryptography 1519 does not mitigate the issue completely because an attacker can replay 1520 a cookie he or she received from the authentic example.com server in 1521 the user's session, with unpredictable results. 1523 Finally, an attacker might be able to force the user agent to delete 1524 cookies by storing a large number of cookies. Once the user agent 1525 reaches its storage limit, the user agent will be forced to evict 1526 some cookies. Servers SHOULD NOT rely upon user agents retaining 1527 cookies. 1529 8.7. Reliance on DNS 1531 Cookies rely upon the Domain Name System (DNS) for security. If the 1532 DNS is partially or fully compromised, the cookie protocol might fail 1533 to provide the security properties required by applications. 1535 9. IANA Considerations 1537 The permanent message header field registry (see [RFC3864]) should be 1538 updated with the following registrations: 1540 9.1. Cookie 1542 Header field name: Cookie 1544 Applicable protocol: http 1546 Status: standard 1548 Author/Change controller: IETF 1550 Specification document: this specification (Section 5.4) 1552 9.2. Set-Cookie 1554 Header field name: Set-Cookie 1556 Applicable protocol: http 1558 Status: standard 1560 Author/Change controller: IETF 1562 Specification document: this specification (Section 5.2) 1564 9.3. Cookie2 1566 Header field name: Cookie2 1568 Applicable protocol: http 1570 Status: obsoleted 1572 Author/Change controller: IETF 1574 Specification document: [RFC2965] 1576 9.4. Set-Cookie2 1578 Header field name: Set-Cookie2 1580 Applicable protocol: http 1582 Status: obsoleted 1583 Author/Change controller: IETF 1585 Specification document: [RFC2965] 1587 10. References 1589 10.1. Normative References 1591 [RFC1034] Mockapetris, P., "Domain names - concepts and facilities", 1592 STD 13, RFC 1034, November 1987. 1594 [RFC1123] Braden, R., "Requirements for Internet Hosts - Application 1595 and Support", STD 3, RFC 1123, October 1989. 1597 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1598 Requirement Levels", BCP 14, RFC 2119, March 1997. 1600 [RFC2616] Fielding, R., Gettys, J., Mogul, J., Frystyk, H., 1601 Masinter, L., Leach, P., and T. Berners-Lee, "Hypertext 1602 Transfer Protocol -- HTTP/1.1", RFC 2616, June 1999. 1604 [RFC3490] Faltstrom, P., Hoffman, P., and A. Costello, 1605 "Internationalizing Domain Names in Applications (IDNA)", 1606 RFC 3490, March 2003. 1608 See Section 6.3 for an explanation why the normative 1609 reference to an obsoleted specification is needed. 1611 [RFC4790] Newman, C., Duerst, M., and A. Gulbrandsen, "Internet 1612 Application Protocol Collation Registry", RFC 4790, 1613 March 2007. 1615 [RFC5234] Crocker, D., Ed. and P. Overell, "Augmented BNF for Syntax 1616 Specifications: ABNF", STD 68, RFC 5234, January 2008. 1618 [RFC5246] Dierks, T. and E. Rescorla, "The Transport Layer Security 1619 (TLS) Protocol Version 1.2", RFC 5246, August 2008. 1621 [RFC5890] Klensin, J., "Internationalized Domain Names for 1622 Applications (IDNA): Definitions and Document Framework", 1623 RFC 5890, August 2010. 1625 10.2. Informative References 1627 [RFC2109] Kristol, D. and L. Montulli, "HTTP State Management 1628 Mechanism", RFC 2109, February 1997. 1630 [RFC2965] Kristol, D. and L. Montulli, "HTTP State Management 1631 Mechanism", RFC 2965, October 2000. 1633 [RFC2818] Rescorla, E., "HTTP Over TLS", RFC 2818, May 2000. 1635 [Netscape] 1636 Netscape Communications Corp., "Persistent Client State -- 1637 HTTP Cookies", 1999, . 1641 [Kri2001] Kristol, D., "HTTP Cookies: Standards, Privacy, and 1642 Politics", ACM Transactions on Internet Technology Vol. 1, 1643 #2, November 2001, . 1645 [RFC3629] Yergeau, F., "UTF-8, a transformation format of ISO 1646 10646", STD 63, RFC 3629, November 2003. 1648 [RFC4648] Josefsson, S., "The Base16, Base32, and Base64 Data 1649 Encodings", RFC 4648, October 2006. 1651 [RFC3864] Klyne, G., Nottingham, M., and J. Mogul, "Registration 1652 Procedures for Message Header Fields", BCP 90, RFC 3864, 1653 September 2004. 1655 [RFC5895] Resnick, P. and P. Hoffman, "Mapping Characters for 1656 Internationalized Domain Names in Applications (IDNA) 1657 2008", RFC 5895, September 2010. 1659 [UTS46] Davis, M. and M. Suignard, "Unicode IDNA Compatibility 1660 Processing", Unicode Technical Standards # 46, 2010, 1661 . 1663 [CSRF] Barth, A., Jackson, C., and J. Mitchell, "Robust Defenses 1664 for Cross-Site Request Forgery", 2008, . 1668 [Aggarwal2010] 1669 Aggarwal, G., Burzstein, E., Jackson, C., and D. Boneh, 1670 "An Analysis of Private Browsing Modes in Modern 1671 Browsers", 2010, . 1674 Appendix A. Acknowledgements 1676 This document borrows heavily from RFC 2109 [RFC2109]. We are 1677 indebted to David M. Kristol and Lou Montulli for their efforts to 1678 specify cookies. David M. Kristol, in particular, provided 1679 invaluable advice on navigating the IETF process. We would also like 1680 to thank Thomas Broyer, Tyler Close, Alissa Cooper, Bil Corry, 1681 corvid, Lisa Dusseault, Roy T. Fielding, Blake Frantz, Anne van 1682 Kesteren, Eran Hammer-Lahav, Jeff Hodges, Bjoern Hoehrmann, Achim 1683 Hoffmann, Georg Koppen, Dean McNamee, Alexey Melnikov, Mark Miller, 1684 Mark Pauley, Yngve N. Pettersen, Julian Reschke, Peter Saint-Andre, 1685 Mark Seaborn, Maciej Stachowiak, Daniel Stenberg, Tatsuhiro 1686 Tsujikawa, David Wagner, Dan Winship, and Dan Witte for their 1687 valuable feedback on this document. 1689 Author's Address 1691 Adam Barth 1692 University of California, Berkeley 1694 Email: abarth@eecs.berkeley.edu 1695 URI: http://www.adambarth.com/