Chromium Code Reviews| 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 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 338 // content might not actually have a null terminator. In that case, we | 338 // content might not actually have a null terminator. In that case, we |
| 339 // pretend the length is content_size. | 339 // pretend the length is content_size. |
| 340 const char* end = static_cast<const char*>(memchr(content, '\0', size)); | 340 const char* end = static_cast<const char*>(memchr(content, '\0', size)); |
| 341 const size_t content_strlen = | 341 const size_t content_strlen = |
| 342 (end != NULL) ? static_cast<size_t>(end - content) : size; | 342 (end != NULL) ? static_cast<size_t>(end - content) : size; |
| 343 | 343 |
| 344 bool match = false; | 344 bool match = false; |
| 345 if (magic_entry.is_string) { | 345 if (magic_entry.is_string) { |
| 346 if (content_strlen >= len) { | 346 if (content_strlen >= len) { |
| 347 // String comparisons are case-insensitive | 347 // String comparisons are case-insensitive |
| 348 match = (base::strncasecmp(magic_entry.magic, content, len) == 0); | 348 match = base::EqualsCaseInsensitiveASCII(magic_entry.magic, |
| 349 base::StringPiece(content, len)); | |
|
Nico
2015/07/08 21:58:23
This previously only compared the first len bytes
brettw
2015/07/08 22:35:17
len is actually the length of magic so it should b
| |
| 349 } | 350 } |
| 350 } else { | 351 } else { |
| 351 if (size >= len) { | 352 if (size >= len) { |
| 352 if (!magic_entry.mask) { | 353 if (!magic_entry.mask) { |
| 353 match = MagicCmp(magic_entry.magic, content, len); | 354 match = MagicCmp(magic_entry.magic, content, len); |
| 354 } else { | 355 } else { |
| 355 match = MagicMaskCmp(magic_entry.magic, content, len, magic_entry.mask); | 356 match = MagicMaskCmp(magic_entry.magic, content, len, magic_entry.mask); |
| 356 } | 357 } |
| 357 } | 358 } |
| 358 } | 359 } |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 455 NULL, &office_version)) | 456 NULL, &office_version)) |
| 456 return false; | 457 return false; |
| 457 | 458 |
| 458 OfficeDocType type = DOC_TYPE_NONE; | 459 OfficeDocType type = DOC_TYPE_NONE; |
| 459 for (size_t i = 0; i < arraysize(kOfficeExtensionTypes); ++i) { | 460 for (size_t i = 0; i < arraysize(kOfficeExtensionTypes); ++i) { |
| 460 std::string url_path = url.path(); | 461 std::string url_path = url.path(); |
| 461 | 462 |
| 462 if (url_path.length() < kOfficeExtensionTypes[i].extension_len) | 463 if (url_path.length() < kOfficeExtensionTypes[i].extension_len) |
| 463 continue; | 464 continue; |
| 464 | 465 |
| 465 const char* extension = | 466 base::StringPiece extension = base::StringPiece(url_path).substr( |
| 466 &url_path[url_path.length() - kOfficeExtensionTypes[i].extension_len]; | 467 url_path.length() - kOfficeExtensionTypes[i].extension_len); |
| 467 | 468 if (base::EqualsCaseInsensitiveASCII( |
| 468 if (0 == base::strncasecmp(extension, kOfficeExtensionTypes[i].extension, | 469 extension, |
| 469 kOfficeExtensionTypes[i].extension_len)) { | 470 base::StringPiece(kOfficeExtensionTypes[i].extension, |
| 471 kOfficeExtensionTypes[i].extension_len))) { | |
| 470 type = kOfficeExtensionTypes[i].doc_type; | 472 type = kOfficeExtensionTypes[i].doc_type; |
| 471 break; | 473 break; |
| 472 } | 474 } |
| 473 } | 475 } |
| 474 | 476 |
| 475 if (type == DOC_TYPE_NONE) | 477 if (type == DOC_TYPE_NONE) |
| 476 return false; | 478 return false; |
| 477 | 479 |
| 478 if (office_version == "CFB") { | 480 if (office_version == "CFB") { |
| 479 switch (type) { | 481 switch (type) { |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 601 if (!counter) { | 603 if (!counter) { |
| 602 counter = UMASnifferHistogramGet("mime_sniffer.kMagicXML2", | 604 counter = UMASnifferHistogramGet("mime_sniffer.kMagicXML2", |
| 603 arraysize(kMagicXML)); | 605 arraysize(kMagicXML)); |
| 604 } | 606 } |
| 605 const int kMaxTagIterations = 5; | 607 const int kMaxTagIterations = 5; |
| 606 for (int i = 0; i < kMaxTagIterations && pos < end; ++i) { | 608 for (int i = 0; i < kMaxTagIterations && pos < end; ++i) { |
| 607 pos = reinterpret_cast<const char*>(memchr(pos, '<', end - pos)); | 609 pos = reinterpret_cast<const char*>(memchr(pos, '<', end - pos)); |
| 608 if (!pos) | 610 if (!pos) |
| 609 return false; | 611 return false; |
| 610 | 612 |
| 611 if ((pos + sizeof("<?xml") - 1 <= end) && | 613 static const char kXmlPrefix[] = "<?xml"; |
| 612 (base::strncasecmp(pos, "<?xml", sizeof("<?xml") - 1) == 0)) { | 614 static const size_t kXmlPrefixLength = arraysize(kXmlPrefix) - 1; |
| 615 static const char kDocTypePrefix[] = "<!DOCTYPE"; | |
| 616 static const size_t kDocTypePrefixLength = arraysize(kDocTypePrefix) - 1; | |
| 617 | |
| 618 if ((pos + kXmlPrefixLength <= end) && | |
| 619 base::EqualsCaseInsensitiveASCII( | |
| 620 base::StringPiece(pos, kXmlPrefixLength), | |
| 621 base::StringPiece(kXmlPrefix, kXmlPrefixLength))) { | |
| 613 // Skip XML declarations. | 622 // Skip XML declarations. |
| 614 ++pos; | 623 ++pos; |
| 615 continue; | 624 continue; |
| 616 } else if ((pos + sizeof("<!DOCTYPE") - 1 <= end) && | 625 } else if ((pos + kDocTypePrefixLength - 1 <= end) && |
| 617 (base::strncasecmp(pos, "<!DOCTYPE", sizeof("<!DOCTYPE") - 1) == | 626 base::EqualsCaseInsensitiveASCII( |
| 618 0)) { | 627 base::StringPiece(pos, kDocTypePrefixLength), |
| 628 base::StringPiece(kDocTypePrefix, kDocTypePrefixLength))) { | |
| 619 // Skip DOCTYPE declarations. | 629 // Skip DOCTYPE declarations. |
| 620 ++pos; | 630 ++pos; |
| 621 continue; | 631 continue; |
| 622 } | 632 } |
| 623 | 633 |
| 624 if (CheckForMagicNumbers(pos, end - pos, | 634 if (CheckForMagicNumbers(pos, end - pos, |
| 625 kMagicXML, arraysize(kMagicXML), | 635 kMagicXML, arraysize(kMagicXML), |
| 626 counter, result)) | 636 counter, result)) |
| 627 return true; | 637 return true; |
| 628 | 638 |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 953 ~(1u << '\t' | 1u << '\n' | 1u << '\r' | 1u << '\f' | 1u << '\x1b'); | 963 ~(1u << '\t' | 1u << '\n' | 1u << '\r' | 1u << '\f' | 1u << '\x1b'); |
| 954 for (size_t i = 0; i < size; ++i) { | 964 for (size_t i = 0; i < size; ++i) { |
| 955 uint8_t byte = static_cast<uint8_t>(content[i]); | 965 uint8_t byte = static_cast<uint8_t>(content[i]); |
| 956 if (byte < 0x20 && (kBinaryBits & (1u << byte))) | 966 if (byte < 0x20 && (kBinaryBits & (1u << byte))) |
| 957 return true; | 967 return true; |
| 958 } | 968 } |
| 959 return false; | 969 return false; |
| 960 } | 970 } |
| 961 | 971 |
| 962 } // namespace net | 972 } // namespace net |
| OLD | NEW |