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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 #include "net/base/mime_util.h" | 103 #include "net/base/mime_util.h" |
104 #include "url/gurl.h" | 104 #include "url/gurl.h" |
105 | 105 |
106 namespace net { | 106 namespace net { |
107 | 107 |
108 // The number of content bytes we need to use all our magic numbers. Feel free | 108 // The number of content bytes we need to use all our magic numbers. Feel free |
109 // to increase this number if you add a longer magic number. | 109 // to increase this number if you add a longer magic number. |
110 static const size_t kBytesRequiredForMagic = 42; | 110 static const size_t kBytesRequiredForMagic = 42; |
111 | 111 |
112 struct MagicNumber { | 112 struct MagicNumber { |
113 const char* mime_type; | 113 const char* const mime_type; |
114 const char* magic; | 114 const char* const magic; |
115 size_t magic_len; | 115 size_t magic_len; |
116 bool is_string; | 116 bool is_string; |
117 const char* mask; // if set, must have same length as |magic| | 117 const char* const mask; // if set, must have same length as |magic| |
118 }; | 118 }; |
119 | 119 |
120 #define MAGIC_NUMBER(mime_type, magic) \ | 120 #define MAGIC_NUMBER(mime_type, magic) \ |
121 { (mime_type), (magic), sizeof(magic)-1, false, NULL }, | 121 { (mime_type), (magic), sizeof(magic)-1, false, NULL }, |
122 | 122 |
123 template <int MagicSize, int MaskSize> | 123 template <int MagicSize, int MaskSize> |
124 class VerifySizes { | 124 class VerifySizes { |
125 static_assert(MagicSize == MaskSize, "sizes must be equal"); | 125 static_assert(MagicSize == MaskSize, "sizes must be equal"); |
126 | 126 |
127 public: | 127 public: |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 | 202 |
203 enum OfficeDocType { | 203 enum OfficeDocType { |
204 DOC_TYPE_WORD, | 204 DOC_TYPE_WORD, |
205 DOC_TYPE_EXCEL, | 205 DOC_TYPE_EXCEL, |
206 DOC_TYPE_POWERPOINT, | 206 DOC_TYPE_POWERPOINT, |
207 DOC_TYPE_NONE | 207 DOC_TYPE_NONE |
208 }; | 208 }; |
209 | 209 |
210 struct OfficeExtensionType { | 210 struct OfficeExtensionType { |
211 OfficeDocType doc_type; | 211 OfficeDocType doc_type; |
212 const char* extension; | 212 const char* const extension; |
213 size_t extension_len; | 213 size_t extension_len; |
214 }; | 214 }; |
215 | 215 |
216 #define OFFICE_EXTENSION(type, extension) \ | 216 #define OFFICE_EXTENSION(type, extension) \ |
217 { (type), (extension), sizeof(extension) - 1 }, | 217 { (type), (extension), sizeof(extension) - 1 }, |
218 | 218 |
219 static const OfficeExtensionType kOfficeExtensionTypes[] = { | 219 static const OfficeExtensionType kOfficeExtensionTypes[] = { |
220 OFFICE_EXTENSION(DOC_TYPE_WORD, ".doc") | 220 OFFICE_EXTENSION(DOC_TYPE_WORD, ".doc") |
221 OFFICE_EXTENSION(DOC_TYPE_EXCEL, ".xls") | 221 OFFICE_EXTENSION(DOC_TYPE_EXCEL, ".xls") |
222 OFFICE_EXTENSION(DOC_TYPE_POWERPOINT, ".ppt") | 222 OFFICE_EXTENSION(DOC_TYPE_POWERPOINT, ".ppt") |
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
717 // have_enough_content because there could be a binary looking byte in the | 717 // have_enough_content because there could be a binary looking byte in the |
718 // truncated data. | 718 // truncated data. |
719 *have_enough_content &= is_truncated; | 719 *have_enough_content &= is_truncated; |
720 result->assign("text/plain"); | 720 result->assign("text/plain"); |
721 return false; | 721 return false; |
722 } | 722 } |
723 | 723 |
724 static bool IsUnknownMimeType(const std::string& mime_type) { | 724 static bool IsUnknownMimeType(const std::string& mime_type) { |
725 // TODO(tc): Maybe reuse some code in net/http/http_response_headers.* here. | 725 // TODO(tc): Maybe reuse some code in net/http/http_response_headers.* here. |
726 // If we do, please be careful not to alter the semantics at all. | 726 // If we do, please be careful not to alter the semantics at all. |
727 static const char* kUnknownMimeTypes[] = { | 727 static const char* const kUnknownMimeTypes[] = { |
728 // Empty mime types are as unknown as they get. | 728 // Empty mime types are as unknown as they get. |
729 "", | 729 "", |
730 // The unknown/unknown type is popular and uninformative | 730 // The unknown/unknown type is popular and uninformative |
731 "unknown/unknown", | 731 "unknown/unknown", |
732 // The second most popular unknown mime type is application/unknown | 732 // The second most popular unknown mime type is application/unknown |
733 "application/unknown", | 733 "application/unknown", |
734 // Firefox rejects a mime type if it is exactly */* | 734 // Firefox rejects a mime type if it is exactly */* |
735 "*/*", | 735 "*/*", |
736 }; | 736 }; |
737 static base::HistogramBase* counter(NULL); | 737 static base::HistogramBase* counter(NULL); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
812 #if defined(OS_ANDROID) | 812 #if defined(OS_ANDROID) |
813 url.SchemeIs("content") || | 813 url.SchemeIs("content") || |
814 #endif | 814 #endif |
815 url.SchemeIsFile() || | 815 url.SchemeIsFile() || |
816 url.SchemeIsFileSystem(); | 816 url.SchemeIsFileSystem(); |
817 if (!sniffable_scheme) { | 817 if (!sniffable_scheme) { |
818 should_sniff_counter->Add(1); | 818 should_sniff_counter->Add(1); |
819 return false; | 819 return false; |
820 } | 820 } |
821 | 821 |
822 static const char* kSniffableTypes[] = { | 822 static const char* const kSniffableTypes[] = { |
823 // Many web servers are misconfigured to send text/plain for many | 823 // Many web servers are misconfigured to send text/plain for many |
824 // different types of content. | 824 // different types of content. |
825 "text/plain", | 825 "text/plain", |
826 // We want to sniff application/octet-stream for | 826 // We want to sniff application/octet-stream for |
827 // application/x-chrome-extension, but nothing else. | 827 // application/x-chrome-extension, but nothing else. |
828 "application/octet-stream", | 828 "application/octet-stream", |
829 // XHTML and Atom/RSS feeds are often served as plain xml instead of | 829 // XHTML and Atom/RSS feeds are often served as plain xml instead of |
830 // their more specific mime types. | 830 // their more specific mime types. |
831 "text/xml", | 831 "text/xml", |
832 "application/xml", | 832 "application/xml", |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
964 // First check the extra table. | 964 // First check the extra table. |
965 if (CheckForMagicNumbers(content, size, kExtraMagicNumbers, | 965 if (CheckForMagicNumbers(content, size, kExtraMagicNumbers, |
966 arraysize(kExtraMagicNumbers), NULL, result)) | 966 arraysize(kExtraMagicNumbers), NULL, result)) |
967 return true; | 967 return true; |
968 // Finally check the original table. | 968 // Finally check the original table. |
969 return CheckForMagicNumbers(content, size, kMagicNumbers, | 969 return CheckForMagicNumbers(content, size, kMagicNumbers, |
970 arraysize(kMagicNumbers), NULL, result); | 970 arraysize(kMagicNumbers), NULL, result); |
971 } | 971 } |
972 | 972 |
973 } // namespace net | 973 } // namespace net |
OLD | NEW |