Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 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 | |
| 7 | #include "content/browser/devtools/devtools_instrumentation.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame^] | 8 | #include "content/browser/renderer_host/frame_tree_node.h" |
| 9 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 10 | #include "content/public/browser/browser_context.h" |
| 11 | #include "content/public/browser/cookie_access_details.h" |
| 12 | #include "content/public/common/content_client.h" |
Jihwan Marc Kim | 3e132f1 | 2020-05-20 17:33:19 | [diff] [blame] | 13 | #include "net/cookies/cookie_inclusion_status.h" |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 14 | #include "services/metrics/public/cpp/ukm_builders.h" |
| 15 | |
| 16 | namespace content { |
| 17 | |
| 18 | namespace { |
| 19 | |
Jihwan Marc Kim | 3e132f1 | 2020-05-20 17:33:19 | [diff] [blame] | 20 | void RecordContextDowngradeUKM(RenderFrameHost* rfh, |
| 21 | CookieAccessDetails::Type access_type, |
| 22 | const net::CookieInclusionStatus& status, |
| 23 | const GURL& url) { |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 24 | DCHECK(rfh); |
| 25 | ukm::SourceId source_id = rfh->GetPageUkmSourceId(); |
| 26 | |
| 27 | if (access_type == CookieAccessDetails::Type::kRead) { |
| 28 | ukm::builders::SchemefulSameSiteContextDowngrade(source_id) |
| 29 | .SetRequestPerCookie(status.GetBreakingDowngradeMetricsEnumValue(url)) |
| 30 | .Record(ukm::UkmRecorder::Get()); |
| 31 | } else { |
| 32 | DCHECK(access_type == CookieAccessDetails::Type::kChange); |
| 33 | ukm::builders::SchemefulSameSiteContextDowngrade(source_id) |
| 34 | .SetResponsePerCookie(status.GetBreakingDowngradeMetricsEnumValue(url)) |
| 35 | .Record(ukm::UkmRecorder::Get()); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | } // namespace |
| 40 | |
| 41 | void SplitCookiesIntoAllowedAndBlocked( |
| 42 | const network::mojom::CookieAccessDetailsPtr& cookie_details, |
| 43 | CookieAccessDetails* allowed, |
| 44 | CookieAccessDetails* blocked) { |
| 45 | *allowed = |
| 46 | CookieAccessDetails({cookie_details->type, |
| 47 | cookie_details->url, |
| 48 | cookie_details->site_for_cookies.RepresentativeUrl(), |
| 49 | {}, |
| 50 | /* blocked_by_policy=*/false}); |
| 51 | *blocked = |
| 52 | CookieAccessDetails({cookie_details->type, |
| 53 | cookie_details->url, |
| 54 | cookie_details->site_for_cookies.RepresentativeUrl(), |
| 55 | {}, |
| 56 | /* blocked_by_policy=*/true}); |
| 57 | |
Ayu Ishii | 2e399890 | 2020-07-14 18:22:30 | [diff] [blame] | 58 | for (auto& cookie_and_access_result : cookie_details->cookie_list) { |
Lily Chen | 70c537a | 2020-07-20 18:02:09 | [diff] [blame] | 59 | if (cookie_and_access_result.access_result.status.HasOnlyExclusionReason( |
Jihwan Marc Kim | 3e132f1 | 2020-05-20 17:33:19 | [diff] [blame] | 60 | net::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES)) { |
Ayu Ishii | 2e399890 | 2020-07-14 18:22:30 | [diff] [blame] | 61 | blocked->cookie_list.push_back( |
| 62 | std::move(cookie_and_access_result.cookie)); |
| 63 | } else if (cookie_and_access_result.access_result.status.IsInclude()) { |
| 64 | allowed->cookie_list.push_back( |
| 65 | std::move(cookie_and_access_result.cookie)); |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void EmitSameSiteCookiesDeprecationWarning( |
| 71 | RenderFrameHostImpl* rfh, |
| 72 | const network::mojom::CookieAccessDetailsPtr& cookie_details) { |
| 73 | RenderFrameHostImpl* root_frame_host = rfh->GetMainFrame(); |
| 74 | |
| 75 | if (!root_frame_host->IsCurrent()) |
| 76 | return; |
| 77 | |
| 78 | bool samesite_treated_as_lax_cookies = false; |
| 79 | bool samesite_none_insecure_cookies = false; |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 80 | bool breaking_context_downgrade = false; |
| 81 | |
Ayu Ishii | 2e399890 | 2020-07-14 18:22:30 | [diff] [blame] | 82 | for (const net::CookieWithAccessResult& excluded_cookie : |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 83 | cookie_details->cookie_list) { |
Ayu Ishii | 2e399890 | 2020-07-14 18:22:30 | [diff] [blame] | 84 | if (excluded_cookie.access_result.status.ShouldWarn()) { |
Lily Chen | 9de4065b | 2020-06-24 20:18:47 | [diff] [blame] | 85 | samesite_treated_as_lax_cookies = |
| 86 | samesite_treated_as_lax_cookies || |
Ayu Ishii | 2e399890 | 2020-07-14 18:22:30 | [diff] [blame] | 87 | excluded_cookie.access_result.status.HasWarningReason( |
Jihwan Marc Kim | 3e132f1 | 2020-05-20 17:33:19 | [diff] [blame] | 88 | net::CookieInclusionStatus:: |
Lily Chen | 9de4065b | 2020-06-24 20:18:47 | [diff] [blame] | 89 | WARN_SAMESITE_UNSPECIFIED_CROSS_SITE_CONTEXT) || |
Ayu Ishii | 2e399890 | 2020-07-14 18:22:30 | [diff] [blame] | 90 | excluded_cookie.access_result.status.HasWarningReason( |
Jihwan Marc Kim | 3e132f1 | 2020-05-20 17:33:19 | [diff] [blame] | 91 | net::CookieInclusionStatus:: |
Lily Chen | 9de4065b | 2020-06-24 20:18:47 | [diff] [blame] | 92 | WARN_SAMESITE_UNSPECIFIED_LAX_ALLOW_UNSAFE); |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 93 | |
Lily Chen | 9de4065b | 2020-06-24 20:18:47 | [diff] [blame] | 94 | samesite_none_insecure_cookies = |
| 95 | samesite_none_insecure_cookies || |
Ayu Ishii | 2e399890 | 2020-07-14 18:22:30 | [diff] [blame] | 96 | excluded_cookie.access_result.status.HasWarningReason( |
Lily Chen | 9de4065b | 2020-06-24 20:18:47 | [diff] [blame] | 97 | net::CookieInclusionStatus::WARN_SAMESITE_NONE_INSECURE); |
| 98 | |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 99 | devtools_instrumentation::ReportSameSiteCookieIssue( |
| 100 | root_frame_host, excluded_cookie, cookie_details->url, |
| 101 | cookie_details->site_for_cookies, |
| 102 | cookie_details->type == CookieAccessDetails::Type::kRead |
Sigurd Schneider | c579e9e | 2020-06-17 06:53:02 | [diff] [blame] | 103 | ? blink::mojom::SameSiteCookieOperation::kReadCookie |
| 104 | : blink::mojom::SameSiteCookieOperation::kSetCookie, |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 105 | cookie_details->devtools_request_id); |
| 106 | } |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 107 | |
Ayu Ishii | 2e399890 | 2020-07-14 18:22:30 | [diff] [blame] | 108 | breaking_context_downgrade = |
| 109 | breaking_context_downgrade || |
| 110 | excluded_cookie.access_result.status.HasDowngradeWarning(); |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 111 | |
Ayu Ishii | 2e399890 | 2020-07-14 18:22:30 | [diff] [blame] | 112 | if (excluded_cookie.access_result.status.HasDowngradeWarning()) { |
Steven Bingler | dad0334 | 2020-05-19 17:21:59 | [diff] [blame] | 113 | // Unlike with UMA, do not record cookies that have no downgrade warning. |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 114 | RecordContextDowngradeUKM(rfh, cookie_details->type, |
Ayu Ishii | 2e399890 | 2020-07-14 18:22:30 | [diff] [blame] | 115 | excluded_cookie.access_result.status, |
| 116 | cookie_details->url); |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Alexander Timin | 1cc31f4 | 2020-05-12 16:26:01 | [diff] [blame] | 120 | if (samesite_treated_as_lax_cookies) { |
| 121 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 122 | rfh, blink::mojom::WebFeature::kCookieNoSameSite); |
| 123 | } |
| 124 | |
| 125 | if (samesite_none_insecure_cookies) { |
| 126 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 127 | rfh, blink::mojom::WebFeature::kCookieInsecureAndSameSiteNone); |
| 128 | } |
| 129 | |
| 130 | if (breaking_context_downgrade) { |
| 131 | GetContentClient()->browser()->LogWebFeatureForCurrentPage( |
| 132 | rfh, blink::mojom::WebFeature::kSchemefulSameSiteContextDowngrade); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } // namespace content |