| Index: content/common/content_security_policy/csp_source_list.cc
|
| diff --git a/content/common/content_security_policy/csp_source_list.cc b/content/common/content_security_policy/csp_source_list.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..f2a478a7da4463e84b456e43857cc46496aaea5e
|
| --- /dev/null
|
| +++ b/content/common/content_security_policy/csp_source_list.cc
|
| @@ -0,0 +1,107 @@
|
| +// Copyright 2017 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 "content/common/content_security_policy/csp_context.h"
|
| +
|
| +namespace content {
|
| +
|
| +namespace {
|
| +
|
| +const GURL ExtractInnerURL(const GURL& url) {
|
| + if (const GURL* inner_url = url.inner_url())
|
| + return *inner_url;
|
| + else
|
| + // TODO(arthursonzogni): revisit this once GURL::inner_url support blob-URL.
|
| + return GURL(url.path());
|
| +}
|
| +
|
| +const GURL GetEffectiveURL(CSPContext* context, const GURL& url) {
|
| + // Due to backwards-compatibility concerns, we allow 'self' to match blob and
|
| + // filesystem inner URLs if we are in a context that bypasses
|
| + // ContentSecurityPolicy in the main world.
|
| + if (context->SelfSchemeShouldBypassCSP()) {
|
| + if (url.SchemeIsFileSystem() || url.SchemeIsBlob())
|
| + return ExtractInnerURL(url);
|
| + }
|
| + return url;
|
| +}
|
| +
|
| +}; // namespace
|
| +
|
| +CSPSourceList::CSPSourceList()
|
| + : allow_self(false), allow_star(false), source_list() {}
|
| +
|
| +CSPSourceList::CSPSourceList(bool allow_self,
|
| + bool allow_star,
|
| + std::vector<CSPSource> source_list)
|
| + : allow_self(allow_self),
|
| + allow_star(allow_star),
|
| + source_list(source_list) {}
|
| +
|
| +CSPSourceList::CSPSourceList(const CSPSourceList&) = default;
|
| +CSPSourceList::~CSPSourceList() = default;
|
| +
|
| +bool CSPSourceList::Allow(CSPContext* context,
|
| + const GURL& url,
|
| + bool is_redirect) const {
|
| + // Wildcards match network schemes ('http', 'https', 'ftp', 'ws', 'wss'), and
|
| + // the scheme of the protected resource:
|
| + // https://w3c.github.io/webappsec-csp/#match-url-to-source-expression. Other
|
| + // schemes, including custom schemes, must be explicitly listed in a source
|
| + // list.
|
| + if (allow_star) {
|
| + if (url.SchemeIsHTTPOrHTTPS() || url.SchemeIsSuborigin() ||
|
| + url.SchemeIsWSOrWSS() || url.SchemeIs("ftp") ||
|
| + context->ProtocolMatchesSelf(url))
|
| + return true;
|
| +
|
| + return AllowFromSources(context, url, is_redirect);
|
| + }
|
| +
|
| + const GURL effective_url = GetEffectiveURL(context, url);
|
| +
|
| + if (allow_self && context->AllowSelf(effective_url))
|
| + return true;
|
| +
|
| + return AllowFromSources(context, effective_url, is_redirect);
|
| +}
|
| +
|
| +bool CSPSourceList::AllowFromSources(CSPContext* context,
|
| + const GURL& url,
|
| + bool is_redirect) const {
|
| + for (const CSPSource& source : source_list) {
|
| + if (source.Allow(context, url, is_redirect))
|
| + return true;
|
| + }
|
| + return false;
|
| +}
|
| +
|
| +std::string CSPSourceList::ToString() const {
|
| + if (IsNone())
|
| + return "'none'";
|
| + if (allow_star)
|
| + return "*";
|
| +
|
| + bool is_empty = true;
|
| + std::stringstream text;
|
| + if (allow_self) {
|
| + text << "'self'";
|
| + is_empty = false;
|
| + }
|
| +
|
| + for (const auto& source : source_list) {
|
| + if (!is_empty)
|
| + text << " ";
|
| + text << source.ToString();
|
| + is_empty = false;
|
| + }
|
| +
|
| + return text.str();
|
| +}
|
| +
|
| +bool CSPSourceList::IsNone() const {
|
| + return !allow_self && !allow_star && source_list.empty();
|
| +}
|
| +
|
| +} // namespace content
|
|
|