Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 5 | #include "content/browser/renderer_host/cookie_utils.h" |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 6 | |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 7 | #include <cstddef> |
| 8 | #include <ostream> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/feature_list.h" |
| 12 | #include "base/logging.h" |
Jayson Adams | 29543020 | 2021-07-27 01:07:57 | [diff] [blame] | 13 | #include "base/ranges/algorithm.h" |
sbingler | 8e26e21 | 2023-09-27 23:23:27 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
Shuran Huang | 7a6757c2 | 2024-04-17 15:17:39 | [diff] [blame] | 15 | #include "base/unguessable_token.h" |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 16 | #include "content/browser/devtools/devtools_instrumentation.h" |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 17 | #include "content/browser/navigation_or_document_handle.h" |
| 18 | #include "content/browser/renderer_host/navigation_request.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 19 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 20 | #include "content/public/browser/browser_context.h" |
| 21 | #include "content/public/browser/cookie_access_details.h" |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 22 | #include "content/public/browser/legacy_tech_cookie_issue_details.h" |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 23 | #include "content/public/common/content_client.h" |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 24 | #include "content/public/common/content_features.h" |
Jihwan Marc Kim | 3e132f1 | 2020-05-20 17:33:19 | [diff] [blame] | 25 | #include "net/cookies/cookie_inclusion_status.h" |
sbingler | 870b63f | 2023-04-25 16:47:58 | [diff] [blame] | 26 | #include "services/metrics/public/cpp/metrics_utils.h" |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 27 | #include "services/metrics/public/cpp/ukm_builders.h" |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 28 | #include "url/gurl.h" |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 29 | |
| 30 | namespace content { |
| 31 | |
| 32 | namespace { |
| 33 | |
sbingler | 8e26e21 | 2023-09-27 23:23:27 | [diff] [blame] | 34 | void PotentiallyRecordNonAsciiCookieNameValue( |
| 35 | RenderFrameHost* rfh, |
| 36 | CookieAccessDetails::Type access_type, |
| 37 | const std::string& name, |
| 38 | const std::string& value) { |
| 39 | CHECK(rfh); |
| 40 | |
| 41 | if (access_type != CookieAccessDetails::Type::kChange) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | // Our data collection policy disallows collecting UKMs while prerendering. |
| 46 | // See //content/browser/preloading/prerender/README.md and ask the team to |
| 47 | // explore options to record data for prerendering pages if we need to |
| 48 | // support the case. |
| 49 | if (rfh->IsInLifecycleState(RenderFrameHost::LifecycleState::kPrerendering)) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | bool name_has_non_ascii = !base::IsStringASCII(name); |
| 54 | bool value_has_non_ascii = !base::IsStringASCII(value); |
| 55 | |
| 56 | if (name_has_non_ascii || value_has_non_ascii) { |
| 57 | ukm::SourceId source_id = rfh->GetPageUkmSourceId(); |
| 58 | |
| 59 | auto event = ukm::builders::CookieHasNonAsciiCharacter(source_id); |
| 60 | |
| 61 | // The event itself is what we're interested in, the value of "true" here |
| 62 | // can be ignored. |
| 63 | if (name_has_non_ascii) { |
| 64 | event.SetName(true); |
| 65 | } |
| 66 | |
| 67 | if (value_has_non_ascii) { |
| 68 | event.SetValue(true); |
| 69 | } |
| 70 | |
| 71 | event.Record(ukm::UkmRecorder::Get()); |
| 72 | } |
| 73 | } |
| 74 | |
selya | 7d96d618 | 2024-04-22 11:35:51 | [diff] [blame^] | 75 | // Relies on checks in RecordPartitionedCookiesUKMs to confirm that that the |
| 76 | // cookie name is not "receive-cookie-deprecation", that cookie is first party |
| 77 | // partitioned and the RenderFrameHost is not prerendering. |
selya | 778265bc | 2023-10-23 14:37:55 | [diff] [blame] | 78 | void RecordFirstPartyPartitionedCookieCrossSiteContextUKM( |
| 79 | RenderFrameHostImpl* render_frame_host_impl, |
selya | 7d96d618 | 2024-04-22 11:35:51 | [diff] [blame^] | 80 | const net::CanonicalCookie& cookie, |
| 81 | const ukm::SourceId& source_id) { |
selya | 778265bc | 2023-10-23 14:37:55 | [diff] [blame] | 82 | // Same-site embed with cross-site ancestors (ABA embeds) have a null site |
| 83 | // for cookies since it is a cross-site context. If the result of |
| 84 | // ComputeSiteForCookies is first-party that means we are not in an ABA |
| 85 | // embedded context. |
selya | 01be4aa2 | 2024-01-25 18:36:24 | [diff] [blame] | 86 | bool has_cross_site_ancestor = |
| 87 | !render_frame_host_impl->ComputeSiteForCookies().IsFirstParty( |
selya | 778265bc | 2023-10-23 14:37:55 | [diff] [blame] | 88 | GURL(base::StrCat({url::kHttpsScheme, url::kStandardSchemeSeparator, |
selya | 01be4aa2 | 2024-01-25 18:36:24 | [diff] [blame] | 89 | cookie.DomainWithoutDot()}))); |
selya | 778265bc | 2023-10-23 14:37:55 | [diff] [blame] | 90 | |
selya | 7d96d618 | 2024-04-22 11:35:51 | [diff] [blame^] | 91 | ukm::builders::Cookies_FirstPartyPartitionedInCrossSiteContextV3(source_id) |
selya | 01be4aa2 | 2024-01-25 18:36:24 | [diff] [blame] | 92 | .SetCookiePresent(has_cross_site_ancestor) |
selya | 778265bc | 2023-10-23 14:37:55 | [diff] [blame] | 93 | .Record(ukm::UkmRecorder::Get()); |
| 94 | } |
| 95 | |
selya | 7d96d618 | 2024-04-22 11:35:51 | [diff] [blame^] | 96 | // Relies on checks in RecordPartitionedCookiesUKMs to confirm that that the |
| 97 | // cookie is partitioned, the cookie name is not |
| 98 | // "receive-cookie-deprecation" and the RenderFrameHost is not prerendering. |
| 99 | void RecordPartitionedCookieUseV2UKM(RenderFrameHost* rfh, |
| 100 | const net::CanonicalCookie& cookie, |
| 101 | const ukm::SourceId& source_id) { |
| 102 | ukm::builders::PartitionedCookiePresentV2(source_id) |
| 103 | .SetPartitionedCookiePresentV2(true) |
| 104 | .Record(ukm::UkmRecorder::Get()); |
| 105 | } |
| 106 | |
| 107 | void RecordPartitionedCookiesUKMs(RenderFrameHostImpl* render_frame_host_impl, |
| 108 | const net::CanonicalCookie& cookie) { |
selya | 27d093e | 2023-08-02 22:20:00 | [diff] [blame] | 109 | // Our data collection policy disallows collecting UKMs while prerendering. |
| 110 | // See //content/browser/preloading/prerender/README.md and ask the team to |
| 111 | // explore options to record data for prerendering pages if we need to |
| 112 | // support the case. |
selya | 7d96d618 | 2024-04-22 11:35:51 | [diff] [blame^] | 113 | if (render_frame_host_impl->IsInLifecycleState( |
| 114 | RenderFrameHost::LifecycleState::kPrerendering)) { |
selya | 27d093e | 2023-08-02 22:20:00 | [diff] [blame] | 115 | return; |
| 116 | } |
selya | 27d093e | 2023-08-02 22:20:00 | [diff] [blame] | 117 | |
selya | 7d96d618 | 2024-04-22 11:35:51 | [diff] [blame^] | 118 | // Cookies_FirstPartyPartitionedInCrossSiteContextV3 and |
| 119 | // PartitionedCookiePresentV2 both measure cookies |
| 120 | // without the name of 'receive-cookie-deprecation'. Return here to ensure |
| 121 | // that the metrics do not include those cookies. |
| 122 | if (cookie.Name() == "receive-cookie-deprecation") { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | ukm::SourceId source_id = render_frame_host_impl->GetPageUkmSourceId(); |
| 127 | |
| 128 | if (cookie.IsFirstPartyPartitioned()) { |
| 129 | RecordFirstPartyPartitionedCookieCrossSiteContextUKM(render_frame_host_impl, |
| 130 | cookie, source_id); |
| 131 | } |
| 132 | |
| 133 | RecordPartitionedCookieUseV2UKM(render_frame_host_impl, cookie, source_id); |
selya | 27d093e | 2023-08-02 22:20:00 | [diff] [blame] | 134 | } |
| 135 | |
sbingler | 870b63f | 2023-04-25 16:47:58 | [diff] [blame] | 136 | void RecordRedirectContextDowngradeUKM(RenderFrameHost* rfh, |
| 137 | CookieAccessDetails::Type access_type, |
| 138 | const net::CanonicalCookie& cookie, |
| 139 | const GURL& url) { |
| 140 | CHECK(rfh); |
Takashi Toyoshima | d540b93 | 2023-06-30 02:30:27 | [diff] [blame] | 141 | |
| 142 | // Our data collection policy disallows collecting UKMs while prerendering. |
| 143 | // See //content/browser/preloading/prerender/README.md and ask the team to |
| 144 | // explore options to record data for prerendering pages if we need to |
| 145 | // support the case. |
| 146 | if (rfh->IsInLifecycleState(RenderFrameHost::LifecycleState::kPrerendering)) { |
| 147 | return; |
| 148 | } |
| 149 | |
sbingler | 870b63f | 2023-04-25 16:47:58 | [diff] [blame] | 150 | ukm::SourceId source_id = rfh->GetPageUkmSourceId(); |
| 151 | |
| 152 | int64_t samesite_value = static_cast<int64_t>(cookie.SameSite()); |
| 153 | if (access_type == CookieAccessDetails::Type::kRead) { |
| 154 | base::TimeDelta cookie_age = base::Time::Now() - cookie.CreationDate(); |
| 155 | |
| 156 | ukm::builders::SamesiteRedirectContextDowngrade(source_id) |
| 157 | .SetSamesiteValueReadPerCookie(samesite_value) |
| 158 | .SetAgePerCookie( |
| 159 | ukm::GetExponentialBucketMinForUserTiming(cookie_age.InMinutes())) |
| 160 | .Record(ukm::UkmRecorder::Get()); |
| 161 | } else { |
| 162 | CHECK(access_type == CookieAccessDetails::Type::kChange); |
| 163 | ukm::builders::SamesiteRedirectContextDowngrade(source_id) |
| 164 | .SetSamesiteValueWritePerCookie(samesite_value) |
| 165 | .Record(ukm::UkmRecorder::Get()); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void RecordSchemefulContextDowngradeUKM( |
| 170 | RenderFrameHost* rfh, |
| 171 | CookieAccessDetails::Type access_type, |
| 172 | const net::CookieInclusionStatus& status, |
| 173 | const GURL& url) { |
| 174 | CHECK(rfh); |
Takashi Toyoshima | d540b93 | 2023-06-30 02:30:27 | [diff] [blame] | 175 | |
| 176 | // Our data collection policy disallows collecting UKMs while prerendering. |
| 177 | // See //content/browser/preloading/prerender/README.md and ask the team to |
| 178 | // explore options to record data for prerendering pages if we need to |
| 179 | // support the case. |
| 180 | if (rfh->IsInLifecycleState(RenderFrameHost::LifecycleState::kPrerendering)) { |
| 181 | return; |
| 182 | } |
| 183 | |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 184 | ukm::SourceId source_id = rfh->GetPageUkmSourceId(); |
| 185 | |
Lei Zhang | 3ab3019 | 2022-08-15 19:52:20 | [diff] [blame] | 186 | auto downgrade_metric = |
| 187 | static_cast<int64_t>(status.GetBreakingDowngradeMetricsEnumValue(url)); |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 188 | if (access_type == CookieAccessDetails::Type::kRead) { |
| 189 | ukm::builders::SchemefulSameSiteContextDowngrade(source_id) |
Lei Zhang | 3ab3019 | 2022-08-15 19:52:20 | [diff] [blame] | 190 | .SetRequestPerCookie(downgrade_metric) |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 191 | .Record(ukm::UkmRecorder::Get()); |
| 192 | } else { |
sbingler | 870b63f | 2023-04-25 16:47:58 | [diff] [blame] | 193 | CHECK(access_type == CookieAccessDetails::Type::kChange); |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 194 | ukm::builders::SchemefulSameSiteContextDowngrade(source_id) |
Lei Zhang | 3ab3019 | 2022-08-15 19:52:20 | [diff] [blame] | 195 | .SetResponsePerCookie(downgrade_metric) |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 196 | .Record(ukm::UkmRecorder::Get()); |
| 197 | } |
| 198 | } |
| 199 | |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 200 | // LINT.IfChange(should_report_dev_tools) |
Simon Zünd | 657178e | 2021-05-27 06:19:55 | [diff] [blame] | 201 | bool ShouldReportDevToolsIssueForStatus( |
| 202 | const net::CookieInclusionStatus& status) { |
| 203 | return status.ShouldWarn() || |
| 204 | status.HasExclusionReason( |
Dylan Cutler | 15fdd1e | 2022-11-15 22:54:46 | [diff] [blame] | 205 | net::CookieInclusionStatus::EXCLUDE_DOMAIN_NON_ASCII) || |
| 206 | status.HasExclusionReason( |
| 207 | net::CookieInclusionStatus:: |
Shuran Huang | dc0b8d8 | 2023-09-19 14:49:30 | [diff] [blame] | 208 | EXCLUDE_THIRD_PARTY_BLOCKED_WITHIN_FIRST_PARTY_SET) || |
| 209 | status.HasExclusionReason( |
| 210 | net::CookieInclusionStatus::EXCLUDE_THIRD_PARTY_PHASEOUT); |
Simon Zünd | 657178e | 2021-05-27 06:19:55 | [diff] [blame] | 211 | } |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 212 | // LINT.ThenChange(//content/browser/renderer_host/cookie_utils.cc:should_report_legacy_tech_report) |
Simon Zünd | 657178e | 2021-05-27 06:19:55 | [diff] [blame] | 213 | |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 214 | // LINT.IfChange(should_report_legacy_tech_report) |
| 215 | bool ShouldReportLegacyTechIssueForStatus( |
| 216 | const net::CookieInclusionStatus& status) { |
| 217 | return status.HasExclusionReason( |
| 218 | net::CookieInclusionStatus:: |
| 219 | EXCLUDE_THIRD_PARTY_BLOCKED_WITHIN_FIRST_PARTY_SET) || |
| 220 | status.HasExclusionReason( |
| 221 | net::CookieInclusionStatus::EXCLUDE_THIRD_PARTY_PHASEOUT) || |
| 222 | status.HasWarningReason( |
| 223 | net::CookieInclusionStatus::WARN_THIRD_PARTY_PHASEOUT); |
| 224 | } |
| 225 | // LINT.ThenChange(//content/browser/renderer_host/cookie_utils.cc:should_report_dev_tools) |
| 226 | |
| 227 | // Logs cookie issues to DevTools Issues Panel and logs events to UseCounters |
Shuran Huang | 8fa11c7 | 2023-12-04 16:53:44 | [diff] [blame] | 228 | // and UKM for a single cookie-accessed event. |
Sandor Major | 0d7fd629 | 2023-10-26 20:15:44 | [diff] [blame] | 229 | // TODO(crbug.com/977040): Remove when no longer needed. |
| 230 | void EmitCookieWarningsAndMetricsOnce( |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 231 | RenderFrameHostImpl* rfh, |
| 232 | const network::mojom::CookieAccessDetailsPtr& cookie_details) { |
| 233 | RenderFrameHostImpl* root_frame_host = rfh->GetMainFrame(); |
| 234 | |
Sreeja Kamishetty | e49854f8 | 2021-06-02 00:52:03 | [diff] [blame] | 235 | if (!root_frame_host->IsActive()) |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 236 | return; |
| 237 | |
| 238 | bool samesite_treated_as_lax_cookies = false; |
| 239 | bool samesite_none_insecure_cookies = false; |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 240 | bool breaking_context_downgrade = false; |
Lily Chen | c4423c0 | 2021-03-11 16:02:02 | [diff] [blame] | 241 | bool lax_allow_unsafe_cookies = false; |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 242 | |
Lily Chen | 2db3a42 | 2021-07-20 18:02:25 | [diff] [blame] | 243 | bool samesite_cookie_inclusion_changed_by_cross_site_redirect = false; |
| 244 | |
Dylan Cutler | 8d5f891 | 2022-03-04 17:39:19 | [diff] [blame] | 245 | bool partitioned_cookies_exist = false; |
| 246 | |
Ari Chivukula | 5f21c11 | 2022-04-26 19:23:34 | [diff] [blame] | 247 | bool cookie_has_not_been_refreshed_in_201_to_300_days = false; |
| 248 | bool cookie_has_not_been_refreshed_in_301_to_350_days = false; |
| 249 | bool cookie_has_not_been_refreshed_in_351_to_400_days = false; |
| 250 | |
Johann Hofmann | e5764d1 | 2022-07-13 23:06:28 | [diff] [blame] | 251 | bool cookie_has_domain_non_ascii = false; |
| 252 | |
cfredric | 76b2d22 | 2021-01-27 20:12:04 | [diff] [blame] | 253 | for (const network::mojom::CookieOrLineWithAccessResultPtr& cookie : |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 254 | cookie_details->cookie_list) { |
Johann Hofmann | 975c0b4 | 2022-08-02 21:05:51 | [diff] [blame] | 255 | const net::CookieInclusionStatus& status = cookie->access_result.status; |
| 256 | if (ShouldReportDevToolsIssueForStatus(status)) { |
Shuran Huang | 7a6757c2 | 2024-04-17 15:17:39 | [diff] [blame] | 257 | std::optional<std::string> devtools_issue_id; |
| 258 | if (status.HasExclusionReason( |
| 259 | net::CookieInclusionStatus::EXCLUDE_THIRD_PARTY_PHASEOUT) || |
| 260 | status.HasWarningReason( |
| 261 | net::CookieInclusionStatus::WARN_THIRD_PARTY_PHASEOUT)) { |
| 262 | devtools_issue_id = base::UnguessableToken::Create().ToString(); |
| 263 | } |
Juba Borgohain | c93969e | 2022-02-25 21:56:12 | [diff] [blame] | 264 | devtools_instrumentation::ReportCookieIssue( |
Simon Zünd | 657178e | 2021-05-27 06:19:55 | [diff] [blame] | 265 | root_frame_host, cookie, cookie_details->url, |
| 266 | cookie_details->site_for_cookies, |
| 267 | cookie_details->type == CookieAccessDetails::Type::kRead |
Juba Borgohain | c93969e | 2022-02-25 21:56:12 | [diff] [blame] | 268 | ? blink::mojom::CookieOperation::kReadCookie |
| 269 | : blink::mojom::CookieOperation::kSetCookie, |
Shuran Huang | 7a6757c2 | 2024-04-17 15:17:39 | [diff] [blame] | 270 | cookie_details->devtools_request_id, devtools_issue_id); |
Shuran Huang | 8fa11c7 | 2023-12-04 16:53:44 | [diff] [blame] | 271 | } |
| 272 | |
cfredric | 76b2d22 | 2021-01-27 20:12:04 | [diff] [blame] | 273 | if (cookie->access_result.status.ShouldWarn()) { |
Lily Chen | 9de4065b | 2020-06-24 20:18:47 | [diff] [blame] | 274 | samesite_treated_as_lax_cookies = |
| 275 | samesite_treated_as_lax_cookies || |
cfredric | a5fb098 | 2021-01-09 00:18:01 | [diff] [blame] | 276 | status.HasWarningReason( |
Jihwan Marc Kim | 3e132f1 | 2020-05-20 17:33:19 | [diff] [blame] | 277 | net::CookieInclusionStatus:: |
Lily Chen | 9de4065b | 2020-06-24 20:18:47 | [diff] [blame] | 278 | WARN_SAMESITE_UNSPECIFIED_CROSS_SITE_CONTEXT) || |
cfredric | a5fb098 | 2021-01-09 00:18:01 | [diff] [blame] | 279 | status.HasWarningReason( |
Jihwan Marc Kim | 3e132f1 | 2020-05-20 17:33:19 | [diff] [blame] | 280 | net::CookieInclusionStatus:: |
Lily Chen | 9de4065b | 2020-06-24 20:18:47 | [diff] [blame] | 281 | WARN_SAMESITE_UNSPECIFIED_LAX_ALLOW_UNSAFE); |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 282 | |
Lily Chen | 9de4065b | 2020-06-24 20:18:47 | [diff] [blame] | 283 | samesite_none_insecure_cookies = |
| 284 | samesite_none_insecure_cookies || |
cfredric | a5fb098 | 2021-01-09 00:18:01 | [diff] [blame] | 285 | status.HasWarningReason( |
Lily Chen | 9de4065b | 2020-06-24 20:18:47 | [diff] [blame] | 286 | net::CookieInclusionStatus::WARN_SAMESITE_NONE_INSECURE); |
| 287 | |
Lily Chen | c4423c0 | 2021-03-11 16:02:02 | [diff] [blame] | 288 | lax_allow_unsafe_cookies = |
| 289 | lax_allow_unsafe_cookies || |
| 290 | status.HasWarningReason( |
| 291 | net::CookieInclusionStatus:: |
| 292 | WARN_SAMESITE_UNSPECIFIED_LAX_ALLOW_UNSAFE); |
| 293 | |
Lily Chen | 2db3a42 | 2021-07-20 18:02:25 | [diff] [blame] | 294 | samesite_cookie_inclusion_changed_by_cross_site_redirect = |
| 295 | samesite_cookie_inclusion_changed_by_cross_site_redirect || |
| 296 | status.HasWarningReason( |
| 297 | net::CookieInclusionStatus:: |
| 298 | WARN_CROSS_SITE_REDIRECT_DOWNGRADE_CHANGES_INCLUSION); |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 299 | } |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 300 | |
Johann Hofmann | 975c0b4 | 2022-08-02 21:05:51 | [diff] [blame] | 301 | cookie_has_domain_non_ascii = |
| 302 | cookie_has_domain_non_ascii || |
| 303 | status.HasWarningReason( |
| 304 | net::CookieInclusionStatus::WARN_DOMAIN_NON_ASCII) || |
| 305 | status.HasExclusionReason( |
| 306 | net::CookieInclusionStatus::EXCLUDE_DOMAIN_NON_ASCII); |
| 307 | |
Dylan Cutler | 8d5f891 | 2022-03-04 17:39:19 | [diff] [blame] | 308 | partitioned_cookies_exist = |
| 309 | partitioned_cookies_exist || |
| 310 | (cookie->cookie_or_line->is_cookie() && |
Dylan Cutler | 051411b4 | 2022-07-12 22:20:07 | [diff] [blame] | 311 | cookie->cookie_or_line->get_cookie().IsPartitioned() && |
| 312 | // Ignore nonced partition keys since this metric is meant to track |
| 313 | // usage of the Partitioned attribute. |
| 314 | !cookie->cookie_or_line->get_cookie().PartitionKey()->nonce()); |
Dylan Cutler | 8d5f891 | 2022-03-04 17:39:19 | [diff] [blame] | 315 | |
selya | 27d093e | 2023-08-02 22:20:00 | [diff] [blame] | 316 | |
selya | 778265bc | 2023-10-23 14:37:55 | [diff] [blame] | 317 | if (partitioned_cookies_exist) { |
selya | 7d96d618 | 2024-04-22 11:35:51 | [diff] [blame^] | 318 | RecordPartitionedCookiesUKMs(rfh, cookie->cookie_or_line->get_cookie()); |
selya | 778265bc | 2023-10-23 14:37:55 | [diff] [blame] | 319 | } |
| 320 | |
Ayu Ishii | 2e399890 | 2020-07-14 18:22:30 | [diff] [blame] | 321 | breaking_context_downgrade = |
| 322 | breaking_context_downgrade || |
sbingler | 870b63f | 2023-04-25 16:47:58 | [diff] [blame] | 323 | cookie->access_result.status.HasSchemefulDowngradeWarning(); |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 324 | |
sbingler | 870b63f | 2023-04-25 16:47:58 | [diff] [blame] | 325 | if (cookie->access_result.status.HasSchemefulDowngradeWarning()) { |
| 326 | // Unlike with UMA, do not record cookies that have no schemeful downgrade |
| 327 | // warning. |
| 328 | RecordSchemefulContextDowngradeUKM(rfh, cookie_details->type, |
| 329 | cookie->access_result.status, |
| 330 | cookie_details->url); |
| 331 | } |
| 332 | |
| 333 | if (status.HasWarningReason( |
| 334 | net::CookieInclusionStatus:: |
| 335 | WARN_CROSS_SITE_REDIRECT_DOWNGRADE_CHANGES_INCLUSION) && |
| 336 | cookie->cookie_or_line->is_cookie()) { |
| 337 | RecordRedirectContextDowngradeUKM(rfh, cookie_details->type, |
| 338 | cookie->cookie_or_line->get_cookie(), |
| 339 | cookie_details->url); |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 340 | } |
Ari Chivukula | 5f21c11 | 2022-04-26 19:23:34 | [diff] [blame] | 341 | |
sbingler | 8e26e21 | 2023-09-27 23:23:27 | [diff] [blame] | 342 | if (cookie->cookie_or_line->is_cookie()) { |
| 343 | PotentiallyRecordNonAsciiCookieNameValue( |
| 344 | rfh, cookie_details->type, |
| 345 | cookie->cookie_or_line->get_cookie().Name(), |
| 346 | cookie->cookie_or_line->get_cookie().Value()); |
| 347 | } |
| 348 | |
Ari Chivukula | 5f21c11 | 2022-04-26 19:23:34 | [diff] [blame] | 349 | // In order to anticipate the potential effects of the expiry limit in |
| 350 | // rfc6265bis, we need to check how long it's been since the cookie was |
| 351 | // refreshed (if LastUpdateDate is populated). These three buckets were |
| 352 | // picked so we could engage sites with some granularity around urgency. |
| 353 | // We ignore the space under 200 days as these cookies are not at risk |
| 354 | // of expiring and we ignore the space over 400 days as these cookies |
| 355 | // have already expired. Metrics will take 200 days from M103 to populate. |
| 356 | base::Time last_update_date = |
| 357 | cookie->cookie_or_line->is_cookie() |
| 358 | ? cookie->cookie_or_line->get_cookie().LastUpdateDate() |
| 359 | : base::Time(); |
| 360 | if (!last_update_date.is_null()) { |
| 361 | int days_since_refresh = (base::Time::Now() - last_update_date).InDays(); |
| 362 | cookie_has_not_been_refreshed_in_201_to_300_days |= |
| 363 | days_since_refresh > 200 && days_since_refresh <= 300; |
| 364 | cookie_has_not_been_refreshed_in_301_to_350_days |= |
| 365 | days_since_refresh > 300 && days_since_refresh <= 350; |
| 366 | cookie_has_not_been_refreshed_in_351_to_400_days |= |
| 367 | days_since_refresh > 350 && days_since_refresh <= 400; |
| 368 | } |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 369 | } |
| 370 | |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 371 | if (samesite_treated_as_lax_cookies) { |
| 372 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 373 | rfh, blink::mojom::WebFeature::kCookieNoSameSite); |
| 374 | } |
| 375 | |
| 376 | if (samesite_none_insecure_cookies) { |
| 377 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 378 | rfh, blink::mojom::WebFeature::kCookieInsecureAndSameSiteNone); |
| 379 | } |
| 380 | |
| 381 | if (breaking_context_downgrade) { |
| 382 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 383 | rfh, blink::mojom::WebFeature::kSchemefulSameSiteContextDowngrade); |
| 384 | } |
cfredric | a5fb098 | 2021-01-09 00:18:01 | [diff] [blame] | 385 | |
Lily Chen | c4423c0 | 2021-03-11 16:02:02 | [diff] [blame] | 386 | if (lax_allow_unsafe_cookies) { |
| 387 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 388 | rfh, blink::mojom::WebFeature::kLaxAllowingUnsafeCookies); |
| 389 | } |
| 390 | |
Lily Chen | 2db3a42 | 2021-07-20 18:02:25 | [diff] [blame] | 391 | if (samesite_cookie_inclusion_changed_by_cross_site_redirect) { |
| 392 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 393 | rfh, blink::mojom::WebFeature:: |
| 394 | kSameSiteCookieInclusionChangedByCrossSiteRedirect); |
| 395 | } |
Dylan Cutler | 8d5f891 | 2022-03-04 17:39:19 | [diff] [blame] | 396 | |
| 397 | if (partitioned_cookies_exist) { |
| 398 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 399 | rfh, blink::mojom::WebFeature::kPartitionedCookies); |
| 400 | } |
Ari Chivukula | 5f21c11 | 2022-04-26 19:23:34 | [diff] [blame] | 401 | |
| 402 | if (cookie_has_not_been_refreshed_in_201_to_300_days) { |
| 403 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 404 | rfh, |
| 405 | blink::mojom::WebFeature::kCookieHasNotBeenRefreshedIn201To300Days); |
| 406 | } |
| 407 | |
| 408 | if (cookie_has_not_been_refreshed_in_301_to_350_days) { |
| 409 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 410 | rfh, |
| 411 | blink::mojom::WebFeature::kCookieHasNotBeenRefreshedIn301To350Days); |
| 412 | } |
| 413 | |
| 414 | if (cookie_has_not_been_refreshed_in_351_to_400_days) { |
| 415 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 416 | rfh, |
| 417 | blink::mojom::WebFeature::kCookieHasNotBeenRefreshedIn351To400Days); |
| 418 | } |
Johann Hofmann | e5764d1 | 2022-07-13 23:06:28 | [diff] [blame] | 419 | |
| 420 | if (cookie_has_domain_non_ascii) { |
| 421 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 422 | rfh, blink::mojom::WebFeature::kCookieDomainNonASCII); |
| 423 | } |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 424 | } |
| 425 | |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 426 | // Logs cookie issues to Legacy Technology Report. |
| 427 | void ReportLegacyTechEvent( |
| 428 | RenderFrameHostImpl* render_frame_host, |
| 429 | NavigationRequest* navigation_request, |
| 430 | const network::mojom::CookieAccessDetailsPtr& cookie_details) { |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 431 | CHECK(render_frame_host); |
| 432 | |
| 433 | for (const network::mojom::CookieOrLineWithAccessResultPtr& cookie : |
| 434 | cookie_details->cookie_list) { |
| 435 | const net::CookieInclusionStatus& status = cookie->access_result.status; |
| 436 | if (ShouldReportLegacyTechIssueForStatus(status) && |
| 437 | cookie->cookie_or_line->is_cookie()) { |
| 438 | std::string type; |
| 439 | if (status.HasExclusionReason( |
| 440 | net::CookieInclusionStatus:: |
| 441 | EXCLUDE_THIRD_PARTY_BLOCKED_WITHIN_FIRST_PARTY_SET) || |
| 442 | status.HasExclusionReason( |
| 443 | net::CookieInclusionStatus::EXCLUDE_THIRD_PARTY_PHASEOUT)) { |
| 444 | type = "ThirdPartyCookieAccessError"; |
| 445 | } else if (status.HasWarningReason( |
| 446 | net::CookieInclusionStatus::WARN_THIRD_PARTY_PHASEOUT)) { |
| 447 | type = "ThirdPartyCookieAccessWarning"; |
| 448 | } else { |
| 449 | DLOG(ERROR) << "Unexpected call of ReportLegacyTechEvent."; |
| 450 | } |
| 451 | |
| 452 | GURL url = render_frame_host->GetOutermostMainFrameOrEmbedder() |
| 453 | ->GetLastCommittedURL(); |
| 454 | GURL frame_url = render_frame_host->GetLastCommittedURL(); |
| 455 | if (navigation_request != nullptr) { |
| 456 | if (!navigation_request->frame_tree_node() |
| 457 | ->GetParentOrOuterDocumentOrEmbedder()) { |
| 458 | url = navigation_request->GetURL(); |
| 459 | frame_url = navigation_request->GetURL(); |
| 460 | } else { |
| 461 | frame_url = navigation_request->GetURL(); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | LegacyTechCookieIssueDetails cookie_issue_details = { |
| 466 | cookie_details->url.spec(), |
| 467 | cookie->cookie_or_line->get_cookie().Name(), |
| 468 | cookie->cookie_or_line->get_cookie().Domain(), |
| 469 | cookie->cookie_or_line->get_cookie().Path(), |
| 470 | cookie_details->type == CookieAccessDetails::Type::kChange |
| 471 | ? LegacyTechCookieIssueDetails::AccessOperation::kWrite |
| 472 | : LegacyTechCookieIssueDetails::AccessOperation::kRead}; |
| 473 | |
| 474 | GetContentClient()->browser()->ReportLegacyTechEvent( |
| 475 | render_frame_host, type, url, frame_url, /*filename=*/"", /*line=*/0, |
| 476 | /*column=*/0, cookie_issue_details); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
Sandor Major | 0d7fd629 | 2023-10-26 20:15:44 | [diff] [blame] | 481 | } // namespace |
| 482 | |
| 483 | void SplitCookiesIntoAllowedAndBlocked( |
| 484 | const network::mojom::CookieAccessDetailsPtr& cookie_details, |
| 485 | CookieAccessDetails* allowed, |
| 486 | CookieAccessDetails* blocked) { |
Victor Tan | e1f2bc6 | 2023-11-15 00:12:20 | [diff] [blame] | 487 | // For some cases `site_for_cookies` representative url is empty when |
| 488 | // OnCookieAccess is triggered for a third party. For example iframe third |
| 489 | // party accesses cookies when TPCD Metadata allows third party cookie access. |
| 490 | // |
| 491 | // Make `first_party_url` considering both `top_frame_origin` and |
| 492 | // `site_for_cookies` which is similar with GetFirstPartyURL() in |
| 493 | // components/content_settings/core/common/cookie_settings_base.h. |
| 494 | // If the `top_frame_origin` is non-opaque, it is chosen; otherwise, the |
| 495 | // `site_for_cookies` representative url is used. |
| 496 | const GURL first_party_url = |
| 497 | cookie_details->top_frame_origin.opaque() |
| 498 | ? cookie_details->site_for_cookies.RepresentativeUrl() |
| 499 | : cookie_details->top_frame_origin.GetURL(); |
| 500 | |
| 501 | *allowed = CookieAccessDetails({cookie_details->type, |
| 502 | cookie_details->url, |
| 503 | first_party_url, |
| 504 | {}, |
| 505 | cookie_details->count, |
| 506 | /* blocked_by_policy=*/false, |
Victor Tan | f288abf | 2023-11-15 05:33:54 | [diff] [blame] | 507 | cookie_details->is_ad_tagged, |
Dylan Cutler | 6aea592 | 2024-04-19 18:23:47 | [diff] [blame] | 508 | cookie_details->cookie_setting_overrides, |
| 509 | cookie_details->site_for_cookies}); |
Sandor Major | 0d7fd629 | 2023-10-26 20:15:44 | [diff] [blame] | 510 | int allowed_count = base::ranges::count_if( |
| 511 | cookie_details->cookie_list, |
| 512 | [](const network::mojom::CookieOrLineWithAccessResultPtr& |
| 513 | cookie_and_access_result) { |
| 514 | // "Included" cookies have no exclusion reasons so we don't also have to |
| 515 | // check for !(net::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES). |
| 516 | return cookie_and_access_result->access_result.status.IsInclude(); |
| 517 | }); |
| 518 | allowed->cookie_list.reserve(allowed_count); |
| 519 | |
Victor Tan | e1f2bc6 | 2023-11-15 00:12:20 | [diff] [blame] | 520 | *blocked = CookieAccessDetails({cookie_details->type, |
| 521 | cookie_details->url, |
| 522 | first_party_url, |
| 523 | {}, |
| 524 | cookie_details->count, |
| 525 | /* blocked_by_policy=*/true, |
Victor Tan | f288abf | 2023-11-15 05:33:54 | [diff] [blame] | 526 | cookie_details->is_ad_tagged, |
Dylan Cutler | 6aea592 | 2024-04-19 18:23:47 | [diff] [blame] | 527 | cookie_details->cookie_setting_overrides, |
| 528 | cookie_details->site_for_cookies}); |
Sandor Major | 0d7fd629 | 2023-10-26 20:15:44 | [diff] [blame] | 529 | int blocked_count = base::ranges::count_if( |
| 530 | cookie_details->cookie_list, |
| 531 | [](const network::mojom::CookieOrLineWithAccessResultPtr& |
| 532 | cookie_and_access_result) { |
| 533 | return cookie_and_access_result->access_result.status |
Shuran Huang | 2359388 | 2024-04-11 14:19:32 | [diff] [blame] | 534 | .ExcludedByUserPreferencesOrTPCD(); |
Sandor Major | 0d7fd629 | 2023-10-26 20:15:44 | [diff] [blame] | 535 | }); |
| 536 | blocked->cookie_list.reserve(blocked_count); |
| 537 | |
| 538 | for (const auto& cookie_and_access_result : cookie_details->cookie_list) { |
| 539 | if (cookie_and_access_result->access_result.status |
Shuran Huang | 2359388 | 2024-04-11 14:19:32 | [diff] [blame] | 540 | .ExcludedByUserPreferencesOrTPCD()) { |
Sandor Major | 0d7fd629 | 2023-10-26 20:15:44 | [diff] [blame] | 541 | blocked->cookie_list.emplace_back( |
| 542 | std::move(cookie_and_access_result->cookie_or_line->get_cookie())); |
| 543 | } else if (cookie_and_access_result->access_result.status.IsInclude()) { |
| 544 | allowed->cookie_list.emplace_back( |
| 545 | std::move(cookie_and_access_result->cookie_or_line->get_cookie())); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | void EmitCookieWarningsAndMetrics( |
| 551 | RenderFrameHostImpl* rfh, |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 552 | NavigationRequest* navigation_request, |
Sandor Major | 0d7fd629 | 2023-10-26 20:15:44 | [diff] [blame] | 553 | const network::mojom::CookieAccessDetailsPtr& cookie_details) { |
Sandor Major | 7c3e612f | 2023-12-01 20:48:08 | [diff] [blame] | 554 | ReportLegacyTechEvent(rfh, navigation_request, cookie_details); |
Sandor Major | 0d7fd629 | 2023-10-26 20:15:44 | [diff] [blame] | 555 | for (size_t i = 0; i < cookie_details->count; ++i) { |
| 556 | EmitCookieWarningsAndMetricsOnce(rfh, cookie_details); |
| 557 | } |
| 558 | } |
| 559 | |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 560 | } // namespace content |