Commit 3059d476 authored by Dylan Cutler's avatar Dylan Cutler Committed by Chromium LUCI CQ

Have net::CanonicalCookie::Create return nullptr if the resulting cookie

is non-canonical.

The function documentation already states this is what the function
should do, but the current code only DCHECKs isCanonical. In production
it is still possible for non-canonical cookies to be sent to the browser
process this way since the DCHECK is not enabled.

Bug: 1162915
Change-Id: I6046477eeb3d042fee65c3e1996fc487795b3cc7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2614366Reviewed-by: default avatarMartin Šrámek <msramek@chromium.org>
Reviewed-by: default avatarLily Chen <chlily@chromium.org>
Reviewed-by: default avatarDenis Kuznetsov [CET] <antrim@chromium.org>
Commit-Queue: Dylan Cutler <dylancutler@google.com>
Cr-Commit-Position: refs/heads/master@{#841642}
parent e401b656
...@@ -40,6 +40,7 @@ HatsSurveyStatusChecker::HatsSurveyStatusChecker(Profile* profile) { ...@@ -40,6 +40,7 @@ HatsSurveyStatusChecker::HatsSurveyStatusChecker(Profile* profile) {
GURL cookie_url = GURL("https://www.google.com"); GURL cookie_url = GURL("https://www.google.com");
auto survey_cookie = net::CanonicalCookie::Create( auto survey_cookie = net::CanonicalCookie::Create(
cookie_url, "PAIDCONTENT=0", base::Time::Now(), base::nullopt); cookie_url, "PAIDCONTENT=0", base::Time::Now(), base::nullopt);
DCHECK(survey_cookie);
network::mojom::CookieManager* cookie_manager = network::mojom::CookieManager* cookie_manager =
GetStoragePartition()->GetCookieManagerForBrowserProcess(); GetStoragePartition()->GetCookieManagerForBrowserProcess();
cookie_manager->SetCanonicalCookie(*survey_cookie, cookie_url, cookie_manager->SetCanonicalCookie(*survey_cookie, cookie_url,
......
...@@ -74,6 +74,8 @@ void SetCookieForPartition( ...@@ -74,6 +74,8 @@ void SetCookieForPartition(
std::unique_ptr<net::CanonicalCookie> cc(net::CanonicalCookie::Create( std::unique_ptr<net::CanonicalCookie> cc(net::CanonicalCookie::Create(
gaia_url, gaps_cookie_value, base::Time::Now(), gaia_url, gaps_cookie_value, base::Time::Now(),
base::nullopt /* server_time */)); base::nullopt /* server_time */));
if (!cc)
return;
const net::CookieOptions options = net::CookieOptions::MakeAllInclusive(); const net::CookieOptions options = net::CookieOptions::MakeAllInclusive();
partition->GetCookieManagerForBrowserProcess()->SetCanonicalCookie( partition->GetCookieManagerForBrowserProcess()->SetCanonicalCookie(
...@@ -244,4 +246,4 @@ void OnlineLoginHelper::OnGetCookiesForCompleteAuthentication( ...@@ -244,4 +246,4 @@ void OnlineLoginHelper::OnGetCookiesForCompleteAuthentication(
std::move(complete_login_callback_).Run(user_context); std::move(complete_login_callback_).Run(user_context);
} }
} // namespace chromeos } // namespace chromeos
\ No newline at end of file
...@@ -428,14 +428,11 @@ std::unique_ptr<CanonicalCookie> CanonicalCookie::Create( ...@@ -428,14 +428,11 @@ std::unique_ptr<CanonicalCookie> CanonicalCookie::Create(
if (parsed_cookie.IsSameParty()) if (parsed_cookie.IsSameParty())
base::UmaHistogramBoolean("Cookie.IsSamePartyValid", is_same_party_valid); base::UmaHistogramBoolean("Cookie.IsSamePartyValid", is_same_party_valid);
// TODO(chlily): Log metrics.
if (!status->IsInclude()) if (!status->IsInclude())
return nullptr; return nullptr;
CookieSameSiteString samesite_string = CookieSameSiteString::kUnspecified; CookieSameSiteString samesite_string = CookieSameSiteString::kUnspecified;
CookieSameSite samesite = parsed_cookie.SameSite(&samesite_string); CookieSameSite samesite = parsed_cookie.SameSite(&samesite_string);
RecordCookieSameSiteAttributeValueHistogram(samesite_string,
parsed_cookie.IsSameParty());
CookieSourceScheme source_scheme = url.SchemeIsCryptographic() CookieSourceScheme source_scheme = url.SchemeIsCryptographic()
? CookieSourceScheme::kSecure ? CookieSourceScheme::kSecure
...@@ -449,9 +446,16 @@ std::unique_ptr<CanonicalCookie> CanonicalCookie::Create( ...@@ -449,9 +446,16 @@ std::unique_ptr<CanonicalCookie> CanonicalCookie::Create(
parsed_cookie.IsHttpOnly(), samesite, parsed_cookie.Priority(), parsed_cookie.IsHttpOnly(), samesite, parsed_cookie.Priority(),
parsed_cookie.IsSameParty(), source_scheme, source_port)); parsed_cookie.IsSameParty(), source_scheme, source_port));
DCHECK(cc->IsCanonical());
// TODO(chlily): Log metrics. // TODO(chlily): Log metrics.
if (!cc->IsCanonical()) {
status->AddExclusionReason(
net::CookieInclusionStatus::EXCLUDE_FAILURE_TO_STORE);
return nullptr;
}
RecordCookieSameSiteAttributeValueHistogram(samesite_string,
parsed_cookie.IsSameParty());
return cc; return cc;
} }
......
...@@ -194,6 +194,21 @@ TEST(CanonicalCookie, CreationCornerCases) { ...@@ -194,6 +194,21 @@ TEST(CanonicalCookie, CreationCornerCases) {
cookie = CanonicalCookie::Create(GURL("http://fool/;/"), "*", creation_time, cookie = CanonicalCookie::Create(GURL("http://fool/;/"), "*", creation_time,
server_time); server_time);
EXPECT_TRUE(cookie.get()); EXPECT_TRUE(cookie.get());
// Control characters in name or value.
CookieInclusionStatus status;
cookie =
CanonicalCookie::Create(GURL("http://www.example.com/test/foo.html"),
"\b=foo", creation_time, server_time, &status);
EXPECT_FALSE(cookie.get());
EXPECT_TRUE(status.HasExclusionReason(
CookieInclusionStatus::ExclusionReason::EXCLUDE_FAILURE_TO_STORE));
cookie =
CanonicalCookie::Create(GURL("http://www.example.com/test/foo.html"),
"bar=\b", creation_time, server_time, &status);
EXPECT_FALSE(cookie.get());
EXPECT_TRUE(status.HasExclusionReason(
CookieInclusionStatus::ExclusionReason::EXCLUDE_FAILURE_TO_STORE));
} }
TEST(CanonicalCookieTest, Create) { TEST(CanonicalCookieTest, Create) {
......
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