forked from whatwg/mimesniff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mimesniff.bs
2979 lines (2126 loc) · 78.7 KB
/
mimesniff.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class=metadata>
Group: WHATWG
H1: MIME Sniffing
Shortname: mimesniff
Text Macro: TWITTER mimesniff
Abstract: The MIME Sniffing standard defines sniffing resources.
!Participate: <a href="https://www.w3.org/Bugs/Public/buglist.cgi?product=WHATWG&component=MIME&resolution=---">legacy open bugs</a>
Translation: ja https://triple-underscore.github.io/mimesniff-ja.html
Markup Shorthands: css off
</pre>
<pre class=biblio>
{
"FTP": {
"aliasOf": "RFC959"
},
"HTTP": {
"aliasOf": "HTTP11"
},
"MIMETYPE": {
"aliasOf": "RFC2046"
},
"SECCONTSNIFF": {
"authors": ["Adam Barth", "Juan Caballero", "Dawn Song"],
"href": "https://www.adambarth.com/papers/2009/barth-caballero-song.pdf",
"title": "Secure Content Sniffing for Web Browsers, or How to Stop Papers from Reviewing Themselves"
}
}
</pre>
<pre class=anchors>
url:https://tools.ietf.org/html/rfc7230#section-3.2.6;text:token;type:dfn;spec:http
url:https://tools.ietf.org/html/rfc7230#section-3.2.6;text:quoted-string;type:dfn;spec:http
url:https://tools.ietf.org/html/rfc7231#section-3.1.1.1;text:media-type;type:dfn;spec:http
# https://github.com/tabatkins/bikeshed/issues/1180
url:https://html.spec.whatwg.org/multipage/scripting.html#attr-script-type;text:type;for:script;type:element-attr;spec:html
</pre>
<pre class=link-defaults>
spec:infra; type:dfn; text:string
spec:html; type:element; text:script
</pre>
<h2 id=introduction>Introduction</h2>
<p>
The HTTP <code>Content-Type</code> header field is intended to indicate the
MIME type of an HTTP response.
However, many HTTP servers supply a <code>Content-Type</code> header field
value that does not match the actual contents of the response.
Historically, web browsers have tolerated these servers by examining the
content of HTTP responses in addition to the <code>Content-Type</code> header
field in order to determine the effective MIME type of the response.
<p>
Without a clear specification for how to "sniff" the MIME type, each user
agent has been forced to reverse-engineer the algorithms of other user agents
in order to maintain interoperability.
Inevitably, these efforts have not been entirely successful, resulting in
divergent behaviors among user agents.
In some cases, these divergent behaviors have had security implications, as a
user agent could interpret an HTTP response as a different MIME type than
the server intended.
<p>
These security issues are most severe when an "honest" server allows
potentially malicious users to upload their own files and then serves the
contents of those files with a low-privilege MIME type.
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.
(Malicious servers, of course, can specify an arbitrary MIME type in the
<code>Content-Type</code> header field.)
<p>
This document describes a content sniffing algorithm that carefully balances
the compatibility needs of user agent with the security constraints imposed
by existing web content.
The algorithm originated from research conducted by Adam Barth, Juan
Caballero, and Dawn Song, based on 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.
[[!SECCONTSNIFF]]
<h2 id=conformance-requirements>Conformance requirements</h2>
<p>
The keywords "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
"SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this
document are to be interpreted as described in RFC 2119.
For readability, these keywords will generally not appear in all uppercase
letters.
[[!RFC2119]]
<p>
Requirements phrased in the imperative as part of algorithms (such as "strip
any leading space characters" or "return false and abort these steps") are to
be interpreted with the meaning of the keyword used in introducing the
algorithm.
<p>
Conformance requirements phrased as algorithms or specific steps can be
implemented in any manner, so long as the end result is equivalent.
In particular, note that the algorithms defined in this specification are
intended to be easy to understand and are not intended to be performant.
<h2 id=terminology>Terminology</h2>
<p>
This specification depends on the Infra Standard. [[!INFRA]]
<p>An <dfn>HTTP token code point</dfn> is U+0021 (!), U+0023 (#), U+0024 ($), U+0025 (%),
U+0026 (&), U+0027 ('), U+002A (*), U+002B (+), U+002D (-), U+002E (.), U+005E (^), U+005F (_),
U+0060 (`), U+007C (|), U+007E (~), or an <a>ASCII alphanumeric</a>.</p>
<p class=note>This matches the value space of the <a spec=http>token</a> token production. [[HTTP]]
<p>An <dfn>HTTP quoted-string token code point</dfn> is U+0009 TAB, a <a>code point</a> in the range
U+0020 SPACE to U+007E (~), inclusive, or a <a>code point</a> in the range U+0080 through
U+00FF (ÿ), inclusive.
<p class=note>This matches the effective value space of the <a spec=http>quoted-string</a> token
production. By definition it is a superset of the <a>HTTP token code points</a>. [[HTTP]]
<p>
A <dfn>binary data byte</dfn> is a <a>byte</a> in the range 0x00 to
0x08 (NUL to BS), the <a>byte</a> 0x0B (VT), a <a>byte</a> in the
range 0x0E to 0x1A (SO to SUB), or a <a>byte</a> in the range 0x1C to
0x1F (FS to US).
<p>
A <dfn>whitespace byte</dfn> (abbreviated
<abbr lt="whitespace byte">0xWS</abbr>) is any one of the following
<a>bytes</a>: 0x09 (HT), 0x0A (LF), 0x0C (FF), 0x0D (CR),
0x20 (SP).
<p>
A <dfn>tag-terminating byte</dfn> (abbreviated
<abbr lt="tag terminating byte">0xTT</abbr>) is any one of the following
<a>bytes</a>: 0x20 (SP), 0x3E ("<code>></code>").
<p>
Equations are using the mathematical operators as defined in
[[!ENCODING]]. In addition, the bitwise NOT is
represented by ~.
<h2 id=understanding-mime-types>MIME types</h2>
<h3 id=mime-type-representation>MIME type representation</h3>
<p>A <dfn export lt="MIME type|MIME type record" id=mime-type>MIME type</dfn> represents an
<i>internet media type</i> as defined by
<cite>Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types</cite>. It can also be
referred to as a <a>MIME type record</a>. [[!MIMETYPE]]
<p class=note>Standards are encouraged to consistently use the term <a>MIME type</a> to avoid
confusion with the use of <i>media type</i> as described in <cite>Media Queries</cite>.
[[MEDIAQUERIES]]
<p>A <a>MIME type</a>'s <dfn export for="MIME type" id=type>type</dfn> is a non-empty
<a>ASCII string</a>.
<p>A <a>MIME type</a>'s <dfn export for="MIME type" id=subtype>subtype</dfn> is a non-empty
<a>ASCII string</a>.
<p>A <a>MIME type</a>'s <dfn export for="MIME type" id=parameters>parameters</dfn> is an
<a>ordered map</a> whose <a for=map>keys</a> and <a for=map>values</a> are <a>ASCII strings</a>. It
is initially empty.
<h3 id=mime-type-miscellaneous>MIME type miscellaneous</h3>
<p>The <dfn export for="MIME type">essence</dfn> of a <a>MIME type</a> <var>mimeType</var> is
<var>mimeType</var>'s <a for="MIME type">type</a>, followed by U+002F (/), followed by
<var>mimeType</var>'s <a for="MIME type">subtype</a>.
<p>A <a>MIME type</a> is <dfn export>supported by the user agent</dfn> if the user agent has the
capability to interpret a <a>resource</a> of that <a>MIME type</a> and present it to the user.
<p class=XXX>This needs more work. See
<a href=https://github.com/w3c/preload/issues/113>w3c/preload #113</a>.
<h3 id=mime-type-writing>MIME type writing</h3>
<p>A <dfn export id=valid-mime-type>valid MIME type string</dfn> is a string that matches the
<a spec=http>media-type</a> token production. In particular, a <a>valid MIME type string</a> may
include <a for="MIME type">parameters</a>. [[!RFC7231]]
<p class=note>A <a>valid MIME type string</a> is supposed to be used for conformance checkers only.
<div class=example id=example-valid-mime-type-string>
<p>"<code>text/html</code>" is a <a>valid MIME type string</a>.
<p>"<code>text/html;</code>" is not a <a>valid MIME type string</a>, though
<a>parse a MIME type</a> returns a <a>MIME type record</a> for it identical to if the input had
been "<code>text/html</code>".
</div>
<p>A
<dfn export id=valid-mime-type-with-no-parameters>valid MIME type string with no parameters</dfn> is
a <a>valid MIME type string</a> that does not contain U+003B (;).
<h3 id=parsing-a-mime-type>Parsing a MIME type</h3>
<p>To <dfn export>parse a MIME type</dfn>, given a string <var>input</var>, run these steps:
<ol>
<li><p>Remove any leading and trailing <a>ASCII whitespace</a> from <var>input</var>.
<li><p>Let <var>position</var> be a <a for=string>position variable</a> for <var>input</var>,
initially pointing at the start of <var>input</var>.
<li><p>Let <var>type</var> be the result of <a>collecting a sequence of code points</a> that are
not U+002F (/) from <var>input</var>, given <var>position</var>.
<li><p>If <var>type</var> is the empty string or does not solely contain
<a>HTTP token code points</a>, then return failure.
<li><p>If <var>position</var> is past the end of <var>input</var>, then return failure.
<li><p>Advance <var>position</var> to the next <a>code point</a> in <var>input</var>. (This skips
past U+002F (/).)
<li><p>Let <var>subtype</var> be the result of <a>collecting a sequence of code points</a> that are
not U+003B (;) from <var>input</var>, given <var>position</var>.
<li><p>Remove any trailing <a>ASCII whitespace</a> from <var>subtype</var>.
<li><p>If <var>subtype</var> is the empty string or does not solely contain
<a>HTTP token code points</a>, then return failure.
<li><p>Let <var>mimeType</var> be a new <a>MIME type record</a> whose <a for="MIME type">type</a>
is <var>type</var>, in <a>ASCII lowercase</a>, and <a for="MIME type">subtype</a> is
<var>subtype</var>, in <a>ASCII lowercase</a>.
<li>
<p>While <var>position</var> is not past the end of <var>input</var>:
<ol>
<li><p>Advance <var>position</var> to the next <a>code point</a> in <var>input</var>. (This skips
past U+003B (;).)
<li><p><a>Skip ASCII whitespace</a> within <var>input</var> given <var>position</var>.
<li><p>Let <var>parameterName</var> be the result of <a>collecting a sequence of code points</a>
that are not U+003B (;) or U+003D (=) from <var>input</var>, given <var>position</var>.
<li><p>Set <var>parameterName</var> to <var>parameterName</var>, in <a>ASCII lowercase</a>.
<li>
<p>If <var>position</var> is not past the end of <var>input</var>, then:
<ol>
<li><p>If the <a>code point</a> at <var>position</var> within <var>input</var> is U+003B (;),
then <a for=iteration>continue</a>.
<li><p>Advance <var>position</var> to the next <a>code point</a> in <var>input</var>. (This
skips past U+003D (=).)
</ol>
<li><p>If <var>position</var> is past the end of <var>input</var>, then
<a for=iteration>break</a>.
<li><p>Let <var>parameterValue</var> be the empty string.
<li>
<p>If the <a>code point</a> at <var>position</var> within <var>input</var> is U+0022 ("),
then:
<ol>
<li><p>Advance <var>position</var> to the next <a>code point</a> in <var>input</var>.
<li>
<p>While true:
<ol>
<li><p>Append the result of <a>collecting a sequence of code points</a> that are not
U+0022 (") or U+005C (\) from <var>input</var>, given <var>position</var>, to
<var>parameterValue</var>.
<li>
<p>If <var>position</var> is not past the end of <var>input</var> and the <a>code point</a>
at <var>position</var> within <var>input</var> is U+005C (\), then:
<ol>
<li><p>Advance <var>position</var> to the next <a>code point</a> in <var>input</var>.
<li>
<p>If <var>position</var> is not past the end of <var>input</var>, then:
<ol>
<li><p>Append the <a>code point</a> at <var>position</var> within <var>input</var> to
<var>parameterValue</var>.
<li><p>Advance <var>position</var> to the next <a>code point</a> in <var>input</var>.
<li><p><a for=iteration>Continue</a>.
</ol>
<li><p>Otherwise, append U+005C (\) to <var>parameterValue</var> and
<a for=iteration>break</a>.
</ol>
<li><p>Otherwise, <a for=iteration>break</a>.
</ol>
<li>
<p><a>Collect a sequence of code points</a> that are not U+003B (;) from <var>input</var>,
given <var>position</var>.
<p class=example id=example-mime-type-parser-trailing-garbage>Given
<code>text/html;charset="shift_jis"iso-2022-jp</code> you end up with
<code>text/html;charset=shift_jis</code>.
</ol>
<li>
<p>Otherwise:
<ol>
<li><p>Set <var>parameterValue</var> to the result of
<a>collecting a sequence of code points</a> that are not U+003B (;) from <var>input</var>,
given <var>position</var>.
<li><p>Remove any trailing <a>ASCII whitespace</a> from <var>parameterValue</var>.
<li><p>If <var>parameterValue</var> is the empty string, then <a for=iteration>continue</a>.
</ol>
<li>
<p>If all of the following are true
<ul class=brief>
<li><var>parameterName</var> is not the empty string
<li><var>parameterName</var> solely contains <a>HTTP token code points</a>
<li><var>parameterValue</var> solely contains <a>HTTP quoted-string token code points</a>
<li><var>mimeType</var>'s <a for="MIME type">parameters</a>[<var>parameterName</var>]
<a for=map lt=exist>does not exist</a>
</ul>
<p>then <a for=map>set</a> <var>mimeType</var>'s
<a for="MIME type">parameters</a>[<var>parameterName</var>] to <var>parameterValue</var>.
</ol>
<li><p>Return <var>mimeType</var>.
</ol>
<hr>
<p>To <dfn export>parse a MIME type from bytes</dfn>, given a <a>byte sequence</a> <var>input</var>,
run these steps:
<ol>
<li><p>Let <var>string</var> be <var>input</var>, <a>isomorphic decoded</a>.
<li><p>Return the result of <a>parse a MIME type</a> with <var>string</var>.
</ol>
<h3 id=serializing-a-mime-type>Serializing a MIME type</h3>
<p>To <dfn export>serialize a MIME type</dfn>, given a <a>MIME type</a> <var>mimeType</var>, run
these steps:
<ol>
<li><p>Let <var>serialization</var> be the concatenation of <var>mimeType</var>'s
<a for="MIME type">type</a>, U+002F (/), and <var>mimeType</var>'s <a for="MIME type">subtype</a>.
<li>
<p><a for=map>For each</a> <var>name</var> → <var>value</var> of <var>mimeType</var>'s
<a for="MIME type">parameters</a>:
<ol>
<li><p>Append U+003B (;) to <var>serialization</var>.
<li><p>Append <var>name</var> to <var>serialization</var>.
<li><p>Append U+003D (=) to <var>serialization</var>.
<li>
<p>If <var>value</var> does not solely contain <a>HTTP token code points</a> or <var>value</var>
is the empty string, then:
<ol>
<li><p>Precede each occurence of U+0022 (") or U+005C (\) in <var>value</var> with U+005C (\).
<li><p>Prepend U+0022 (") to <var>value</var>.
<li><p>Append U+0022 (") to <var>value</var>.
</ol>
<li><p>Append <var>value</var> to <var>serialization</var>.
</ol>
<li><p>Return <var>serialization</var>.
</ol>
<hr>
<p>To <dfn export>serialize a MIME type to bytes</dfn>, given a <a>MIME type</a>
<var>mimeType</var>, run these steps:
<ol>
<li><p>Let <var>stringSerialization</var> be the result of <a>serialize a MIME type</a> with
<var>mimeType</var>.
<li><p>Return <var>stringSerialization</var>, <a>isomorphic encoded</a>.
</ol>
<h3 id=mime-type-groups>MIME type groups</h3>
<p>An <dfn export>image MIME type</dfn> is a <a>MIME type</a> whose <a for="MIME type">type</a> is
"<code>image</code>".
<p>An <dfn export>audio or video MIME type</dfn> is any <a>MIME type</a> whose
<a for="MIME type">type</a> is "<code>audio</code>" or "<code>video</code>", or whose
<a for="MIME type">essence</a> is "<code>application/ogg</code>".
<p>A <dfn export>font MIME type</dfn> is any <a>MIME type</a> whose <a for="MIME type">type</a> is
"<code>font</code>", or whose <a for="MIME type">essence</a> is one of the following: [[!RFC8081]]
<ul class="brief">
<li><code>application/font-cff</code>
<li><code>application/font-off</code>
<li><code>application/font-sfnt</code>
<li><code>application/font-ttf</code>
<li><code>application/font-woff</code>
<li><code>application/vnd.ms-fontobject</code>
<li><code>application/vnd.ms-opentype</code>
</ul>
<p>A <dfn export>ZIP-based MIME type</dfn> is any <a>MIME type</a> whose
<a for="MIME type">subtype</a> ends in "<code>+zip</code>" or whose <a for="MIME type">essence</a>
is one of the following:
<ul class="XXX brief">
<li><code>application/zip</code>
</ul>
<p>An <dfn export>archive MIME type</dfn> is any <a>MIME type</a> whose
<!--<span>type</span> is equal to "<code title>archive</code>" or-->
<a for="MIME type">essence</a> is one of the following:
<ul class="brief">
<li><code>application/x-rar-compressed</code>
<li><code>application/zip</code>
<li><code>application/x-gzip</code>
</ul>
<p>An <dfn export>XML MIME type</dfn> is any <a>MIME type</a> whose <a for="MIME type">subtype</a>
ends in "<code>+xml</code>" or whose <a for="MIME type">essence</a> is "<code>text/xml</code>" or
"<code>application/xml</code>". [[!RFC7303]]
<p>An <dfn export>HTML MIME type</dfn> is any <a>MIME type</a> whose <a for="MIME type">essence</a>
is "<code>text/html</code>".
<p>A <dfn export>scriptable MIME type</dfn> is an <a>XML MIME type</a>, <a>HTML MIME type</a>, or
any <a>MIME type</a> whose <a for="MIME type">essence</a> is "<code>application/pdf</code>".
<p>A <dfn export>JavaScript MIME type</dfn> is any <a>MIME type</a> whose
<a for="MIME type">essence</a> is one of the following:
<ul class="brief">
<li><code>application/ecmascript</code>
<li><code>application/javascript</code>
<li><code>application/x-ecmascript</code>
<li><code>application/x-javascript</code>
<li><code>text/ecmascript</code>
<li><code>text/javascript</code>
<li><code>text/javascript1.0</code>
<li><code>text/javascript1.1</code>
<li><code>text/javascript1.2</code>
<li><code>text/javascript1.3</code>
<li><code>text/javascript1.4</code>
<li><code>text/javascript1.5</code>
<li><code>text/jscript</code>
<li><code>text/livescript</code>
<li><code>text/x-ecmascript</code>
<li><code>text/x-javascript</code>
</ul>
<p>A <a>string</a> is a <dfn export>JavaScript MIME type essence match</dfn> if it is an
<a>ASCII case-insensitive</a> match for one of the <a>JavaScript MIME type</a> essence strings.
<p class="note">This hook is used by the <{script/type}> attribute of <{script}> elements.
<p>A <dfn export>JSON MIME type</dfn> is any <a>MIME type</a> whose <a for="MIME type">subtype</a>
ends in "<code>+json</code>" or whose <a for="MIME type">essence</a> is
"<code>application/json</code>" or "<code>text/json</code>".
<h2 id=handling-a-resource>Handling a resource</h2>
<p class=XXX>
A <dfn>resource</dfn> is ….
<p>
For each <a>resource</a> it handles, the user agent must keep track of
the following associated metadata:
<ul>
<li>
A <dfn export>supplied MIME type</dfn>, the <a>MIME type</a> determined by
the <a>supplied MIME type detection algorithm</a>.
<li>
A <dfn>check-for-apache-bug flag</dfn>, which defaults to unset.
<li>
A <dfn>no-sniff flag</dfn>, which defaults to set if the user agent does
not wish to perform sniffing on the <a>resource</a> and unset
otherwise.
<p class=note>
The user agent can choose to use outside information, such as previous
experience with a site, to determine whether to opt out of sniffing for a
particular <a>resource</a>. The user agent can also choose to opt
out of sniffing for all <a>resources</a>. However,
opting out of sniffing does not exempt the user agent from using the
<a>MIME type sniffing algorithm</a>.
<li>
A <dfn export>computed MIME type</dfn>, the <a>MIME type</a>
determined by the <a>MIME type sniffing algorithm</a>.
</ul>
<h3 id=interpreting-the-resource-metadata>Interpreting the resource metadata</h3>
<p>
The <a>supplied MIME type</a> of a <a>resource</a> is provided
to the user agent by an external source associated with that
<a>resource</a>.
The method of obtaining this information varies depending upon how the
<a>resource</a> is retrieved.
<p>
To determine the <a>supplied MIME type</a> of a <a>resource</a>,
user agents must use the following <dfn>supplied MIME type detection
algorithm</dfn>:
<ol>
<li>
Let <var>supplied-type</var> be null.
<li>
If the <a>resource</a> is retrieved via HTTP, execute the following
steps:
<ol>
<li>
If one or more <code>Content-Type</code> headers are associated with the
<a>resource</a>, execute the following steps:
<ol>
<li>
Set <var>supplied-type</var> to the value of the last
<code>Content-Type</code> header associated with the
<a>resource</a>.
<p class=note>
File extensions are not used to determine the <a>supplied MIME
type</a> of a <a>resource</a> retrieved via HTTP because they are
unreliable and easily spoofed.
<li>
Set the <a>check-for-apache-bug flag</a> if
<var>supplied-type</var> is <strong>exactly</strong> equal to one of
the values in the following table:
<table>
<thead>
<tr>
<th>Bytes in Hexadecimal
<th>Bytes in ASCII
<tbody>
<tr>
<td>
74 65 78 74 2F 70 6C 61 69 6E
<td>
<code>text/plain</code>
<tr>
<td>
74 65 78 74 2F 70 6C 61 69 6E<br>
3B 20 63 68 61 72 73 65 74 3D<br>
49 53 4F 2D 38 38 35 39 2D 31
<td>
<code>text/plain; charset=ISO-8859-1</code>
<tr>
<td>
74 65 78 74 2F 70 6C 61 69 6E<br>
3B 20 63 68 61 72 73 65 74 3D<br>
69 73 6F 2D 38 38 35 39 2D 31
<td>
<code>text/plain; charset=iso-8859-1</code>
<tr>
<td>
74 65 78 74 2F 70 6C 61 69 6E<br>
3B 20 63 68 61 72 73 65 74 3D<br>
55 54 46 2D 38
<td>
<code>text/plain; charset=UTF-8</code>
</table>
<p class=note>
The <a>supplied MIME type detection algorithm</a> detects these
exact <a>byte</a> sequences because some older installations of
Apache contain
<a href=https://issues.apache.org/bugzilla/show_bug.cgi?id=13986>a
bug</a> that causes them to supply one of these Content-Type headers
when serving files with unrecognized <a lt="MIME type">MIME
types</a>.
</ol>
</ol>
[[!HTTP]]
<li>
If the <a>resource</a> is retrieved directly from the file system,
set <var>supplied-type</var> to the <a>MIME type</a> provided by the
file system.
<li>
If the <a>resource</a> is retrieved via another protocol (such as
FTP), set <var>supplied-type</var> to the <a>MIME type</a> as
determined by that protocol, if any.
[[!FTP]]
<li>
If <var>supplied-type</var> is not a <a>MIME type</a>, the
<a>supplied MIME type</a> is undefined.
Abort these steps.
<li>
The <a>supplied MIME type</a> is <var>supplied-type</var>.
</ol>
<h3 id=reading-the-resource-header>Reading the resource header</h3>
<p>
A <dfn>resource header</dfn> is the <a>byte sequence</a> at the
beginning of a <a>resource</a>, as determined by
<a lt="read the resource header">reading the resource header</a>.
<p>
To <dfn>read the resource header</dfn>, perform the following steps:
<ol>
<li>
Let <var>buffer</var> be a <a>byte sequence</a>.
<li>
Read <a>bytes</a> of the <a>resource</a> into
<var>buffer</var> until one of the following conditions is met:
<ul>
<li>
the end of the <a>resource</a> is reached.
<li>
the number of <a>bytes</a> in <var>buffer</var> is
greater than or equal to 1445.
<li>
a reasonable amount of time has elapsed, as determined by the user
agent.
</ul>
<p class=note>
If the number of <a>bytes</a> in <var>buffer</var> is
greater than or equal to 1445, the <a>MIME type sniffing
algorithm</a> will be deterministic for the majority of cases.
However, certain factors (such as a slow connection) may prevent the
user agent from reading 1445 <a>bytes</a> in a
reasonable amount of time.
<li>
The <a>resource header</a> is <var>buffer</var>.
</ol>
<p class=note>
The <a>resource header</a> need only be determined once per
<a>resource</a>.
<h2 id=matching-a-mime-type-pattern>Matching a MIME type pattern</h2>
<p>
A <dfn>byte pattern</dfn> is a <a>byte sequence</a> used as a template
to be matched against in the <a>pattern matching algorithm</a>.
<p>
A <dfn>pattern mask</dfn> is a <a>byte sequence</a> used to determine
the significance of <a>bytes</a> being compared against a
<a>byte pattern</a> in the <a>pattern matching algorithm</a>.
<p class=note>
In a <a>pattern mask</a>, 0xFF indicates the <a>byte</a> is
strictly significant, 0xDF indicates that the <a>byte</a> is
significant in an ASCII case-insensitive way, and 0x00 indicates that the
<a>byte</a> is not significant.
<p>To determine whether a <a>byte sequence</a> matches a particular <a>byte pattern</a>, use the
following <dfn>pattern matching algorithm</dfn>. It is given a <a>byte sequence</a>
<var>input</var>, a <a>byte pattern</a> <var>pattern</var>, a <a>pattern mask</a> <var>mask</var>,
and a <a for=/>set</a> of <a>bytes</a> to be ignored <var>ignored</var>, and returns true or false.
<ol>
<li><p>Assert: <var>pattern</var>'s <a for="byte sequence">length</a> is equal to
<var>mask</var>'s <a for="byte sequence">length</a>.
<li><p>If <var>input</var>'s <a for="byte sequence">length</a> is less than <var>pattern</var>'s
<a for="byte sequence">length</a>, return false.
<li><p>Let <var>s</var> be 0.
<li>
<p>While <var>s</var> < <var>input</var>'s <a for="byte sequence">length</a>:
<ol>
<li><p>If <var>ignored</var> does not <a for=set>contain</a> <var>input</var>[<var>s</var>],
<a for=iteration>break</a>.
<li><p>Set <var>s</var> to <var>s</var> + 1.
</ol>
<li><p>Let <var>p</var> be 0.
<li>
<p>While <var>p</var> < <var>pattern</var>'s <a for="byte sequence">length</a>:
<ol>
<li><p>Let <var>maskedData</var> be the result of applying the bitwise AND operator to
<var>input</var>[<var>s</var>] and <var>mask</var>[<var>p</var>].
<li><p>If <var>maskedData</var> is not equal to <var>pattern</var>[<var>p</var>], return false.
<li><p>Set <var>s</var> to <var>s</var> + 1.
<li><p>Set <var>p</var> to <var>p</var> + 1.
</ol>
<li><p>Return true.
</ol>
<h3 id=matching-an-image-type-pattern>Matching an image type pattern</h3>
<p>To determine which <a>image MIME type</a> <a>byte pattern</a> a <a>byte sequence</a>
<var>input</var> matches, if any, use the following
<dfn>image type pattern matching algorithm</dfn>:
<ol>
<li><p>Execute the following steps for each row <var>row</var> in the following table:
<ol>
<li><p>Let <var>patternMatched</var> be the result of the <a>pattern matching algorithm</a>
given <var>input</var>, the value in the first column of <var>row</var>, the value in the second
column of <var>row</var>, and the value in the third column of <var>row</var>.
<li><p>If <var>patternMatched</var> is true, return the value in the fourth column of
<var>row</var>.
</ol>
<table>
<thead>
<tr>
<th>
<a>Byte Pattern</a>
<th>
<a>Pattern Mask</a>
<th>
Leading <a lt=byte>Bytes</a> to Be Ignored
<th>
<a>Image MIME Type</a>
<th>
Note
<tbody>
<!-- https://www.iana.org/assignments/media-types/image/vnd.microsoft.icon -->
<!-- https://msdn.microsoft.com/en-us/library/ms997538.aspx -->
<tr>
<td>
00 00 01 00
<td>
FF FF FF FF
<td>
None.
<td>
<code>image/x-icon</code>
<td>
A Windows Icon signature.
<!-- https://msdn.microsoft.com/en-us/library/ms997538.aspx -->
<tr>
<td>
00 00 02 00
<td>
FF FF FF FF
<td>
None.
<td>
<code>image/x-icon</code>
<td>
A Windows Cursor signature.
<tr>
<td>
42 4D
<td>
FF FF
<td>
None.
<td>
<code>image/bmp</code>
<td>
The string "<code>BM</code>", a BMP signature.
<!-- https://www.w3.org/Graphics/GIF/spec-gif87.txt -->
<tr>
<td>
47 49 46 38 37 61
<td>
FF FF FF FF FF FF
<td>
None.
<td>
<code>image/gif</code>
<td>
The string "<code>GIF87a</code>", a GIF signature.
<!-- https://www.w3.org/Graphics/GIF/spec-gif89a.txt -->
<tr>
<td>
47 49 46 38 39 61
<td>
FF FF FF FF FF FF
<td>
None.
<td>
<code>image/gif</code>
<td>
The string "<code>GIF89a</code>", a GIF signature.
<!-- https://developers.google.com/speed/webp/docs/riff_container#webp-file-header -->
<tr>
<td>
52 49 46 46 00 00 00 00 57 45 42 50 56 50
<td>
FF FF FF FF 00 00 00 00 FF FF FF FF FF FF
<td>
None.
<td>
<code>image/webp</code>
<td>
The string "<code>RIFF</code>" followed by four
<a>bytes</a> followed by the string
"<code>WEBPVP</code>".
<!-- https://www.w3.org/TR/PNG/#5PNG-file-signature -->
<tr>
<td>
89 50 4E 47 0D 0A 1A 0A
<td>
FF FF FF FF FF FF FF FF
<td>
None.
<td>
<code>image/png</code>
<td>
An error-checking <a>byte</a> followed by the string
"<code>PNG</code>" followed by CR LF SUB LF, the PNG signature.
<!-- https://www.digicamsoft.com/itu/itu-t81-36.html -->
<tr>
<td>
FF D8 FF
<td>
FF FF FF
<td>
None.
<td>
<code>image/jpeg</code>
<td>
The JPEG Start of Image marker followed by the indicator
<a>byte</a> of another marker.
</table>
<li><p>Return undefined.
</ol>
<h3 id=matching-an-audio-or-video-type-pattern>Matching an audio or video type pattern</h3>
<p>To determine which <a>audio or video MIME type</a> <a>byte pattern</a> a <a>byte sequence</a>
<var>input</var> matches, if any, use the following <dfn>audio or video type pattern matching
algorithm</dfn>:
<ol>
<li><p>Execute the following steps for each row <var>row</var> in the following table:
<ol>
<li><p>Let <var>patternMatched</var> be the result of the <a>pattern matching algorithm</a>
given <var>input</var>, the value in the first column of <var>row</var>, the value in the second
column of <var>row</var>, and the value in the third column of <var>row</var>.
<li><p>If <var>patternMatched</var> is true, return the value in the fourth column of
<var>row</var>.
</ol>
<table>
<thead>
<tr>