0

Scope floc computation to url visits with publicly routable IP

Bug: 1062736
Change-Id: I4a0bfc9bb7910d4fd85e2327ba5863d57af8b1f5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2219011
Reviewed-by: Josh Karlin <jkarlin@chromium.org>
Commit-Queue: Yao Xiao <yaoxia@chromium.org>
Cr-Commit-Position: refs/heads/master@{#777510}
This commit is contained in:
Yao Xiao
2020-06-11 21:57:42 +00:00
committed by Commit Bot
parent c366d29562
commit 8f06f5aa76
6 changed files with 75 additions and 19 deletions

@ -191,6 +191,8 @@ class FlocIdProviderWithCustomizedServicesBrowserTest
IN_PROC_BROWSER_TEST_F(FlocIdProviderWithCustomizedServicesBrowserTest,
FlocIdValue_OneNavigation) {
net::IPAddress::ConsiderLoopbackIPToBePubliclyRoutableForTesting();
ui_test_utils::NavigateToURL(
browser(), https_server_.GetURL(test_host(), "/title1.html"));

@ -146,6 +146,7 @@ void FlocIdProviderImpl::GetRecentlyVisitedURLs(
GetRecentlyVisitedURLsCallback callback) {
history::QueryOptions options;
options.SetRecentDayRange(kQueryHistoryWindowInDays);
options.duplicate_policy = history::QueryOptions::KEEP_ALL_DUPLICATES;
history_service_->QueryHistory(base::string16(), options, std::move(callback),
&history_task_tracker_);
@ -160,9 +161,9 @@ void FlocIdProviderImpl::OnGetRecentlyVisitedURLsCompleted(
std::unordered_set<std::string> domains;
for (const history::URLResult& url_result : results) {
// TODO(yaoxia): Filter out those visits with private ip. It's fine to not
// do any filtering for now, as all external messages that depend on this
// logic are still gated behind a disabled flag.
if (!url_result.publicly_routable())
continue;
domains.insert(net::registry_controlled_domains::GetDomainAndRegistry(
url_result.url(),
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));

@ -128,9 +128,12 @@ class FlocIdProviderUnitTest : public testing::Test {
TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) {
// Add a history entry with a timestamp exactly 7 days back from now.
std::string domain = "foo.com";
history_service_->AddPage(GURL(base::StrCat({"https://www.", domain})),
base::Time::Now() - base::TimeDelta::FromDays(7),
history::VisitSource::SOURCE_BROWSED);
history::HistoryAddPageArgs add_page_args;
add_page_args.url = GURL(base::StrCat({"https://www.", domain}));
add_page_args.time = base::Time::Now() - base::TimeDelta::FromDays(7);
add_page_args.publicly_routable = true;
history_service_->AddPage(add_page_args);
task_environment_.RunUntilIdle();
@ -164,11 +167,14 @@ TEST_F(FlocIdProviderUnitTest, QualifiedInitialHistory) {
}
TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) {
// Add a history entry with a timestamp 8 days back from now.
std::string domain = "foo.com";
history_service_->AddPage(GURL(base::StrCat({"https://www.", domain})),
base::Time::Now() - base::TimeDelta::FromDays(8),
history::VisitSource::SOURCE_BROWSED);
// Add a history entry with a timestamp 8 days back from now.
history::HistoryAddPageArgs add_page_args;
add_page_args.url = GURL(base::StrCat({"https://www.", domain}));
add_page_args.time = base::Time::Now() - base::TimeDelta::FromDays(8);
add_page_args.publicly_routable = true;
history_service_->AddPage(add_page_args);
task_environment_.RunUntilIdle();
@ -191,9 +197,8 @@ TEST_F(FlocIdProviderUnitTest, UnqualifiedInitialHistory) {
ASSERT_EQ(1u, floc_session_count());
// Add a history entry with a timestamp 6 days back from now.
history_service_->AddPage(GURL(base::StrCat({"https://www.", domain})),
base::Time::Now() - base::TimeDelta::FromDays(6),
history::VisitSource::SOURCE_BROWSED);
add_page_args.time = base::Time::Now() - base::TimeDelta::FromDays(6);
history_service_->AddPage(add_page_args);
// Advance the clock by 23 hours. Expect no floc id update notification,
// as the id refresh interval is 24 hours.
@ -300,12 +305,31 @@ TEST_F(FlocIdProviderUnitTest, EventLogging) {
EXPECT_EQ(999ULL, event2.floc_id());
}
TEST_F(FlocIdProviderUnitTest, HistoryEntriesWithPrivateIP) {
history::QueryResults query_results;
query_results.SetURLResults(
{history::URLResult(GURL("https://a.test"),
base::Time::Now() - base::TimeDelta::FromDays(1))});
set_floc_session_count(1u);
OnGetRecentlyVisitedURLsCompleted(1u, std::move(query_results));
ASSERT_FALSE(floc_id().IsValid());
}
TEST_F(FlocIdProviderUnitTest, MultipleHistoryEntries) {
base::Time time = base::Time::Now() - base::TimeDelta::FromDays(1);
std::vector<history::URLResult> url_results{
history::URLResult(GURL("https://a.test"), time),
history::URLResult(GURL("https://b.test"), time)};
history::URLResult url_result_a(GURL("https://a.test"), time);
url_result_a.set_publicly_routable(true);
history::URLResult url_result_b(GURL("https://b.test"), time);
url_result_b.set_publicly_routable(true);
history::URLResult url_result_c(GURL("https://c.test"), time);
std::vector<history::URLResult> url_results{url_result_a, url_result_b,
url_result_c};
history::QueryResults query_results;
query_results.SetURLResults(std::move(url_results));
@ -320,9 +344,12 @@ TEST_F(FlocIdProviderUnitTest, MultipleHistoryEntries) {
TEST_F(FlocIdProviderUnitTest, TurnSyncOffAndOn) {
std::string domain = "foo.com";
history_service_->AddPage(GURL(base::StrCat({"https://www.", domain})),
base::Time::Now() - base::TimeDelta::FromDays(1),
history::VisitSource::SOURCE_BROWSED);
history::HistoryAddPageArgs add_page_args;
add_page_args.url = GURL(base::StrCat({"https://www.", domain}));
add_page_args.time = base::Time::Now() - base::TimeDelta::FromDays(1);
add_page_args.publicly_routable = true;
history_service_->AddPage(add_page_args);
task_environment_.RunUntilIdle();

@ -22,6 +22,8 @@
namespace net {
namespace {
bool g_consider_loopback_ip_to_be_publicly_routable_for_testing = false;
// The prefix for IPv6 mapped IPv4 addresses.
// https://tools.ietf.org/html/rfc4291#section-2.5.5.2
constexpr uint8_t kIPv4MappedPrefix[] = {0, 0, 0, 0, 0, 0,
@ -234,6 +236,11 @@ bool IPAddress::IsValid() const {
}
bool IPAddress::IsPubliclyRoutable() const {
if (g_consider_loopback_ip_to_be_publicly_routable_for_testing &&
IsLoopback()) {
return true;
}
if (IsIPv4()) {
return IsPubliclyRoutableIPv4(ip_address_);
} else if (IsIPv6()) {
@ -242,6 +249,11 @@ bool IPAddress::IsPubliclyRoutable() const {
return true;
}
// static
void IPAddress::ConsiderLoopbackIPToBePubliclyRoutableForTesting() {
g_consider_loopback_ip_to_be_publicly_routable_for_testing = true;
}
bool IPAddress::IsZero() const {
for (auto x : ip_address_) {
if (x != 0)

@ -157,6 +157,10 @@ class NET_EXPORT IPAddress {
// IPv4-mapped-to-IPv6 addresses are considered publicly routable.
bool IsPubliclyRoutable() const;
// Let future IsPubliclyRoutable() calls in the current process always return
// true for a loopback ip.
static void ConsiderLoopbackIPToBePubliclyRoutableForTesting();
// Returns true if the IP is "zero" (e.g. the 0.0.0.0 IPv4 address).
bool IsZero() const;

@ -302,6 +302,16 @@ TEST(IPAddressTest, IsPubliclyRoutableIPv6) {
}
}
TEST(IPAddressTest, ConsiderLoopbackIPToBePubliclyRoutableForTestingMethod) {
IPAddress address;
EXPECT_TRUE(address.AssignFromIPLiteral("127.0.0.1"));
ASSERT_TRUE(address.IsValid());
EXPECT_FALSE(address.IsPubliclyRoutable());
IPAddress::ConsiderLoopbackIPToBePubliclyRoutableForTesting();
EXPECT_TRUE(address.IsPubliclyRoutable());
}
TEST(IPAddressTest, IsZero) {
uint8_t address1[4] = {};
IPAddress zero_ipv4_address(address1);