| 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 "content/browser/media/session/media_metadata_sanitizer.h" | 5 #include "content/browser/media/session/media_metadata_sanitizer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "content/public/common/media_metadata.h" | 10 #include "content/public/common/media_metadata.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // Maximum length for all the strings inside the MediaMetadata when it is sent | 16 // Maximum length for all the strings inside the MediaMetadata when it is sent |
| 17 // over IPC. The renderer process should truncate the strings before sending | 17 // over IPC. The renderer process should truncate the strings before sending |
| 18 // the MediaMetadata and the browser process must do the same when receiving | 18 // the MediaMetadata and the browser process must do the same when receiving |
| 19 // it. | 19 // it. |
| 20 const size_t kMaxIPCStringLength = 4 * 1024; | 20 const size_t kMaxIPCStringLength = 4 * 1024; |
| 21 | 21 |
| 22 // Maximum type length of Artwork, which conforms to RFC 4288 | 22 // Maximum type length of MediaImage, which conforms to RFC 4288 |
| 23 // (https://tools.ietf.org/html/rfc4288). | 23 // (https://tools.ietf.org/html/rfc4288). |
| 24 const size_t kMaxArtworkTypeLength = 2 * 127 + 1; | 24 const size_t kMaxMediaImageTypeLength = 2 * 127 + 1; |
| 25 | 25 |
| 26 // Maximum number of artwork images inside the MediaMetadata. | 26 // Maximum number of MediaImages inside the MediaMetadata. |
| 27 const size_t kMaxNumberOfArtworkImages = 10; | 27 const size_t kMaxNumberOfMediaImages = 10; |
| 28 | 28 |
| 29 // Maximum of sizes in an artwork image. | 29 // Maximum of sizes in a MediaImage. |
| 30 const size_t kMaxNumberOfArtworkSizes = 10; | 30 const size_t kMaxNumberOfMediaImageSizes = 10; |
| 31 | 31 |
| 32 bool CheckArtworkSrcSanity(const GURL& src) { | 32 bool CheckMediaImageSrcSanity(const GURL& src) { |
| 33 if (!src.is_valid()) | 33 if (!src.is_valid()) |
| 34 return false; | 34 return false; |
| 35 if (!src.SchemeIsHTTPOrHTTPS() && !src.SchemeIs(url::kDataScheme)) | 35 if (!src.SchemeIsHTTPOrHTTPS() && !src.SchemeIs(url::kDataScheme)) |
| 36 return false; | 36 return false; |
| 37 if (src.spec().size() > url::kMaxURLChars) | 37 if (src.spec().size() > url::kMaxURLChars) |
| 38 return false; | 38 return false; |
| 39 | 39 |
| 40 return true; | 40 return true; |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool CheckArtworkSanity(const MediaMetadata::Artwork& artwork) { | 43 bool CheckMediaImageSanity(const MediaMetadata::MediaImage& image) { |
| 44 if (!CheckArtworkSrcSanity(artwork.src)) | 44 if (!CheckMediaImageSrcSanity(image.src)) |
| 45 return false; | 45 return false; |
| 46 if (artwork.type.size() > kMaxArtworkTypeLength) | 46 if (image.type.size() > kMaxMediaImageTypeLength) |
| 47 return false; | 47 return false; |
| 48 if (artwork.sizes.size() > kMaxNumberOfArtworkSizes) | 48 if (image.sizes.size() > kMaxNumberOfMediaImageSizes) |
| 49 return false; | 49 return false; |
| 50 | 50 |
| 51 return true; | 51 return true; |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Sanitize artwork. The method should not be called if |artwork.src| is bad. | 54 // Sanitize MediaImage. The method should not be called if |image.src| is bad. |
| 55 MediaMetadata::Artwork SanitizeArtwork(const MediaMetadata::Artwork& artwork) { | 55 MediaMetadata::MediaImage SanitizeMediaImage( |
| 56 MediaMetadata::Artwork sanitized_artwork; | 56 const MediaMetadata::MediaImage& image) { |
| 57 MediaMetadata::MediaImage sanitized_image; |
| 57 | 58 |
| 58 sanitized_artwork.src = artwork.src; | 59 sanitized_image.src = image.src; |
| 59 sanitized_artwork.type = artwork.type.substr(0, kMaxArtworkTypeLength); | 60 sanitized_image.type = image.type.substr(0, kMaxMediaImageTypeLength); |
| 60 for (const auto& size : artwork.sizes) { | 61 for (const auto& size : image.sizes) { |
| 61 sanitized_artwork.sizes.push_back(size); | 62 sanitized_image.sizes.push_back(size); |
| 62 if (sanitized_artwork.sizes.size() == kMaxNumberOfArtworkSizes) | 63 if (sanitized_image.sizes.size() == kMaxNumberOfMediaImageSizes) |
| 63 break; | 64 break; |
| 64 } | 65 } |
| 65 | 66 |
| 66 return sanitized_artwork; | 67 return sanitized_image; |
| 67 } | 68 } |
| 68 | 69 |
| 69 } // anonymous namespace | 70 } // anonymous namespace |
| 70 | 71 |
| 71 bool MediaMetadataSanitizer::CheckSanity(const MediaMetadata& metadata) { | 72 bool MediaMetadataSanitizer::CheckSanity(const MediaMetadata& metadata) { |
| 72 if (metadata.title.size() > kMaxIPCStringLength) | 73 if (metadata.title.size() > kMaxIPCStringLength) |
| 73 return false; | 74 return false; |
| 74 if (metadata.artist.size() > kMaxIPCStringLength) | 75 if (metadata.artist.size() > kMaxIPCStringLength) |
| 75 return false; | 76 return false; |
| 76 if (metadata.album.size() > kMaxIPCStringLength) | 77 if (metadata.album.size() > kMaxIPCStringLength) |
| 77 return false; | 78 return false; |
| 78 if (metadata.artwork.size() > kMaxNumberOfArtworkImages) | 79 if (metadata.artwork.size() > kMaxNumberOfMediaImages) |
| 79 return false; | 80 return false; |
| 80 | 81 |
| 81 for (const auto& artwork : metadata.artwork) { | 82 for (const auto& image : metadata.artwork) { |
| 82 if (!CheckArtworkSanity(artwork)) | 83 if (!CheckMediaImageSanity(image)) |
| 83 return false; | 84 return false; |
| 84 } | 85 } |
| 85 | 86 |
| 86 return true; | 87 return true; |
| 87 } | 88 } |
| 88 | 89 |
| 89 MediaMetadata MediaMetadataSanitizer::Sanitize(const MediaMetadata& metadata) { | 90 MediaMetadata MediaMetadataSanitizer::Sanitize(const MediaMetadata& metadata) { |
| 90 MediaMetadata sanitized_metadata; | 91 MediaMetadata sanitized_metadata; |
| 91 | 92 |
| 92 sanitized_metadata.title = metadata.title.substr(0, kMaxIPCStringLength); | 93 sanitized_metadata.title = metadata.title.substr(0, kMaxIPCStringLength); |
| 93 sanitized_metadata.artist = metadata.artist.substr(0, kMaxIPCStringLength); | 94 sanitized_metadata.artist = metadata.artist.substr(0, kMaxIPCStringLength); |
| 94 sanitized_metadata.album = metadata.album.substr(0, kMaxIPCStringLength); | 95 sanitized_metadata.album = metadata.album.substr(0, kMaxIPCStringLength); |
| 95 | 96 |
| 96 for (const auto& artwork : metadata.artwork) { | 97 for (const auto& image : metadata.artwork) { |
| 97 if (!CheckArtworkSrcSanity(artwork.src)) | 98 if (!CheckMediaImageSrcSanity(image.src)) |
| 98 continue; | 99 continue; |
| 99 | 100 |
| 100 sanitized_metadata.artwork.push_back( | 101 sanitized_metadata.artwork.push_back( |
| 101 CheckArtworkSanity(artwork) ? artwork : SanitizeArtwork(artwork)); | 102 CheckMediaImageSanity(image) ? image : SanitizeMediaImage(image)); |
| 102 | 103 |
| 103 if (sanitized_metadata.artwork.size() == kMaxNumberOfArtworkImages) | 104 if (sanitized_metadata.artwork.size() == kMaxNumberOfMediaImages) |
| 104 break; | 105 break; |
| 105 } | 106 } |
| 106 | 107 |
| 107 return sanitized_metadata; | 108 return sanitized_metadata; |
| 108 } | 109 } |
| 109 | 110 |
| 110 } // namespace content | 111 } // namespace content |
| OLD | NEW |