Content-Type Processing Model
University of California, Berkeley
abarth@eecs.berkeley.eduhttp://www.adambarth.com/
Google, Inc.
ian@hixie.chhttp://ln.hixie.ch/Working GroupInternet-DraftMany web servers supply incorrect Content-Type headers with their
HTTP responses. In order to be compatible with these servers, user
agents consider the content of HTTP responses as well as the
Content-Type header when determining the effective media type of the
response. This document describes an algorithm for determining the
effective media type of HTTP responses that balances security and
compatibility considerations.The HTTP Content-Type header indicates the media type of an HTTP
response. However, many HTTP servers supply a Content-Type that does
not match the actual contents of the response. Historically, web
browsers have been tolerated these servers by examining the content of
HTTP responses in addition to the Content-Type header to determine the
effective media type of the response.Without a clear specification of how to "sniff" the media type,
each user agent implementor was forced to reverse engineer the
behavior of the other user agents and to developed their own
algorithm. These divergent algorithms have lead to a lack of
interoperability between user agents and to security issues when the
server intends an HTTP response to be interpreted as one media type
but some user agents interpret the responses as another media
type.These security issues are most severe when an "honest" server lets
potentially malicious users upload files and then serves the contents
of those files with a low-privilege media type (such as text/plain or
image/jpeg). (Malicious servers, of course, can specify an arbitrary
media type in the Content-Type header.) In the absense of mime
sniffing, this user-generated content would not be interpreted as a
high-privilege media type, such as text/html. However, if a user
agent does interpret a low-privilege media type, such as image/gif, as
a high-privilege media type, such as text/html, the user agent as
created a privilege escalation vulnerability in the server. For
example, a malicious user might be able to leverage content sniffing
to mount a cross-site script attack by including JavaScript code in
the uploaded file that a user agent treats as text/html.This document describes a content sniffing algorithm that carefully
balances the compatibility needs of user agent implementors with the
security constraints. The algorithm has been constructed with
reference to content sniffing algorithms present in popular user
agents, an extensive database of existing web content, and metrics
collected from implementations deployed to a sizable number of
users .WARNING! Whenever possible, user agents should avoid employing a
content sniffing algorithm. However, if a user agent does employ a
content sniffing algorithm, the user agent should use the algorithm in
this document exactly because using a different content sniffing
algorithm than servers expect causes security problems. For example,
if a server believes that the client will treat a contributed file as
an image (and thus treat it as benign), but a user agent believes the
content to be HTML (and thus privileged to execute any scripts
contained therein), an attacker might be able to steal the user's
authentication credentials and mount other cross-site scripting
attacks. The explicit Content-Type metadata associated with the resource
(the resource's type information) depends on the protocol that was
used to fetch the resource.For HTTP resources, only the last Content-Type HTTP header, if
any, contributes any type information; the official type of the
resource is then the value of that header, interpreted as described by
the HTTP specifications. If the Content-Type HTTP header is present
but the value of the last such header cannot be interpreted as
described by the HTTP specifications (e.g. because its value doesn't
contain a U+002F SOLIDUS ('/') character), then the resource has no
type information (even if there are multiple Content-Type HTTP headers
and one of the other ones is syntactically correct).For resources fetched from the file system, user agents should use
platform-specific conventions, e.g. operating system file
extension/type mappings.
Note: It is essential that file extensions are not used for
determining the media type for resources fetched over HTTP because
file extensions can often by supplied by malicious parties.For resources fetched over most other protocols, e.g. FTP, there is
no type information.The algorithm for extracting an encoding from a Content-Type, given
a string s, is as follows. It either returns an encoding or nothing.
Find the first seven characters in s that are an ASCII
case-insensitive match for the word "charset". If no such match is
found, return nothing.Skip any U+0009, U+000A, U+000C, U+000D, or U+0020 characters
that immediately follow the word 'charset' (there might not be
any).If the next character is not a U+003D EQUALS SIGN ('='), return
nothing.Skip any U+0009, U+000A, U+000C, U+000D, or U+0020 characters
that immediately follow the equals sign (there might not be
any).Process the next character as follows:
If it is a U+0022 QUOTATION MARK ('"') and there is a later
U+0022 QUOTATION MARK ('"') in s, orIf it is a U+0027 APOSTROPHE ("'") and there is a later U+0027
APOSTROPHE ("'") in s
Return the string between this character and the next
earliest occurrence of this character.If it is an unmatched U+0022 QUOTATION MARK ('"'),If it is an unmatched U+0027 APOSTROPHE ("'"), orIf there is no next character
Return nothing.Otherwise
Return the string from this character to the first U+0009,
U+000A, U+000C, U+000D, U+0020, or U+003B character or the end
of s, whichever comes first.Note: The above algorithm is a willful violation of the HTTP
specification. [RFC2616]The /sniffed type/ of a resource is found as follows:
Let /official type/ be the type given by the Content-Type metadata
for the resource, ignoring parameters. Comparisons with this type, as
defined by MIME specifications, are done in an ASCII case-insensitive
manner. [RFC2046]If the user agent is configured to strictly obey Content-Type
headers for this resource, then jump to the last step in this set of
steps.If the resource was fetched over an HTTP protocol and there is an
HTTP Content-Type header and the value of the last such header has
bytes that exactly match one of the following lines:
If there is no /official type/, jump to the unknown type step
below.If /official type/ is "unknown/unknown", "application/unknown", or
"*/*", jump to the unknown type step below.If /official type/ ends in "+xml", or if it is either "text/xml" or
"application/xml", then the /sniffed type/ of the resource is
/official type/; return that and abort these steps.If /official type/ is an image type supported by the user agent
(e.g. "image/png", "image/gif", "image/jpeg", etc), then jump to the
"images" section below, passing it the /official type/.If /official type/ is "text/html", then jump to the feed or HTML
section below.The /sniffed type/ of the resource is /official type/.The user agent MAY wait for 512 or more bytes of the resource to
be available.Let n be the smaller of either 512 or the number of bytes already
available.If n is greater than or equal to 3, and the first 2 or 3 bytes of
the resource match one of the following byte sequences:
If none of the first n bytes of the resource are binary data
bytes then the /sniffed type/ of the resource is "text/plain". Abort
these steps.
If the first bytes of the resource match one of the byte
sequences in the "pattern" column of the table in the unknown type
section below, ignoring any rows whose cell in the "security" column
says "scriptable" (or "n/a"), then the /sniffed type/ of the resource
is the type given in the corresponding cell in the "sniffed type"
column on that row; abort these steps.
WARNING! It is critical that this step not ever return a
scriptable type (e.g. text/html), as otherwise that would allow a
privilege escalation attack.Otherwise, the /sniffed type/ of the resource is
"application/octet-stream".The user agent MAY wait for 512 or more bytes of the resource to
be available.Let /stream length/ be the smaller of either 512 or the number of
bytes already available.For each row in the table below:
If the row has no "WS" bytes:
Let /pattern length/ be the length of the pattern (number of
bytes described by the cell in the second column of the
row).If /stream length/ is smaller than /pattern length/ then skip
this row.Apply the "and" operator to the first /pattern length/ bytes of
the resource and the given mask (the bytes in the cell of first
column of that row), and let the result be the data.If the bytes of the data matches the given pattern bytes
exactly, then the /sniffed type/ of the resource is the type given
in the cell of the third column in that row; abort these
steps.If the row has a "WS" byte:
Let /index pattern/ be an index into the mask and pattern
byte strings of the row.Let /index stream/ be an index into the byte stream being
examined.Loop: If /index stream/ points beyond the end of the byte
stream, then this row doesn't match, skip this row.Examine the /index stream/th byte of the byte stream as
follows:
If the /index pattern/th byte of the pattern is a normal
hexadecimal byte and not a "WS" byte:
If the "and" operator, applied to the /index stream/th
byte of the stream and the /index pattern/th byte of the
mask, yield a value different that the /index pattern/th
byte of the pattern, then skip this row.Otherwise, increment /index pattern/ to the next byte in
the mask and pattern and /index stream/ to the next byte in
the byte stream.Otherwise, if the /index pattern/th byte of the pattern is a
"WS" byte:
"WS" means "whitespace", and allows insignificant
whitespace to be skipped when sniffing for a type
signature.If the /index stream/th byte of the stream is one of 0x09
(ASCII TAB), 0x0A (ASCII LF), 0x0C (ASCII FF), 0x0D (ASCII
CR), or 0x20 (ASCII space), then increment only the /index
stream/ to the next byte in the byte stream.Otherwise, increment only the /index pattern/ to the next
byte in the mask and pattern.If /index pattern/ does not point beyond the end of the mask
and pattern byte strings, then jump back to the loop step in
this algorithm.Otherwise, the /sniffed type/ of the resource is the type given
in the cell of the third column in that row; abort these
steps.If none of the first n bytes of the resource are binary data
bytes then the sniffed type of the resource is "text/plain". Abort
these steps.Otherwise, the sniffed type of the resource is
"application/octet-stream".The table used by the above algorithm is:
User agents may support additional types if desired, by implicitly
adding to the above table. However, user agents should not use any
other patterns for types already mentioned in the table above because
this could then be used for privilege escalation (where, e.g., a server
uses the above table to determine that content is not HTML and thus
safe from cross-site scriping attacks, but then a user agent detects
it as HTML anyway and allows script to execute).The column marked "security" is used by the algorithm in the "text
or binary" section, to avoid sniffing text/plain content as a type
that can be used for a privilege escalation attack.If the resource's /official type/ is "image/svg+xml", then the
/sniffed type/ of the resource is its /official type/ (an XML
type).Otherwise, if the first bytes of the resource match one of the byte
sequences in the first column of the following table, then the
/sniffed type/ of the resource is the type given in the corresponding
cell in the second column on the same row:
Otherwise, the /sniffed type/ of the resource is the same as its
/official type/.The user agent MAY wait for 512 or more bytes of the resource to
be available.Let s be the stream of bytes, and let s[i] represent the byte in
s with position i, treating s as zero-indexed (so the first byte is
at i=0).If at any point this algorithm requires the user agent to
determine the value of a byte in s which is not yet available, or
which is past the first 512 bytes of the resource, or which is
beyond the end of the resource, the algorithm stops and the /sniffed
type/ of the resource is "text/html".
Note: User agents are allowed, by the first step of this
algorithm, to wait until the first 512 bytes of the resource are
available.Initialize pos to 0.If s[0] equals 0xEF, s[1] equals 0xBB, and s[2] equals 0xBF, then
set pos to 3. (This skips over a leading UTF-8 BOM, if any.)Loop start: Examine s[pos].
If it equals 0x09 (ASCII tab), 0x20 (ASCII space), 0x0A (ASCII
LF), or 0x0D (ASCII CR)
Increase pos by 1 and repeat this step.If it equals 0x3C (ASCII "<")
Increase pos by 1 and go to the next step.If it is anything else
The sniffed type of the resource is "text/html". Abort these
steps.If the bytes with positions pos to pos+2 in s are exactly equal
to 0x21, 0x2D, 0x2D respectively (ASCII for "!--"), then:
Increase pos by 3.If the bytes with positions pos to pos+2 in s are exactly equal
to 0x2D, 0x2D, 0x3E respectively (ASCII for "-->"), then increase
pos by 3 and jump back to the previous step (the step labeled loop
start) in the overall algorithm in this section.Otherwise, increase pos by 1.Return to step 2 in these substeps.If s[pos] equals 0x21 (ASCII "!"):
Increase pos by 1.If s[pos] equals 0x3E, then increase pos by 1 and jump back to
the step labeled loop start in the overall algorithm in this
section.Otherwise, return to step 1 in these substeps.If s[pos] equals 0x3F (ASCII "?"):
Increase pos by 1.If s[pos] and s[pos+1] equal 0x3F and 0x3E respectively, then
increase pos by 1 and jump back to the step labeled loop start in
the overall algorithm in this section.Otherwise, return to step 1 in these substeps.Otherwise, if the bytes in s starting at pos match any of the
sequences of bytes in the first column of the following table, then
the user agent must follow the steps given in the corresponding cell
in the second column of the same row.
Initialize /RDF flag/ to 0.Initialize /RSS flag/ to 0.If the bytes with positions pos to pos+23 in s are exactly equal to
0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x70, 0x75, 0x72, 0x6C, 0x2E,
0x6F, 0x72, 0x67, 0x2F, 0x72, 0x73, 0x73, 0x2F, 0x31, 0x2E, 0x30, 0x2F
respectively (ASCII for "http://purl.org/rss/1.0/"), then:
Increase pos by 23.Set /RSS flag/ to 1.If the bytes with positions pos to pos+42 in s are exactly equal to
0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x77, 0x77, 0x77, 0x2E, 0x77,
0x33, 0x2E, 0x6F, 0x72, 0x67, 0x2F, 0x31, 0x39, 0x39, 0x39, 0x2F, 0x30,
0x32, 0x2F, 0x32, 0x32, 0x2D, 0x72, 0x64, 0x66, 0x2D, 0x73, 0x79, 0x6E,
0x74, 0x61, 0x78, 0x2D, 0x6E, 0x73, 0x23 respectively (ASCII for
"http://www.w3.org/1999/02/22-rdf-syntax-ns#"), then:
Increase pos by 42.Set /RDF flag/ to 1.Increase pos by 1.If /RDF flag/ is 1 and /RSS flag/ is 1, then the /sniffed type/ of
the resource is "application/rss+xml". Abort these steps.If pos points beyond the end of the byte stream s, then continue to
step 19 of this algorithm.Jump back to step 13 of this algorithm.The /sniffed type/ of the resource is "text/html".For efficiency reasons, implementations may wish to implement this
algorithm and the algorithm for detecting the character encoding of
HTML documents in parallel.Secure Content Sniffing for Web Browsers, or How to Stop
Papers from Reviewing ThemselvesUC BerkeleyUC Berkeley and CMUUC BerkeleyTODO:
* Transcribe the tables into C and auto generate the tables.
* Investigate charset parsing.