Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "modules/mediasession/MediaMetadataSanitizer.h" | 5 #include "modules/mediasession/MediaMetadataSanitizer.h" |
| 6 | 6 |
| 7 #include "core/dom/ExecutionContext.h" | |
| 8 #include "core/inspector/ConsoleMessage.h" | |
| 7 #include "modules/mediasession/MediaImage.h" | 9 #include "modules/mediasession/MediaImage.h" |
| 8 #include "modules/mediasession/MediaMetadata.h" | 10 #include "modules/mediasession/MediaMetadata.h" |
| 9 #include "public/platform/WebIconSizesParser.h" | 11 #include "public/platform/WebIconSizesParser.h" |
| 10 #include "public/platform/WebSize.h" | 12 #include "public/platform/WebSize.h" |
| 11 #include "url/url_constants.h" | 13 #include "url/url_constants.h" |
| 14 #include "wtf/text/StringOperators.h" | |
| 12 | 15 |
| 13 namespace blink { | 16 namespace blink { |
| 14 | 17 |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| 17 // Constants used by the sanitizer, must be consistent with | 20 // Constants used by the sanitizer, must be consistent with |
| 18 // content::MediaMetdataSanitizer. | 21 // content::MediaMetdataSanitizer. |
| 19 | 22 |
| 20 // Maximum length of all strings inside MediaMetadata when it is sent over mojo. | 23 // Maximum length of all strings inside MediaMetadata when it is sent over mojo. |
| 21 const size_t kMaxStringLength = 4 * 1024; | 24 const size_t kMaxStringLength = 4 * 1024; |
| 22 | 25 |
| 23 // Maximum type length of MediaImage, which conforms to RFC 4288 | 26 // Maximum type length of MediaImage, which conforms to RFC 4288 |
| 24 // (https://tools.ietf.org/html/rfc4288). | 27 // (https://tools.ietf.org/html/rfc4288). |
| 25 const size_t kMaxImageTypeLength = 2 * 127 + 1; | 28 const size_t kMaxImageTypeLength = 2 * 127 + 1; |
| 26 | 29 |
| 27 // Maximum number of MediaImages inside the MediaMetadata. | 30 // Maximum number of MediaImages inside the MediaMetadata. |
| 28 const size_t kMaxNumberOfMediaImages = 10; | 31 const size_t kMaxNumberOfMediaImages = 10; |
| 29 | 32 |
| 30 // Maximum of sizes in a MediaImage. | 33 // Maximum of sizes in a MediaImage. |
| 31 const size_t kMaxNumberOfImageSizes = 10; | 34 const size_t kMaxNumberOfImageSizes = 10; |
| 32 | 35 |
| 33 bool checkMediaImageSrcSanity(const KURL& src) { | 36 bool checkMediaImageSrcSanity(const KURL& src, ExecutionContext* context) { |
| 37 // Console warning for invalid src is printed upon MediaImage creation. | |
|
whywhat
2016/11/11 20:09:02
Do we want to do that? Maybe it's better to show t
Zhiqiang Zhang (Slow)
2016/11/11 20:56:44
Invalid src is dropped when passing metadata throu
| |
| 34 if (!src.isValid()) | 38 if (!src.isValid()) |
| 35 return false; | 39 return false; |
| 40 | |
| 36 if (!src.protocolIs(url::kHttpScheme) && !src.protocolIs(url::kHttpsScheme) && | 41 if (!src.protocolIs(url::kHttpScheme) && !src.protocolIs(url::kHttpsScheme) && |
| 37 !src.protocolIs(url::kDataScheme)) { | 42 !src.protocolIs(url::kDataScheme)) { |
| 43 context->addConsoleMessage(ConsoleMessage::create( | |
| 44 JSMessageSource, WarningMessageLevel, | |
| 45 "MediaImage src can only be of http/https/data scheme: " + | |
| 46 src.getString())); | |
| 38 return false; | 47 return false; |
| 39 } | 48 } |
| 40 DCHECK(src.getString().is8Bit()); | 49 DCHECK(src.getString().is8Bit()); |
| 41 if (src.getString().length() > url::kMaxURLChars) | 50 if (src.getString().length() > url::kMaxURLChars) { |
| 51 context->addConsoleMessage(ConsoleMessage::create( | |
| 52 JSMessageSource, WarningMessageLevel, | |
| 53 "MediaImage src exceeds maximum URL length: " + src.getString())); | |
| 42 return false; | 54 return false; |
| 55 } | |
| 43 return true; | 56 return true; |
| 44 } | 57 } |
| 45 | 58 |
| 46 // Sanitize MediaImage and do mojo serialization. Returns null when | 59 // Sanitize MediaImage and do mojo serialization. Returns null when |
| 47 // |image.src()| is bad. | 60 // |image.src()| is bad. |
| 48 blink::mojom::blink::MediaImagePtr sanitizeMediaImageAndConvertToMojo( | 61 blink::mojom::blink::MediaImagePtr sanitizeMediaImageAndConvertToMojo( |
| 49 const MediaImage* image) { | 62 const MediaImage* image, |
| 63 ExecutionContext* context) { | |
| 50 DCHECK(image); | 64 DCHECK(image); |
| 51 | 65 |
| 52 blink::mojom::blink::MediaImagePtr mojoImage; | 66 blink::mojom::blink::MediaImagePtr mojoImage; |
| 53 | 67 |
| 54 KURL url = KURL(ParsedURLString, image->src()); | 68 KURL url = KURL(ParsedURLString, image->src()); |
| 55 if (!checkMediaImageSrcSanity(url)) | 69 if (!checkMediaImageSrcSanity(url, context)) |
| 56 return mojoImage; | 70 return mojoImage; |
| 57 | 71 |
| 58 mojoImage = blink::mojom::blink::MediaImage::New(); | 72 mojoImage = blink::mojom::blink::MediaImage::New(); |
| 59 mojoImage->src = url; | 73 mojoImage->src = url; |
| 60 mojoImage->type = image->type().left(kMaxImageTypeLength); | 74 mojoImage->type = image->type().left(kMaxImageTypeLength); |
| 61 for (const auto& webSize : | 75 for (const auto& webSize : |
| 62 WebIconSizesParser::parseIconSizes(image->sizes())) { | 76 WebIconSizesParser::parseIconSizes(image->sizes())) { |
| 63 mojoImage->sizes.append(webSize); | 77 mojoImage->sizes.append(webSize); |
| 64 if (mojoImage->sizes.size() == kMaxNumberOfImageSizes) | 78 if (mojoImage->sizes.size() == kMaxNumberOfImageSizes) { |
| 79 context->addConsoleMessage(ConsoleMessage::create( | |
| 80 JSMessageSource, WarningMessageLevel, | |
| 81 "The number of MediaImage sizes exceeds the upper limit. " | |
| 82 "All remaining MediaImage will be ignored")); | |
| 65 break; | 83 break; |
| 84 } | |
| 66 } | 85 } |
| 67 return mojoImage; | 86 return mojoImage; |
| 68 } | 87 } |
| 69 | 88 |
| 70 } // anonymous namespace | 89 } // anonymous namespace |
| 71 | 90 |
| 72 blink::mojom::blink::MediaMetadataPtr | 91 blink::mojom::blink::MediaMetadataPtr |
| 73 MediaMetadataSanitizer::sanitizeAndConvertToMojo( | 92 MediaMetadataSanitizer::sanitizeAndConvertToMojo(const MediaMetadata* metadata, |
| 74 const MediaMetadata* metadata) { | 93 ExecutionContext* context) { |
| 75 blink::mojom::blink::MediaMetadataPtr mojoMetadata; | 94 blink::mojom::blink::MediaMetadataPtr mojoMetadata; |
| 76 if (!metadata) | 95 if (!metadata) |
| 77 return mojoMetadata; | 96 return mojoMetadata; |
| 78 | 97 |
| 79 mojoMetadata = blink::mojom::blink::MediaMetadata::New(); | 98 mojoMetadata = blink::mojom::blink::MediaMetadata::New(); |
| 80 | 99 |
| 81 mojoMetadata->title = metadata->title().left(kMaxStringLength); | 100 mojoMetadata->title = metadata->title().left(kMaxStringLength); |
| 82 mojoMetadata->artist = metadata->artist().left(kMaxStringLength); | 101 mojoMetadata->artist = metadata->artist().left(kMaxStringLength); |
| 83 mojoMetadata->album = metadata->album().left(kMaxStringLength); | 102 mojoMetadata->album = metadata->album().left(kMaxStringLength); |
| 84 | 103 |
| 85 for (const auto image : metadata->artwork()) { | 104 for (const auto image : metadata->artwork()) { |
| 86 blink::mojom::blink::MediaImagePtr mojoImage = | 105 blink::mojom::blink::MediaImagePtr mojoImage = |
| 87 sanitizeMediaImageAndConvertToMojo(image.get()); | 106 sanitizeMediaImageAndConvertToMojo(image.get(), context); |
| 88 if (!mojoImage.is_null()) | 107 if (!mojoImage.is_null()) |
| 89 mojoMetadata->artwork.append(std::move(mojoImage)); | 108 mojoMetadata->artwork.append(std::move(mojoImage)); |
| 90 if (mojoMetadata->artwork.size() == kMaxNumberOfMediaImages) | 109 if (mojoMetadata->artwork.size() == kMaxNumberOfMediaImages) { |
| 110 context->addConsoleMessage(ConsoleMessage::create( | |
| 111 JSMessageSource, WarningMessageLevel, | |
| 112 "The number of MediaImage sizes exceeds the upper limit. " | |
| 113 "All remaining MediaImage will be ignored")); | |
| 91 break; | 114 break; |
| 115 } | |
| 92 } | 116 } |
| 93 return mojoMetadata; | 117 return mojoMetadata; |
| 94 } | 118 } |
| 95 | 119 |
| 96 } // namespace blink | 120 } // namespace blink |
| OLD | NEW |