OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Detecting mime types is a tricky business because we need to balance | 5 // Detecting mime types is a tricky business because we need to balance |
6 // compatibility concerns with security issues. Here is a survey of how other | 6 // compatibility concerns with security issues. Here is a survey of how other |
7 // browsers behave and then a description of how we intend to behave. | 7 // browsers behave and then a description of how we intend to behave. |
8 // | 8 // |
9 // HTML payload, no Content-Type header: | 9 // HTML payload, no Content-Type header: |
10 // * IE 7: Render as HTML | 10 // * IE 7: Render as HTML |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 // * Firefox 2: Render as HTML | 85 // * Firefox 2: Render as HTML |
86 // * Safari 3: Render as HTML | 86 // * Safari 3: Render as HTML |
87 // * Opera 9: Render as HTML | 87 // * Opera 9: Render as HTML |
88 // The layout tests rely on us rendering this as HTML. | 88 // The layout tests rely on us rendering this as HTML. |
89 // But we're conservative in XHTML detection, as this runs afoul of the | 89 // But we're conservative in XHTML detection, as this runs afoul of the |
90 // "don't detect dangerous mime types" rule. | 90 // "don't detect dangerous mime types" rule. |
91 // | 91 // |
92 // Note that our definition of HTML payload is much stricter than IE's | 92 // Note that our definition of HTML payload is much stricter than IE's |
93 // definition and roughly the same as Firefox's definition. | 93 // definition and roughly the same as Firefox's definition. |
94 | 94 |
| 95 #include <stdint.h> |
95 #include <string> | 96 #include <string> |
96 | 97 |
97 #include "net/base/mime_sniffer.h" | 98 #include "net/base/mime_sniffer.h" |
98 | 99 |
99 #include "base/basictypes.h" | |
100 #include "base/logging.h" | 100 #include "base/logging.h" |
101 #include "base/metrics/histogram.h" | 101 #include "base/metrics/histogram.h" |
102 #include "base/strings/string_util.h" | 102 #include "base/strings/string_util.h" |
103 #include "url/gurl.h" | 103 #include "url/gurl.h" |
104 | 104 |
105 namespace net { | 105 namespace net { |
106 | 106 |
107 // The number of content bytes we need to use all our magic numbers. Feel free | 107 // The number of content bytes we need to use all our magic numbers. Feel free |
108 // to increase this number if you add a longer magic number. | 108 // to increase this number if you add a longer magic number. |
109 static const size_t kBytesRequiredForMagic = 42; | 109 static const size_t kBytesRequiredForMagic = 42; |
(...skipping 832 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
942 } | 942 } |
943 | 943 |
944 bool LooksLikeBinary(const char* content, size_t size) { | 944 bool LooksLikeBinary(const char* content, size_t size) { |
945 // The definition of "binary bytes" is from the spec at | 945 // The definition of "binary bytes" is from the spec at |
946 // https://mimesniff.spec.whatwg.org/#binary-data-byte | 946 // https://mimesniff.spec.whatwg.org/#binary-data-byte |
947 // | 947 // |
948 // The bytes which are considered to be "binary" are all < 0x20. Encode them | 948 // The bytes which are considered to be "binary" are all < 0x20. Encode them |
949 // one bit per byte, with 1 for a "binary" bit, and 0 for a "text" bit. The | 949 // one bit per byte, with 1 for a "binary" bit, and 0 for a "text" bit. The |
950 // least-significant bit represents byte 0x00, the most-significant bit | 950 // least-significant bit represents byte 0x00, the most-significant bit |
951 // represents byte 0x1F. | 951 // represents byte 0x1F. |
952 const uint32 kBinaryBits = | 952 const uint32_t kBinaryBits = |
953 ~(1u << '\t' | 1u << '\n' | 1u << '\r' | 1u << '\f' | 1u << '\x1b'); | 953 ~(1u << '\t' | 1u << '\n' | 1u << '\r' | 1u << '\f' | 1u << '\x1b'); |
954 for (size_t i = 0; i < size; ++i) { | 954 for (size_t i = 0; i < size; ++i) { |
955 uint8 byte = static_cast<uint8>(content[i]); | 955 uint8_t byte = static_cast<uint8_t>(content[i]); |
956 if (byte < 0x20 && (kBinaryBits & (1u << byte))) | 956 if (byte < 0x20 && (kBinaryBits & (1u << byte))) |
957 return true; | 957 return true; |
958 } | 958 } |
959 return false; | 959 return false; |
960 } | 960 } |
961 | 961 |
962 } // namespace net | 962 } // namespace net |
OLD | NEW |