Commit 78132cb5 authored by yilkal's avatar yilkal Committed by Commit Bot

Ensure that www.website.com matches website.com

This cl ensures that www.website.com matches website.com
in SupervisedUserUrlFilter.

Bug: 1032811
Change-Id: Ic76ffde2457d029fbb25acaf6f41e048cbca17b5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2092125
Commit-Queue: Yilkal Abe <yilkal@chromium.org>
Reviewed-by: default avatarAga Wronska <agawronska@chromium.org>
Cr-Commit-Position: refs/heads/master@{#753788}
parent 13feec20
......@@ -20,6 +20,7 @@
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "base/task/thread_pool.h"
#include "base/task_runner_util.h"
......@@ -29,6 +30,7 @@
#include "chrome/common/chrome_features.h"
#include "components/policy/core/browser/url_blacklist_manager.h"
#include "components/policy/core/browser/url_util.h"
#include "components/url_formatter/url_formatter.h"
#include "components/url_matcher/url_matcher.h"
#include "components/variations/service/variations_service.h"
#include "content/public/browser/browser_thread.h"
......@@ -244,8 +246,21 @@ bool SupervisedUserURLFilter::HasFilteredScheme(const GURL& url) {
bool SupervisedUserURLFilter::HostMatchesPattern(
const std::string& canonical_host,
const std::string& pattern) {
std::string trimmed_pattern = pattern;
// If |canonical_host| starts with |www.| but |pattern| starts with neither
// |www.| nor |*.| then trim |www.| part of canonical host.
bool is_host_www =
base::StartsWith(canonical_host, "www.", base::CompareCase::SENSITIVE);
bool patern_accepts =
base::StartsWith(pattern, "www.", base::CompareCase::SENSITIVE) ||
base::StartsWith(pattern, "*.", base::CompareCase::SENSITIVE);
std::string trimmed_host = canonical_host;
if (is_host_www && !patern_accepts) {
trimmed_host = base::UTF16ToASCII(
url_formatter::StripWWW(base::ASCIIToUTF16(canonical_host)));
}
std::string trimmed_pattern = pattern;
if (base::EndsWith(pattern, ".*", base::CompareCase::SENSITIVE)) {
size_t registry_length = GetCanonicalHostRegistryLength(
trimmed_host, EXCLUDE_UNKNOWN_REGISTRIES, EXCLUDE_PRIVATE_REGISTRIES);
......
......@@ -92,10 +92,11 @@ class SupervisedUserURLFilter {
// or accounts.google.com).
// - If the pattern ends with ".*", it matches the host on any known TLD
// (e.g. the pattern "google.*" would match google.com or google.co.uk).
// See the SupervisedUserURLFilterTest.HostMatchesPattern unit test for more
// examples.
// Asterisks in other parts of the pattern are not allowed.
// |host| and |pattern| are assumed to be normalized to lower-case.
// If the |host| starts with "www." but the |pattern| starts with neither
// "www." nor "*.", the function strips the "www." part of |host| and tries to
// match again. See the SupervisedUserURLFilterTest.HostMatchesPattern unit
// test for more examples. Asterisks in other parts of the pattern are not
// allowed. |host| and |pattern| are assumed to be normalized to lower-case.
// This method is public for testing.
static bool HostMatchesPattern(const std::string& canonical_host,
const std::string& pattern);
......
......@@ -336,6 +336,8 @@ TEST_F(SupervisedUserURLFilterTest, HasFilteredScheme) {
}
TEST_F(SupervisedUserURLFilterTest, HostMatchesPattern) {
EXPECT_TRUE(SupervisedUserURLFilter::HostMatchesPattern("www.google.com",
"google.com"));
EXPECT_TRUE(
SupervisedUserURLFilter::HostMatchesPattern("www.google.com",
"*.google.com"));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment