idnits 2.17.1 draft-ietf-httpstate-cookie-15.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 are 1 instance of lines with non-RFC6890-compliant IPv4 addresses in the document. If these are example addresses, they should be changed. -- The draft header indicates that this document obsoletes RFC2109, 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 lack the recommended RFC 2119 boilerplate, even if it appears to use RFC 2119 keywords. (The document does seem to have the reference to RFC 2119 which the ID-Checklist requires). == 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 (October 10, 2010) is 4939 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) ** 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: 3 errors (**), 0 flaws (~~), 4 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: 2109 (if approved) October 10, 2010 5 Intended status: Standards Track 6 Expires: April 13, 2011 8 HTTP State Management Mechanism 9 draft-ietf-httpstate-cookie-15 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 April 13, 2011. 45 Copyright Notice 47 Copyright (c) 2010 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 . . . . . . . . . . . . . . . . . . . . . . . . . 6 76 2.1. Conformance Criteria . . . . . . . . . . . . . . . . . . . 6 77 2.2. Syntax Notation . . . . . . . . . . . . . . . . . . . . . 6 78 2.3. Terminology . . . . . . . . . . . . . . . . . . . . . . . 6 79 3. Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 80 3.1. Examples . . . . . . . . . . . . . . . . . . . . . . . . . 8 81 4. Server Requirements . . . . . . . . . . . . . . . . . . . . . 11 82 4.1. Set-Cookie . . . . . . . . . . . . . . . . . . . . . . . . 11 83 4.1.1. Syntax . . . . . . . . . . . . . . . . . . . . . . . . 11 84 4.1.2. Semantics (Non-Normative) . . . . . . . . . . . . . . 12 85 4.2. Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . 15 86 4.2.1. Syntax . . . . . . . . . . . . . . . . . . . . . . . . 15 87 4.2.2. Semantics . . . . . . . . . . . . . . . . . . . . . . 15 88 5. User Agent Requirements . . . . . . . . . . . . . . . . . . . 16 89 5.1. Subcomponent Algorithms . . . . . . . . . . . . . . . . . 16 90 5.1.1. Dates . . . . . . . . . . . . . . . . . . . . . . . . 16 91 5.1.2. Canonicalized host names . . . . . . . . . . . . . . . 18 92 5.1.3. Domain matching . . . . . . . . . . . . . . . . . . . 18 93 5.1.4. Paths and path-match . . . . . . . . . . . . . . . . . 18 94 5.2. The Set-Cookie Header . . . . . . . . . . . . . . . . . . 19 95 5.2.1. The Expires Attribute . . . . . . . . . . . . . . . . 21 96 5.2.2. The Max-Age Attribute . . . . . . . . . . . . . . . . 21 97 5.2.3. The Domain Attribute . . . . . . . . . . . . . . . . . 22 98 5.2.4. The Path Attribute . . . . . . . . . . . . . . . . . . 22 99 5.2.5. The Secure Attribute . . . . . . . . . . . . . . . . . 23 100 5.2.6. The HttpOnly Attribute . . . . . . . . . . . . . . . . 23 101 5.3. Storage Model . . . . . . . . . . . . . . . . . . . . . . 23 102 5.4. The Cookie Header . . . . . . . . . . . . . . . . . . . . 26 103 6. Implementation Considerations . . . . . . . . . . . . . . . . 29 104 6.1. Limits . . . . . . . . . . . . . . . . . . . . . . . . . . 29 105 6.2. Application Programming Interfaces . . . . . . . . . . . . 29 106 6.3. IDNA dependency and migration . . . . . . . . . . . . . . 29 107 7. Privacy Considerations . . . . . . . . . . . . . . . . . . . . 30 108 7.1. Third-Party Cookies . . . . . . . . . . . . . . . . . . . 30 109 7.2. User Controls . . . . . . . . . . . . . . . . . . . . . . 30 110 8. Security Considerations . . . . . . . . . . . . . . . . . . . 32 111 8.1. Overview . . . . . . . . . . . . . . . . . . . . . . . . . 32 112 8.2. Ambient Authority . . . . . . . . . . . . . . . . . . . . 32 113 8.3. Clear Text . . . . . . . . . . . . . . . . . . . . . . . . 33 114 8.4. Session Identifiers . . . . . . . . . . . . . . . . . . . 33 115 8.5. Weak Confidentiality . . . . . . . . . . . . . . . . . . . 34 116 8.6. Weak Integrity . . . . . . . . . . . . . . . . . . . . . . 34 117 8.7. Reliance on DNS . . . . . . . . . . . . . . . . . . . . . 35 118 9. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 36 119 9.1. Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . 36 120 9.2. Set-Cookie . . . . . . . . . . . . . . . . . . . . . . . . 36 121 10. References . . . . . . . . . . . . . . . . . . . . . . . . . . 37 122 10.1. Normative References . . . . . . . . . . . . . . . . . . . 37 123 10.2. Informative References . . . . . . . . . . . . . . . . . . 37 124 Appendix A. Acknowledgements . . . . . . . . . . . . . . . . . . 39 125 Author's Address . . . . . . . . . . . . . . . . . . . . . . . . . 40 127 1. Introduction 129 This document defines the HTTP Cookie and Set-Cookie header fields. 130 Using the Set-Cookie header field, an HTTP server can pass name/value 131 pairs and associated metadata (called cookies) to a user agent. When 132 the user agent makes subsequent requests to the server, the user 133 agent uses the metadata and other information to determine whether to 134 return the name/value pairs in the Cookie header. 136 Although simple on its surface, cookies have a number of 137 complexities. For example, the server indicates a scope for each 138 cookie when sending them to the user agent. The scope indicates the 139 maximum amount of time the user agent should return the cookie, the 140 servers to which the user agent should return the cookie, and the URI 141 schemes for which the cookie is applicable. 143 For historical reasons, cookies contain a number of security and 144 privacy infelicities. For example, a server can indicate that a 145 given cookie is intended for "secure" connections, but the Secure 146 attribute does not provide integrity in the presence of an active 147 network attackers. Similarly, cookies for a given host are shared 148 across all the ports on that host, even though the usual "same-origin 149 policy" used by web browsers isolates content retrieved via different 150 ports. 152 Prior to this document, there were at least three descriptions of 153 cookies: the so-called "Netscape cookie specification" [Netscape], 154 RFC 2109 [RFC2109], and RFC 2965 [RFC2965]. However, none of these 155 documents describe how the Cookie and Set-Cookie headers are actually 156 used on the Internet (see [Kri2001] for historical context). This 157 document attempts to specify the syntax and semantics of these 158 headers as they are actually used on the Internet. 160 2. Conventions 162 2.1. Conformance Criteria 164 The keywords "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", 165 "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be 166 interpreted as described in [RFC2119]. 168 Requirements phrased in the imperative as part of algorithms (such as 169 "strip any leading space characters" or "return false and abort these 170 steps") are to be interpreted with the meaning of the key word 171 ("MUST", "SHOULD", "MAY", etc) used in introducing the algorithm. 173 Conformance requirements phrased as algorithms or specific steps can 174 be implemented in any manner, so long as the end result is 175 equivalent. In particular, the algorithms defined in this 176 specification are intended to be easy to understand and are not 177 intended to be performant. 179 2.2. Syntax Notation 181 This specification uses the Augmented Backus-Naur Form (ABNF) 182 notation of [RFC5234]. 184 The following core rules are included by reference, as defined in 185 [RFC5234], Appendix B.1: ALPHA (letters), CR (carriage return), CRLF 186 (CR LF), CTL (controls), DIGIT (decimal 0-9), DQUOTE (double quote), 187 HEXDIG (hexadecimal 0-9/A-F/a-f), LF (line feed), OCTET (any 8-bit 188 sequence of data), SP (space), HTAB (horizontal tab), CHAR (any US- 189 ASCII character), VCHAR (any visible US-ASCII character), and WSP 190 (whitespace). 192 The OWS (optional whitespace) rule is used where zero or more linear 193 whitespace characters MAY appear: 195 OWS = *( [ obs-fold ] WSP ) 196 ; "optional" whitespace 197 obs-fold = CRLF 199 OWS SHOULD either not be produced or be produced as a single SP 200 character. 202 2.3. Terminology 204 The terms user agent, client, server, proxy, and origin server have 205 the same meaning as in the HTTP/1.1 specification ([RFC2616], Section 206 1.3). 208 The request-host is the name of the host, as known by the user agent, 209 to which the user agent is sending an HTTP request or is receiving an 210 HTTP response from (i.e., the name of the host to which it sent the 211 corresponding HTTP request). 213 The term request-uri is defined in Section 5.1.2 of [RFC2616]. 215 Two sequences of octets are said to case-insensitively match each 216 other if and only if they are equivalent under the i;ascii-casemap 217 collation defined in [RFC4790]. 219 The term string means a sequence of octets. 221 3. Overview 223 This section outlines a way for an origin server to send state 224 information to a user agent and for the user agent to return the 225 state information to the origin server. 227 To store state, the origin server includes a Set-Cookie header in an 228 HTTP response. In subsequent requests, the user agent returns a 229 Cookie request header to the origin server. The Cookie header 230 contains cookies the user agent received in previous Set-Cookie 231 headers. The origin server is free to ignore the Cookie header or 232 use its contents for an application-defined purpose. 234 Origin servers can send a Set-Cookie response header with any 235 response. An origin server can include multiple Set-Cookie header 236 fields in a single response. 238 Note that folding multiple Set-Cookie header fields into a single 239 header field might change the semantics of the header because the 240 %x2C (",") character is used by the Set-Cookie header in a way that 241 conflicts with such folding. This historical infelicity is 242 incompatible with the usual mechanism for folding HTTP headers as 243 defined in [RFC2616]. 245 3.1. Examples 247 Using the Set-Cookie header, a server can send the user agent a short 248 string in an HTTP response that the user agent will return in future 249 HTTP requests. For example, the server can send the user agent a 250 "session identifier" named SID with the value 31d4d96e407aad42. The 251 user agent then returns the session identifier in subsequent 252 requests. 254 == Server -> User Agent == 256 Set-Cookie: SID=31d4d96e407aad42 258 == User Agent -> Server == 260 Cookie: SID=31d4d96e407aad42 262 The server can alter the default scope of the cookie using the Path 263 and Domain attributes. For example, the server can instruct the user 264 agent to return the cookie to every path and every subdomain of 265 example.com. 267 == Server -> User Agent == 269 Set-Cookie: SID=31d4d96e407aad42; Path=/; Domain=example.com 271 == User Agent -> Server == 273 Cookie: SID=31d4d96e407aad42 275 As shown in the next example, the server can store multiple cookies 276 at the user agent. For example, the server can store a session 277 identifier as well as the user's preferred language by returning two 278 Set-Cookie header fields. Notice that the server uses the Secure and 279 HttpOnly attributes to provide additional security protections for 280 the more-sensitive session identifier (see Section 4.1.2.) 282 == Server -> User Agent == 284 Set-Cookie: SID=31d4d96e407aad42; Path=/; Secure; HttpOnly 285 Set-Cookie: lang=en-US; Path=/; Domain=example.com 287 == User Agent -> Server == 289 Cookie: SID=31d4d96e407aad42; lang=en-US 291 Notice that the Cookie header above contains two cookies, one named 292 SID and one named lang. If the server wishes the user agent to 293 persist the cookie over multiple "sessions" (e.g., user agent 294 restarts), the server can specify an expiration date in the Expires 295 attribute. Note that the user agent might delete the cookie before 296 the expiration date if the user agent's cookie store exceeds its 297 quota or if the user manually deletes the server's cookie. 299 == Server -> User Agent == 301 Set-Cookie: lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT 303 == User Agent -> Server == 305 Cookie: SID=31d4d96e407aad42; lang=en-US 307 Finally, to remove a cookie, the server returns a Set-Cookie header 308 with an expiration date in the past. The server will be successful 309 in removing the cookie only if the Path and the Domain attribute in 310 the Set-Cookie header match the values used when the cookie was 311 created. 313 == Server -> User Agent == 315 Set-Cookie: lang=; Expires=Sun, 06 Nov 1994 08:49:37 GMT 317 == User Agent -> Server == 319 Cookie: SID=31d4d96e407aad42 321 4. Server Requirements 323 This section describes the syntax and semantics of a well-behaved 324 profile of the Cookie and Set-Cookie headers. Servers SHOULD limit 325 themselves to the profile described in this section, both to maximize 326 interoperability with existing user agents and because a future 327 version of the Cookie or Set-Cookie headers could remove support for 328 some of the esoteric semantics described in Section 5. User agents, 329 however, MUST implement the requirements in Section 5 to ensure 330 interoperability with servers making use of the full semantics. 332 4.1. Set-Cookie 334 The Set-Cookie HTTP response header is used to send cookies from the 335 server to the user agent. 337 4.1.1. Syntax 339 Informally, the Set-Cookie response header contains the header name 340 "Set-Cookie" followed by a ":" and a cookie. Each cookie begins with 341 a name-value pair, followed by zero or more attribute-value pairs. 342 Servers SHOULD NOT send Set-Cookie headers that fail to conform to 343 the following grammar: 345 set-cookie-header = "Set-Cookie:" SP set-cookie-string 346 set-cookie-string = cookie-pair *( ";" SP cookie-av ) 347 cookie-pair = cookie-name "=" cookie-value 348 cookie-name = token 349 cookie-value = token 350 token = 352 cookie-av = expires-av / max-age-av / domain-av / 353 path-av / secure-av / httponly-av / 354 extension-av 355 expires-av = "Expires=" sane-cookie-date 356 sane-cookie-date = 357 max-age-av = "Max-Age=" 1*DIGIT 358 domain-av = "Domain=" domain-value 359 domain-value = 360 ; defined in [RFC1034], Section 3.5, as 361 ; enhanced by [RFC1123], Section 2.1 362 path-av = "Path=" path-value 363 path-value = 364 secure-av = "Secure" 365 httponly-av = "HttpOnly" 366 extension-av = 367 Note that some of the grammatical terms above reference documents 368 that use different grammatical notations than this document (which 369 uses ABNF from [RFC5234]). 371 The semantics of the cookie-value are not defined by this document. 373 To maximize compatibility with user agents, servers that wish to 374 store non-ASCII data in a cookie-value SHOULD encode that data using 375 a printable ASCII encoding. 377 The portions of the set-cookie-string produced by the cookie-av term 378 are known as attributes. To maximize compatibility with user agents, 379 servers SHOULD NOT produce two attributes with the same name in the 380 same set-cookie-string. 382 Servers SHOULD NOT include more than one Set-Cookie header field in 383 the same response with the same cookie-name. 385 If a server sends multiple responses containing Set-Cookie headers 386 concurrently to the user agent (e.g., when communicating with the 387 user agent over multiple sockets), these responses create a "race 388 condition" that can lead to unpredictable behavior. 390 NOTE: Some legacy user agents differ on their interpretation of two- 391 digit years. To avoid compatibility issues, servers SHOULD use the 392 rfc1123-date format, which requires a four-digit year. 394 NOTE: Some user agents represent dates using 32-bit UNIX time_t 395 values. Some of these user agents might contain bugs that cause them 396 to process dates after the year 2038 incorrectly. 398 4.1.2. Semantics (Non-Normative) 400 This section describes a simplified semantics of the Set-Cookie 401 header. These semantics are detailed enough to be useful for 402 understanding the most common uses of cookies by servers. The full 403 semantics are described in Section 5. 405 When the user agent receives a Set-Cookie header, the user agent 406 stores the cookie together with its attributes. Subsequently, when 407 the user agent makes an HTTP request, the user agent includes the 408 applicable, non-expired cookies in the Cookie header. 410 If the user agent receives a new cookie with the same cookie-name, 411 domain-value, and path-value as a cookie that it has already stored, 412 the existing cookie is evicted and replaced with the new cookie. 413 Notice that servers can delete cookies by sending the user agent a 414 new cookie with an Expires attribute with a value in the past. 416 Unless the cookie's attributes indicate otherwise, the cookie is 417 returned only to the origin server, and it expires at the end of the 418 current session (as defined by the user agent). User agents ignore 419 unrecognized cookie attributes. 421 4.1.2.1. The Expires Attribute 423 The Expires attribute indicates the maximum lifetime of the cookie, 424 represented as the date and time at which the cookie expires. The 425 user agent is not required to retain the cookie until the specified 426 date has passed. In fact, user agents often evict cookies due to 427 memory pressure or privacy concerns. 429 4.1.2.2. The Max-Age Attribute 431 The Max-Age attribute indicates the maximum lifetime of the cookie, 432 represented as the number of seconds until the cookie expires. The 433 user agent is not required to retain the cookie for the specified 434 duration. In fact, user agents often evict cookies from due to 435 memory pressure or privacy concerns. 437 NOTE: Some legacy user agents do not support the Max-Age 438 attribute. User agents that do not support the Max-Age attribute 439 ignore the attribute. 441 If a cookie has both the Max-Age and the Expires attribute, the Max- 442 Age attribute has precedence and controls the expiration date of the 443 cookie. If a cookie has neither the Max-Age nor the Expires 444 attribute, the user agent will retain the cookie until "the current 445 session is over" (as defined by the user agent). 447 4.1.2.3. The Domain Attribute 449 The Domain attribute specifies those hosts to which the cookie will 450 be sent. For example, if the value of the Domain attribute is 451 "example.com", the user agent will include the cookie in the Cookie 452 header when making HTTP requests to example.com, www.example.com, and 453 www.corp.example.com. (Note that a leading %x2E ("."), if present, 454 is ignored even though that character is not permitted.) If the 455 server omits the Domain attribute, the user agent will return the 456 cookie only to the origin server. 458 WARNING: Some legacy user agents treat an absent Domain attribute 459 as if the Domain attribute were present and contained the current 460 host name. For example, if example.com returns a Set-Cookie 461 header without a Domain attribute, these user agents will 462 erroneously send the cookie to www.example.com as well. 464 The user agent will reject cookies unless the Domain attribute 465 specifies a scope for the cookie that would include the origin 466 server. For example, the user agent will accept a cookie with a 467 Domain attribute of "example.com" or of "foo.example.com" from 468 foo.example.com, but the user agent will not accept a cookie with a 469 Domain attribute of "bar.example.com" or of "baz.foo.example.com". 471 NOTE: For security reasons, many user agents are configured to reject 472 Domain attributes that correspond to "public suffixes." For example, 473 some user agents will reject Domain attributes of "com" or "co.uk". 475 4.1.2.4. The Path Attribute 477 The scope of each cookie is limited to a set of paths, controlled by 478 the Path attribute. If the server omits the Path attribute, the user 479 agent will use the "directory" of the request-uri's path component as 480 the default value. (See Section 5.1.4 for more details.) 482 The user agent will include the cookie in an HTTP request only if the 483 path portion of the request-uri matches (or is a subdirectory of) the 484 cookie's Path attribute, where the %x2F ("/") character is 485 interpreted as a directory separator. 487 Although seemingly useful for isolating cookies between different 488 paths within a given host, the Path attribute cannot be relied upon 489 for security (see Section 8). 491 4.1.2.5. The Secure Attribute 493 The Secure attribute limits the scope of the cookie to "secure" 494 channels (where "secure" is defined by the user agent). When a 495 cookie has the Secure attribute, the user agent will include the 496 cookie in an HTTP request only if the request is transmitted over a 497 secure channel (typically HTTP over SSL, HTTP over TLS [RFC2818], and 498 TLS [RFC5246] itself). 500 Although seemingly useful for protecting cookies from active network 501 attackers, the Secure attribute protects only the cookie's 502 confidentiality. An active network attacker can overwrite Secure 503 cookies from an insecure channel, disrupting their integrity (see 504 Section 8.6 for more details). 506 4.1.2.6. The HttpOnly Attribute 508 The HttpOnly attribute limits the scope of the cookie to HTTP 509 requests. In particular, the attribute instructs the user agent to 510 omit the cookie when providing access to cookies via "non-HTTP" APIs 511 (such as a web browser API that exposes cookies to scripts). 513 4.2. Cookie 515 4.2.1. Syntax 517 The user agent sends stored cookies to the origin server in the 518 Cookie header. If the server conforms to the requirements in 519 Section 4.1 (and the user agent conforms to the requirements in the 520 Section 5), the user agent will send a Cookie header that conforms to 521 the following grammar: 523 cookie-header = "Cookie:" OWS cookie-string OWS 524 cookie-string = cookie-pair *( ";" SP cookie-pair ) 526 4.2.2. Semantics 528 Each cookie-pair represents a cookie stored by the user agent. The 529 cookie-pair contains the cookie-name and cookie-value the user agent 530 received in the Set-Cookie header. 532 Notice that the cookie attributes are not returned. In particular, 533 the server cannot determine from the Cookie header alone when a 534 cookie will expire, for which hosts the cookie is valid, for which 535 paths the cookie is valid, or whether the cookie was set with the 536 Secure or HttpOnly attributes. 538 The semantics of individual cookies in the Cookie header are not 539 defined by this document. Servers are expected to imbue these 540 cookies with application-specific semantics. 542 Although cookies are serialized linearly in the Cookie header, 543 servers SHOULD NOT rely upon the serialization order. In particular, 544 if the Cookie header contains two cookies with the same name (e.g., 545 that were set with different Path or Domain attributes), servers 546 SHOULD NOT rely upon the order in which these cookies appear in the 547 header. 549 5. User Agent Requirements 551 For historical reasons, the full semantics of cookies (as presently 552 deployed on the Internet) contain a number of exotic quirks. This 553 section is intended to specify the Cookie and Set-Cookie headers in 554 sufficient detail to allow a user agent implementing these 555 requirements precisely to interoperate with existing servers. 557 5.1. Subcomponent Algorithms 559 This section defines some algorithms used by user agents to process 560 specific subcomponents of the Cookie and Set-Cookie headers. 562 5.1.1. Dates 564 The user agent MUST use an algorithm equivalent to the following 565 algorithm to parse a cookie-date. Note that the various boolean 566 flags defined as a part of the algorithm are initially "not set". 568 1. Using the grammar below, divide the cookie-date into date-tokens. 570 cookie-date = *delimiter date-token-list *delimiter 571 date-token-list = date-token *( 1*delimiter date-token ) 572 date-token = 1*non-delimiter 574 delimiter = %x09 / %x20-2F / %x3B-40 / %x5B-60 / %x7B-7E 575 non-delimiter = %x00-08 / %x0A-1F / DIGIT / ":" / ALPHA / %x7F-FF 576 non-digit = %x00-2F / %x3A-FF 578 day-of-month = 1*2DIGIT ( non-digit *OCTET ) 579 month = ( "jan" / "feb" / "mar" / "apr" / 580 "may" / "jun" / "jul" / "aug" / 581 "sep" / "oct" / "nov" / "dec" ) *OCTET 582 year = 1*4DIGIT ( non-digit *OCTET ) 583 time = hms-time ( non-digit *OCTET ) 584 hms-time = time-field ":" time-field ":" time-field 585 time-field = 1*2DIGIT 587 2. Process each date-token sequentially in the order the date-tokens 588 appear in the cookie-date: 590 1. If the found-time flag is not set and the token matches the 591 time production, set the found-time flag and set the hour- 592 value, minute-value, and second-value to the numbers denoted 593 by the digits in the date-token, respectively. Skip the 594 remaining sub-steps and continue to the next date-token. 596 2. If the found-day-of-month flag is not set and the date-token 597 matches the day-of-month production, set the found-day-of- 598 month flag and set the day-of-month-value to the number 599 denoted by the date-token. Skip the remaining sub-steps and 600 continue to the next date-token. 602 3. If the found-month flag is not set and the date-token matches 603 the month production, set the found-month flag and set the 604 month-value to the month denoted by the date-token. Skip the 605 remaining sub-steps and continue to the next date-token. 607 4. If the found-year flag is not set and the date-token matches 608 the year production, set the found-year flag and set the 609 year-value to the number denoted by the date-token. Skip the 610 remaining sub-steps and continue to the next date-token. 612 3. If the year-value is greater than or equal to 70 and less than or 613 equal to 99, increment the year-value by 1900. 615 4. If the year-value is greater than or equal to 0 and less than or 616 equal to 69, increment the year-value by 2000. 618 1. NOTE: Some legacy user agents interpret two-digit years 619 differently. 621 5. Abort these steps and fail to parse the cookie-date if 623 * at least one of the found-day-of-month, found-month, found- 624 year, or found-time flags is not set, 626 * the day-of-month-value is less than 1 or greater than 31, 628 * the year-value is less than 1601, 630 * the hour-value is greater than 23, 632 * the minute-value is greater than 59, or 634 * the second-value is greater than 59. 636 6. Let the parsed-cookie-date be the date whose day-of-month, month, 637 year, hour, minute, and second (in GMT) are the day-of-month- 638 value, the month-value, the year-value, the hour-value, the 639 minute-value, and the second-value, respectively. If no such 640 date exists, abort these steps and fail to parse the cookie-date. 642 7. Return the parsed-cookie-date as the result of this algorithm. 644 5.1.2. Canonicalized host names 646 A canonicalized host name is the string generated by the following 647 algorithm: 649 1. Convert the host name to a sequence of NR-LDH labels (see Section 650 2.3.2.2 of [RFC5890]) and/or A-labels according to the 651 appropriate IDNA specification [RFC5891] or [RFC3490] (see 652 Section 6.3 of this specification) 654 2. Convert the labels to lower case. 656 3. Concatenate the labels, separating each label from the next with 657 a %x2E (".") character. 659 5.1.3. Domain matching 661 A string domain-matches a given domain string if at least one of the 662 following conditions hold: 664 o The domain string and the string are identical. 666 o All of the following conditions hold: 668 * The domain string is a suffix of the string. 670 * The last character of the string that is not included in the 671 domain string is a %x2E (".") character. 673 * The string is a host name (i.e., not an IP address). 675 5.1.4. Paths and path-match 677 The user agent MUST use an algorithm equivalent to the following 678 algorithm to compute the default-path of a cookie: 680 1. Let uri-path be the path portion of the request-uri if such a 681 portion exists (and empty otherwise). For example, if the 682 request-uri contains just a path (and optional query string), 683 then the uri-path is that path (without the %x3F ("?") character 684 or query string), and if the request-uri contains a full 685 absoluteURI, the uri-path is the path component of that URI. 687 2. If the uri-path is empty or if first character of the uri-path is 688 not a %x2F ("/") character, output %x2F ("/") and skip the 689 remaining steps. 691 3. If the uri-path contains only a single %x2F ("/") character, 692 output %x2F ("/") and skip the remaining steps. 694 4. Output the characters of the uri-path from the first character up 695 to, but not including, the right-most %x2F ("/"). 697 A request-path path-matches a given cookie-path if at least one of 698 the following conditions hold: 700 o The cookie-path and the request-path are identical. 702 o The cookie-path is a prefix of the request-path and the last 703 character of the cookie-path is %x2F ("/"). 705 o The cookie-path is a prefix of the request-path and the first 706 character of the request-path that is not included in the cookie- 707 path is a %x2F ("/") character. 709 5.2. The Set-Cookie Header 711 When a user agent receives a Set-Cookie header field in an HTTP 712 response, the user agent MUST parse the field-value of the Set-Cookie 713 header field as a set-cookie-string (defined below). 715 NOTE: The algorithm below is more permissive than the grammar in 716 Section 4.1. For example, the algorithm strips leading and trailing 717 whitespace from the cookie name and value (but maintains internal 718 whitespace), whereas the grammar in Section 4.1 forbids whitespace in 719 these positions. User agents use this algorithm so as to 720 interoperate with servers that do not follow the recommendations in 721 Section 4. 723 A user agent MUST use an algorithm equivalent to the following 724 algorithm to parse a "set-cookie-string": 726 1. If the set-cookie-string contains a %x3B (";") character: 728 The name-value-pair string consists of the characters up to, 729 but not including, the first %x3B (";"), and the unparsed- 730 attributes consist of the remainder of the set-cookie-string 731 (including the %x3B (";") in question). 733 Otherwise: 735 The name-value-pair string consists of all the characters 736 contained in the set-cookie-string, and the unparsed- 737 attributes is the empty string. 739 2. If the name-value-pair string lacks a %x3D ("=") character, 740 ignore the set-cookie-string entirely. 742 3. The (possibly empty) name string consists of the characters up 743 to, but not including, the first %x3D ("=") character, and the 744 (possibly empty) value string consists of the characters after 745 the first %x3D ("=") character. 747 4. Remove any leading or trailing WSP characters from the name 748 string and the value string. 750 5. If the name string is empty, ignore the set-cookie-string 751 entirely. 753 6. The cookie-name is the name string, and the cookie-value is the 754 value string. 756 The user agent MUST use an algorithm equivalent to the following 757 algorithm to parse the unparsed-attributes: 759 1. If the unparsed-attributes string is empty, skip the rest of 760 these steps. 762 2. Discard the first character of the unparsed-attributes (which 763 will be a %x3B (";") character). 765 3. If the remaining unparsed-attributes contains a %x3B (";") 766 character: 768 Consume the characters of the unparsed-attributes up to, but 769 not including, the first %x3B (";") character. 771 Otherwise: 773 Consume the remainder of the unparsed-attributes. 775 Let the cookie-av string be the characters consumed in this step. 777 4. If the cookie-av string contains a %x3D ("=") character: 779 The (possibly empty) attribute-name string consists of the 780 characters up to, but not including, the first %x3D ("=") 781 character, and the (possibly empty) attribute-value string 782 consists of the characters after the first %x3D ("=") 783 character. 785 Otherwise: 787 The attribute-name string consists of the entire cookie-av 788 string, and the attribute-value string is empty. 790 5. Remove any leading or trailing WSP characters from the attribute- 791 name string and the attribute-value string. 793 6. Process the attribute-name and attribute-value according to the 794 requirements in the following subsections. (Notice that 795 attributes with unrecognized attribute-names are ignored.) 797 7. Return to Step 1 of this algorithm. 799 When the user agent finishes parsing the set-cookie-string, the user 800 agent is said to "receive a cookie" from the request-uri with name 801 cookie-name, value cookie-value, and attributes cookie-attribute- 802 list. (See Section 5.3 for additional requirements triggered by 803 receiving a cookie.) 805 5.2.1. The Expires Attribute 807 If the attribute-name case-insensitively matches the string 808 "Expires", the user agent MUST process the cookie-av as follows. 810 Let the expiry-time be the result of parsing the attribute-value as 811 cookie-date (see Section 5.1.1). 813 If the attribute-value failed to parse as a cookie date, ignore the 814 cookie-av. 816 If the expiry-time is later than the last date the user agent can 817 represent, the user agent MAY replace the expiry-time with the last 818 representable date. 820 If the expiry-time is earlier than the earliest date the user agent 821 can represent, the user agent MAY replace the expiry-time with the 822 earliest representable date. 824 Append an attribute to the cookie-attribute-list with an attribute- 825 name of Expires and an attribute-value of expiry-time. 827 5.2.2. The Max-Age Attribute 829 If the attribute-name case-insensitively matches the string "Max- 830 Age", the user agent MUST process the cookie-av as follows. 832 If the first character of the attribute-value is not a DIGIT or a "-" 833 character, ignore the cookie-av. 835 If the remainder of attribute-value contains a non-DIGIT character, 836 ignore the cookie-av. 838 Let delta-seconds be the attribute-value converted to an integer. 840 If delta-seconds is less than or equal to zero (0), let expiry-time 841 be the earliest representable date and time. Otherwise, let the 842 expiry-time be the current date and time plus delta-seconds seconds. 844 Append an attribute to the cookie-attribute-list with an attribute- 845 name of Max-Age and an attribute-value of expiry-time. 847 5.2.3. The Domain Attribute 849 If the attribute-name case-insensitively matches the string "Domain", 850 the user agent MUST process the cookie-av as follows. 852 If the attribute-value is empty, the behavior is undefined. However, 853 user agent SHOULD ignore the cookie-av entirely. 855 If the first character of the attribute-value string is %x2E ("."): 857 Let cookie-domain be the attribute-value without the leading %x2E 858 (".") character. 860 Otherwise: 862 Let cookie-domain be the entire attribute-value. 864 Convert the cookie-domain to lower case. 866 Append an attribute to the cookie-attribute-list with an attribute- 867 name of Domain and an attribute-value of cookie-domain. 869 5.2.4. The Path Attribute 871 If the attribute-name case-insensitively matches the string "Path", 872 the user agent MUST process the cookie-av as follows. 874 If the attribute-value is empty or if the first character of the 875 attribute-value is not %x2F ("/"): 877 Let cookie-path be the default-path. 879 Otherwise: 881 Let cookie-path be the attribute-value. 883 Append an attribute to the cookie-attribute-list with an attribute- 884 name of Path and an attribute-value of cookie-path. 886 5.2.5. The Secure Attribute 888 If the attribute-name case-insensitively matches the string "Secure", 889 the user agent MUST append an attribute to the cookie-attribute-list 890 with an attribute-name of Secure and an empty attribute-value. 892 5.2.6. The HttpOnly Attribute 894 If the attribute-name case-insensitively matches the string 895 "HttpOnly", the user agent MUST append an attribute to the cookie- 896 attribute-list with an attribute-name of HttpOnly and an empty 897 attribute-value. 899 5.3. Storage Model 901 The user agent stores the following fields about each cookie: name, 902 value, expiry-time, domain, path, creation-time, last-access-time, 903 persistent-flag, host-only-flag, secure-only-flag, and http-only- 904 flag. 906 When the user agent "receives a cookie" from a request-uri with name 907 cookie-name, value cookie-value, and attributes cookie-attribute- 908 list, the user agent MUST process the cookie as follows: 910 1. A user agent MAY ignore a received cookie in its entirety. For 911 example, the user agent might wish to block receiving cookies 912 from "third-party" responses or the user agent might not wish to 913 store cookies that exceed some size. 915 2. Create a new cookie with name cookie-name, value cookie-value. 916 Set the creation-time and the last-access-time to the current 917 date and time. 919 3. If the cookie-attribute-list contains an attribute with an 920 attribute-name of "Max-Age": 922 Set the cookie's persistent-flag to true. 924 Set the cookie's expiry-time to attribute-value of the last 925 attribute in the cookie-attribute-list with an attribute-name 926 of "Max-Age". 928 Otherwise, if the cookie-attribute-list contains an attribute 929 with an attribute-name of "Expires" (and does not contain an 930 attribute with an attribute-name of "Max-Age"): 932 Set the cookie's persistent-flag to true. 934 Set the cookie's expiry-time to attribute-value of the last 935 attribute in the cookie-attribute-list with an attribute-name 936 of "Expires". 938 Otherwise: 940 Set the cookie's persistent-flag to false. 942 Set the cookie's expiry-time to the latest representable 943 date. 945 4. If the cookie-attribute-list contains an attribute with an 946 attribute-name of "Domain": 948 Let the domain-attribute be the attribute-value of the last 949 attribute in the cookie-attribute-list with an attribute-name 950 of "Domain". 952 Otherwise: 954 Let the domain-attribute be the empty string. 956 5. If the user agent is configured to reject "public suffixes" and 957 the domain-attribute is a public suffix: 959 If the domain-attribute is identical to the canonicalized 960 request-host: 962 Let the domain-attribute be the empty string. 964 Otherwise: 966 Ignore the cookie entirely and abort these steps 968 NOTE: A "public suffix" is a domain that is controlled by a 969 public registry, such as "com", "co.uk", and "pvt.k12.wy.us". 970 This step is essential for preventing attacker.com from 971 disrupting the integrity of example.com by setting a cookie 972 with a Domain attribute of "com". Unfortunately, the set of 973 public suffixes (also known as "registry controlled domains") 974 changes over time. If feasible, user agents SHOULD use an 975 up-to-date public suffix list, such as the one maintained by 976 the Mozilla project at . 978 6. If the domain-attribute is non-empty: 980 If the canonicalized request-host does not domain-match the 981 domain-attribute, ignore the cookie entirely and abort these 982 steps. 984 Set the cookie's host-only-flag to false. 986 Set the cookie's domain to the domain-attribute. 988 Otherwise: 990 Set the cookie's host-only-flag to true. 992 Set the cookie's domain to the canonicalized request-host. 994 7. If the cookie-attribute-list contains an attribute with an 995 attribute-name of "Path", set the cookie's path to attribute- 996 value of the last attribute in the cookie-attribute-list with an 997 attribute-name of "Path". Otherwise, set cookie's path to the 998 default-path of the request-uri. 1000 8. If the cookie-attribute-list contains an attribute with an 1001 attribute-name of "Secure", set the cookie's secure-only-flag to 1002 true. Otherwise, set cookie's secure-only-flag to false. 1004 9. If the cookie-attribute-list contains an attribute with an 1005 attribute-name of "HttpOnly", set the cookie's http-only-flag to 1006 true. Otherwise, set cookie's http-only-flag to false. 1008 10. If the cookie was received from a "non-HTTP" API and the 1009 cookie's http-only-flag is set, abort these steps and ignore the 1010 cookie entirely. 1012 11. If the cookie store contains a cookie with the same name, 1013 domain, and path as the newly created cookie: 1015 1. Let old-cookie be the existing cookie with the same name, 1016 domain, and path as the newly created cookie. (Notice that 1017 this algorithm maintains the invariant that there is at most 1018 one such cookie.) 1020 2. If the newly created cookie was received from an "non-HTTP" 1021 API and the old-cookie's http-only-flag is set, abort these 1022 steps and ignore the newly created cookie entirely. 1024 3. Update the creation-time of the newly created cookie to 1025 match the creation-time of the old-cookie. 1027 4. Remove the old-cookie from the cookie store. 1029 12. Insert the newly created cookie into the cookie store. 1031 A cookie is "expired" if the cookie has an expiry date in the past. 1033 The user agent MUST evict all expired cookies from the cookie store 1034 if, at any time, an expired cookie exists in the cookie store. 1036 At any time, the user agent MAY "remove excess cookies" from the 1037 cookie store if the number of cookies sharing a domain field exceeds 1038 some implementaiton defined upper bound (such as 50 cookies). 1040 At any time, the user agent MAY "remove excess cookies" from the 1041 cookie store if the cookie store exceeds some predetermined upper 1042 bound (such as 3000 cookies). 1044 When the user agent removes excess cookies from the cookie store, the 1045 user agent MUST evict cookies in the following priority order: 1047 1. Expired cookies. 1049 2. Cookies that share a domain field with more than a predetermined 1050 number of other cookies. 1052 3. All cookies. 1054 If two cookies have the same removal priority, the user agent MUST 1055 evict the cookie with the earliest last-access date first. 1057 When "the current session is over" (as defined by the user agent), 1058 the user agent MUST remove from the cookie store all cookies with the 1059 persistent-flag set to false. 1061 5.4. The Cookie Header 1063 The user agent includes stored cookies in the Cookie HTTP request 1064 header. 1066 When the user agent generates an HTTP request, the user agent MUST 1067 NOT attach more than one Cookie header field. 1069 A user agent MAY omit the Cookie header in its entirety. For 1070 example, the user agent might wish to block sending cookies during 1071 "third-party" requests. 1073 If the user agent does attach a Cookie header field to an HTTP 1074 request, the user agent MUST send the cookie-string (defined below) 1075 as the value of the header field. 1077 The user agent MUST use an algorithm equivalent to the following 1078 algorithm to compute the "cookie-string" from a cookie store and a 1079 request-uri: 1081 1. Let cookie-list be the set of cookies from the cookie store that 1082 meet all of the following requirements: 1084 * Either: 1086 The cookie's host-only-flag is true and the canonicalized 1087 request-host is identical to the cookie's domain. 1089 Or: 1091 The cookie's host-only-flag is false and the canonicalized 1092 request-host domain-matches cookie's domain. 1094 * The request-uri's path path-matches cookie's path. 1096 * If the cookie's secure-only-flag is true, then the request- 1097 uri's scheme must denote a "secure" protocol (as defined by 1098 the user agent). 1100 NOTE: The notion of a "secure" protocol is not defined by 1101 this document. Typically, user agents consider a protocol 1102 secure if the protocol makes use of transport-layer 1103 security, such as SSL or TLS. For example, most user 1104 agents consider "https" to be a scheme that denotes a 1105 secure protocol. 1107 * If the cookie's http-only-flag is true, then exclude the 1108 cookie if the cookie-string is being generated for a "non- 1109 HTTP" API (as defined by the user agent). 1111 2. The user agent SHOULD sort the cookie-list in the following 1112 order: 1114 * Cookies with longer paths are listed before cookies with 1115 shorter paths. 1117 * Among cookies that have equal length path fields, cookies with 1118 earlier creation-times are listed before cookies with later 1119 creation-times. 1121 NOTE: Not all user agents sort the cookie-list in this order, but 1122 this order reflects common practice when this document was 1123 written, and, historically, there have been servers that 1124 (erroneously) depended on this order. 1126 3. Update the last-access-time of each cookie in the cookie-list to 1127 the current date and time. 1129 4. Serialize the cookie-list into a cookie-string by processing each 1130 cookie in the cookie-list in order: 1132 1. Output the cookie's name, the %x3D ("=") character, and the 1133 cookie's value. 1135 2. If there is an unprocessed cookie in the cookie-list, output 1136 the characters %x3B and %x20 ("; "). 1138 NOTE: Despite its name, the cookie-string is actually a sequence of 1139 octets, not a sequence of characters. To convert the cookie-string 1140 (or components thereof) into a sequence of characters (e.g., for 1141 presentation to the user), the user agent might wish use the UTF-8 1142 character encoding [RFC3629] to decode the octet sequence. 1144 6. Implementation Considerations 1146 6.1. Limits 1148 Practical user agent implementations have limits on the number and 1149 size of cookies that they can store. General-use user agents SHOULD 1150 provide each of the following minimum capabilities: 1152 o At least 4096 bytes per cookie (as measured by the sum of the 1153 length of the cookie's name, value, and attributes). 1155 o At least 50 cookies per domain. 1157 o At least 3000 cookies total. 1159 Servers SHOULD use as few and as small cookies as possible to avoid 1160 reaching these implementation limits and to minimize network 1161 bandwidth due to the Cookie header being included in every request. 1163 Servers SHOULD gracefully degrade if the user agent fails to return 1164 one or more cookies in the Cookie header because the user agent might 1165 evict any cookie at any time on orders from the user. 1167 6.2. Application Programming Interfaces 1169 One reason the Cookie and Set-Cookie headers uses such esoteric 1170 syntax is because many platforms (both in servers and user agents) 1171 provide a string-based application programing interface (API) to 1172 cookies, requiring application-layer programmers to generate and 1173 parse the syntax used by the Cookie and Set-Cookie headers, which 1174 many programmers have done incorrectly, resulting in interoperability 1175 problems. 1177 Instead of providing string-based APIs to cookies, platforms would be 1178 well-served by providing more semantic APIs. It is beyond the scope 1179 of this document to recommend specific API designs, but there are 1180 clear benefits to accepting an abstract "Date" object instead of a 1181 serialized date string. 1183 6.3. IDNA dependency and migration 1185 IDNA2008 [RFC5890] supersedes IDNA2003 [RFC3490] but is not 1186 backwards-compatible. For this reason, there will be a transition 1187 period (possibly of a number of years). User agents SHOULD implement 1188 IDNA2008 [RFC5890] and MAY implement [Unicode Technical Standard #46 1189 ] in order to facilitate a smoother 1190 IDNA transition. If a user agent does not implement IDNA2008, the 1191 user agent MUST implement IDNA2003 [RFC3490]. 1193 7. Privacy Considerations 1195 Cookies are often criticized for letting servers track users. For 1196 example, a number of "web analytics" companies use cookies to 1197 recognize when a user returns to a web site or visits another web 1198 site. Although cookies are not the only mechanism servers can use to 1199 track users across HTTP requests, cookies facilitate tracking because 1200 they are persistent across user agent sessions and can be shared 1201 between hosts. 1203 7.1. Third-Party Cookies 1205 Particularly worrisome are so-called "third-party" cookies. In 1206 rendering an HTML document, a user agent often requests resources 1207 from other servers (such as advertising networks). These third-party 1208 servers can use cookies to track the user even if the user never 1209 visits the server directly. 1211 Some user agents restrict how third-party cookies behave. For 1212 example, some of these user agents refuse to send the Cookie header 1213 in third-party requests. Others refuse to process the Set-Cookie 1214 header in responses to third-party requests. User agents vary widely 1215 in their third-party cookie policies. This document grants user 1216 agents wide latitude to experiment with third-party cookie policies 1217 that balance the privacy and compatibility needs of their users. 1218 However, this document does not endorse any particular third-party 1219 cookie policy. 1221 Third-party cookie blocking policies are often ineffective at 1222 achieving their privacy goals if servers attempt to work around their 1223 restrictions to track users. In particular, two collaborating 1224 servers can often track users without using cookies at all. 1226 7.2. User Controls 1228 User agents should provide users with a mechanism for managing the 1229 cookies stored in the cookie store. For example, a user agent might 1230 let users delete all cookies received during a specified time period 1231 or all the cookies related to a particular domain. In addition, many 1232 user agent include a user interface element that lets users examine 1233 the cookies stored in their cookie store. 1235 User agents should provide users with a mechanism for disabling 1236 cookies. When cookies are disabled, the user agent MUST NOT include 1237 a Cookie header in outbound HTTP requests and the user agent MUST NOT 1238 process Set-Cookie headers in inbound HTTP responses. 1240 Some user agents provide users the option of preventing persistent 1241 storage of cookies across sessions. When configured thusly, user 1242 agents MUST treat all received cookies as if the persistent-flag were 1243 set to false. 1245 Some user agents provide users with the ability to approve individual 1246 writes to the cookie store. In many common usage scenarios, these 1247 controls generate a large number of prompts. However, some privacy- 1248 conscious users find these controls useful nonetheless. 1250 8. Security Considerations 1252 8.1. Overview 1254 Cookies have a number of security pitfalls. This section overviews a 1255 few of the more salient issues. 1257 In particular, cookies encourage developers to rely on ambient 1258 authority for authentication, often becoming vulnerable to attacks 1259 such as cross-site request forgery. Also, when storing session 1260 identifiers in cookies, developers often create session fixation 1261 vulnerabilities. 1263 Transport-layer encryption, such as that employed in HTTPS, is 1264 insufficient to prevent a network attacker from obtaining or altering 1265 a victim's cookies because the cookie protocol itself has various 1266 vulnerabilities (see "Weak Confidentiality" and "Weak Integrity", 1267 below). In addition, by default, cookies do not provide 1268 confidentiality or integrity from network attackers, even when used 1269 in conjunction with HTTPS. 1271 8.2. Ambient Authority 1273 A server that uses cookies to authenticate users can suffer security 1274 vulnerabilities because some user agents let remote parties issue 1275 HTTP requests from the user agent (e.g., via HTTP redirects or HTML 1276 forms). When issuing those requests, user agents attach cookies even 1277 if the remote party does not know the contents of the cookies, 1278 potentially letting the remote party exercise authority at an unwary 1279 server. 1281 Although this security concern goes by a number of names (e.g., 1282 cross-site request forgery, confused deputy), the issue stems from 1283 cookies being a form of ambient authority. Cookies encourage server 1284 operators to separate designation (in the form of URLs) from 1285 authorization (in the form of cookies). Consequently, the user agent 1286 might supply the authorization for a resource designated by the 1287 attacker, possibly causing the server or its clients to undertake 1288 actions designated by the attacker as though they were authorized by 1289 the user. 1291 Instead of using cookies for authorization, server operators might 1292 wish to consider entangling designation and authorization by treating 1293 URLs as capabilities. Instead of storing secrets in cookies, this 1294 approach stores secrets in URLs, requiring the remote entity to 1295 supply the secret itself. Although this approach is not a panacea, 1296 judicious application of these principles can lead to more robust 1297 security. 1299 8.3. Clear Text 1301 Unless sent over a secure channel (such as TLS), the information in 1302 the Cookie and Set-Cookie headers is transmitted in the clear. 1304 1. All sensitive information conveyed in these headers is exposed to 1305 an eavesdropper. 1307 2. A malicious intermediary could alter the headers as they travel 1308 in either direction, with unpredictable results. 1310 3. A malicious client could alter the Cookie header before 1311 transmission, with unpredictable results. 1313 Servers SHOULD encrypt and sign the contents of cookies when 1314 transmitting them to the user agent (even when sending the cookies 1315 over a secure channel). However, encrypting and signing cookie 1316 contents does not prevent an attacker from transplanting a cookie 1317 from one user agent to another or from replaying the cookie at a 1318 later time. 1320 In addition to encrypting and signing the contents of every cookie, 1321 servers that require a higher level of security SHOULD use the Cookie 1322 and Set-Cookie headers only over a secure channel. When using 1323 cookies over a secure channel, servers SHOULD set the Secure 1324 attribute (see Section 4.1.2.5) for every cookie. If a server does 1325 not set the Secure attribute, the protection provided by the secure 1326 channel will be largely moot. 1328 8.4. Session Identifiers 1330 Instead of storing session information directly in a cookie (where it 1331 might be exposed to or replayed by an attacker), servers commonly 1332 store a nonce (or "session identifier") in a cookie. When the server 1333 receives an HTTP request with a nonce, the server can look up state 1334 information associated with the cookie using the nonce as a key. 1336 Using session identifier cookies limits the damage an attacker can 1337 cause if the attacker learns the contents of a cookie because the 1338 nonce is useful only for interacting with the server (unlike non- 1339 nonce cookie content, which might itself be sensitive). Furthermore, 1340 using a single nonce prevents an attacker from "splicing" together 1341 cookie content from two interactions with the server, which could 1342 cause the server to behave unexpectedly. 1344 Using session identifiers is not without risk. For example, the 1345 server SHOULD take care to avoid "session fixation" vulnerabilities. 1346 A session fixation attack proceeds in three steps. First, the 1347 attacker transplants a session identifier from his or her user agent 1348 to the victim's user agent. Second, the victim uses that session 1349 identifier to interact with the server, possibly imbuing the session 1350 identifier with the user's credentials or confidential information. 1351 Third, the attacker uses the session identifier to interact with 1352 server directly, possibly obtaining the user's authority or 1353 confidential information. 1355 8.5. Weak Confidentiality 1357 Cookies do not provide isolation by port. If a cookie is readable by 1358 a service running on one port, the cookie is also readable by a 1359 service running on another port of the same server. If a cookie is 1360 writable by a service on one port, the cookie is also writable by a 1361 service running on another port of the same server. For this reason, 1362 servers SHOULD NOT both run mutually distrusting services on 1363 different ports of the same host and use cookies to store security- 1364 sensitive information. 1366 Cookies do not provide isolation by scheme. Although most commonly 1367 used with the http and https schemes, the cookies for a given host 1368 might also be available to other schemes, such as ftp and gopher. 1369 Although this lack of isolation by scheme is most apparent in non- 1370 HTTP APIs that permit access to cookies (e.g., HTML's document.cookie 1371 API), the lack of isolation by scheme is actually present in 1372 requirements for processing cookies themselves (e.g., consider 1373 retrieving a URI with the gopher scheme via HTTP). 1375 Cookies do not always provide isolation by path. Although the 1376 network-level protocol does not send cookies stored for one path to 1377 another, some user agents expose cookies via non-HTTP APIs, such as 1378 HTML's document.cookie API. Because some of these user agents (e.g., 1379 web browsers) do not isolate resources received from different paths, 1380 a resource retrieved from one path might be able to access cookies 1381 stored for another path. 1383 8.6. Weak Integrity 1385 Cookies do not provide integrity guarantees for sibling domains (and 1386 their subdomains). For example, consider foo.example.com and 1387 bar.example.com. The foo.example.com server can set a cookie with a 1388 Domain attribute of "example.com" (possibly overwriting an existing 1389 "example.com" cookie set by bar.example.com), and the user agent will 1390 include that cookie in HTTP requests to bar.example.com. In the 1391 worst case, bar.example.com will be unable to distinguish this cookie 1392 from a cookie it set itself. The foo.example.com server might be 1393 able to leverage this ability to mount an attack against 1394 bar.example.com. 1396 Even though the Set-Cookie header supports the Path attribute, the 1397 Path attribute does not provide any integrity protection because the 1398 user agent will accept an arbitrary Path attribute in a Set-Cookie 1399 header. For example, an HTTP response to a request for 1400 http://example.com/foo/bar can set a cookie with a Path attribute of 1401 "/qux". Consequently, servers SHOULD NOT both run mutually 1402 distrusting services on different paths of the same host and use 1403 cookies to store security-sensitive information. 1405 An active network attacker can also inject cookies into the Cookie 1406 header sent to https://example.com/ by impersonating a response from 1407 http://example.com/ and injecting a Set-Cookie header. The HTTPS 1408 server at example.com will be unable to distinguish these cookies 1409 from cookies that it set itself in an HTTPS response. An active 1410 network attacker might be able to leverage this ability to mount an 1411 attack against example.com even if example.com uses HTTPS 1412 exclusively. 1414 Servers can partially mitigate these attacks by encrypting and 1415 signing the contents of their cookies. However, using cryptography 1416 does not mitigate the issue completely because an attacker can replay 1417 a cookie he or she received from the authentic example.com server in 1418 the user's session, with unpredictable results. 1420 Finally, an attacker might be able to force the user agent to delete 1421 cookies by storing a large number of cookies. Once the user agent 1422 reaches its storage limit, the user agent will be forced to evict 1423 some cookies. Servers SHOULD NOT rely upon user agents retaining 1424 cookies. 1426 8.7. Reliance on DNS 1428 Cookies rely upon the Domain Name System (DNS) for security. If the 1429 DNS is partially or fully compromised, the cookie protocol might fail 1430 to provide the security properties required by applications. 1432 9. IANA Considerations 1434 The permanent message header field registry (see [RFC3864]) should be 1435 updated with the following registrations: 1437 9.1. Cookie 1439 Header field name: Cookie 1441 Applicable protocol: http 1443 Status: standard 1445 Author/Change controller: IETF 1447 Specification document: this specification (Section 5.4) 1449 9.2. Set-Cookie 1451 Header field name: Set-Cookie 1453 Applicable protocol: http 1455 Status: standard 1457 Author/Change controller: IETF 1459 Specification document: this specification (Section 5.2) 1461 10. References 1463 10.1. Normative References 1465 [RFC1034] Mockapetris, P., "Domain names - concepts and facilities", 1466 STD 13, RFC 1034, November 1987. 1468 [RFC1123] Braden, R., "Requirements for Internet Hosts - Application 1469 and Support", STD 3, RFC 1123, October 1989. 1471 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1472 Requirement Levels", BCP 14, RFC 2119, March 1997. 1474 [RFC2616] Fielding, R., Gettys, J., Mogul, J., Frystyk, H., 1475 Masinter, L., Leach, P., and T. Berners-Lee, "Hypertext 1476 Transfer Protocol -- HTTP/1.1", RFC 2616, June 1999. 1478 [RFC3490] Faltstrom, P., Hoffman, P., and A. Costello, 1479 "Internationalizing Domain Names in Applications (IDNA)", 1480 RFC 3490, March 2003. 1482 See Section 6.3 for an explanation why the normative 1483 reference to an obsoleted specification is needed. 1485 [RFC3629] Yergeau, F., "UTF-8, a transformation format of ISO 1486 10646", STD 63, RFC 3629, November 2003. 1488 [RFC4790] Newman, C., Duerst, M., and A. Gulbrandsen, "Internet 1489 Application Protocol Collation Registry", RFC 4790, 1490 March 2007. 1492 [RFC5234] Crocker, D., Ed. and P. Overell, "Augmented BNF for Syntax 1493 Specifications: ABNF", STD 68, RFC 5234, January 2008. 1495 [RFC5246] Dierks, T. and E. Rescorla, "The Transport Layer Security 1496 (TLS) Protocol Version 1.2", RFC 5246, August 2008. 1498 [RFC5890] Klensin, J., "Internationalized Domain Names for 1499 Applications (IDNA): Definitions and Document Framework", 1500 RFC 5890, August 2010. 1502 [RFC5891] Klensin, J., "Internationalized Domain Names in 1503 Applications (IDNA): Protocol", RFC 5891, August 2010. 1505 10.2. Informative References 1507 [RFC2109] Kristol, D. and L. Montulli, "HTTP State Management 1508 Mechanism", RFC 2109, February 1997. 1510 [RFC2965] Kristol, D. and L. Montulli, "HTTP State Management 1511 Mechanism", RFC 2965, October 2000. 1513 [RFC2818] Rescorla, E., "HTTP Over TLS", RFC 2818, May 2000. 1515 [Netscape] 1516 Netscape Communications Corp., "Persistent Client State -- 1517 HTTP Cookies", 1999, . 1521 [Kri2001] Kristol, D., "HTTP Cookies: Standards, Privacy, and 1522 Politics", ACM Transactions on Internet Technology Vol. 1, 1523 #2, November 2001, . 1525 [RFC3864] Klyne, G., Nottingham, M., and J. Mogul, "Registration 1526 Procedures for Message Header Fields", BCP 90, RFC 3864, 1527 September 2004. 1529 Appendix A. Acknowledgements 1531 This document borrows heavily from RFC 2109 [RFC2109]. We are 1532 indebted to David M. Kristol and Lou Montulli for their efforts to 1533 specify cookies. David M. Kristol, in particular, provided 1534 invaluable advice on navigating the IETF process. We would also like 1535 to thank Thomas Broyer, Tyler Close, Bil Corry, corvid, Lisa 1536 Dusseault, Roy T. Fielding, Blake Frantz, Anne van Kesteren, Eran 1537 Hammer-Lahav, Jeff Hodges, Bjoern Hoehrmann, Achim Hoffmann, Georg 1538 Koppen, Dean McNamee, Mark Miller, Mark Pauley, Yngve N. Pettersen, 1539 Julian Reschke, Peter Saint-Andre, Mark Seaborn, Maciej Stachowiak, 1540 Daniel Stenberg, Tatsuhiro Tsujikawa, David Wagner, Dan Winship, and 1541 Dan Witte for their valuable feedback on this document. 1543 Author's Address 1545 Adam Barth 1546 University of California, Berkeley 1548 Email: abarth@eecs.berkeley.edu 1549 URI: http://www.adambarth.com/