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 #include "content/public/common/media_metadata.h" | |
11 | |
12 namespace content { | |
13 | |
14 namespace { | |
15 | |
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 | |
18 // the MediaMetadata and the browser process must do the same when receiving | |
19 // it. | |
20 const size_t kMaxIPCStringLength = 4 * 1024; | |
21 | |
22 // Maximum type length of Artwork, which conforms to RFC 4288 | |
23 // (https://tools.ietf.org/html/rfc4288). | |
24 const size_t kMaxArtworkTypeLength = 2 * 127 + 1; | |
25 | |
26 // Maximum number of artwork images inside the MediaMetadata. | |
27 const size_t kMaxNumberOfArtworkImages = 10; | |
28 | |
29 // Maximum of sizes in an artwork image. | |
30 const size_t kMaxNumberOfArtworkSizes = 10; | |
31 | |
32 bool CheckArtworkSrcSanity(const GURL& src) { | |
33 if (!src.is_valid()) | |
34 return false; | |
35 if (!src.SchemeIsHTTPOrHTTPS() && !src.SchemeIs(url::kDataScheme)) | |
36 return false; | |
37 if (src.spec().size() > url::kMaxURLChars) | |
38 return false; | |
39 | |
40 return true; | |
41 } | |
42 | |
43 bool CheckArtworkSanity(const MediaMetadata::Artwork& artwork) { | |
44 if (!CheckArtworkSrcSanity(artwork.src)) | |
45 return false; | |
46 if (artwork.type.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.substr(0, kMaxArtworkTypeLength); | |
60 for (const auto& size : artwork.sizes) { | |
61 sanitized_artwork.sizes.push_back(size); | |
62 if (sanitized_artwork.sizes.size() == kMaxNumberOfArtworkSizes) | |
63 break; | |
64 } | |
65 | |
66 return sanitized_artwork; | |
67 } | |
68 | |
69 } // anonymous namespace | |
70 | |
71 bool MediaMetadataSanitizer::CheckSanity(const MediaMetadata& metadata) { | |
72 if (metadata.title.size() > kMaxIPCStringLength) | |
73 return false; | |
74 if (metadata.artist.size() > kMaxIPCStringLength) | |
75 return false; | |
76 if (metadata.album.size() > kMaxIPCStringLength) | |
77 return false; | |
78 if (metadata.artwork.size() > kMaxNumberOfArtworkImages) | |
79 return false; | |
80 | |
81 for (const auto& artwork : metadata.artwork) { | |
82 if (!CheckArtworkSanity(artwork)) | |
83 return false; | |
84 } | |
85 | |
86 return true; | |
87 } | |
88 | |
89 MediaMetadata MediaMetadataSanitizer::Sanitize(const MediaMetadata& metadata) { | |
90 MediaMetadata sanitized_metadata; | |
91 | |
92 sanitized_metadata.title = metadata.title.substr(0, kMaxIPCStringLength); | |
93 sanitized_metadata.artist = metadata.artist.substr(0, kMaxIPCStringLength); | |
94 sanitized_metadata.album = metadata.album.substr(0, kMaxIPCStringLength); | |
95 | |
96 for (const auto& artwork : metadata.artwork) { | |
97 if (!CheckArtworkSrcSanity(artwork.src)) | |
98 continue; | |
99 | |
100 sanitized_metadata.artwork.push_back( | |
101 CheckArtworkSanity(artwork) ? artwork : SanitizeArtwork(artwork)); | |
102 | |
103 if (sanitized_metadata.artwork.size() == kMaxNumberOfArtworkImages) | |
104 break; | |
105 } | |
106 | |
107 return sanitized_metadata; | |
108 } | |
109 | |
110 } // namespace content | |
OLD | NEW |