Skip to content

Commit 4917adb

Browse files
DCtheTallChromium LUCI CQ
authored and
Chromium LUCI CQ
committed
Add UMA metric for max SameSite=None cookies per host
This metric will be used to get a sense of how many cross-site cookies sites are using. This should give us more information on how many partitioned cookies we should allow domains to use per-partition, which is being discussed in privacycg/CHIPS#48. Since CHIPS is not shipped and only enabled on a small fraction of clients' machines, this metric only tracks unpartitioned cookies. Bug: 1351841 Change-Id: I50f4b6f9bfd6e3bef2ca36d5621bf98aa6476ba9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3822813 Reviewed-by: Chris Fredrickson <[email protected]> Commit-Queue: Dylan Cutler <[email protected]> Cr-Commit-Position: refs/heads/main@{#1034162}
1 parent 7df4a71 commit 4917adb

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

net/cookies/cookie_monster.cc

+14
Original file line numberDiff line numberDiff line change
@@ -2265,6 +2265,20 @@ bool CookieMonster::DoRecordPeriodicStats() {
22652265
// Can be up to kMaxCookies.
22662266
UMA_HISTOGRAM_COUNTS_10000("Cookie.NumKeys", num_keys_);
22672267

2268+
std::map<std::string, size_t> n_same_site_none_cookies;
2269+
for (const auto& [host_key, host_cookie] : cookies_) {
2270+
if (!host_cookie || !host_cookie->IsEffectivelySameSiteNone())
2271+
continue;
2272+
n_same_site_none_cookies[host_key]++;
2273+
}
2274+
size_t max_n_cookies = 0;
2275+
for (const auto& entry : n_same_site_none_cookies) {
2276+
max_n_cookies = std::max(max_n_cookies, entry.second);
2277+
}
2278+
// Can be up to 180 cookies, the max per-domain.
2279+
base::UmaHistogramCounts1000("Cookie.MaxSameSiteNoneCookiesPerKey",
2280+
max_n_cookies);
2281+
22682282
// Collect stats for partitioned cookies if they are enabled.
22692283
if (base::FeatureList::IsEnabled(features::kPartitionedCookies)) {
22702284
base::UmaHistogramCounts1000("Cookie.PartitionCount",

net/cookies/cookie_monster_unittest.cc

+51
Original file line numberDiff line numberDiff line change
@@ -3469,6 +3469,57 @@ TEST_F(CookieMonsterTest, NumKeysHistogram) {
34693469
}
34703470
}
34713471

3472+
TEST_F(CookieMonsterTest, MaxSameSiteNoneCookiesPerKey) {
3473+
const char kHistogramName[] = "Cookie.MaxSameSiteNoneCookiesPerKey";
3474+
3475+
auto store = base::MakeRefCounted<MockPersistentCookieStore>();
3476+
auto cm = std::make_unique<CookieMonster>(store.get(), net::NetLog::Get(),
3477+
kFirstPartySetsDefault);
3478+
ASSERT_EQ(0u, GetAllCookies(cm.get()).size());
3479+
3480+
{ // Only SameSite cookies should not log a sample.
3481+
base::HistogramTester histogram_tester;
3482+
3483+
ASSERT_TRUE(CreateAndSetCookie(cm.get(), GURL("https://domain1.test"),
3484+
"A=1;SameSite=Lax",
3485+
CookieOptions::MakeAllInclusive()));
3486+
ASSERT_EQ(1u, GetAllCookies(cm.get()).size());
3487+
ASSERT_TRUE(cm->DoRecordPeriodicStatsForTesting());
3488+
histogram_tester.ExpectUniqueSample(kHistogramName, 0 /* sample */,
3489+
1 /* count */);
3490+
}
3491+
3492+
{ // SameSite=None cookie should log a sample.
3493+
base::HistogramTester histogram_tester;
3494+
3495+
ASSERT_TRUE(CreateAndSetCookie(cm.get(), GURL("https://domain1.test"),
3496+
"B=2;SameSite=None;Secure",
3497+
CookieOptions::MakeAllInclusive()));
3498+
ASSERT_EQ(2u, GetAllCookies(cm.get()).size());
3499+
ASSERT_TRUE(cm->DoRecordPeriodicStatsForTesting());
3500+
histogram_tester.ExpectUniqueSample(kHistogramName, 1 /* sample */,
3501+
1 /* count */);
3502+
}
3503+
3504+
{ // Should log the maximum number of SameSite=None cookies.
3505+
base::HistogramTester histogram_tester;
3506+
3507+
ASSERT_TRUE(CreateAndSetCookie(cm.get(), GURL("https://domain2.test"),
3508+
"A=1;SameSite=None;Secure",
3509+
CookieOptions::MakeAllInclusive()));
3510+
ASSERT_TRUE(CreateAndSetCookie(cm.get(), GURL("https://domain2.test"),
3511+
"B=2;SameSite=None;Secure",
3512+
CookieOptions::MakeAllInclusive()));
3513+
ASSERT_TRUE(CreateAndSetCookie(cm.get(), GURL("https://domain3.test"),
3514+
"A=1;SameSite=None;Secure",
3515+
CookieOptions::MakeAllInclusive()));
3516+
ASSERT_EQ(5u, GetAllCookies(cm.get()).size());
3517+
ASSERT_TRUE(cm->DoRecordPeriodicStatsForTesting());
3518+
histogram_tester.ExpectUniqueSample(kHistogramName, 2 /* sample */,
3519+
1 /* count */);
3520+
}
3521+
}
3522+
34723523
// Test that localhost URLs can set and get secure cookies, even if
34733524
// non-cryptographic.
34743525
TEST_F(CookieMonsterTest, SecureCookieLocalhost) {

tools/metrics/histograms/metadata/cookie/histograms.xml

+12
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,18 @@ [email protected].
471471
</summary>
472472
</histogram>
473473

474+
<histogram name="Cookie.MaxSameSiteNoneCookiesPerKey" units="units"
475+
expires_after="2023-02-01">
476+
<owner>[email protected]</owner>
477+
<owner>src/net/cookies/OWNERS</owner>
478+
<summary>
479+
Maximum number of SameSite=None cookies that belong to a single domain on
480+
the client. This histogram will be used to inform the
481+
per-partition-per-domain limit for partitioned cookies. Recorded every 10
482+
minutes of active browsing time.
483+
</summary>
484+
</histogram>
485+
474486
<histogram name="Cookie.NumDomainPurgedKeys" units="keys"
475487
expires_after="2022-08-28">
476488
<owner>[email protected]</owner>

0 commit comments

Comments
 (0)