idnits 2.17.1 draft-abarth-mime-sniff-05.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- ** You're using the IETF Trust Provisions' Section 6.b License Notice from 12 Sep 2009 rather than the newer Notice from 28 Dec 2009. (See https://trustee.ietf.org/license-info/) Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- == No 'Intended status' indicated for this document; assuming Proposed Standard Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- ** The document seems to lack a Security Considerations section. ** The document seems to lack an IANA Considerations section. (See Section 2.2 of https://www.ietf.org/id-info/checklist for how to handle the case when there are no actions for IANA.) ** The document seems to lack separate sections for Informative/Normative References. All references will be assumed normative when checking for downward references. ** The document seems to lack a both a reference to RFC 2119 and the recommended RFC 2119 boilerplate, even if it appears to use RFC 2119 keywords. RFC 2119 keyword, line 113: '...ble, user agents SHOULD NOT employ a c...' RFC 2119 keyword, line 115: '..., the user agent SHOULD use the algori...' RFC 2119 keyword, line 125: '...ed as algorithms or specific steps MAY...' RFC 2119 keyword, line 145: '..., the user agent MUST use the textuall...' RFC 2119 keyword, line 173: '... The user agent MUST use the followin...' (8 more instances...) Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document date (May 4, 2010) is 5099 days in the past. Is this intentional? -- Found something which looks like a code comment -- if you have code sections in the document, please surround them with '' and '' lines. Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) == Missing Reference: 'RFC2046' is mentioned on line 169, but not defined -- Looks like a reference, but probably isn't: '0' on line 558 -- Looks like a reference, but probably isn't: '1' on line 558 -- Looks like a reference, but probably isn't: '2' on line 558 -- Possible downref: Non-RFC (?) normative reference: ref. 'BarthCaballeroSong2009' Summary: 5 errors (**), 0 flaws (~~), 3 warnings (==), 6 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 None A. Barth 3 Internet-Draft U.C. Berkeley 4 Expires: November 5, 2010 I. Hickson 5 Google, Inc. 6 May 4, 2010 8 Media Type Sniffing 9 draft-abarth-mime-sniff-05 11 Abstract 13 Many web servers supply incorrect Content-Type header fields with 14 their HTTP responses. In order to be compatible with these servers, 15 user agents consider the content of HTTP responses as well as the 16 Content-Type header fields when determining the effective media type 17 of the response. This document describes an algorithm for 18 determining the effective media type of HTTP responses that balances 19 security and compatibility considerations. 21 Please send feedback on this draft to apps-discuss@ietf.org. 23 Status of this Memo 25 This Internet-Draft is submitted to IETF in full conformance with the 26 provisions of BCP 78 and BCP 79. 28 Internet-Drafts are working documents of the Internet Engineering 29 Task Force (IETF), its areas, and its working groups. Note that 30 other groups may also distribute working documents as Internet- 31 Drafts. 33 Internet-Drafts are draft documents valid for a maximum of six months 34 and may be updated, replaced, or obsoleted by other documents at any 35 time. It is inappropriate to use Internet-Drafts as reference 36 material or to cite them other than as "work in progress." 38 The list of current Internet-Drafts can be accessed at 39 http://www.ietf.org/ietf/1id-abstracts.txt. 41 The list of Internet-Draft Shadow Directories can be accessed at 42 http://www.ietf.org/shadow.html. 44 This Internet-Draft will expire on November 5, 2010. 46 Copyright Notice 48 Copyright (c) 2010 IETF Trust and the persons identified as the 49 document authors. All rights reserved. 51 This document is subject to BCP 78 and the IETF Trust's Legal 52 Provisions Relating to IETF Documents 53 (http://trustee.ietf.org/license-info) in effect on the date of 54 publication of this document. Please review these documents 55 carefully, as they describe your rights and restrictions with respect 56 to this document. Code Components extracted from this document must 57 include Simplified BSD License text as described in Section 4.e of 58 the Trust Legal Provisions and are provided without warranty as 59 described in the BSD License. 61 Table of Contents 63 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 3 64 2. Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 65 3. Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . 6 66 4. Text or Binary . . . . . . . . . . . . . . . . . . . . . . . . 8 67 5. Unknown Type . . . . . . . . . . . . . . . . . . . . . . . . . 10 68 6. Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 69 7. Feed or HTML . . . . . . . . . . . . . . . . . . . . . . . . . 16 70 8. References . . . . . . . . . . . . . . . . . . . . . . . . . . 19 71 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . . 20 73 1. Introduction 75 The HTTP Content-Type header field indicates the media type of an 76 HTTP response. However, many HTTP servers supply a Content-Type that 77 does not match the actual contents of the response. Historically, 78 web browsers have tolerated these servers by examining the content of 79 HTTP responses in addition to the Content-Type header field to 80 determine the effective media type of the response. 82 Without a clear specification of how to "sniff" the media type, each 83 user agent implementor was forced to reverse engineer the behavior of 84 the other user agents and to develop their own algorithm. These 85 divergent algorithms have lead to a lack of interoperability between 86 user agents and to security issues when the server intends an HTTP 87 response to be interpreted as one media type but some user agents 88 interpret the responses as another media type. 90 These security issues are most severe when an "honest" server lets 91 potentially malicious users upload files and then serves the contents 92 of those files with a low-privilege media type (such as text/plain or 93 image/jpeg). (Malicious servers, of course, can specify an arbitrary 94 media type in the Content-Type header field.) In the absence of 95 media type sniffing, this user-generated content would not be 96 interpreted as a high-privilege media type, such as text/html. 97 However, if a user agent does interpret a low-privilege media type, 98 such as image/gif, as a high-privilege media type, such as text/html, 99 the user agent has created a privilege escalation vulnerability in 100 the server. For example, a malicious user might be able to leverage 101 content sniffing to mount a cross-site script attack by including 102 JavaScript code in the uploaded file that a user agent treats as 103 text/html. 105 This document describes a content sniffing algorithm that carefully 106 balances the compatibility needs of user agent implementors with the 107 security constraints. The algorithm has been constructed with 108 reference to content sniffing algorithms present in popular user 109 agents, an extensive database of existing web content, and metrics 110 collected from implementations deployed to a sizable number of users 111 [BarthCaballeroSong2009]. 113 WARNING! Whenever possible, user agents SHOULD NOT employ a content 114 sniffing algorithm. However, if a user agent does employ a content 115 sniffing algorithm, the user agent SHOULD use the algorithm in this 116 document because using a different content sniffing algorithm than 117 servers expect causes security problems. For example, if a server 118 believes that the client will treat a contributed file as an image 119 (and thus treat it as benign), but a user agent believes the content 120 to be HTML (and thus privileged to execute any scripts contained 121 therein), an attacker might be able to steal the user's 122 authentication credentials and mount other cross-site scripting 123 attacks. 125 Conformance requirements phrased as algorithms or specific steps MAY 126 be implemented in any manner, so long as the end result is 127 equivalent. (In particular, the algorithms defined in this 128 specification are intended to be easy to follow, and not intended to 129 be performant.) 131 2. Metadata 133 The explicit media type metadata information associated with sequence 134 of octets depends on the protocol that was used to fetch the octets. 136 For octets received via HTTP, the Content-Type HTTP header field, if 137 present, indicates the media type. Let the official-type be the 138 media type indicted by the HTTP Content-Type header field, if 139 present. If the Content-Type header field is absent or if its value 140 cannot be interpreted as a media type (e.g. because its value doesn't 141 contain a U+002F SOLIDUS ('/') character), then there is no official- 142 type. 144 Note: If an HTTP response contains multiple Content-Type header 145 fields, the user agent MUST use the textually last Content-Type 146 header field to the official-type. For example, if the last 147 Content-Type header field contains the value "foo", then there is 148 no official media type because "foo" cannot be interpreted as a 149 media type (even if the HTTP response contains another Content- 150 Type header field that could be interpreted as a media type). 152 For octets fetched from the file system, user agents should use 153 platform-specific conventions (e.g., operating system file extension/ 154 type mappings) to determine the official-type. 156 Note: It is essential that file extensions are not used for 157 determining the media type for octets fetched over HTTP because, 158 in some cases, file extensions can be supplied by malicious 159 parties. For example, most PHP installations let the attacker 160 append arbitrary path information to URLs (e.g., 161 http://example.com/foo.php/bar.html) and thereby determine the 162 file extension. 164 For octets fetched over some other protocols, e.g. FTP, there is no 165 type information. 167 Note: Comparisons between media types, as defined by MIME 168 specifications, are done in an ASCII case-insensitive manner. 169 [RFC2046] 171 3. Web Pages 173 The user agent MUST use the following algorithm to determine the 174 sniffed-type of a sequence of octets: 176 1. If the user agent is configured to strictly obey the official- 177 type, then let the sniffed-type be the official-type and abort 178 these steps. 180 2. If the octets were fetched via HTTP and there is an HTTP Content- 181 Type header field and the value of the last such header field has 182 octets that *exactly* match the octets contained in one of the 183 following lines: 185 +-------------------------------+--------------------------------+ 186 | Bytes in Hexadecimal | Textual Representation | 187 +-------------------------------+--------------------------------+ 188 | 74 65 78 74 2f 70 6c 61 69 6e | text/plain | 189 +-------------------------------+--------------------------------+ 190 | 74 65 78 74 2f 70 6c 61 69 6e | text/plain; charset=ISO-8859-1 | 191 | 3b 20 63 68 61 72 73 65 74 3d | | 192 | 49 53 4f 2d 38 38 35 39 2d 31 | | 193 +-------------------------------+--------------------------------+ 194 | 74 65 78 74 2f 70 6c 61 69 6e | text/plain; charset=iso-8859-1 | 195 | 3b 20 63 68 61 72 73 65 74 3d | | 196 | 69 73 6f 2d 38 38 35 39 2d 31 | | 197 +-------------------------------+--------------------------------+ 198 | 74 65 78 74 2f 70 6c 61 69 6e | text/plain; charset=UTF-8 | 199 | 3b 20 63 68 61 72 73 65 74 3d | | 200 | 55 54 46 2d 38 | | 201 +-------------------------------+--------------------------------+ 203 ...then jump to the "text or binary" section below. 205 3. If there is no official-type, jump to the "unknown type" section 206 below. 208 4. If the official-type is "unknown/unknown", "application/unknown", 209 or "*/*", jump to the "unknown" type section below. 211 5. If the official-type ends in "+xml", or if it is either "text/ 212 xml" or "application/xml", then let the sniffed-type be the 213 official-type and abort these steps. 215 6. If the official-type is an image type supported by the user agent 216 (e.g., "image/png", "image/gif", "image/jpeg", etc), then jump to 217 the "images" section below. 219 7. If the official-type is "text/html", then jump to the "feed or 220 HTML" section below. 222 8. Let the sniffed-type be the official type. 224 4. Text or Binary 226 This section defines the *rules for distingushing if a resource is 227 text or binary*. 229 1. The user agent MAY wait for 512 or more octets be to arrive. 231 Note: Waiting for 512 octets octets to arrive causes the text- 232 or-binary algorithm to be deterministic for a given sequence 233 of octets. However, in some cases, the user agent might need 234 to wait an arbitrary length of time for these octets to 235 arrive. User agents SHOULD wait for 512 octets to arrive, 236 when feasible. 238 2. Let n be the smaller of either 512 or the number of octets that 239 have already arrived. 241 3. If n is greater than or equal to 3, and the first 2 or 3 octets 242 match one of the following octet sequences: 244 +----------------------+--------------+ 245 | Bytes in Hexadecimal | Description | 246 +----------------------+--------------+ 247 | FE FF | UTF-16BE BOM | 248 | FF FE | UTF-16LE BOM | 249 | EF BB BF | UTF-8 BOM | 250 +----------------------+--------------+ 252 ...then let the sniffed-type be "text/plain" and abort these 253 steps. 255 4. If none of the first n octets are binary data octets then let the 256 sniffed-type be "text/plain" and abort these steps. 258 +-------------------------+ 259 | Binary Data Byte Ranges | 260 +-------------------------+ 261 | 0x00 -- 0x08 | 262 | 0x0B | 263 | 0x0E -- 0x1A | 264 | 0x1C -- 0x1F | 265 +-------------------------+ 267 5. If the first octets match one of the octet sequences in the 268 "pattern" column of the table in the "unknown type" section 269 below, ignoring any rows whose cell in the "security" column says 270 "scriptable" (or "n/a"), then let the sniffed-type be the type 271 given in the corresponding cell in the "sniffed type" column on 272 that row and abort these steps. 274 WARNING! It is critical that this step not ever return a 275 scriptable type (e.g., text/html), because otherwise that 276 would allow a privilege escalation attack. 278 6. Otherwise, let the sniffed-type be "application/octet-stream" and 279 abort these steps. 281 5. Unknown Type 283 1. The user agent MAY wait for 512 or more octets to arrive for the 284 same reason as in the "text or binary" section above. 286 2. Let n be the smaller of either 512 or the number of octets that 287 have already arrived. 289 3. For each row in the table below: 291 * If the row has no "WS" octets: 293 1. Let pattern-length be the length of the pattern. 295 2. If n is smaller than pattern-length then skip this row. 297 3. Apply the bit-wise "and" operator to the first pattern- 298 length octets and the given mask, and let the result be 299 the masked-data. 301 4. If the octets of the masked-data matches the given pattern 302 octets exactly, then let the sniffed-type be the type 303 given in the cell of the third column in that row and 304 abort these steps. 306 * If the row has a "WS" octet or a "_>" octet: 308 1. Let index-pattern be an index into the mask and pattern 309 octet strings of the row. 311 2. Let index-stream be an index into the octet stream being 312 examined. 314 3. LOOP: If index-stream points beyond the end of the octet 315 stream, then this row doesn't match and skip this row. 317 4. Examine the index-stream-th octet of the octet stream as 318 follows: 320 - If the index-pattern-th octet of the pattern is a 321 normal hexadecimal octet and not a "WS" octet or a "SB" 322 octet: 324 If the bit-wise "and" operator, applied to the 325 index-stream-th octet of the stream and the index- 326 pattern-th octet of the mask, yield a value 327 different than the index-pattern-th octet of the 328 pattern, then skip this row. 330 Otherwise, increment index-pattern to the next octet 331 in the mask and pattern and index-stream to the next 332 octet in the octet stream. 334 - Otherwise, if the index-pattern-th octet of the pattern 335 is a "WS" octet: 337 "WS" means "whitespace", and allows insignificant 338 whitespace to be skipped when sniffing for a type 339 signature. 341 If the index-stream-th octet of the stream is one of 342 0x09 (ASCII TAB), 0x0A (ASCII LF), 0x0C (ASCII FF), 343 0x0D (ASCII CR), or 0x20 (ASCII space), then 344 increment only the index-stream to the next octet in 345 the octet stream. 347 Otherwise, increment only the index-pattern to the 348 next octet in the mask and pattern. 350 - Otherwise, if the index-pattern-th octet of the pattern 351 is a "_>" octet: 353 "_>" means "space-or-bracket", and allows HTML tag 354 names to terminate with either a space or a greater 355 than sign. 357 If index-stream-th octet of the stream different 358 than 0x20 (ASCII space) or 0x3E (ASCII ">"), then 359 skip this row. 361 Otherwise, increment index-pattern to the next octet 362 in the mask and pattern and index-stream to the next 363 octet in the octet stream. 365 5. If index-pattern does not point beyond the end of the mask 366 and pattern octet strings, then jump back to the LOOP step 367 in this algorithm. 369 6. Otherwise, let the sniffed-type be the type given in the 370 cell of the third column in that row and abort these 371 steps. 373 4. If none of the first n octets are binary data (as defined in the 374 "text or binary" section), then let the sniffed-type be "text/ 375 plain" and abort these steps. 377 5. Otherwise, let the sniffed-type be "application/octet-stream" and 378 abort these steps. 380 The table used by the above algorithm is: 382 +-------------------+-------------------+-----------------+------------+ 383 | Mask in Hex | Pattern in Hex | Sniffed Type | Security | 384 +-------------------+-------------------+-----------------+------------+ 385 | FF FF FF DF DF DF | WS 3C 21 44 4F 43 | text/html | Scriptable | 386 | DF DF DF DF FF DF | 54 59 50 45 20 48 | | | 387 | DF DF DF FF | 54 4D 4C _> | | | 388 | Comment: | | | 392 | Comment: | | | 396 | Comment: | | | 400 | Comment: