idnits 2.17.1 draft-ietf-httpstate-cookie-16.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 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 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 25, 2010) is 4932 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) -- Obsolete informational reference (is this intentional?): RFC 3548 (Obsoleted by RFC 4648) Summary: 3 errors (**), 0 flaws (~~), 4 warnings (==), 6 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) October 25, 2010 5 Intended status: Standards Track 6 Expires: April 28, 2011 8 HTTP State Management Mechanism 9 draft-ietf-httpstate-cookie-16 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 28, 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 their surface, cookies have a number of 137 complexities. For example, the server indicates a scope for each 138 cookie when sending it 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 attacker. 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 Therefore, in relation to previous IETF specifications of HTTP state 161 management mechanisms, this document requests the following actions: 163 1. Change the status of [RFC2109] to Historic (it has already been 164 obsoleted by [RFC2965]). 166 2. Change the status of [RFC2965] to Historic. 168 3. Indicate that [RFC2965] is obsoleted by this document. 170 2. Conventions 172 2.1. Conformance Criteria 174 The keywords "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", 175 "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be 176 interpreted as described in [RFC2119]. 178 Requirements phrased in the imperative as part of algorithms (such as 179 "strip any leading space characters" or "return false and abort these 180 steps") are to be interpreted with the meaning of the key word 181 ("MUST", "SHOULD", "MAY", etc) used in introducing the algorithm. 183 Conformance requirements phrased as algorithms or specific steps can 184 be implemented in any manner, so long as the end result is 185 equivalent. In particular, the algorithms defined in this 186 specification are intended to be easy to understand and are not 187 intended to be performant. 189 2.2. Syntax Notation 191 This specification uses the Augmented Backus-Naur Form (ABNF) 192 notation of [RFC5234]. 194 The following core rules are included by reference, as defined in 195 [RFC5234], Appendix B.1: ALPHA (letters), CR (carriage return), CRLF 196 (CR LF), CTL (controls), DIGIT (decimal 0-9), DQUOTE (double quote), 197 HEXDIG (hexadecimal 0-9/A-F/a-f), LF (line feed), OCTET (any 8-bit 198 sequence of data), SP (space), HTAB (horizontal tab), CHAR (any US- 199 ASCII character), VCHAR (any visible US-ASCII character), and WSP 200 (whitespace). 202 The OWS (optional whitespace) rule is used where zero or more linear 203 whitespace characters MAY appear: 205 OWS = *( [ obs-fold ] WSP ) 206 ; "optional" whitespace 207 obs-fold = CRLF 209 OWS SHOULD either not be produced or be produced as a single SP 210 character. 212 2.3. Terminology 214 The terms user agent, client, server, proxy, and origin server have 215 the same meaning as in the HTTP/1.1 specification ([RFC2616], Section 216 1.3). 218 The request-host is the name of the host, as known by the user agent, 219 to which the user agent is sending an HTTP request or is receiving an 220 HTTP response from (i.e., the name of the host to which it sent the 221 corresponding HTTP request). 223 The term request-uri is defined in Section 5.1.2 of [RFC2616]. 225 Two sequences of octets are said to case-insensitively match each 226 other if and only if they are equivalent under the i;ascii-casemap 227 collation defined in [RFC4790]. 229 The term string means a sequence of octets. 231 3. Overview 233 This section outlines a way for an origin server to send state 234 information to a user agent and for the user agent to return the 235 state information to the origin server. 237 To store state, the origin server includes a Set-Cookie header in an 238 HTTP response. In subsequent requests, the user agent returns a 239 Cookie request header to the origin server. The Cookie header 240 contains cookies the user agent received in previous Set-Cookie 241 headers. The origin server is free to ignore the Cookie header or 242 use its contents for an application-defined purpose. 244 Origin servers can send a Set-Cookie response header with any 245 response. An origin server can include multiple Set-Cookie header 246 fields in a single response. 248 Note that folding multiple Set-Cookie header fields into a single 249 header field might change the semantics of the header because the 250 %x2C (",") character is used by the Set-Cookie header in a way that 251 conflicts with such folding. This historical infelicity is 252 incompatible with the usual mechanism for folding HTTP headers as 253 defined in [RFC2616]. 255 3.1. Examples 257 Using the Set-Cookie header, a server can send the user agent a short 258 string in an HTTP response that the user agent will return in future 259 HTTP requests. For example, the server can send the user agent a 260 "session identifier" named SID with the value 31d4d96e407aad42. The 261 user agent then returns the session identifier in subsequent 262 requests. 264 == Server -> User Agent == 266 Set-Cookie: SID=31d4d96e407aad42 268 == User Agent -> Server == 270 Cookie: SID=31d4d96e407aad42 272 The server can alter the default scope of the cookie using the Path 273 and Domain attributes. For example, the server can instruct the user 274 agent to return the cookie to every path and every subdomain of 275 example.com. 277 == Server -> User Agent == 279 Set-Cookie: SID=31d4d96e407aad42; Path=/; Domain=example.com 281 == User Agent -> Server == 283 Cookie: SID=31d4d96e407aad42 285 As shown in the next example, the server can store multiple cookies 286 at the user agent. For example, the server can store a session 287 identifier as well as the user's preferred language by returning two 288 Set-Cookie header fields. Notice that the server uses the Secure and 289 HttpOnly attributes to provide additional security protections for 290 the more-sensitive session identifier (see Section 4.1.2.) 292 == Server -> User Agent == 294 Set-Cookie: SID=31d4d96e407aad42; Path=/; Secure; HttpOnly 295 Set-Cookie: lang=en-US; Path=/; Domain=example.com 297 == User Agent -> Server == 299 Cookie: SID=31d4d96e407aad42; lang=en-US 301 Notice that the Cookie header above contains two cookies, one named 302 SID and one named lang. If the server wishes the user agent to 303 persist the cookie over multiple "sessions" (e.g., user agent 304 restarts), the server can specify an expiration date in the Expires 305 attribute. Note that the user agent might delete the cookie before 306 the expiration date if the user agent's cookie store exceeds its 307 quota or if the user manually deletes the server's cookie. 309 == Server -> User Agent == 311 Set-Cookie: lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT 313 == User Agent -> Server == 315 Cookie: SID=31d4d96e407aad42; lang=en-US 317 Finally, to remove a cookie, the server returns a Set-Cookie header 318 with an expiration date in the past. The server will be successful 319 in removing the cookie only if the Path and the Domain attribute in 320 the Set-Cookie header match the values used when the cookie was 321 created. 323 == Server -> User Agent == 325 Set-Cookie: lang=; Expires=Sun, 06 Nov 1994 08:49:37 GMT 327 == User Agent -> Server == 329 Cookie: SID=31d4d96e407aad42 331 4. Server Requirements 333 This section describes the syntax and semantics of a well-behaved 334 profile of the Cookie and Set-Cookie headers. Servers SHOULD limit 335 themselves to the profile described in this section, both to maximize 336 interoperability with existing user agents and because a future 337 version of the Cookie or Set-Cookie headers could remove support for 338 some of the esoteric semantics described in Section 5. User agents, 339 however, MUST implement the requirements in Section 5 to ensure 340 interoperability with servers making use of the full semantics. 342 4.1. Set-Cookie 344 The Set-Cookie HTTP response header is used to send cookies from the 345 server to the user agent. 347 4.1.1. Syntax 349 Informally, the Set-Cookie response header contains the header name 350 "Set-Cookie" followed by a ":" and a cookie. Each cookie begins with 351 a name-value pair, followed by zero or more attribute-value pairs. 352 Servers SHOULD NOT send Set-Cookie headers that fail to conform to 353 the following grammar: 355 set-cookie-header = "Set-Cookie:" SP set-cookie-string 356 set-cookie-string = cookie-pair *( ";" SP cookie-av ) 357 cookie-pair = cookie-name "=" cookie-value 358 cookie-name = token 359 cookie-value = token 360 token = 362 cookie-av = expires-av / max-age-av / domain-av / 363 path-av / secure-av / httponly-av / 364 extension-av 365 expires-av = "Expires=" sane-cookie-date 366 sane-cookie-date = 367 max-age-av = "Max-Age=" 1*DIGIT 368 domain-av = "Domain=" domain-value 369 domain-value = 370 ; defined in [RFC1034], Section 3.5, as 371 ; enhanced by [RFC1123], Section 2.1 372 path-av = "Path=" path-value 373 path-value = 374 secure-av = "Secure" 375 httponly-av = "HttpOnly" 376 extension-av = 377 Note that some of the grammatical terms above reference documents 378 that use different grammatical notations than this document (which 379 uses ABNF from [RFC5234]). 381 The semantics of the cookie-value are not defined by this document. 383 To maximize compatibility with user agents, servers that wish to 384 store arbitrary data in a cookie-value SHOULD encode that data, for 385 example, using Base 16 [RFC3548]. 387 The portions of the set-cookie-string produced by the cookie-av term 388 are known as attributes. To maximize compatibility with user agents, 389 servers SHOULD NOT produce two attributes with the same name in the 390 same set-cookie-string. 392 Servers SHOULD NOT include more than one Set-Cookie header field in 393 the same response with the same cookie-name. 395 If a server sends multiple responses containing Set-Cookie headers 396 concurrently to the user agent (e.g., when communicating with the 397 user agent over multiple sockets), these responses create a "race 398 condition" that can lead to unpredictable behavior. 400 NOTE: Some existing user agents differ on their interpretation of 401 two-digit years. To avoid compatibility issues, servers SHOULD use 402 the rfc1123-date format, which requires a four-digit year. 404 NOTE: Some user agents represent dates using 32-bit UNIX time_t 405 values. Some of these user agents might contain bugs that cause them 406 to process dates after the year 2038 incorrectly. 408 4.1.2. Semantics (Non-Normative) 410 This section describes a simplified semantics of the Set-Cookie 411 header. These semantics are detailed enough to be useful for 412 understanding the most common uses of cookies by servers. The full 413 semantics are described in Section 5. 415 When the user agent receives a Set-Cookie header, the user agent 416 stores the cookie together with its attributes. Subsequently, when 417 the user agent makes an HTTP request, the user agent includes the 418 applicable, non-expired cookies in the Cookie header. 420 If the user agent receives a new cookie with the same cookie-name, 421 domain-value, and path-value as a cookie that it has already stored, 422 the existing cookie is evicted and replaced with the new cookie. 423 Notice that servers can delete cookies by sending the user agent a 424 new cookie with an Expires attribute with a value in the past. 426 Unless the cookie's attributes indicate otherwise, the cookie is 427 returned only to the origin server, and it expires at the end of the 428 current session (as defined by the user agent). User agents ignore 429 unrecognized cookie attributes (but not the entire cookie). 431 4.1.2.1. The Expires Attribute 433 The Expires attribute indicates the maximum lifetime of the cookie, 434 represented as the date and time at which the cookie expires. The 435 user agent is not required to retain the cookie until the specified 436 date has passed. In fact, user agents often evict cookies due to 437 memory pressure or privacy concerns. 439 4.1.2.2. The Max-Age Attribute 441 The Max-Age attribute indicates the maximum lifetime of the cookie, 442 represented as the number of seconds until the cookie expires. The 443 user agent is not required to retain the cookie for the specified 444 duration. In fact, user agents often evict cookies from due to 445 memory pressure or privacy concerns. 447 NOTE: Some existing user agents do not support the Max-Age 448 attribute. User agents that do not support the Max-Age attribute 449 ignore the attribute. 451 If a cookie has both the Max-Age and the Expires attribute, the Max- 452 Age attribute has precedence and controls the expiration date of the 453 cookie. If a cookie has neither the Max-Age nor the Expires 454 attribute, the user agent will retain the cookie until "the current 455 session is over" (as defined by the user agent). 457 4.1.2.3. The Domain Attribute 459 The Domain attribute specifies those hosts to which the cookie will 460 be sent. For example, if the value of the Domain attribute is 461 "example.com", the user agent will include the cookie in the Cookie 462 header when making HTTP requests to example.com, www.example.com, and 463 www.corp.example.com. (Note that a leading %x2E ("."), if present, 464 is ignored even though that character is not permitted.) If the 465 server omits the Domain attribute, the user agent will return the 466 cookie only to the origin server. 468 WARNING: Some existing user agents treat an absent Domain 469 attribute as if the Domain attribute were present and contained 470 the current host name. For example, if example.com returns a Set- 471 Cookie header without a Domain attribute, these user agents will 472 erroneously send the cookie to www.example.com as well. 474 The user agent will reject cookies unless the Domain attribute 475 specifies a scope for the cookie that would include the origin 476 server. For example, the user agent will accept a cookie with a 477 Domain attribute of "example.com" or of "foo.example.com" from 478 foo.example.com, but the user agent will not accept a cookie with a 479 Domain attribute of "bar.example.com" or of "baz.foo.example.com". 481 NOTE: For security reasons, many user agents are configured to reject 482 Domain attributes that correspond to "public suffixes". For example, 483 some user agents will reject Domain attributes of "com" or "co.uk". 484 (See Section 5.3 for more information.) 486 4.1.2.4. The Path Attribute 488 The scope of each cookie is limited to a set of paths, controlled by 489 the Path attribute. If the server omits the Path attribute, the user 490 agent will use the "directory" of the request-uri's path component as 491 the default value. (See Section 5.1.4 for more details.) 493 The user agent will include the cookie in an HTTP request only if the 494 path portion of the request-uri matches (or is a subdirectory of) the 495 cookie's Path attribute, where the %x2F ("/") character is 496 interpreted as a directory separator. 498 Although seemingly useful for isolating cookies between different 499 paths within a given host, the Path attribute cannot be relied upon 500 for security (see Section 8). 502 4.1.2.5. The Secure Attribute 504 The Secure attribute limits the scope of the cookie to "secure" 505 channels (where "secure" is defined by the user agent). When a 506 cookie has the Secure attribute, the user agent will include the 507 cookie in an HTTP request only if the request is transmitted over a 508 secure channel (typically HTTP over Secure Sockets Layer (SSL), HTTP 509 over Transport Layer Security (TLS) [RFC2818], and TLS [RFC5246] 510 itself). 512 Although seemingly useful for protecting cookies from active network 513 attackers, the Secure attribute protects only the cookie's 514 confidentiality. An active network attacker can overwrite Secure 515 cookies from an insecure channel, disrupting their integrity (see 516 Section 8.6 for more details). 518 4.1.2.6. The HttpOnly Attribute 520 The HttpOnly attribute limits the scope of the cookie to HTTP 521 requests. In particular, the attribute instructs the user agent to 522 omit the cookie when providing access to cookies via "non-HTTP" APIs 523 (such as a web browser API that exposes cookies to scripts). 525 4.2. Cookie 527 4.2.1. Syntax 529 The user agent sends stored cookies to the origin server in the 530 Cookie header. If the server conforms to the requirements in 531 Section 4.1 (and the user agent conforms to the requirements in 532 Section 5), the user agent will send a Cookie header that conforms to 533 the following grammar: 535 cookie-header = "Cookie:" OWS cookie-string OWS 536 cookie-string = cookie-pair *( ";" SP cookie-pair ) 538 4.2.2. Semantics 540 Each cookie-pair represents a cookie stored by the user agent. The 541 cookie-pair contains the cookie-name and cookie-value the user agent 542 received in the Set-Cookie header. 544 Notice that the cookie attributes are not returned. In particular, 545 the server cannot determine from the Cookie header alone when a 546 cookie will expire, for which hosts the cookie is valid, for which 547 paths the cookie is valid, or whether the cookie was set with the 548 Secure or HttpOnly attributes. 550 The semantics of individual cookies in the Cookie header are not 551 defined by this document. Servers are expected to imbue these 552 cookies with application-specific semantics. 554 Although cookies are serialized linearly in the Cookie header, 555 servers SHOULD NOT rely upon the serialization order. In particular, 556 if the Cookie header contains two cookies with the same name (e.g., 557 that were set with different Path or Domain attributes), servers 558 SHOULD NOT rely upon the order in which these cookies appear in the 559 header. 561 5. User Agent Requirements 563 For historical reasons, the full semantics of cookies (as presently 564 deployed on the Internet) contain a number of exotic quirks. This 565 section is intended to specify the Cookie and Set-Cookie headers in 566 sufficient detail to allow a user agent implementing these 567 requirements precisely to interoperate with existing servers. 569 5.1. Subcomponent Algorithms 571 This section defines some algorithms used by user agents to process 572 specific subcomponents of the Cookie and Set-Cookie headers. 574 5.1.1. Dates 576 The user agent MUST use an algorithm equivalent to the following 577 algorithm to parse a cookie-date. Note that the various boolean 578 flags defined as a part of the algorithm are initially "not set". 580 1. Using the grammar below, divide the cookie-date into date-tokens. 582 cookie-date = *delimiter date-token-list *delimiter 583 date-token-list = date-token *( 1*delimiter date-token ) 584 date-token = 1*non-delimiter 586 delimiter = %x09 / %x20-2F / %x3B-40 / %x5B-60 / %x7B-7E 587 non-delimiter = %x00-08 / %x0A-1F / DIGIT / ":" / ALPHA / %x7F-FF 588 non-digit = %x00-2F / %x3A-FF 590 day-of-month = 1*2DIGIT ( non-digit *OCTET ) 591 month = ( "jan" / "feb" / "mar" / "apr" / 592 "may" / "jun" / "jul" / "aug" / 593 "sep" / "oct" / "nov" / "dec" ) *OCTET 594 year = 1*4DIGIT ( non-digit *OCTET ) 595 time = hms-time ( non-digit *OCTET ) 596 hms-time = time-field ":" time-field ":" time-field 597 time-field = 1*2DIGIT 599 2. Process each date-token sequentially in the order the date-tokens 600 appear in the cookie-date: 602 1. If the found-time flag is not set and the token matches the 603 time production, set the found-time flag and set the hour- 604 value, minute-value, and second-value to the numbers denoted 605 by the digits in the date-token, respectively. Skip the 606 remaining sub-steps and continue to the next date-token. 608 2. If the found-day-of-month flag is not set and the date-token 609 matches the day-of-month production, set the found-day-of- 610 month flag and set the day-of-month-value to the number 611 denoted by the date-token. Skip the remaining sub-steps and 612 continue to the next date-token. 614 3. If the found-month flag is not set and the date-token matches 615 the month production, set the found-month flag and set the 616 month-value to the month denoted by the date-token. Skip the 617 remaining sub-steps and continue to the next date-token. 619 4. If the found-year flag is not set and the date-token matches 620 the year production, set the found-year flag and set the 621 year-value to the number denoted by the date-token. Skip the 622 remaining sub-steps and continue to the next date-token. 624 3. If the year-value is greater than or equal to 70 and less than or 625 equal to 99, increment the year-value by 1900. 627 4. If the year-value is greater than or equal to 0 and less than or 628 equal to 69, increment the year-value by 2000. 630 1. NOTE: Some existing user agents interpret two-digit years 631 differently. 633 5. Abort these steps and fail to parse the cookie-date if 635 * at least one of the found-day-of-month, found-month, found- 636 year, or found-time flags is not set, 638 * the day-of-month-value is less than 1 or greater than 31, 640 * the year-value is less than 1601, 642 * the hour-value is greater than 23, 644 * the minute-value is greater than 59, or 646 * the second-value is greater than 59. 648 6. Let the parsed-cookie-date be the date whose day-of-month, month, 649 year, hour, minute, and second (in UTC) are the day-of-month- 650 value, the month-value, the year-value, the hour-value, the 651 minute-value, and the second-value, respectively. If no such 652 date exists, abort these steps and fail to parse the cookie-date. 654 7. Return the parsed-cookie-date as the result of this algorithm. 656 5.1.2. Canonicalized host names 658 A canonicalized host name is the string generated by the following 659 algorithm: 661 1. Convert the host name to a sequence of NR-LDH labels (see Section 662 2.3.2.2 of [RFC5890]) or a sequence of A-labels according to the 663 appropriate IDNA specification [RFC5891] or [RFC3490] (see 664 Section 6.3 of this specification) 666 2. Concatenate the labels resulting from the previous step, 667 separating each label from the next with a %x2E (".") character. 669 5.1.3. Domain matching 671 A string domain-matches a given domain string if at least one of the 672 following conditions hold: 674 o The domain string and the string are identical. 676 o All of the following conditions hold: 678 * The domain string is a suffix of the string. 680 * The last character of the string that is not included in the 681 domain string is a %x2E (".") character. 683 * The string is a host name (i.e., not an IP address). 685 5.1.4. Paths and path-match 687 The user agent MUST use an algorithm equivalent to the following 688 algorithm to compute the default-path of a cookie: 690 1. Let uri-path be the path portion of the request-uri if such a 691 portion exists (and empty otherwise). For example, if the 692 request-uri contains just a path (and optional query string), 693 then the uri-path is that path (without the %x3F ("?") character 694 or query string), and if the request-uri contains a full 695 absoluteURI, the uri-path is the path component of that URI. 697 2. If the uri-path is empty or if the first character of the uri- 698 path is not a %x2F ("/") character, output %x2F ("/") and skip 699 the remaining steps. 701 3. If the uri-path contains only a single %x2F ("/") character, 702 output %x2F ("/") and skip the remaining steps. 704 4. Output the characters of the uri-path from the first character up 705 to, but not including, the right-most %x2F ("/"). 707 A request-path path-matches a given cookie-path if at least one of 708 the following conditions hold: 710 o The cookie-path and the request-path are identical. 712 o The cookie-path is a prefix of the request-path and the last 713 character of the cookie-path is %x2F ("/"). 715 o The cookie-path is a prefix of the request-path and the first 716 character of the request-path that is not included in the cookie- 717 path is a %x2F ("/") character. 719 5.2. The Set-Cookie Header 721 When a user agent receives a Set-Cookie header field in an HTTP 722 response, the user agent MAY ignore the Set-Cookie header field in 723 its entirety. For example, the user agent might wish to block 724 responses to "third-party" requests from setting cookies. 726 If the user agent does not ignore the Set-Cookie header field in its 727 entirety, the user agent MUST parse the field-value of the Set-Cookie 728 header field as a set-cookie-string (defined below). 730 NOTE: The algorithm below is more permissive than the grammar in 731 Section 4.1. For example, the algorithm strips leading and trailing 732 whitespace from the cookie name and value (but maintains internal 733 whitespace), whereas the grammar in Section 4.1 forbids whitespace in 734 these positions. User agents use this algorithm so as to 735 interoperate with servers that do not follow the recommendations in 736 Section 4. 738 A user agent MUST use an algorithm equivalent to the following 739 algorithm to parse a "set-cookie-string": 741 1. If the set-cookie-string contains a %x3B (";") character: 743 The name-value-pair string consists of the characters up to, 744 but not including, the first %x3B (";"), and the unparsed- 745 attributes consist of the remainder of the set-cookie-string 746 (including the %x3B (";") in question). 748 Otherwise: 750 The name-value-pair string consists of all the characters 751 contained in the set-cookie-string, and the unparsed- 752 attributes is the empty string. 754 2. If the name-value-pair string lacks a %x3D ("=") character, 755 ignore the set-cookie-string entirely. 757 3. The (possibly empty) name string consists of the characters up 758 to, but not including, the first %x3D ("=") character, and the 759 (possibly empty) value string consists of the characters after 760 the first %x3D ("=") character. 762 4. Remove any leading or trailing WSP characters from the name 763 string and the value string. 765 5. If the name string is empty, ignore the set-cookie-string 766 entirely. 768 6. The cookie-name is the name string, and the cookie-value is the 769 value string. 771 The user agent MUST use an algorithm equivalent to the following 772 algorithm to parse the unparsed-attributes: 774 1. If the unparsed-attributes string is empty, skip the rest of 775 these steps. 777 2. Discard the first character of the unparsed-attributes (which 778 will be a %x3B (";") character). 780 3. If the remaining unparsed-attributes contains a %x3B (";") 781 character: 783 Consume the characters of the unparsed-attributes up to, but 784 not including, the first %x3B (";") character. 786 Otherwise: 788 Consume the remainder of the unparsed-attributes. 790 Let the cookie-av string be the characters consumed in this step. 792 4. If the cookie-av string contains a %x3D ("=") character: 794 The (possibly empty) attribute-name string consists of the 795 characters up to, but not including, the first %x3D ("=") 796 character, and the (possibly empty) attribute-value string 797 consists of the characters after the first %x3D ("=") 798 character. 800 Otherwise: 802 The attribute-name string consists of the entire cookie-av 803 string, and the attribute-value string is empty. 805 5. Remove any leading or trailing WSP characters from the attribute- 806 name string and the attribute-value string. 808 6. Process the attribute-name and attribute-value according to the 809 requirements in the following subsections. (Notice that 810 attributes with unrecognized attribute-names are ignored.) 812 7. Return to Step 1 of this algorithm. 814 When the user agent finishes parsing the set-cookie-string, the user 815 agent is said to "receive a cookie" from the request-uri with name 816 cookie-name, value cookie-value, and attributes cookie-attribute- 817 list. (See Section 5.3 for additional requirements triggered by 818 receiving a cookie.) 820 5.2.1. The Expires Attribute 822 If the attribute-name case-insensitively matches the string 823 "Expires", the user agent MUST process the cookie-av as follows. 825 Let the expiry-time be the result of parsing the attribute-value as 826 cookie-date (see Section 5.1.1). 828 If the attribute-value failed to parse as a cookie date, ignore the 829 cookie-av. 831 If the expiry-time is later than the last date the user agent can 832 represent, the user agent MAY replace the expiry-time with the last 833 representable date. 835 If the expiry-time is earlier than the earliest date the user agent 836 can represent, the user agent MAY replace the expiry-time with the 837 earliest representable date. 839 Append an attribute to the cookie-attribute-list with an attribute- 840 name of Expires and an attribute-value of expiry-time. 842 5.2.2. The Max-Age Attribute 844 If the attribute-name case-insensitively matches the string "Max- 845 Age", the user agent MUST process the cookie-av as follows. 847 If the first character of the attribute-value is not a DIGIT or a "-" 848 character, ignore the cookie-av. 850 If the remainder of attribute-value contains a non-DIGIT character, 851 ignore the cookie-av. 853 Let delta-seconds be the attribute-value converted to an integer. 855 If delta-seconds is less than or equal to zero (0), let expiry-time 856 be the earliest representable date and time. Otherwise, let the 857 expiry-time be the current date and time plus delta-seconds seconds. 859 Append an attribute to the cookie-attribute-list with an attribute- 860 name of Max-Age and an attribute-value of expiry-time. 862 5.2.3. The Domain Attribute 864 If the attribute-name case-insensitively matches the string "Domain", 865 the user agent MUST process the cookie-av as follows. 867 If the attribute-value is empty, the behavior is undefined. However, 868 user agent SHOULD ignore the cookie-av entirely. 870 If the first character of the attribute-value string is %x2E ("."): 872 Let cookie-domain be the attribute-value without the leading %x2E 873 (".") character. 875 Otherwise: 877 Let cookie-domain be the entire attribute-value. 879 Convert the cookie-domain to lower case. 881 Append an attribute to the cookie-attribute-list with an attribute- 882 name of Domain and an attribute-value of cookie-domain. 884 5.2.4. The Path Attribute 886 If the attribute-name case-insensitively matches the string "Path", 887 the user agent MUST process the cookie-av as follows. 889 If the attribute-value is empty or if the first character of the 890 attribute-value is not %x2F ("/"): 892 Let cookie-path be the default-path. 894 Otherwise: 896 Let cookie-path be the attribute-value. 898 Append an attribute to the cookie-attribute-list with an attribute- 899 name of Path and an attribute-value of cookie-path. 901 5.2.5. The Secure Attribute 903 If the attribute-name case-insensitively matches the string "Secure", 904 the user agent MUST append an attribute to the cookie-attribute-list 905 with an attribute-name of Secure and an empty attribute-value. 907 5.2.6. The HttpOnly Attribute 909 If the attribute-name case-insensitively matches the string 910 "HttpOnly", the user agent MUST append an attribute to the cookie- 911 attribute-list with an attribute-name of HttpOnly and an empty 912 attribute-value. 914 5.3. Storage Model 916 The user agent stores the following fields about each cookie: name, 917 value, expiry-time, domain, path, creation-time, last-access-time, 918 persistent-flag, host-only-flag, secure-only-flag, and http-only- 919 flag. 921 When the user agent "receives a cookie" from a request-uri with name 922 cookie-name, value cookie-value, and attributes cookie-attribute- 923 list, the user agent MUST process the cookie as follows: 925 1. A user agent MAY ignore a received cookie in its entirety. For 926 example, the user agent might wish to block receiving cookies 927 from "third-party" responses or the user agent might not wish to 928 store cookies that exceed some size. 930 2. Create a new cookie with name cookie-name, value cookie-value. 931 Set the creation-time and the last-access-time to the current 932 date and time. 934 3. If the cookie-attribute-list contains an attribute with an 935 attribute-name of "Max-Age": 937 Set the cookie's persistent-flag to true. 939 Set the cookie's expiry-time to attribute-value of the last 940 attribute in the cookie-attribute-list with an attribute-name 941 of "Max-Age". 943 Otherwise, if the cookie-attribute-list contains an attribute 944 with an attribute-name of "Expires" (and does not contain an 945 attribute with an attribute-name of "Max-Age"): 947 Set the cookie's persistent-flag to true. 949 Set the cookie's expiry-time to attribute-value of the last 950 attribute in the cookie-attribute-list with an attribute-name 951 of "Expires". 953 Otherwise: 955 Set the cookie's persistent-flag to false. 957 Set the cookie's expiry-time to the latest representable 958 date. 960 4. If the cookie-attribute-list contains an attribute with an 961 attribute-name of "Domain": 963 Let the domain-attribute be the attribute-value of the last 964 attribute in the cookie-attribute-list with an attribute-name 965 of "Domain". 967 Otherwise: 969 Let the domain-attribute be the empty string. 971 5. If the user agent is configured to reject "public suffixes" and 972 the domain-attribute is a public suffix: 974 If the domain-attribute is identical to the canonicalized 975 request-host: 977 Let the domain-attribute be the empty string. 979 Otherwise: 981 Ignore the cookie entirely and abort these steps. 983 NOTE: A "public suffix" is a domain that is controlled by a 984 public registry, such as "com", "co.uk", and "pvt.k12.wy.us". 985 This step is essential for preventing attacker.com from 986 disrupting the integrity of example.com by setting a cookie 987 with a Domain attribute of "com". Unfortunately, the set of 988 public suffixes (also known as "registry controlled domains") 989 changes over time. If feasible, user agents SHOULD use an 990 up-to-date public suffix list, such as the one maintained by 991 the Mozilla project at . 993 6. If the domain-attribute is non-empty: 995 If the canonicalized request-host does not domain-match the 996 domain-attribute: 998 Ignore the cookie entirely and abort these steps. 1000 Otherwise: 1002 Set the cookie's host-only-flag to false. 1004 Set the cookie's domain to the domain-attribute. 1006 Otherwise: 1008 Set the cookie's host-only-flag to true. 1010 Set the cookie's domain to the canonicalized request-host. 1012 7. If the cookie-attribute-list contains an attribute with an 1013 attribute-name of "Path", set the cookie's path to attribute- 1014 value of the last attribute in the cookie-attribute-list with an 1015 attribute-name of "Path". Otherwise, set cookie's path to the 1016 default-path of the request-uri. 1018 8. If the cookie-attribute-list contains an attribute with an 1019 attribute-name of "Secure", set the cookie's secure-only-flag to 1020 true. Otherwise, set cookie's secure-only-flag to false. 1022 9. If the cookie-attribute-list contains an attribute with an 1023 attribute-name of "HttpOnly", set the cookie's http-only-flag to 1024 true. Otherwise, set cookie's http-only-flag to false. 1026 10. If the cookie was received from a "non-HTTP" API and the 1027 cookie's http-only-flag is set, abort these steps and ignore the 1028 cookie entirely. 1030 11. If the cookie store contains a cookie with the same name, 1031 domain, and path as the newly created cookie: 1033 1. Let old-cookie be the existing cookie with the same name, 1034 domain, and path as the newly created cookie. (Notice that 1035 this algorithm maintains the invariant that there is at most 1036 one such cookie.) 1038 2. If the newly created cookie was received from a "non-HTTP" 1039 API and the old-cookie's http-only-flag is set, abort these 1040 steps and ignore the newly created cookie entirely. 1042 3. Update the creation-time of the newly created cookie to 1043 match the creation-time of the old-cookie. 1045 4. Remove the old-cookie from the cookie store. 1047 12. Insert the newly created cookie into the cookie store. 1049 A cookie is "expired" if the cookie has an expiry date in the past. 1051 The user agent MUST evict all expired cookies from the cookie store 1052 if, at any time, an expired cookie exists in the cookie store. 1054 At any time, the user agent MAY "remove excess cookies" from the 1055 cookie store if the number of cookies sharing a domain field exceeds 1056 some implementaiton defined upper bound (such as 50 cookies). 1058 At any time, the user agent MAY "remove excess cookies" from the 1059 cookie store if the cookie store exceeds some predetermined upper 1060 bound (such as 3000 cookies). 1062 When the user agent removes excess cookies from the cookie store, the 1063 user agent MUST evict cookies in the following priority order: 1065 1. Expired cookies. 1067 2. Cookies that share a domain field with more than a predetermined 1068 number of other cookies. 1070 3. All cookies. 1072 If two cookies have the same removal priority, the user agent MUST 1073 evict the cookie with the earliest last-access date first. 1075 When "the current session is over" (as defined by the user agent), 1076 the user agent MUST remove from the cookie store all cookies with the 1077 persistent-flag set to false. 1079 5.4. The Cookie Header 1081 The user agent includes stored cookies in the Cookie HTTP request 1082 header. 1084 When the user agent generates an HTTP request, the user agent MUST 1085 NOT attach more than one Cookie header field. 1087 A user agent MAY omit the Cookie header in its entirety. For 1088 example, the user agent might wish to block sending cookies during 1089 "third-party" requests. 1091 If the user agent does attach a Cookie header field to an HTTP 1092 request, the user agent MUST send the cookie-string (defined below) 1093 as the value of the header field. 1095 The user agent MUST use an algorithm equivalent to the following 1096 algorithm to compute the "cookie-string" from a cookie store and a 1097 request-uri: 1099 1. Let cookie-list be the set of cookies from the cookie store that 1100 meet all of the following requirements: 1102 * Either: 1104 The cookie's host-only-flag is true and the canonicalized 1105 request-host is identical to the cookie's domain. 1107 Or: 1109 The cookie's host-only-flag is false and the canonicalized 1110 request-host domain-matches cookie's domain. 1112 * The request-uri's path path-matches cookie's path. 1114 * If the cookie's secure-only-flag is true, then the request- 1115 uri's scheme must denote a "secure" protocol (as defined by 1116 the user agent). 1118 NOTE: The notion of a "secure" protocol is not defined by 1119 this document. Typically, user agents consider a protocol 1120 secure if the protocol makes use of transport-layer 1121 security, such as SSL or TLS. For example, most user 1122 agents consider "https" to be a scheme that denotes a 1123 secure protocol. 1125 * If the cookie's http-only-flag is true, then exclude the 1126 cookie if the cookie-string is being generated for a "non- 1127 HTTP" API (as defined by the user agent). 1129 2. The user agent SHOULD sort the cookie-list in the following 1130 order: 1132 * Cookies with longer paths are listed before cookies with 1133 shorter paths. 1135 * Among cookies that have equal length path fields, cookies with 1136 earlier creation-times are listed before cookies with later 1137 creation-times. 1139 NOTE: Not all user agents sort the cookie-list in this order, but 1140 this order reflects common practice when this document was 1141 written, and, historically, there have been servers that 1142 (erroneously) depended on this order. 1144 3. Update the last-access-time of each cookie in the cookie-list to 1145 the current date and time. 1147 4. Serialize the cookie-list into a cookie-string by processing each 1148 cookie in the cookie-list in order: 1150 1. Output the cookie's name, the %x3D ("=") character, and the 1151 cookie's value. 1153 2. If there is an unprocessed cookie in the cookie-list, output 1154 the characters %x3B and %x20 ("; "). 1156 NOTE: Despite its name, the cookie-string is actually a sequence of 1157 octets, not a sequence of characters. To convert the cookie-string 1158 (or components thereof) into a sequence of characters (e.g., for 1159 presentation to the user), the user agent might wish use the UTF-8 1160 character encoding [RFC3629] to decode the octet sequence. 1162 6. Implementation Considerations 1164 6.1. Limits 1166 Practical user agent implementations have limits on the number and 1167 size of cookies that they can store. General-use user agents SHOULD 1168 provide each of the following minimum capabilities: 1170 o At least 4096 bytes per cookie (as measured by the sum of the 1171 length of the cookie's name, value, and attributes). 1173 o At least 50 cookies per domain. 1175 o At least 3000 cookies total. 1177 Servers SHOULD use as few and as small cookies as possible to avoid 1178 reaching these implementation limits and to minimize network 1179 bandwidth due to the Cookie header being included in every request. 1181 Servers SHOULD gracefully degrade if the user agent fails to return 1182 one or more cookies in the Cookie header because the user agent might 1183 evict any cookie at any time on orders from the user. 1185 6.2. Application Programming Interfaces 1187 One reason the Cookie and Set-Cookie headers uses such esoteric 1188 syntax is because many platforms (both in servers and user agents) 1189 provide a string-based application programing interface (API) to 1190 cookies, requiring application-layer programmers to generate and 1191 parse the syntax used by the Cookie and Set-Cookie headers, which 1192 many programmers have done incorrectly, resulting in interoperability 1193 problems. 1195 Instead of providing string-based APIs to cookies, platforms would be 1196 well-served by providing more semantic APIs. It is beyond the scope 1197 of this document to recommend specific API designs, but there are 1198 clear benefits to accepting an abstract "Date" object instead of a 1199 serialized date string. 1201 6.3. IDNA dependency and migration 1203 IDNA2008 [RFC5890] supersedes IDNA2003 [RFC3490] but is not 1204 backwards-compatible. For this reason, there will be a transition 1205 period (possibly of a number of years). User agents SHOULD implement 1206 IDNA2008 [RFC5890] and MAY implement [Unicode Technical Standard #46 1207 ] in order to facilitate a smoother 1208 IDNA transition. If a user agent does not implement IDNA2008, the 1209 user agent MUST implement IDNA2003 [RFC3490]. 1211 7. Privacy Considerations 1213 Cookies are often criticized for letting servers track users. For 1214 example, a number of "web analytics" companies use cookies to 1215 recognize when a user returns to a web site or visits another web 1216 site. Although cookies are not the only mechanism servers can use to 1217 track users across HTTP requests, cookies facilitate tracking because 1218 they are persistent across user agent sessions and can be shared 1219 between hosts. 1221 7.1. Third-Party Cookies 1223 Particularly worrisome are so-called "third-party" cookies. In 1224 rendering an HTML document, a user agent often requests resources 1225 from other servers (such as advertising networks). These third-party 1226 servers can use cookies to track the user even if the user never 1227 visits the server directly. 1229 Some user agents restrict how third-party cookies behave. For 1230 example, some of these user agents refuse to send the Cookie header 1231 in third-party requests. Others refuse to process the Set-Cookie 1232 header in responses to third-party requests. User agents vary widely 1233 in their third-party cookie policies. This document grants user 1234 agents wide latitude to experiment with third-party cookie policies 1235 that balance the privacy and compatibility needs of their users. 1236 However, this document does not endorse any particular third-party 1237 cookie policy. 1239 Third-party cookie blocking policies are often ineffective at 1240 achieving their privacy goals if servers attempt to work around their 1241 restrictions to track users. In particular, two collaborating 1242 servers can often track users without using cookies at all. 1244 7.2. User Controls 1246 User agents should provide users with a mechanism for managing the 1247 cookies stored in the cookie store. For example, a user agent might 1248 let users delete all cookies received during a specified time period 1249 or all the cookies related to a particular domain. In addition, many 1250 user agents include a user interface element that lets users examine 1251 the cookies stored in their cookie store. 1253 User agents should provide users with a mechanism for disabling 1254 cookies. When cookies are disabled, the user agent MUST NOT include 1255 a Cookie header in outbound HTTP requests and the user agent MUST NOT 1256 process Set-Cookie headers in inbound HTTP responses. 1258 Some user agents provide users the option of preventing persistent 1259 storage of cookies across sessions. When configured thusly, user 1260 agents MUST treat all received cookies as if the persistent-flag were 1261 set to false. 1263 Some user agents provide users with the ability to approve individual 1264 writes to the cookie store. In many common usage scenarios, these 1265 controls generate a large number of prompts. However, some privacy- 1266 conscious users find these controls useful nonetheless. 1268 8. Security Considerations 1270 8.1. Overview 1272 Cookies have a number of security pitfalls. This section overviews a 1273 few of the more salient issues. 1275 In particular, cookies encourage developers to rely on ambient 1276 authority for authentication, often becoming vulnerable to attacks 1277 such as cross-site request forgery. Also, when storing session 1278 identifiers in cookies, developers often create session fixation 1279 vulnerabilities. 1281 Transport-layer encryption, such as that employed in HTTPS, is 1282 insufficient to prevent a network attacker from obtaining or altering 1283 a victim's cookies because the cookie protocol itself has various 1284 vulnerabilities (see "Weak Confidentiality" and "Weak Integrity", 1285 below). In addition, by default, cookies do not provide 1286 confidentiality or integrity from network attackers, even when used 1287 in conjunction with HTTPS. 1289 8.2. Ambient Authority 1291 A server that uses cookies to authenticate users can suffer security 1292 vulnerabilities because some user agents let remote parties issue 1293 HTTP requests from the user agent (e.g., via HTTP redirects or HTML 1294 forms). When issuing those requests, user agents attach cookies even 1295 if the remote party does not know the contents of the cookies, 1296 potentially letting the remote party exercise authority at an unwary 1297 server. 1299 Although this security concern goes by a number of names (e.g., 1300 cross-site request forgery, confused deputy), the issue stems from 1301 cookies being a form of ambient authority. Cookies encourage server 1302 operators to separate designation (in the form of URLs) from 1303 authorization (in the form of cookies). Consequently, the user agent 1304 might supply the authorization for a resource designated by the 1305 attacker, possibly causing the server or its clients to undertake 1306 actions designated by the attacker as though they were authorized by 1307 the user. 1309 Instead of using cookies for authorization, server operators might 1310 wish to consider entangling designation and authorization by treating 1311 URLs as capabilities. Instead of storing secrets in cookies, this 1312 approach stores secrets in URLs, requiring the remote entity to 1313 supply the secret itself. Although this approach is not a panacea, 1314 judicious application of these principles can lead to more robust 1315 security. 1317 8.3. Clear Text 1319 Unless sent over a secure channel (such as TLS), the information in 1320 the Cookie and Set-Cookie headers is transmitted in the clear. 1322 1. All sensitive information conveyed in these headers is exposed to 1323 an eavesdropper. 1325 2. A malicious intermediary could alter the headers as they travel 1326 in either direction, with unpredictable results. 1328 3. A malicious client could alter the Cookie header before 1329 transmission, with unpredictable results. 1331 Servers SHOULD encrypt and sign the contents of cookies when 1332 transmitting them to the user agent (even when sending the cookies 1333 over a secure channel). However, encrypting and signing cookie 1334 contents does not prevent an attacker from transplanting a cookie 1335 from one user agent to another or from replaying the cookie at a 1336 later time. 1338 In addition to encrypting and signing the contents of every cookie, 1339 servers that require a higher level of security SHOULD use the Cookie 1340 and Set-Cookie headers only over a secure channel. When using 1341 cookies over a secure channel, servers SHOULD set the Secure 1342 attribute (see Section 4.1.2.5) for every cookie. If a server does 1343 not set the Secure attribute, the protection provided by the secure 1344 channel will be largely moot. 1346 8.4. Session Identifiers 1348 Instead of storing session information directly in a cookie (where it 1349 might be exposed to or replayed by an attacker), servers commonly 1350 store a nonce (or "session identifier") in a cookie. When the server 1351 receives an HTTP request with a nonce, the server can look up state 1352 information associated with the cookie using the nonce as a key. 1354 Using session identifier cookies limits the damage an attacker can 1355 cause if the attacker learns the contents of a cookie because the 1356 nonce is useful only for interacting with the server (unlike non- 1357 nonce cookie content, which might itself be sensitive). Furthermore, 1358 using a single nonce prevents an attacker from "splicing" together 1359 cookie content from two interactions with the server, which could 1360 cause the server to behave unexpectedly. 1362 Using session identifiers is not without risk. For example, the 1363 server SHOULD take care to avoid "session fixation" vulnerabilities. 1364 A session fixation attack proceeds in three steps. First, the 1365 attacker transplants a session identifier from his or her user agent 1366 to the victim's user agent. Second, the victim uses that session 1367 identifier to interact with the server, possibly imbuing the session 1368 identifier with the user's credentials or confidential information. 1369 Third, the attacker uses the session identifier to interact with 1370 server directly, possibly obtaining the user's authority or 1371 confidential information. 1373 8.5. Weak Confidentiality 1375 Cookies do not provide isolation by port. If a cookie is readable by 1376 a service running on one port, the cookie is also readable by a 1377 service running on another port of the same server. If a cookie is 1378 writable by a service on one port, the cookie is also writable by a 1379 service running on another port of the same server. For this reason, 1380 servers SHOULD NOT both run mutually distrusting services on 1381 different ports of the same host and use cookies to store security- 1382 sensitive information. 1384 Cookies do not provide isolation by scheme. Although most commonly 1385 used with the http and https schemes, the cookies for a given host 1386 might also be available to other schemes, such as ftp and gopher. 1387 Although this lack of isolation by scheme is most apparent in non- 1388 HTTP APIs that permit access to cookies (e.g., HTML's document.cookie 1389 API), the lack of isolation by scheme is actually present in 1390 requirements for processing cookies themselves (e.g., consider 1391 retrieving a URI with the gopher scheme via HTTP). 1393 Cookies do not always provide isolation by path. Although the 1394 network-level protocol does not send cookies stored for one path to 1395 another, some user agents expose cookies via non-HTTP APIs, such as 1396 HTML's document.cookie API. Because some of these user agents (e.g., 1397 web browsers) do not isolate resources received from different paths, 1398 a resource retrieved from one path might be able to access cookies 1399 stored for another path. 1401 8.6. Weak Integrity 1403 Cookies do not provide integrity guarantees for sibling domains (and 1404 their subdomains). For example, consider foo.example.com and 1405 bar.example.com. The foo.example.com server can set a cookie with a 1406 Domain attribute of "example.com" (possibly overwriting an existing 1407 "example.com" cookie set by bar.example.com), and the user agent will 1408 include that cookie in HTTP requests to bar.example.com. In the 1409 worst case, bar.example.com will be unable to distinguish this cookie 1410 from a cookie it set itself. The foo.example.com server might be 1411 able to leverage this ability to mount an attack against 1412 bar.example.com. 1414 Even though the Set-Cookie header supports the Path attribute, the 1415 Path attribute does not provide any integrity protection because the 1416 user agent will accept an arbitrary Path attribute in a Set-Cookie 1417 header. For example, an HTTP response to a request for 1418 http://example.com/foo/bar can set a cookie with a Path attribute of 1419 "/qux". Consequently, servers SHOULD NOT both run mutually 1420 distrusting services on different paths of the same host and use 1421 cookies to store security-sensitive information. 1423 An active network attacker can also inject cookies into the Cookie 1424 header sent to https://example.com/ by impersonating a response from 1425 http://example.com/ and injecting a Set-Cookie header. The HTTPS 1426 server at example.com will be unable to distinguish these cookies 1427 from cookies that it set itself in an HTTPS response. An active 1428 network attacker might be able to leverage this ability to mount an 1429 attack against example.com even if example.com uses HTTPS 1430 exclusively. 1432 Servers can partially mitigate these attacks by encrypting and 1433 signing the contents of their cookies. However, using cryptography 1434 does not mitigate the issue completely because an attacker can replay 1435 a cookie he or she received from the authentic example.com server in 1436 the user's session, with unpredictable results. 1438 Finally, an attacker might be able to force the user agent to delete 1439 cookies by storing a large number of cookies. Once the user agent 1440 reaches its storage limit, the user agent will be forced to evict 1441 some cookies. Servers SHOULD NOT rely upon user agents retaining 1442 cookies. 1444 8.7. Reliance on DNS 1446 Cookies rely upon the Domain Name System (DNS) for security. If the 1447 DNS is partially or fully compromised, the cookie protocol might fail 1448 to provide the security properties required by applications. 1450 9. IANA Considerations 1452 The permanent message header field registry (see [RFC3864]) should be 1453 updated with the following registrations: 1455 9.1. Cookie 1457 Header field name: Cookie 1459 Applicable protocol: http 1461 Status: standard 1463 Author/Change controller: IETF 1465 Specification document: this specification (Section 5.4) 1467 9.2. Set-Cookie 1469 Header field name: Set-Cookie 1471 Applicable protocol: http 1473 Status: standard 1475 Author/Change controller: IETF 1477 Specification document: this specification (Section 5.2) 1479 10. References 1481 10.1. Normative References 1483 [RFC1034] Mockapetris, P., "Domain names - concepts and facilities", 1484 STD 13, RFC 1034, November 1987. 1486 [RFC1123] Braden, R., "Requirements for Internet Hosts - Application 1487 and Support", STD 3, RFC 1123, October 1989. 1489 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1490 Requirement Levels", BCP 14, RFC 2119, March 1997. 1492 [RFC2616] Fielding, R., Gettys, J., Mogul, J., Frystyk, H., 1493 Masinter, L., Leach, P., and T. Berners-Lee, "Hypertext 1494 Transfer Protocol -- HTTP/1.1", RFC 2616, June 1999. 1496 [RFC3490] Faltstrom, P., Hoffman, P., and A. Costello, 1497 "Internationalizing Domain Names in Applications (IDNA)", 1498 RFC 3490, March 2003. 1500 See Section 6.3 for an explanation why the normative 1501 reference to an obsoleted specification is needed. 1503 [RFC3629] Yergeau, F., "UTF-8, a transformation format of ISO 1504 10646", STD 63, RFC 3629, November 2003. 1506 [RFC4790] Newman, C., Duerst, M., and A. Gulbrandsen, "Internet 1507 Application Protocol Collation Registry", RFC 4790, 1508 March 2007. 1510 [RFC5234] Crocker, D., Ed. and P. Overell, "Augmented BNF for Syntax 1511 Specifications: ABNF", STD 68, RFC 5234, January 2008. 1513 [RFC5246] Dierks, T. and E. Rescorla, "The Transport Layer Security 1514 (TLS) Protocol Version 1.2", RFC 5246, August 2008. 1516 [RFC5890] Klensin, J., "Internationalized Domain Names for 1517 Applications (IDNA): Definitions and Document Framework", 1518 RFC 5890, August 2010. 1520 [RFC5891] Klensin, J., "Internationalized Domain Names in 1521 Applications (IDNA): Protocol", RFC 5891, August 2010. 1523 10.2. Informative References 1525 [RFC2109] Kristol, D. and L. Montulli, "HTTP State Management 1526 Mechanism", RFC 2109, February 1997. 1528 [RFC2965] Kristol, D. and L. Montulli, "HTTP State Management 1529 Mechanism", RFC 2965, October 2000. 1531 [RFC2818] Rescorla, E., "HTTP Over TLS", RFC 2818, May 2000. 1533 [Netscape] 1534 Netscape Communications Corp., "Persistent Client State -- 1535 HTTP Cookies", 1999, . 1539 [Kri2001] Kristol, D., "HTTP Cookies: Standards, Privacy, and 1540 Politics", ACM Transactions on Internet Technology Vol. 1, 1541 #2, November 2001, . 1543 [RFC3548] Josefsson, S., "The Base16, Base32, and Base64 Data 1544 Encodings", RFC 3548, July 2003. 1546 [RFC3864] Klyne, G., Nottingham, M., and J. Mogul, "Registration 1547 Procedures for Message Header Fields", BCP 90, RFC 3864, 1548 September 2004. 1550 Appendix A. Acknowledgements 1552 This document borrows heavily from RFC 2109 [RFC2109]. We are 1553 indebted to David M. Kristol and Lou Montulli for their efforts to 1554 specify cookies. David M. Kristol, in particular, provided 1555 invaluable advice on navigating the IETF process. We would also like 1556 to thank Thomas Broyer, Tyler Close, Bil Corry, corvid, Lisa 1557 Dusseault, Roy T. Fielding, Blake Frantz, Anne van Kesteren, Eran 1558 Hammer-Lahav, Jeff Hodges, Bjoern Hoehrmann, Achim Hoffmann, Georg 1559 Koppen, Dean McNamee, Mark Miller, Mark Pauley, Yngve N. Pettersen, 1560 Julian Reschke, Peter Saint-Andre, Mark Seaborn, Maciej Stachowiak, 1561 Daniel Stenberg, Tatsuhiro Tsujikawa, David Wagner, Dan Winship, and 1562 Dan Witte for their valuable feedback on this document. 1564 Author's Address 1566 Adam Barth 1567 University of California, Berkeley 1569 Email: abarth@eecs.berkeley.edu 1570 URI: http://www.adambarth.com/