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