Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1 | // Copyright 2017 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 | |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 5 | #include <cctype> |
| 6 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 7 | #include "base/bind.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 8 | #include "base/command_line.h" |
Gabriel Charette | b71eec89 | 2017-09-14 22:52:56 | [diff] [blame] | 9 | #include "base/run_loop.h" |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 10 | #include "base/stl_util.h" |
Devlin Cronin | 626d80c | 2018-06-01 01:08:36 | [diff] [blame^] | 11 | #include "base/test/metrics/histogram_tester.h" |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 12 | #include "build/build_config.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 13 | #include "chrome/browser/content_settings/cookie_settings_factory.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 14 | #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 15 | #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 16 | #include "chrome/browser/metrics/subprocess_metrics_provider.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 17 | #include "chrome/browser/profiles/profile.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 18 | #include "chrome/browser/ui/browser.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 19 | #include "chrome/test/base/in_process_browser_test.h" |
| 20 | #include "chrome/test/base/ui_test_utils.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 21 | #include "components/content_settings/core/browser/cookie_settings.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 22 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 23 | #include "components/content_settings/core/common/pref_names.h" |
| 24 | #include "components/prefs/pref_service.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 25 | #include "content/public/browser/browser_thread.h" |
| 26 | #include "content/public/common/content_switches.h" |
| 27 | #include "content/public/test/browser_test_utils.h" |
| 28 | #include "content/public/test/test_utils.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 29 | #include "content/public/test/url_loader_interceptor.h" |
| 30 | #include "net/dns/mock_host_resolver.h" |
| 31 | #include "net/http/http_request_headers.h" |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 32 | #include "net/nqe/effective_connection_type.h" |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 33 | #include "net/test/embedded_test_server/embedded_test_server.h" |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 34 | #include "net/test/embedded_test_server/http_request.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 35 | #include "net/test/embedded_test_server/http_response.h" |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 36 | #include "services/network/public/cpp/cors/cors.h" |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 37 | #include "services/network/public/cpp/features.h" |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 38 | #include "services/network/public/cpp/network_switches.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 39 | #include "third_party/blink/public/common/client_hints/client_hints.h" |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 40 | |
| 41 | namespace { |
| 42 | |
| 43 | // An interceptor that records count of fetches and client hint headers for |
| 44 | // requests to https://foo.com/non-existing-image.jpg. |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 45 | class ThirdPartyURLLoaderInterceptor { |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 46 | public: |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 47 | explicit ThirdPartyURLLoaderInterceptor(const GURL intercepted_url) |
| 48 | : intercepted_url_(intercepted_url), |
| 49 | interceptor_(base::BindRepeating( |
| 50 | &ThirdPartyURLLoaderInterceptor::InterceptURLRequest, |
| 51 | base::Unretained(this))) {} |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 52 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 53 | ~ThirdPartyURLLoaderInterceptor() = default; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 54 | |
| 55 | size_t request_count_seen() const { return request_count_seen_; } |
| 56 | |
| 57 | size_t client_hints_count_seen() const { return client_hints_count_seen_; } |
| 58 | |
| 59 | private: |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 60 | bool InterceptURLRequest( |
| 61 | content::URLLoaderInterceptor::RequestParams* params) { |
| 62 | if (params->url_request.url != intercepted_url_) |
| 63 | return false; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 64 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 65 | request_count_seen_++; |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 66 | for (size_t i = 0; i < blink::kClientHintsHeaderMappingCount; ++i) { |
| 67 | if (params->url_request.headers.HasHeader( |
| 68 | blink::kClientHintsHeaderMapping[i])) { |
| 69 | client_hints_count_seen_++; |
| 70 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 71 | } |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 72 | return false; |
| 73 | } |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 74 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 75 | GURL intercepted_url_; |
| 76 | |
| 77 | size_t request_count_seen_ = 0u; |
| 78 | |
| 79 | size_t client_hints_count_seen_ = 0u; |
| 80 | |
| 81 | content::URLLoaderInterceptor interceptor_; |
| 82 | |
| 83 | DISALLOW_COPY_AND_ASSIGN(ThirdPartyURLLoaderInterceptor); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 84 | }; |
| 85 | |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 86 | // Returns true only if |header_value| satisfies ABNF: 1*DIGIT [ "." 1*DIGIT ] |
| 87 | bool IsSimilarToDoubleABNF(const std::string& header_value) { |
| 88 | if (header_value.empty()) |
| 89 | return false; |
| 90 | char first_char = header_value.at(0); |
| 91 | if (!isdigit(first_char)) |
| 92 | return false; |
| 93 | |
| 94 | bool period_found = false; |
| 95 | bool digit_found_after_period = false; |
| 96 | for (char ch : header_value) { |
| 97 | if (isdigit(ch)) { |
| 98 | if (period_found) { |
| 99 | digit_found_after_period = true; |
| 100 | } |
| 101 | continue; |
| 102 | } |
| 103 | if (ch == '.') { |
| 104 | if (period_found) |
| 105 | return false; |
| 106 | period_found = true; |
| 107 | continue; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | if (period_found) |
| 112 | return digit_found_after_period; |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | // Returns true only if |header_value| satisfies ABNF: 1*DIGIT |
| 117 | bool IsSimilarToIntABNF(const std::string& header_value) { |
| 118 | if (header_value.empty()) |
| 119 | return false; |
| 120 | |
| 121 | for (char ch : header_value) { |
| 122 | if (!isdigit(ch)) |
| 123 | return false; |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 128 | } // namespace |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 129 | |
| 130 | class ClientHintsBrowserTest : public InProcessBrowserTest { |
| 131 | public: |
| 132 | ClientHintsBrowserTest() |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 133 | : http_server_(net::EmbeddedTestServer::TYPE_HTTP), |
| 134 | https_server_(net::EmbeddedTestServer::TYPE_HTTPS), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 135 | expect_client_hints_on_main_frame_(false), |
| 136 | expect_client_hints_on_subresources_(false), |
| 137 | count_client_hints_headers_seen_(0), |
| 138 | request_interceptor_(nullptr) { |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 139 | http_server_.ServeFilesFromSourceDirectory("chrome/test/data/client_hints"); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 140 | https_server_.ServeFilesFromSourceDirectory( |
| 141 | "chrome/test/data/client_hints"); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 142 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 143 | http_server_.RegisterRequestMonitor( |
| 144 | base::Bind(&ClientHintsBrowserTest::MonitorResourceRequest, |
| 145 | base::Unretained(this))); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 146 | https_server_.RegisterRequestMonitor( |
| 147 | base::Bind(&ClientHintsBrowserTest::MonitorResourceRequest, |
| 148 | base::Unretained(this))); |
| 149 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 150 | EXPECT_TRUE(http_server_.Start()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 151 | EXPECT_TRUE(https_server_.Start()); |
| 152 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 153 | accept_ch_with_lifetime_http_local_url_ = |
| 154 | http_server_.GetURL("/accept_ch_with_lifetime.html"); |
| 155 | EXPECT_TRUE(accept_ch_with_lifetime_http_local_url_.SchemeIsHTTPOrHTTPS()); |
| 156 | EXPECT_FALSE( |
| 157 | accept_ch_with_lifetime_http_local_url_.SchemeIsCryptographic()); |
| 158 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 159 | accept_ch_with_lifetime_url_ = |
| 160 | https_server_.GetURL("/accept_ch_with_lifetime.html"); |
| 161 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsHTTPOrHTTPS()); |
| 162 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsCryptographic()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 163 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 164 | accept_ch_without_lifetime_url_ = |
| 165 | https_server_.GetURL("/accept_ch_without_lifetime.html"); |
| 166 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsHTTPOrHTTPS()); |
| 167 | EXPECT_TRUE(accept_ch_with_lifetime_url_.SchemeIsCryptographic()); |
| 168 | |
| 169 | without_accept_ch_without_lifetime_url_ = |
| 170 | https_server_.GetURL("/without_accept_ch_without_lifetime.html"); |
| 171 | EXPECT_TRUE(without_accept_ch_without_lifetime_url_.SchemeIsHTTPOrHTTPS()); |
| 172 | EXPECT_TRUE( |
| 173 | without_accept_ch_without_lifetime_url_.SchemeIsCryptographic()); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 174 | |
| 175 | without_accept_ch_without_lifetime_local_url_ = |
| 176 | http_server_.GetURL("/without_accept_ch_without_lifetime.html"); |
| 177 | EXPECT_TRUE( |
| 178 | without_accept_ch_without_lifetime_local_url_.SchemeIsHTTPOrHTTPS()); |
| 179 | EXPECT_FALSE( |
| 180 | without_accept_ch_without_lifetime_local_url_.SchemeIsCryptographic()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 181 | |
| 182 | without_accept_ch_without_lifetime_img_localhost_ = https_server_.GetURL( |
| 183 | "/without_accept_ch_without_lifetime_img_localhost.html"); |
| 184 | without_accept_ch_without_lifetime_img_foo_com_ = https_server_.GetURL( |
| 185 | "/without_accept_ch_without_lifetime_img_foo_com.html"); |
| 186 | accept_ch_without_lifetime_with_iframe_url_ = |
| 187 | https_server_.GetURL("/accept_ch_without_lifetime_with_iframe.html"); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 188 | accept_ch_without_lifetime_img_localhost_ = |
| 189 | https_server_.GetURL("/accept_ch_without_lifetime_img_localhost.html"); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | ~ClientHintsBrowserTest() override {} |
| 193 | |
| 194 | void SetUpOnMainThread() override { |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 195 | host_resolver()->AddRule("*", "127.0.0.1"); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 196 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 197 | request_interceptor_ = std::make_unique<ThirdPartyURLLoaderInterceptor>( |
| 198 | GURL("https://foo.com/non-existing-image.jpg")); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 199 | base::RunLoop().RunUntilIdle(); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 200 | } |
| 201 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 202 | void TearDownOnMainThread() override { request_interceptor_.reset(); } |
| 203 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 204 | void SetUpCommandLine(base::CommandLine* cmd) override { |
| 205 | cmd->AppendSwitch(switches::kEnableExperimentalWebPlatformFeatures); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 206 | cmd->AppendSwitchASCII(network::switches::kForceEffectiveConnectionType, |
| 207 | net::kEffectiveConnectionType2G); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 208 | } |
| 209 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 210 | void SetClientHintExpectationsOnMainFrame(bool expect_client_hints) { |
| 211 | expect_client_hints_on_main_frame_ = expect_client_hints; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 212 | } |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 213 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 214 | void SetClientHintExpectationsOnSubresources(bool expect_client_hints) { |
| 215 | expect_client_hints_on_subresources_ = expect_client_hints; |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 216 | } |
| 217 | |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 218 | // Verify that the user is not notified that cookies or JavaScript were |
| 219 | // blocked on the webpage due to the checks done by client hints. |
| 220 | void VerifyContentSettingsNotNotified() const { |
| 221 | content::WebContents* web_contents = |
| 222 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 223 | EXPECT_FALSE(TabSpecificContentSettings::FromWebContents(web_contents) |
| 224 | ->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 225 | |
| 226 | EXPECT_FALSE(TabSpecificContentSettings::FromWebContents(web_contents) |
| 227 | ->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT)); |
| 228 | } |
| 229 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 230 | const GURL& accept_ch_with_lifetime_http_local_url() const { |
| 231 | return accept_ch_with_lifetime_http_local_url_; |
| 232 | } |
| 233 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 234 | // A URL whose response headers include Accept-CH and Accept-CH-Lifetime |
| 235 | // headers. |
| 236 | const GURL& accept_ch_with_lifetime_url() const { |
| 237 | return accept_ch_with_lifetime_url_; |
| 238 | } |
| 239 | |
| 240 | // A URL whose response headers include only Accept-CH header. |
| 241 | const GURL& accept_ch_without_lifetime_url() const { |
| 242 | return accept_ch_without_lifetime_url_; |
| 243 | } |
| 244 | |
| 245 | // A URL whose response headers do not include either Accept-CH or |
| 246 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image. |
| 247 | const GURL& without_accept_ch_without_lifetime_url() const { |
| 248 | return without_accept_ch_without_lifetime_url_; |
| 249 | } |
| 250 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 251 | // A URL whose response headers do not include either Accept-CH or |
| 252 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image. |
| 253 | const GURL& without_accept_ch_without_lifetime_local_url() const { |
| 254 | return without_accept_ch_without_lifetime_local_url_; |
| 255 | } |
| 256 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 257 | // A URL whose response headers do not include either Accept-CH or |
| 258 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image |
| 259 | // from localhost. |
| 260 | const GURL& without_accept_ch_without_lifetime_img_localhost() const { |
| 261 | return without_accept_ch_without_lifetime_img_localhost_; |
| 262 | } |
| 263 | |
| 264 | // A URL whose response headers do not include either Accept-CH or |
| 265 | // Accept-CH-Lifetime headers. Navigating to this URL also fetches an image |
| 266 | // from foo.com. |
| 267 | const GURL& without_accept_ch_without_lifetime_img_foo_com() const { |
| 268 | return without_accept_ch_without_lifetime_img_foo_com_; |
| 269 | } |
| 270 | |
| 271 | // A URL whose response does not include Accept-CH or Accept-CH-Lifetime |
| 272 | // headers. The response loads accept_ch_with_lifetime_url() in an iframe. |
| 273 | const GURL& accept_ch_without_lifetime_with_iframe_url() const { |
| 274 | return accept_ch_without_lifetime_with_iframe_url_; |
| 275 | } |
| 276 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 277 | // A URL whose response headers includes only Accept-CH header. Navigating to |
| 278 | // this URL also fetches two images: One from the localhost, and one from |
| 279 | // foo.com. |
| 280 | const GURL& accept_ch_without_lifetime_img_localhost() const { |
| 281 | return accept_ch_without_lifetime_img_localhost_; |
| 282 | } |
| 283 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 284 | size_t count_client_hints_headers_seen() const { |
| 285 | return count_client_hints_headers_seen_; |
| 286 | } |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 287 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 288 | size_t third_party_request_count_seen() const { |
| 289 | return request_interceptor_->request_count_seen(); |
| 290 | } |
| 291 | |
| 292 | size_t third_party_client_hints_count_seen() const { |
| 293 | return request_interceptor_->client_hints_count_seen(); |
| 294 | } |
| 295 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 296 | base::test::ScopedFeatureList scoped_feature_list_; |
| 297 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 298 | private: |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 299 | // Called by |https_server_|. |
| 300 | void MonitorResourceRequest(const net::test_server::HttpRequest& request) { |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 301 | bool is_main_frame_navigation = |
| 302 | request.GetURL().spec().find(".html") != std::string::npos; |
| 303 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 304 | if (is_main_frame_navigation) { |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 305 | VerifyClientHintsReceived(expect_client_hints_on_main_frame_, request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 306 | if (expect_client_hints_on_main_frame_) { |
| 307 | double value = 0.0; |
| 308 | EXPECT_TRUE(base::StringToDouble( |
| 309 | request.headers.find("device-memory")->second, &value)); |
| 310 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 311 | EXPECT_TRUE(IsSimilarToDoubleABNF( |
| 312 | request.headers.find("device-memory")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 313 | main_frame_device_memory_observed_ = value; |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 314 | |
| 315 | EXPECT_TRUE( |
| 316 | base::StringToDouble(request.headers.find("dpr")->second, &value)); |
| 317 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 318 | EXPECT_TRUE(IsSimilarToDoubleABNF(request.headers.find("dpr")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 319 | main_frame_dpr_observed_ = value; |
| 320 | |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 321 | EXPECT_TRUE(base::StringToDouble( |
| 322 | request.headers.find("viewport-width")->second, &value)); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 323 | EXPECT_TRUE( |
| 324 | IsSimilarToIntABNF(request.headers.find("viewport-width")->second)); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 325 | #if !defined(OS_ANDROID) |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 326 | EXPECT_LT(0.0, value); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 327 | #else |
| 328 | EXPECT_EQ(980, value); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 329 | #endif |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 330 | main_frame_viewport_width_observed_ = value; |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 331 | VerifyNetworkQualityClientHints(request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 332 | } |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 333 | } |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 334 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 335 | if (!is_main_frame_navigation) { |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 336 | VerifyClientHintsReceived(expect_client_hints_on_subresources_, request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 337 | |
| 338 | if (expect_client_hints_on_subresources_) { |
| 339 | double value = 0.0; |
| 340 | EXPECT_TRUE(base::StringToDouble( |
| 341 | request.headers.find("device-memory")->second, &value)); |
| 342 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 343 | EXPECT_TRUE(IsSimilarToDoubleABNF( |
| 344 | request.headers.find("device-memory")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 345 | if (main_frame_device_memory_observed_ > 0) { |
| 346 | EXPECT_EQ(main_frame_device_memory_observed_, value); |
| 347 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 348 | |
| 349 | EXPECT_TRUE( |
| 350 | base::StringToDouble(request.headers.find("dpr")->second, &value)); |
| 351 | EXPECT_LT(0.0, value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 352 | EXPECT_TRUE(IsSimilarToDoubleABNF(request.headers.find("dpr")->second)); |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 353 | if (main_frame_dpr_observed_ > 0) { |
| 354 | EXPECT_EQ(main_frame_dpr_observed_, value); |
| 355 | } |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 356 | |
| 357 | EXPECT_TRUE(base::StringToDouble( |
| 358 | request.headers.find("viewport-width")->second, &value)); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 359 | EXPECT_TRUE( |
| 360 | IsSimilarToIntABNF(request.headers.find("viewport-width")->second)); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 361 | #if !defined(OS_ANDROID) |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 362 | EXPECT_LT(0.0, value); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 363 | #else |
| 364 | EXPECT_EQ(980, value); |
| 365 | #endif |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 366 | #if defined(OS_ANDROID) |
| 367 | // TODO(tbansal): https://crbug.com/825892: Viewport width on main |
| 368 | // frame requests may be incorrect when the Chrome window is not |
| 369 | // maximized. |
| 370 | if (main_frame_viewport_width_observed_ > 0) { |
| 371 | EXPECT_EQ(main_frame_viewport_width_observed_, value); |
| 372 | } |
| 373 | #endif |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 374 | VerifyNetworkQualityClientHints(request); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 375 | } |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 376 | } |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 377 | |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 378 | for (size_t i = 0; i < blink::kClientHintsHeaderMappingCount; ++i) { |
| 379 | if (base::ContainsKey(request.headers, |
| 380 | blink::kClientHintsHeaderMapping[i])) { |
| 381 | count_client_hints_headers_seen_++; |
| 382 | } |
| 383 | } |
| 384 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 385 | |
Tarun Bansal | f9cf989 | 2018-04-06 04:38:01 | [diff] [blame] | 386 | void VerifyClientHintsReceived(bool expect_client_hints, |
| 387 | const net::test_server::HttpRequest& request) { |
| 388 | for (size_t i = 0; i < blink::kClientHintsHeaderMappingCount; ++i) { |
| 389 | // Resource width client hint is only attached on image subresources. |
| 390 | if (std::string(blink::kClientHintsHeaderMapping[i]) == "width") { |
| 391 | continue; |
| 392 | } |
| 393 | EXPECT_EQ(expect_client_hints, |
| 394 | base::ContainsKey(request.headers, |
| 395 | blink::kClientHintsHeaderMapping[i])); |
| 396 | } |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 397 | } |
| 398 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 399 | void VerifyNetworkQualityClientHints( |
| 400 | const net::test_server::HttpRequest& request) const { |
| 401 | // Effective connection type is forced to 2G using command line in these |
| 402 | // tests. |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 403 | int rtt_value = 0.0; |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 404 | EXPECT_TRUE( |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 405 | base::StringToInt(request.headers.find("rtt")->second, &rtt_value)); |
| 406 | EXPECT_LE(0, rtt_value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 407 | EXPECT_TRUE(IsSimilarToIntABNF(request.headers.find("rtt")->second)); |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 408 | // Verify that RTT value is a multiple of 50 milliseconds. |
| 409 | EXPECT_EQ(0, rtt_value % 50); |
| 410 | EXPECT_GE(3000, rtt_value); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 411 | |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 412 | double mbps_value = 0.0; |
| 413 | EXPECT_TRUE(base::StringToDouble(request.headers.find("downlink")->second, |
| 414 | &mbps_value)); |
| 415 | EXPECT_LE(0, mbps_value); |
Tarun Bansal | 62efba1 | 2018-05-04 22:58:35 | [diff] [blame] | 416 | EXPECT_TRUE( |
| 417 | IsSimilarToDoubleABNF(request.headers.find("downlink")->second)); |
Tarun Bansal | 509a8dd | 2018-04-10 17:19:16 | [diff] [blame] | 418 | // Verify that the mbps value is a multiple of 0.050 mbps. |
| 419 | // Allow for small amount of noise due to double to integer conversions. |
| 420 | EXPECT_NEAR(0, (static_cast<int>(mbps_value * 1000)) % 50, 1); |
| 421 | EXPECT_GE(10.0, mbps_value); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 422 | |
| 423 | EXPECT_FALSE(request.headers.find("ect")->second.empty()); |
Tarun Bansal | ceab959 | 2018-05-01 18:57:35 | [diff] [blame] | 424 | |
| 425 | // TODO(tbansal): https://crbug.com/819244: When network servicification is |
| 426 | // enabled, the UI thread NQE observers do not receive notifications on |
| 427 | // change in the network quality. |
| 428 | if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) { |
| 429 | // Effective connection type is forced to 2G using command line in these |
| 430 | // tests. RTT is expected to be 1800 msec but leave some gap to account |
| 431 | // for added noise and randomization. |
| 432 | EXPECT_NEAR(1800, rtt_value, 360); |
| 433 | |
| 434 | // Effective connection type is forced to 2G using command line in these |
| 435 | // tests. downlink is expected to be 0.075 Mbps but leave some gap to |
| 436 | // account for added noise and randomization. |
| 437 | EXPECT_NEAR(0.075, mbps_value, 0.05); |
| 438 | |
| 439 | EXPECT_EQ("2g", request.headers.find("ect")->second); |
| 440 | } |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 441 | } |
| 442 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 443 | net::EmbeddedTestServer http_server_; |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 444 | net::EmbeddedTestServer https_server_; |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 445 | GURL accept_ch_with_lifetime_http_local_url_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 446 | GURL accept_ch_with_lifetime_url_; |
| 447 | GURL accept_ch_without_lifetime_url_; |
| 448 | GURL without_accept_ch_without_lifetime_url_; |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 449 | GURL without_accept_ch_without_lifetime_local_url_; |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 450 | GURL accept_ch_without_lifetime_with_iframe_url_; |
| 451 | GURL without_accept_ch_without_lifetime_img_foo_com_; |
| 452 | GURL without_accept_ch_without_lifetime_img_localhost_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 453 | GURL accept_ch_without_lifetime_img_localhost_; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 454 | |
Tarun Bansal | 44ad9688 | 2018-03-28 17:47:42 | [diff] [blame] | 455 | double main_frame_dpr_observed_ = -1; |
| 456 | double main_frame_viewport_width_observed_ = -1; |
| 457 | double main_frame_device_memory_observed_ = -1; |
| 458 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 459 | // Expect client hints on all the main frame request. |
| 460 | bool expect_client_hints_on_main_frame_; |
| 461 | // Expect client hints on all the subresource requests. |
| 462 | bool expect_client_hints_on_subresources_; |
| 463 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 464 | size_t count_client_hints_headers_seen_; |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 465 | |
Jay Civelli | 1ff872d | 2018-03-09 21:52:16 | [diff] [blame] | 466 | std::unique_ptr<ThirdPartyURLLoaderInterceptor> request_interceptor_; |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 467 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 468 | DISALLOW_COPY_AND_ASSIGN(ClientHintsBrowserTest); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 469 | }; |
| 470 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 471 | class ClientHintsAllowThirdPartyBrowserTest : public ClientHintsBrowserTest { |
| 472 | void SetUpCommandLine(base::CommandLine* cmd) override { |
| 473 | scoped_feature_list_.InitFromCommandLine("AllowClientHintsToThirdParty", |
| 474 | ""); |
| 475 | ClientHintsBrowserTest::SetUpCommandLine(cmd); |
| 476 | } |
| 477 | }; |
| 478 | |
Tarun Bansal | 74e189d | 2018-05-07 19:07:35 | [diff] [blame] | 479 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, CorsChecks) { |
| 480 | for (size_t i = 0; i < blink::kClientHintsHeaderMappingCount; ++i) { |
| 481 | // Do not test for headers that have not been enabled on the blink "stable" |
| 482 | // yet. |
| 483 | if (std::string(blink::kClientHintsHeaderMapping[i]) == "rtt" || |
| 484 | std::string(blink::kClientHintsHeaderMapping[i]) == "downlink" || |
| 485 | std::string(blink::kClientHintsHeaderMapping[i]) == "ect") { |
| 486 | continue; |
| 487 | } |
| 488 | EXPECT_TRUE(network::cors::IsCORSSafelistedHeader( |
| 489 | blink::kClientHintsHeaderMapping[i], "42" /* value */)); |
| 490 | } |
| 491 | EXPECT_FALSE(network::cors::IsCORSSafelistedHeader("not-a-client-hint-header", |
| 492 | "" /* value */)); |
| 493 | EXPECT_TRUE( |
| 494 | network::cors::IsCORSSafelistedHeader("save-data", "on" /* value */)); |
| 495 | } |
| 496 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 497 | // Loads a webpage that requests persisting of client hints. Verifies that |
| 498 | // the browser receives the mojo notification from the renderer and persists the |
| 499 | // client hints to the disk. |
| 500 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, ClientHintsHttps) { |
| 501 | base::HistogramTester histogram_tester; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 502 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 503 | |
| 504 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 505 | |
| 506 | content::FetchHistogramsFromChildProcesses(); |
| 507 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 508 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 509 | // client_hints_url() sets six client hints. |
| 510 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 511 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
| 512 | // seconds. |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 513 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 514 | 3600 * 1000, 1); |
| 515 | } |
| 516 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 517 | // Test that client hints are attached to subresources only if they belong |
| 518 | // to the same host as document host. |
| 519 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 520 | ClientHintsHttpsSubresourceDifferentOrigin) { |
| 521 | base::HistogramTester histogram_tester; |
| 522 | |
| 523 | // Add client hints for the embedded test server. |
| 524 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 525 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 526 | |
| 527 | // Verify that the client hints settings for localhost have been saved. |
| 528 | ContentSettingsForOneType client_hints_settings; |
| 529 | HostContentSettingsMap* host_content_settings_map = |
| 530 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 531 | host_content_settings_map->GetSettingsForOneType( |
| 532 | CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 533 | &client_hints_settings); |
| 534 | ASSERT_EQ(1U, client_hints_settings.size()); |
| 535 | |
| 536 | // Copy the client hints setting for localhost to foo.com. |
| 537 | host_content_settings_map->SetWebsiteSettingDefaultScope( |
| 538 | GURL("https://foo.com/"), GURL(), CONTENT_SETTINGS_TYPE_CLIENT_HINTS, |
| 539 | std::string(), |
Jeremy Roman | ec48d7a | 2018-03-01 17:35:09 | [diff] [blame] | 540 | std::make_unique<base::Value>( |
Oksana Zhuravlova | b14dc88 | 2018-04-12 17:34:57 | [diff] [blame] | 541 | client_hints_settings.at(0).setting_value.Clone())); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 542 | |
| 543 | // Verify that client hints for the two hosts has been saved. |
| 544 | host_content_settings_map = |
| 545 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 546 | host_content_settings_map->GetSettingsForOneType( |
| 547 | CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 548 | &client_hints_settings); |
| 549 | ASSERT_EQ(2U, client_hints_settings.size()); |
| 550 | |
| 551 | // Navigating to without_accept_ch_without_lifetime_img_localhost() should |
| 552 | // attach client hints to the image subresouce contained in that page since |
| 553 | // the image is located on the same server as the document origin. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 554 | SetClientHintExpectationsOnMainFrame(true); |
| 555 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 556 | ui_test_utils::NavigateToURL( |
| 557 | browser(), without_accept_ch_without_lifetime_img_localhost()); |
| 558 | base::RunLoop().RunUntilIdle(); |
| 559 | content::FetchHistogramsFromChildProcesses(); |
| 560 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 561 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 562 | // Six client hints are attached to the image request, and six to the main |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 563 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 564 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 565 | |
| 566 | // Navigating to without_accept_ch_without_lifetime_img_foo_com() should not |
| 567 | // attach client hints to the image subresouce contained in that page since |
| 568 | // the image is located on a different server as the document origin. |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 569 | ui_test_utils::NavigateToURL( |
| 570 | browser(), without_accept_ch_without_lifetime_img_foo_com()); |
| 571 | base::RunLoop().RunUntilIdle(); |
| 572 | content::FetchHistogramsFromChildProcesses(); |
| 573 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 574 | |
Tarun Bansal | b30b753 | 2018-03-14 21:50:38 | [diff] [blame] | 575 | // The device-memory and dprheader is attached to the main frame request. |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 576 | #if defined(OS_ANDROID) |
Tarun Bansal | b30b753 | 2018-03-14 21:50:38 | [diff] [blame] | 577 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 578 | #else |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 579 | EXPECT_EQ(18u, count_client_hints_headers_seen()); |
Tarun Bansal | 5c28afb | 2018-03-17 02:55:20 | [diff] [blame] | 580 | #endif |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 581 | // Requests to third party servers should not have client hints attached. |
| 582 | EXPECT_EQ(1u, third_party_request_count_seen()); |
| 583 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 584 | } |
| 585 | |
Tarun Bansal | ea0d826 | 2018-05-21 16:11:50 | [diff] [blame] | 586 | // Test that client hints are attached to third party subresources if |
| 587 | // AllowClientHintsToThirdParty feature is enabled. |
| 588 | IN_PROC_BROWSER_TEST_F(ClientHintsAllowThirdPartyBrowserTest, |
| 589 | ClientHintsThirdPartyAllowed) { |
| 590 | base::HistogramTester histogram_tester; |
| 591 | |
| 592 | SetClientHintExpectationsOnMainFrame(false); |
| 593 | SetClientHintExpectationsOnSubresources(true); |
| 594 | |
| 595 | // Add client hints for the embedded test server. |
| 596 | ui_test_utils::NavigateToURL(browser(), |
| 597 | accept_ch_without_lifetime_img_localhost()); |
| 598 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 599 | |
| 600 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
| 601 | |
| 602 | // Requests to third party servers should not have client hints attached. |
| 603 | EXPECT_EQ(1u, third_party_request_count_seen()); |
| 604 | |
| 605 | // Device memory, viewport width and DRP client hints should be sent to the |
| 606 | // third-party when feature "AllowClientHintsToThirdParty" is enabled. |
| 607 | EXPECT_EQ(3u, third_party_client_hints_count_seen()); |
| 608 | } |
| 609 | |
| 610 | // Test that client hints are not attached to third party subresources if |
| 611 | // AllowClientHintsToThirdParty feature is not enabled. |
| 612 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 613 | ClientHintsThirdPartyNotAllowed) { |
| 614 | base::HistogramTester histogram_tester; |
| 615 | |
| 616 | SetClientHintExpectationsOnMainFrame(false); |
| 617 | SetClientHintExpectationsOnSubresources(true); |
| 618 | |
| 619 | // Add client hints for the embedded test server. |
| 620 | ui_test_utils::NavigateToURL(browser(), |
| 621 | accept_ch_without_lifetime_img_localhost()); |
| 622 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 623 | |
| 624 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
| 625 | |
| 626 | // Requests to third party servers should not have client hints attached. |
| 627 | EXPECT_EQ(1u, third_party_request_count_seen()); |
| 628 | |
| 629 | // Client hints should not be sent to the third-party when feature |
| 630 | // "AllowClientHintsToThirdParty" is not enabled. |
| 631 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
| 632 | } |
| 633 | |
Tarun Bansal | add5e181 | 2018-02-09 19:07:58 | [diff] [blame] | 634 | // Loads a HTTPS webpage that does not request persisting of client hints. |
| 635 | // An iframe loaded by the webpage requests persistence of client hints. |
| 636 | // Verify that the request from the iframe is not honored, and client hints |
| 637 | // preference is not persisted. |
| 638 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 639 | DisregardPersistenceRequestIframe) { |
| 640 | base::HistogramTester histogram_tester; |
| 641 | ContentSettingsForOneType host_settings; |
| 642 | |
| 643 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 644 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 645 | &host_settings); |
| 646 | EXPECT_EQ(0u, host_settings.size()); |
| 647 | |
| 648 | ui_test_utils::NavigateToURL(browser(), |
| 649 | accept_ch_without_lifetime_with_iframe_url()); |
| 650 | |
| 651 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 652 | |
| 653 | content::FetchHistogramsFromChildProcesses(); |
| 654 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 655 | |
| 656 | // accept_ch_without_lifetime_with_iframe_url() loads |
| 657 | // accept_ch_with_lifetime() in an iframe. The request to persist client |
| 658 | // hints from accept_ch_with_lifetime() should be disregarded. |
| 659 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 660 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 661 | } |
| 662 | |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 663 | // Loads a HTTP local webpage (which qualifies as a secure context) that |
| 664 | // requests persisting of client hints. Verifies that the browser receives the |
| 665 | // mojo notification from the renderer and persists the client hints to the |
| 666 | // disk. |
| 667 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 668 | ClientHintsLifetimeFollowedByNoClientHintHttpLocal) { |
| 669 | base::HistogramTester histogram_tester; |
| 670 | ContentSettingsForOneType host_settings; |
| 671 | |
| 672 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 673 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 674 | &host_settings); |
| 675 | EXPECT_EQ(0u, host_settings.size()); |
| 676 | |
| 677 | ui_test_utils::NavigateToURL(browser(), |
| 678 | accept_ch_with_lifetime_http_local_url()); |
| 679 | |
| 680 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 681 | |
| 682 | content::FetchHistogramsFromChildProcesses(); |
| 683 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 684 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 685 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 686 | // accept_ch_with_lifetime_http_local_url() sets client hints persist duration |
| 687 | // to 3600 seconds. |
| 688 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 689 | 3600 * 1000, 1); |
| 690 | |
| 691 | base::RunLoop().RunUntilIdle(); |
| 692 | |
| 693 | // Clients hints preferences for one origin should be persisted. |
| 694 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 695 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 696 | &host_settings); |
| 697 | EXPECT_EQ(1u, host_settings.size()); |
| 698 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 699 | SetClientHintExpectationsOnMainFrame(true); |
| 700 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 701 | ui_test_utils::NavigateToURL(browser(), |
| 702 | without_accept_ch_without_lifetime_local_url()); |
| 703 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 704 | // Six client hints are attached to the image request, and six to the main |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 705 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 706 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | 3b330b0 | 2017-11-09 19:03:14 | [diff] [blame] | 707 | } |
| 708 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 709 | // Loads a webpage that does not request persisting of client hints. |
| 710 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, NoClientHintsHttps) { |
| 711 | base::HistogramTester histogram_tester; |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 712 | ui_test_utils::NavigateToURL(browser(), |
| 713 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 714 | |
| 715 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 716 | |
| 717 | content::FetchHistogramsFromChildProcesses(); |
| 718 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 719 | |
| 720 | // no_client_hints_url() does not sets the client hints. |
| 721 | histogram_tester.ExpectTotalCount("ClientHints.UpdateSize", 0); |
| 722 | histogram_tester.ExpectTotalCount("ClientHints.PersistDuration", 0); |
| 723 | } |
| 724 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 725 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 726 | ClientHintsLifetimeFollowedByNoClientHint) { |
| 727 | base::HistogramTester histogram_tester; |
| 728 | ContentSettingsForOneType host_settings; |
| 729 | |
| 730 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 731 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 732 | &host_settings); |
| 733 | EXPECT_EQ(0u, host_settings.size()); |
| 734 | |
| 735 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 736 | // client hints. |
| 737 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 738 | |
| 739 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 740 | |
| 741 | content::FetchHistogramsFromChildProcesses(); |
| 742 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 743 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 744 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 745 | // accept_ch_with_lifetime_url() sets client hints persist duration to 3600 |
| 746 | // seconds. |
| 747 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 748 | 3600 * 1000, 1); |
| 749 | base::RunLoop().RunUntilIdle(); |
| 750 | |
| 751 | // Clients hints preferences for one origin should be persisted. |
| 752 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 753 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 754 | &host_settings); |
| 755 | EXPECT_EQ(1u, host_settings.size()); |
| 756 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 757 | SetClientHintExpectationsOnMainFrame(true); |
| 758 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 759 | ui_test_utils::NavigateToURL(browser(), |
| 760 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 6bf5430 | 2017-10-02 07:39:14 | [diff] [blame] | 761 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 762 | // Six client hints are attached to the image request, and six to the main |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 763 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 764 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 765 | } |
| 766 | |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 767 | // Ensure that when cookies are blocked, client hint preferences are not |
| 768 | // persisted. |
| 769 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 770 | ClientHintsLifetimeNotPersistedCookiesBlocked) { |
| 771 | scoped_refptr<content_settings::CookieSettings> cookie_settings_ = |
| 772 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
| 773 | base::HistogramTester histogram_tester; |
| 774 | ContentSettingsForOneType host_settings; |
| 775 | |
| 776 | // Block cookies. |
| 777 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 778 | ->SetContentSettingDefaultScope(accept_ch_without_lifetime_url(), GURL(), |
| 779 | CONTENT_SETTINGS_TYPE_COOKIES, |
| 780 | std::string(), CONTENT_SETTING_BLOCK); |
| 781 | |
| 782 | // Fetching accept_ch_with_lifetime_url() should not persist the request for |
| 783 | // client hints since cookies are blocked. |
| 784 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 785 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 0); |
| 786 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 787 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 788 | &host_settings); |
| 789 | EXPECT_EQ(0u, host_settings.size()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 790 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 791 | |
| 792 | // Allow cookies. |
| 793 | cookie_settings_->SetCookieSetting(accept_ch_without_lifetime_url(), |
| 794 | CONTENT_SETTING_ALLOW); |
| 795 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 796 | // client hints since cookies are allowed. |
| 797 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 798 | histogram_tester.ExpectTotalCount("ClientHints.UpdateEventCount", 1); |
| 799 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 800 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 801 | &host_settings); |
| 802 | EXPECT_EQ(1u, host_settings.size()); |
| 803 | } |
| 804 | |
| 805 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 806 | ClientHintsLifetimeNotAttachedCookiesBlocked) { |
| 807 | base::HistogramTester histogram_tester; |
| 808 | ContentSettingsForOneType host_settings; |
| 809 | |
| 810 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 811 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 812 | &host_settings); |
| 813 | EXPECT_EQ(0u, host_settings.size()); |
| 814 | |
| 815 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 816 | // client hints. |
| 817 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 818 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 819 | content::FetchHistogramsFromChildProcesses(); |
| 820 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 821 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 822 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 823 | // accept_ch_with_lifetime_url() tries to set client hints persist duration to |
| 824 | // 3600 seconds. |
| 825 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 826 | 3600 * 1000, 1); |
| 827 | base::RunLoop().RunUntilIdle(); |
| 828 | |
| 829 | // Clients hints preferences for one origin should be persisted. |
| 830 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 831 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 832 | &host_settings); |
| 833 | EXPECT_EQ(1u, host_settings.size()); |
| 834 | |
| 835 | // Block the cookies: Client hints should not be attached. |
| 836 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 837 | ->SetContentSettingDefaultScope(accept_ch_without_lifetime_url(), GURL(), |
| 838 | CONTENT_SETTINGS_TYPE_COOKIES, |
| 839 | std::string(), CONTENT_SETTING_BLOCK); |
| 840 | |
| 841 | ui_test_utils::NavigateToURL(browser(), |
| 842 | without_accept_ch_without_lifetime_url()); |
| 843 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 844 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 845 | |
| 846 | // Allow the cookies: Client hints should now be attached. |
| 847 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 848 | ->SetContentSettingDefaultScope(accept_ch_without_lifetime_url(), GURL(), |
| 849 | CONTENT_SETTINGS_TYPE_COOKIES, |
| 850 | std::string(), CONTENT_SETTING_ALLOW); |
| 851 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 852 | SetClientHintExpectationsOnMainFrame(true); |
| 853 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 854 | ui_test_utils::NavigateToURL(browser(), |
| 855 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 856 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 857 | // Six client hints are attached to the image request, and six to the main |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 858 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 859 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 860 | |
| 861 | // Clear settings. |
| 862 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 863 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_COOKIES); |
| 864 | } |
| 865 | |
| 866 | // Ensure that when the JavaScript is blocked, client hint preferences are not |
| 867 | // persisted. |
| 868 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 869 | ClientHintsLifetimeNotPersistedJavaScriptBlocked) { |
| 870 | ContentSettingsForOneType host_settings; |
| 871 | |
| 872 | // Start a navigation. This navigation makes it possible to block JavaScript |
| 873 | // later. |
| 874 | ui_test_utils::NavigateToURL(browser(), |
| 875 | without_accept_ch_without_lifetime_url()); |
| 876 | |
| 877 | // Block the JavaScript: Client hint preferences should not be persisted. |
| 878 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 879 | ->SetContentSettingDefaultScope(accept_ch_with_lifetime_url(), GURL(), |
| 880 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 881 | std::string(), CONTENT_SETTING_BLOCK); |
| 882 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 883 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 884 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 885 | &host_settings); |
| 886 | EXPECT_EQ(0u, host_settings.size()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 887 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 888 | |
| 889 | // Allow the JavaScript: Client hint preferences should be persisted. |
| 890 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 891 | ->SetContentSettingDefaultScope(accept_ch_with_lifetime_url(), GURL(), |
| 892 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 893 | std::string(), CONTENT_SETTING_ALLOW); |
| 894 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 895 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 896 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 897 | &host_settings); |
| 898 | EXPECT_EQ(1u, host_settings.size()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 899 | |
| 900 | // Clear settings. |
| 901 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 902 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | // Ensure that when the JavaScript is blocked, persisted client hints are not |
| 906 | // attached to the request headers. |
| 907 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 908 | ClientHintsLifetimeNotAttachedJavaScriptBlocked) { |
| 909 | base::HistogramTester histogram_tester; |
| 910 | ContentSettingsForOneType host_settings; |
| 911 | |
| 912 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 913 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 914 | &host_settings); |
| 915 | EXPECT_EQ(0u, host_settings.size()); |
| 916 | |
| 917 | // Fetching accept_ch_with_lifetime_url() should persist the request for |
| 918 | // client hints. |
| 919 | ui_test_utils::NavigateToURL(browser(), accept_ch_with_lifetime_url()); |
| 920 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 921 | content::FetchHistogramsFromChildProcesses(); |
| 922 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 923 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 924 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 925 | // accept_ch_with_lifetime_url() tries to set client hints persist duration to |
| 926 | // 3600 seconds. |
| 927 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 928 | 3600 * 1000, 1); |
| 929 | base::RunLoop().RunUntilIdle(); |
| 930 | |
| 931 | // Clients hints preferences for one origin should be persisted. |
| 932 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 933 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 934 | &host_settings); |
| 935 | EXPECT_EQ(1u, host_settings.size()); |
| 936 | |
| 937 | // Block the Javascript: Client hints should not be attached. |
| 938 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 939 | ->SetContentSettingDefaultScope(without_accept_ch_without_lifetime_url(), |
| 940 | GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 941 | std::string(), CONTENT_SETTING_BLOCK); |
| 942 | ui_test_utils::NavigateToURL(browser(), |
| 943 | without_accept_ch_without_lifetime_url()); |
| 944 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 945 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 946 | |
| 947 | // Allow the Javascript: Client hints should now be attached. |
| 948 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 949 | ->SetContentSettingDefaultScope(without_accept_ch_without_lifetime_url(), |
| 950 | GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 951 | std::string(), CONTENT_SETTING_ALLOW); |
| 952 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 953 | SetClientHintExpectationsOnMainFrame(true); |
| 954 | SetClientHintExpectationsOnSubresources(true); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 955 | ui_test_utils::NavigateToURL(browser(), |
| 956 | without_accept_ch_without_lifetime_url()); |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 957 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 958 | // Six client hints are attached to the image request, and six to the main |
Tarun Bansal | 79df868e | 2018-03-20 23:01:36 | [diff] [blame] | 959 | // frame request. |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 960 | EXPECT_EQ(12u, count_client_hints_headers_seen()); |
Tarun Bansal | a61f0f6 | 2017-10-24 23:53:05 | [diff] [blame] | 961 | |
| 962 | // Clear settings. |
| 963 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 964 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
| 965 | } |
| 966 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 967 | // Ensure that when the JavaScript is blocked, client hints requested using |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 968 | // Accept-CH are not attached to the request headers for subresources. |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 969 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 970 | ClientHintsNoLifetimeScriptNotAllowed) { |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 971 | base::HistogramTester histogram_tester; |
| 972 | ContentSettingsForOneType host_settings; |
| 973 | |
| 974 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 975 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 976 | &host_settings); |
| 977 | EXPECT_EQ(0u, host_settings.size()); |
| 978 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 979 | // Block the Javascript: Client hints should not be attached. |
| 980 | SetClientHintExpectationsOnSubresources(false); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 981 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 982 | ->SetContentSettingDefaultScope( |
| 983 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 984 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), |
| 985 | CONTENT_SETTING_BLOCK); |
| 986 | ui_test_utils::NavigateToURL(browser(), |
| 987 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 988 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 989 | EXPECT_EQ(1u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 990 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 991 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 992 | // Allow the Javascript: Client hints should now be attached. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 993 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 994 | ->SetContentSettingDefaultScope( |
| 995 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 996 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), |
| 997 | CONTENT_SETTING_ALLOW); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 998 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 999 | SetClientHintExpectationsOnSubresources(true); |
| 1000 | ui_test_utils::NavigateToURL(browser(), |
| 1001 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1002 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 1003 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1004 | EXPECT_EQ(2u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1005 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 1006 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1007 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1008 | // Clear settings. |
| 1009 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1010 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1011 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1012 | // Block the Javascript again: Client hints should not be attached. |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1013 | SetClientHintExpectationsOnSubresources(false); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1014 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1015 | ->SetContentSettingDefaultScope( |
| 1016 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 1017 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), |
| 1018 | CONTENT_SETTING_BLOCK); |
| 1019 | ui_test_utils::NavigateToURL(browser(), |
| 1020 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 1021 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1022 | EXPECT_EQ(3u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1023 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 1024 | |
| 1025 | // Clear settings. |
| 1026 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1027 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1028 | } |
| 1029 | |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1030 | // Ensure that when the cookies is blocked, client hints are not attached to the |
| 1031 | // request headers. |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1032 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, |
| 1033 | ClientHintsNoLifetimeCookiesNotAllowed) { |
| 1034 | base::HistogramTester histogram_tester; |
| 1035 | ContentSettingsForOneType host_settings; |
| 1036 | scoped_refptr<content_settings::CookieSettings> cookie_settings_ = |
| 1037 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
| 1038 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1039 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1040 | ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_CLIENT_HINTS, std::string(), |
| 1041 | &host_settings); |
| 1042 | EXPECT_EQ(0u, host_settings.size()); |
| 1043 | |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1044 | // Block cookies. |
| 1045 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1046 | ->SetContentSettingDefaultScope( |
| 1047 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 1048 | CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); |
| 1049 | base::RunLoop().RunUntilIdle(); |
| 1050 | |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1051 | ui_test_utils::NavigateToURL(browser(), |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1052 | accept_ch_without_lifetime_img_localhost()); |
| 1053 | EXPECT_EQ(0u, count_client_hints_headers_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1054 | // Client hints are not attached to third party subresources even though |
| 1055 | // cookies are allowed only for the first party origin. |
| 1056 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | c211d8b | 2018-03-19 19:21:58 | [diff] [blame] | 1057 | VerifyContentSettingsNotNotified(); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1058 | |
| 1059 | // Allow cookies. |
| 1060 | cookie_settings_->SetCookieSetting(accept_ch_without_lifetime_img_localhost(), |
| 1061 | CONTENT_SETTING_ALLOW); |
| 1062 | base::RunLoop().RunUntilIdle(); |
| 1063 | |
| 1064 | SetClientHintExpectationsOnSubresources(true); |
| 1065 | ui_test_utils::NavigateToURL(browser(), |
| 1066 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 1067 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1068 | EXPECT_EQ(2u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1069 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1070 | |
| 1071 | // Block cookies again. |
| 1072 | SetClientHintExpectationsOnSubresources(false); |
| 1073 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1074 | ->SetContentSettingDefaultScope( |
| 1075 | accept_ch_without_lifetime_img_localhost(), GURL(), |
| 1076 | CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); |
| 1077 | base::RunLoop().RunUntilIdle(); |
| 1078 | |
| 1079 | ui_test_utils::NavigateToURL(browser(), |
| 1080 | accept_ch_without_lifetime_img_localhost()); |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 1081 | EXPECT_EQ(6u, count_client_hints_headers_seen()); |
Tarun Bansal | 229647bd00 | 2018-02-27 17:33:36 | [diff] [blame] | 1082 | EXPECT_EQ(3u, third_party_request_count_seen()); |
Tarun Bansal | 3f343d7c | 2018-03-02 18:48:00 | [diff] [blame] | 1083 | EXPECT_EQ(0u, third_party_client_hints_count_seen()); |
Tarun Bansal | 59379011 | 2018-03-20 04:53:34 | [diff] [blame] | 1084 | |
| 1085 | // Clear settings. |
| 1086 | HostContentSettingsMapFactory::GetForProfile(browser()->profile()) |
| 1087 | ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_COOKIES); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1088 | } |
| 1089 | |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1090 | // Check the client hints for the given URL in an incognito window. |
| 1091 | // Start incognito browser twice to ensure that client hints prefs are |
| 1092 | // not carried over. |
| 1093 | IN_PROC_BROWSER_TEST_F(ClientHintsBrowserTest, ClientHintsHttpsIncognito) { |
| 1094 | for (size_t i = 0; i < 2; ++i) { |
| 1095 | base::HistogramTester histogram_tester; |
| 1096 | |
| 1097 | Browser* incognito = CreateIncognitoBrowser(); |
Tarun Bansal | 1965b04 | 2017-09-07 04:59:19 | [diff] [blame] | 1098 | ui_test_utils::NavigateToURL(incognito, accept_ch_with_lifetime_url()); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1099 | |
| 1100 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateEventCount", 1, 1); |
| 1101 | |
| 1102 | content::FetchHistogramsFromChildProcesses(); |
| 1103 | SubprocessMetricsProvider::MergeHistogramDeltasForTesting(); |
| 1104 | |
Tarun Bansal | 7f3fe8c | 2018-04-06 22:37:47 | [diff] [blame] | 1105 | // accept_ch_with_lifetime_url() sets six client hints. |
| 1106 | histogram_tester.ExpectUniqueSample("ClientHints.UpdateSize", 6, 1); |
Tarun Bansal | 0b8b7afd | 2017-08-25 03:52:16 | [diff] [blame] | 1107 | |
| 1108 | // At least one renderer must have been created. All the renderers created |
| 1109 | // must have read 0 client hints. |
| 1110 | EXPECT_LE(1u, |
| 1111 | histogram_tester.GetAllSamples("ClientHints.CountRulesReceived") |
| 1112 | .size()); |
| 1113 | for (const auto& bucket : |
| 1114 | histogram_tester.GetAllSamples("ClientHints.CountRulesReceived")) { |
| 1115 | EXPECT_EQ(0, bucket.min); |
| 1116 | } |
| 1117 | // |url| sets client hints persist duration to 3600 seconds. |
| 1118 | histogram_tester.ExpectUniqueSample("ClientHints.PersistDuration", |
| 1119 | 3600 * 1000, 1); |
| 1120 | |
| 1121 | CloseBrowserSynchronously(incognito); |
| 1122 | } |
| 1123 | } |