Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Unified Diff: components/content_settings/core/common/content_settings_pattern.cc

Issue 2730533002: chrome.contentSettings API: Block patterns that match extension URLs for mic and video
Patch Set: Fix MatchesExtensionUrls Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/content_settings/core/common/content_settings_pattern.cc
diff --git a/components/content_settings/core/common/content_settings_pattern.cc b/components/content_settings/core/common/content_settings_pattern.cc
index a83a6959a02240575eae95fe7168e4ea75c644bc..985da824976b66aa3cc583023693a7d8204a8119 100644
--- a/components/content_settings/core/common/content_settings_pattern.cc
+++ b/components/content_settings/core/common/content_settings_pattern.cc
@@ -13,6 +13,7 @@
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "components/content_settings/core/common/content_settings_pattern_parser.h"
+#include "components/crx_file/id_util.h"
#include "net/base/url_util.h"
#include "url/gurl.h"
@@ -21,6 +22,9 @@ namespace {
// The component supports only one scheme for simplicity.
const char* non_port_non_domain_wildcard_scheme = NULL;
+// Hardcode this value to avoid additional dependency.
+const char kExtensionScheme[] = "chrome-extension";
+
// Keep it consistent with enum SchemeType in content_settings_pattern.h.
const char* const kSchemeNames[] = {
"wildcard",
@@ -28,7 +32,7 @@ const char* const kSchemeNames[] = {
url::kHttpScheme,
url::kHttpsScheme,
url::kFileScheme,
- "chrome-extension",
+ kExtensionScheme,
};
static_assert(arraysize(kSchemeNames) == ContentSettingsPattern::SCHEME_MAX,
@@ -533,6 +537,19 @@ ContentSettingsPattern::ContentSettingsPattern(
is_valid_(valid) {
}
+bool ContentSettingsPattern::MatchesExtensionUrls() const {
+ // Check if the host pattern matches an extension ID. This can happen in two
+ // cases:
+ // 1. Host pattern is a valid extension ID.
+ // 2. Host pattern has a wildcard and hostname is empty. This means the host
+ // pattern is simply "*" and not a subdomain pattern like "[*.]google.com."
+ const bool host_pattern_matches_extension_id =
+ crx_file::id_util::IdIsValid(parts_.host) ||
+ (parts_.has_domain_wildcard && parts_.host.empty());
+ return host_pattern_matches_extension_id &&
+ (parts_.is_scheme_wildcard || parts_.scheme == kExtensionScheme);
+}
+
bool ContentSettingsPattern::Matches(
const GURL& url) const {
// An invalid pattern matches nothing.

Powered by Google App Engine
This is Rietveld 408576698