| Index: third_party/WebKit/Source/modules/mediasession/MediaMetadataSanitizer.cpp
|
| diff --git a/third_party/WebKit/Source/modules/mediasession/MediaMetadataSanitizer.cpp b/third_party/WebKit/Source/modules/mediasession/MediaMetadataSanitizer.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8a796b8ea1a5987126099a92602d98945228b6c8
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/modules/mediasession/MediaMetadataSanitizer.cpp
|
| @@ -0,0 +1,92 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "modules/mediasession/MediaMetadataSanitizer.h"
|
| +
|
| +#include "modules/mediasession/MediaArtwork.h"
|
| +#include "modules/mediasession/MediaMetadata.h"
|
| +#include "modules/mediasession/MojoMediaMetadata.h"
|
| +#include "public/platform/WebIconSizesParser.h"
|
| +#include "public/platform/WebString.h"
|
| +#include "url/url_constants.h"
|
| +
|
| +namespace blink {
|
| +
|
| +namespace {
|
| +
|
| +// Constants used by the sanitizer, must be consistent with
|
| +// content::MediaMetdataSanitizer.
|
| +
|
| +// Maximum length of all strings inside MediaMetadata when it is sent over mojo.
|
| +const size_t kMaxStringLength = 4 * 1024;
|
| +
|
| +// Maximum type length of MediaArtwork, which conforms to RFC 4288
|
| +// (https://tools.ietf.org/html/rfc4288).
|
| +const size_t kMaxArtworkTypeLength = 2 * 127 + 1;
|
| +
|
| +// Maximum number of artwork images inside the MediaMetadata.
|
| +const size_t kMaxNumberOfArtworkImages = 10;
|
| +
|
| +// Maximum of sizes in an artwork image.
|
| +const size_t kMaxNumberOfArtworkSizes = 10;
|
| +
|
| +bool checkArtworkSrcSanity(const KURL& src) {
|
| + if (!src.isValid())
|
| + return false;
|
| + if (!src.protocolIs(url::kHttpScheme) && !src.protocolIs(url::kHttpsScheme) &&
|
| + !src.protocolIs(url::kDataScheme)) {
|
| + return false;
|
| + }
|
| + DCHECK(src.getString().is8Bit());
|
| + if (src.getString().length() > url::kMaxURLChars)
|
| + return false;
|
| + return true;
|
| +}
|
| +
|
| +Optional<MojoMediaArtwork> sanitizeArtworkAndConvertToMojo(
|
| + const MediaArtwork* artwork) {
|
| + DCHECK(artwork);
|
| +
|
| + KURL url = KURL(ParsedURLString, artwork->src());
|
| +
|
| + if (!checkArtworkSrcSanity(url))
|
| + return WTF::nullopt;
|
| +
|
| + MojoMediaArtwork mojoArtwork;
|
| + mojoArtwork.src = url;
|
| + mojoArtwork.type = artwork->type().left(kMaxArtworkTypeLength);
|
| + for (const auto& webSize :
|
| + WebIconSizesParser::parseIconSizes(artwork->sizes())) {
|
| + mojoArtwork.sizes.append(webSize);
|
| + if (mojoArtwork.sizes.size() == kMaxNumberOfArtworkSizes)
|
| + break;
|
| + }
|
| + return mojoArtwork;
|
| +}
|
| +
|
| +} // anonymous namespace
|
| +
|
| +Optional<MojoMediaMetadata> MediaMetadataSanitizer::sanitizeAndConvertToMojo(
|
| + const MediaMetadata* metadata) {
|
| + if (!metadata)
|
| + return WTF::nullopt;
|
| +
|
| + MojoMediaMetadata mojoMetadata;
|
| +
|
| + mojoMetadata.title = metadata->title().left(kMaxStringLength);
|
| + mojoMetadata.artist = metadata->artist().left(kMaxStringLength);
|
| + mojoMetadata.album = metadata->album().left(kMaxStringLength);
|
| +
|
| + for (const auto artwork : metadata->artwork()) {
|
| + Optional<MojoMediaArtwork> mojoArtwork =
|
| + sanitizeArtworkAndConvertToMojo(artwork.get());
|
| + if (mojoArtwork.has_value())
|
| + mojoMetadata.artwork.append(mojoArtwork.value());
|
| + if (mojoMetadata.artwork.size() == kMaxNumberOfArtworkImages)
|
| + break;
|
| + }
|
| + return mojoMetadata;
|
| +}
|
| +
|
| +} // namespace blink
|
|
|