Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [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 | |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 5 | #include <string> |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 6 | #include <vector> |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 7 | |
Lukasz Anforowicz | a2ab868 | 2020-10-26 17:13:02 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
| 9 | #include "base/files/file_util.h" |
| 10 | #include "base/files/scoped_temp_dir.h" |
| 11 | #include "base/run_loop.h" |
Lei Zhang | 0a85e65a | 2025-05-23 19:22:06 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
Xiaochen Zhou | e377ec8 | 2024-06-28 14:50:53 | [diff] [blame] | 14 | #include "base/test/bind.h" |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 15 | #include "base/test/scoped_feature_list.h" |
Andrew Williams | 43633130 | 2024-08-21 23:05:51 | [diff] [blame] | 16 | #include "base/test/test_future.h" |
Lukasz Anforowicz | a2ab868 | 2020-10-26 17:13:02 | [diff] [blame] | 17 | #include "base/threading/thread_restrictions.h" |
Kevin Marshall | 6293657 | 2022-12-01 22:49:32 | [diff] [blame] | 18 | #include "build/build_config.h" |
Tsuyoshi Horo | f6017cd | 2019-05-07 03:01:52 | [diff] [blame] | 19 | #include "content/browser/loader/prefetch_browsertest_base.h" |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 20 | #include "content/browser/web_package/mock_signed_exchange_handler.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 21 | #include "content/public/browser/browser_task_traits.h" |
Gabriel Charette | 790754c | 2018-03-16 21:32:59 | [diff] [blame] | 22 | #include "content/public/browser/browser_thread.h" |
Kinuko Yasuda | db67b53 | 2018-02-19 09:11:52 | [diff] [blame] | 23 | #include "content/public/browser/web_contents.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 24 | #include "content/public/test/browser_test.h" |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 25 | #include "content/public/test/browser_test_utils.h" |
| 26 | #include "content/public/test/content_browser_test.h" |
| 27 | #include "content/public/test/content_browser_test_utils.h" |
Xiaochen Zhou | 4faa25c2 | 2024-06-13 17:35:49 | [diff] [blame] | 28 | #include "content/public/test/fenced_frame_test_util.h" |
| 29 | #include "content/public/test/test_frame_navigation_observer.h" |
Matt Menke | 76af5ab | 2020-04-14 20:00:29 | [diff] [blame] | 30 | #include "content/public/test/url_loader_monitor.h" |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 31 | #include "content/shell/browser/shell.h" |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 32 | #include "net/base/features.h" |
Lukasz Anforowicz | a2ab868 | 2020-10-26 17:13:02 | [diff] [blame] | 33 | #include "net/base/filename_util.h" |
Matt Menke | 37c850e0 | 2020-04-20 22:17:45 | [diff] [blame] | 34 | #include "net/base/isolation_info.h" |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 35 | #include "net/dns/mock_host_resolver.h" |
Xiaochen Zhou | 4faa25c2 | 2024-06-13 17:35:49 | [diff] [blame] | 36 | #include "net/test/embedded_test_server/default_handlers.h" |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 37 | #include "net/test/scoped_mutually_exclusive_feature_list.h" |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 38 | #include "services/network/public/cpp/features.h" |
Matt Menke | 76af5ab | 2020-04-14 20:00:29 | [diff] [blame] | 39 | #include "services/network/public/cpp/resource_request.h" |
Dominic Farolino | 693fd72d | 2019-08-05 11:09:44 | [diff] [blame] | 40 | #include "third_party/blink/public/common/features.h" |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 41 | |
| 42 | namespace content { |
| 43 | |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | enum class SplitCacheTestCase { |
| 47 | kDisabled, |
| 48 | kEnabledTripleKeyed, |
| 49 | kEnabledTriplePlusCredsBool, |
| 50 | kEnabledTriplePlusCrossSiteMainFrameNavBool, |
Andrew Williams | 8260573 | 2025-03-03 18:30:55 | [diff] [blame] | 51 | // TODO(crbug.com/40186884): If we decide to launch SplitCacheByCredentials, |
| 52 | // we should add a test case for the SplitCacheByCredentials feature and the |
| 53 | // SplitCacheByCrossSiteMainFrameNavigationBoolean feature both enabled. |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 54 | }; |
| 55 | |
Adam Rice | c30c091 | 2024-08-27 04:45:16 | [diff] [blame] | 56 | const struct { |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 57 | const SplitCacheTestCase test_case; |
| 58 | base::test::FeatureRef feature; |
| 59 | } kTestCaseToFeatureMapping[] = { |
| 60 | {SplitCacheTestCase::kEnabledTriplePlusCredsBool, |
| 61 | net::features::kSplitCacheByIncludeCredentials}, |
| 62 | {SplitCacheTestCase::kEnabledTriplePlusCrossSiteMainFrameNavBool, |
Andrew Williams | 8260573 | 2025-03-03 18:30:55 | [diff] [blame] | 63 | net::features::kSplitCacheByCrossSiteMainFrameNavigationBoolean}}; |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 64 | |
| 65 | } // namespace |
| 66 | |
Tsuyoshi Horo | 2b541fe8 | 2019-04-17 07:46:47 | [diff] [blame] | 67 | class PrefetchBrowserTest |
| 68 | : public PrefetchBrowserTestBase, |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 69 | public testing::WithParamInterface<SplitCacheTestCase> { |
Tsuyoshi Horo | 2b541fe8 | 2019-04-17 07:46:47 | [diff] [blame] | 70 | public: |
Dominic Farolino | 80369b2 | 2019-08-23 08:53:34 | [diff] [blame] | 71 | PrefetchBrowserTest() |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 72 | : cross_origin_server_(std::make_unique<net::EmbeddedTestServer>()), |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 73 | split_cache_test_case_(GetParam()), |
| 74 | split_cache_experiment_feature_list_(GetParam(), |
Adam Rice | c30c091 | 2024-08-27 04:45:16 | [diff] [blame] | 75 | kTestCaseToFeatureMapping) { |
Kenichi Ishibashi | 7996511 | 2025-02-06 06:43:14 | [diff] [blame] | 76 | std::vector<base::test::FeatureRef> enabled_features; |
| 77 | std::vector<base::test::FeatureRef> disabled_features; |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 78 | if (IsSplitCacheEnabled()) { |
Kenichi Ishibashi | 7996511 | 2025-02-06 06:43:14 | [diff] [blame] | 79 | enabled_features.emplace_back( |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 80 | net::features::kSplitCacheByNetworkIsolationKey); |
| 81 | } else { |
Kenichi Ishibashi | 7996511 | 2025-02-06 06:43:14 | [diff] [blame] | 82 | disabled_features.emplace_back( |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 83 | net::features::kSplitCacheByNetworkIsolationKey); |
| 84 | } |
Kenichi Ishibashi | 7996511 | 2025-02-06 06:43:14 | [diff] [blame] | 85 | enabled_features.emplace_back(net::features::kHttpCacheNoVarySearch); |
| 86 | split_cache_enabled_feature_list_.InitWithFeatures(enabled_features, |
| 87 | disabled_features); |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 88 | } |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 89 | |
| 90 | PrefetchBrowserTest(const PrefetchBrowserTest&) = delete; |
| 91 | PrefetchBrowserTest& operator=(const PrefetchBrowserTest&) = delete; |
| 92 | |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 93 | ~PrefetchBrowserTest() override = default; |
Tsuyoshi Horo | 2b541fe8 | 2019-04-17 07:46:47 | [diff] [blame] | 94 | |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 95 | void SetUpOnMainThread() override { |
| 96 | PrefetchBrowserTestBase::SetUpOnMainThread(); |
| 97 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 98 | } |
| 99 | |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 100 | bool IsSplitCacheEnabled() const { |
| 101 | return split_cache_test_case_ != SplitCacheTestCase::kDisabled; |
Tsuyoshi Horo | 2b541fe8 | 2019-04-17 07:46:47 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | protected: |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 105 | std::unique_ptr<net::EmbeddedTestServer> cross_origin_server_; |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 106 | const SplitCacheTestCase split_cache_test_case_; |
Kinuko Yasuda | db67b53 | 2018-02-19 09:11:52 | [diff] [blame] | 107 | |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 108 | private: |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 109 | net::test::ScopedMutuallyExclusiveFeatureList |
| 110 | split_cache_experiment_feature_list_; |
| 111 | base::test::ScopedFeatureList split_cache_enabled_feature_list_; |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 112 | }; |
| 113 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 114 | // TODO(crbug.com/40256279): De-flake and re-enable. |
Dominic Farolino | 80369b2 | 2019-08-23 08:53:34 | [diff] [blame] | 115 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, |
L. David Baron | 8acd3af | 2023-02-17 17:56:04 | [diff] [blame] | 116 | DISABLED_CrossOriginDocumentHasNoSameSiteCookies) { |
Matt Menke | 76af5ab | 2020-04-14 20:00:29 | [diff] [blame] | 117 | const char* prefetch_path = "/prefetch.html"; |
| 118 | const char* target_path = "/target.html"; |
| 119 | RegisterResponse( |
| 120 | target_path, |
| 121 | ResponseEntry("<head><title>Prefetch Target</title></head>")); |
| 122 | |
| 123 | base::RunLoop prefetch_waiter; |
| 124 | auto request_counter = RequestCounter::CreateAndMonitor( |
| 125 | cross_origin_server_.get(), target_path, &prefetch_waiter); |
| 126 | RegisterRequestHandler(cross_origin_server_.get()); |
| 127 | ASSERT_TRUE(cross_origin_server_->Start()); |
| 128 | |
| 129 | const GURL cross_origin_target_url = |
| 130 | cross_origin_server_->GetURL("3p.example", target_path); |
| 131 | RegisterResponse( |
| 132 | prefetch_path, |
| 133 | ResponseEntry(base::StringPrintf( |
| 134 | "<body><link rel='prefetch' as='document' href='%s'></body>", |
| 135 | cross_origin_target_url.spec().c_str()))); |
| 136 | RegisterRequestHandler(embedded_test_server()); |
| 137 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 138 | EXPECT_EQ(0, request_counter->GetRequestCount()); |
| 139 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 140 | |
| 141 | URLLoaderMonitor monitor({cross_origin_target_url}); |
| 142 | |
| 143 | // Loading a page that prefetches the target URL would increment the |
| 144 | // |request_counter|. |
| 145 | EXPECT_TRUE( |
| 146 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
| 147 | prefetch_waiter.Run(); |
| 148 | EXPECT_EQ(1, request_counter->GetRequestCount()); |
| 149 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 150 | |
| 151 | monitor.WaitForUrls(); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 152 | std::optional<network::ResourceRequest> request = |
Matt Menke | 76af5ab | 2020-04-14 20:00:29 | [diff] [blame] | 153 | monitor.GetRequestInfo(cross_origin_target_url); |
| 154 | ASSERT_TRUE(request); |
| 155 | ASSERT_TRUE(request->site_for_cookies.IsNull()); |
| 156 | ASSERT_TRUE(request->trusted_params); |
| 157 | url::Origin cross_origin = url::Origin::Create(cross_origin_target_url); |
Matt Menke | 37c850e0 | 2020-04-20 22:17:45 | [diff] [blame] | 158 | EXPECT_TRUE(net::IsolationInfo::Create( |
Andrew Williams | 85ba9d5 | 2024-08-09 12:09:30 | [diff] [blame] | 159 | net::IsolationInfo::RequestType::kMainFrame, cross_origin, |
shivanigithub | 4e78015f59 | 2020-10-21 13:26:23 | [diff] [blame] | 160 | cross_origin, net::SiteForCookies()) |
Matt Menke | 37c850e0 | 2020-04-20 22:17:45 | [diff] [blame] | 161 | .IsEqualForTesting(request->trusted_params->isolation_info)); |
Matt Menke | 76af5ab | 2020-04-14 20:00:29 | [diff] [blame] | 162 | } |
| 163 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 164 | // TODO(crbug.com/40256279): De-flake and re-enable. |
Matt Menke | 76af5ab | 2020-04-14 20:00:29 | [diff] [blame] | 165 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, |
L. David Baron | 8acd3af | 2023-02-17 17:56:04 | [diff] [blame] | 166 | DISABLED_CrossOriginDocumentReusedAsNavigation) { |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 167 | const char* prefetch_path = "/prefetch.html"; |
| 168 | const char* target_path = "/target.html"; |
Andrew Williams | 85ba9d5 | 2024-08-09 12:09:30 | [diff] [blame] | 169 | RegisterResponse(target_path, |
| 170 | ResponseEntry("<head><title>Prefetch Target</title></head>", |
| 171 | // The empty content type prevents this |
| 172 | // response from being blocked by ORB. |
| 173 | /*content_types=*/"")); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 174 | |
| 175 | base::RunLoop prefetch_waiter; |
| 176 | auto request_counter = RequestCounter::CreateAndMonitor( |
| 177 | cross_origin_server_.get(), target_path, &prefetch_waiter); |
| 178 | RegisterRequestHandler(cross_origin_server_.get()); |
| 179 | ASSERT_TRUE(cross_origin_server_->Start()); |
| 180 | |
| 181 | const GURL cross_origin_target_url = |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 182 | cross_origin_server_->GetURL("3p.example", target_path); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 183 | RegisterResponse( |
| 184 | prefetch_path, |
| 185 | ResponseEntry(base::StringPrintf( |
| 186 | "<body><link rel='prefetch' as='document' href='%s'></body>", |
| 187 | cross_origin_target_url.spec().c_str()))); |
| 188 | RegisterRequestHandler(embedded_test_server()); |
| 189 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 190 | EXPECT_EQ(0, request_counter->GetRequestCount()); |
| 191 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 192 | |
| 193 | // Loading a page that prefetches the target URL would increment the |
| 194 | // |request_counter|. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 195 | EXPECT_TRUE( |
| 196 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 197 | prefetch_waiter.Run(); |
| 198 | EXPECT_EQ(1, request_counter->GetRequestCount()); |
| 199 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 200 | |
| 201 | // Shutdown the servers. |
| 202 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 203 | EXPECT_TRUE(cross_origin_server_->ShutdownAndWaitUntilComplete()); |
| 204 | |
| 205 | // Subsequent navigation to the cross-origin target URL shouldn't hit the |
| 206 | // network, and should be loaded from cache. |
| 207 | NavigateToURLAndWaitTitle(cross_origin_target_url, "Prefetch Target"); |
| 208 | } |
| 209 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 210 | // TODO(crbug.com/40256279): De-flake and re-enable. |
Dominic Farolino | 80369b2 | 2019-08-23 08:53:34 | [diff] [blame] | 211 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, |
L. David Baron | 8acd3af | 2023-02-17 17:56:04 | [diff] [blame] | 212 | DISABLED_CrossOriginDocumentFromOpaqueOrigin) { |
Dominic Farolino | 5e9b51c | 2020-06-21 01:35:07 | [diff] [blame] | 213 | // Prefetching as=document from a data: URL does not crash the renderer. |
| 214 | EXPECT_TRUE(NavigateToURL( |
| 215 | shell(), |
| 216 | GURL("data:text/html,<title>Data URL Prefetch Target</title><link " |
| 217 | "rel=prefetch as=document href=https://google.com>"))); |
| 218 | } |
| 219 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 220 | // TODO(crbug.com/40256279): De-flake and re-enable. |
L. David Baron | 8acd3af | 2023-02-17 17:56:04 | [diff] [blame] | 221 | IN_PROC_BROWSER_TEST_P( |
| 222 | PrefetchBrowserTest, |
| 223 | DISABLED_CrossOriginDocumentNotReusedAsNestedFrameNavigation) { |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 224 | // TODO(crbug.com/40093267): Remove this early-return when SplitCache is |
| 225 | // enabled by default. |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 226 | if (!IsSplitCacheEnabled()) { |
| 227 | GTEST_SKIP() << "This test is relevant only with SplitCache."; |
| 228 | } |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 229 | const char* prefetch_path = "/prefetch.html"; |
| 230 | const char* host_path = "/host.html"; |
| 231 | const char* iframe_path = "/iframe.html"; |
| 232 | RegisterResponse( |
| 233 | host_path, |
| 234 | ResponseEntry(base::StringPrintf( |
| 235 | "<head><title>Cross-Origin Host</title></head><body><iframe " |
| 236 | "onload='document.title=\"Host Loaded\"' src='%s'></iframe></body>", |
| 237 | iframe_path))); |
| 238 | RegisterResponse(iframe_path, ResponseEntry("<h1>I am an iframe</h1>")); |
| 239 | |
| 240 | base::RunLoop prefetch_waiter; |
| 241 | auto cross_origin_iframe_counter = RequestCounter::CreateAndMonitor( |
| 242 | cross_origin_server_.get(), iframe_path, &prefetch_waiter); |
| 243 | RegisterRequestHandler(cross_origin_server_.get()); |
| 244 | ASSERT_TRUE(cross_origin_server_->Start()); |
| 245 | |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 246 | const GURL cross_origin_host_url = |
| 247 | cross_origin_server_->GetURL("3p.example", host_path); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 248 | const GURL cross_origin_iframe_url = |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 249 | cross_origin_server_->GetURL("3p.example", iframe_path); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 250 | RegisterResponse( |
| 251 | prefetch_path, |
| 252 | ResponseEntry(base::StringPrintf( |
| 253 | "<body><link rel='prefetch' as='document' href='%s'></body>", |
| 254 | cross_origin_iframe_url.spec().c_str()))); |
| 255 | RegisterRequestHandler(embedded_test_server()); |
| 256 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 257 | EXPECT_EQ(0, cross_origin_iframe_counter->GetRequestCount()); |
| 258 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 259 | |
| 260 | // Loading a page that prefetches the cross-origin iframe URL increments its |
| 261 | // counter. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 262 | EXPECT_TRUE( |
| 263 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 264 | prefetch_waiter.Run(); |
| 265 | EXPECT_EQ(1, cross_origin_iframe_counter->GetRequestCount()); |
| 266 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 267 | |
| 268 | // Subsequent navigation to the cross-origin host site will trigger an iframe |
| 269 | // load which will not reuse the iframe that was prefetched from |
| 270 | // |prefetch_path|. This is because cross-origin document prefetches must |
| 271 | // only be reused for top-level navigations, and cannot be reused as |
| 272 | // cross-origin iframes. |
| 273 | NavigateToURLAndWaitTitle(cross_origin_host_url, "Host Loaded"); |
| 274 | EXPECT_EQ(2, cross_origin_iframe_counter->GetRequestCount()); |
| 275 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 276 | |
| 277 | // Shutdown the servers. |
| 278 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 279 | EXPECT_TRUE(cross_origin_server_->ShutdownAndWaitUntilComplete()); |
| 280 | } |
| 281 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 282 | // TODO(crbug.com/40256279): De-flake and re-enable. |
L. David Baron | 8acd3af | 2023-02-17 17:56:04 | [diff] [blame] | 283 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, |
| 284 | DISABLED_CrossOriginSubresourceNotReused) { |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 285 | // TODO(crbug.com/40093267): Remove this early-return when SplitCache is |
| 286 | // enabled by default. |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 287 | if (!IsSplitCacheEnabled()) { |
| 288 | GTEST_SKIP() << "This test is relevant only with SplitCache."; |
| 289 | } |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 290 | const char* prefetch_path = "/prefetch.html"; |
| 291 | const char* host_path = "/host.html"; |
| 292 | const char* subresource_path = "/subresource.js"; |
| 293 | RegisterResponse( |
| 294 | host_path, |
| 295 | ResponseEntry(base::StringPrintf( |
| 296 | "<head><title>Cross-Origin Host</title></head><body><script src='%s' " |
| 297 | "onload='document.title=\"Host Loaded\"'></script></body>", |
| 298 | subresource_path))); |
| 299 | RegisterResponse(subresource_path, ResponseEntry("console.log('I loaded')")); |
| 300 | |
| 301 | base::RunLoop prefetch_waiter; |
| 302 | auto cross_origin_subresource_counter = RequestCounter::CreateAndMonitor( |
| 303 | cross_origin_server_.get(), subresource_path, &prefetch_waiter); |
| 304 | RegisterRequestHandler(cross_origin_server_.get()); |
| 305 | ASSERT_TRUE(cross_origin_server_->Start()); |
| 306 | |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 307 | const GURL cross_origin_host_url = |
| 308 | cross_origin_server_->GetURL("3p.example", host_path); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 309 | const GURL cross_origin_subresource_url = |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 310 | cross_origin_server_->GetURL("3p.example", subresource_path); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 311 | RegisterResponse(prefetch_path, |
| 312 | ResponseEntry(base::StringPrintf( |
| 313 | "<body><link rel='prefetch' href='%s'></body>", |
| 314 | cross_origin_subresource_url.spec().c_str()))); |
| 315 | RegisterRequestHandler(embedded_test_server()); |
| 316 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 317 | EXPECT_EQ(0, cross_origin_subresource_counter->GetRequestCount()); |
| 318 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 319 | |
| 320 | // Loading a page that prefetches the cross-origin subresource URL |
| 321 | // increments its counter. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 322 | EXPECT_TRUE( |
| 323 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 324 | prefetch_waiter.Run(); |
| 325 | EXPECT_EQ(1, cross_origin_subresource_counter->GetRequestCount()); |
| 326 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 327 | |
| 328 | // Subsequent navigation to the cross-origin host attempting to reuse the |
| 329 | // resource that was prefetched results in the request hitting the network. |
| 330 | // This is because cross-origin subresources must only be reused within the |
| 331 | // frame they were fetched from. |
| 332 | NavigateToURLAndWaitTitle(cross_origin_host_url, "Host Loaded"); |
| 333 | EXPECT_EQ(2, cross_origin_subresource_counter->GetRequestCount()); |
| 334 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 335 | |
| 336 | // Shutdown the servers. |
| 337 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 338 | EXPECT_TRUE(cross_origin_server_->ShutdownAndWaitUntilComplete()); |
| 339 | } |
| 340 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 341 | // TODO(crbug.com/40256279): De-flake and re-enable. |
Dominic Farolino | 80369b2 | 2019-08-23 08:53:34 | [diff] [blame] | 342 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, |
L. David Baron | 8acd3af | 2023-02-17 17:56:04 | [diff] [blame] | 343 | DISABLED_CrossOriginSubresourceReusedByCurrentFrame) { |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 344 | const char* prefetch_path = "/prefetch.html"; |
| 345 | const char* use_prefetch_path = "/use-prefetch.html"; |
| 346 | const char* subresource_path = "/subresource.js"; |
| 347 | RegisterResponse(subresource_path, ResponseEntry("console.log('I loaded')")); |
| 348 | |
| 349 | base::RunLoop prefetch_waiter; |
| 350 | auto cross_origin_subresource_counter = RequestCounter::CreateAndMonitor( |
| 351 | cross_origin_server_.get(), subresource_path, &prefetch_waiter); |
| 352 | RegisterRequestHandler(cross_origin_server_.get()); |
| 353 | ASSERT_TRUE(cross_origin_server_->Start()); |
| 354 | |
| 355 | const GURL cross_origin_subresource_url = |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 356 | cross_origin_server_->GetURL("3p.example", subresource_path); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 357 | RegisterResponse(prefetch_path, |
| 358 | ResponseEntry(base::StringPrintf( |
| 359 | "<body><link rel='prefetch' href='%s'></body>", |
| 360 | cross_origin_subresource_url.spec().c_str()))); |
| 361 | RegisterResponse(use_prefetch_path, |
| 362 | ResponseEntry(base::StringPrintf( |
| 363 | "<body><script src='%s' onload='document.title=\"Use " |
| 364 | "Prefetch Loaded\"'></script></body>", |
| 365 | cross_origin_subresource_url.spec().c_str()))); |
| 366 | RegisterRequestHandler(embedded_test_server()); |
| 367 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 368 | EXPECT_EQ(0, cross_origin_subresource_counter->GetRequestCount()); |
| 369 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 370 | |
| 371 | // Loading a page that prefetches the cross-origin subresource URL |
| 372 | // increments its counter. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 373 | EXPECT_TRUE( |
| 374 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 375 | prefetch_waiter.Run(); |
| 376 | EXPECT_EQ(1, cross_origin_subresource_counter->GetRequestCount()); |
| 377 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 378 | |
| 379 | // Shut down the cross-origin server. |
| 380 | EXPECT_TRUE(cross_origin_server_->ShutdownAndWaitUntilComplete()); |
| 381 | |
| 382 | // Subsequent navigation to the same-origin document that attempts to reuse |
| 383 | // the cross-origin prefetch is able to reuse the resource from the cache. |
| 384 | NavigateToURLAndWaitTitle(embedded_test_server()->GetURL(use_prefetch_path), |
| 385 | "Use Prefetch Loaded"); |
| 386 | |
| 387 | // Shutdown the same-origin server. |
| 388 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 389 | } |
| 390 | |
| 391 | // This tests more of an implementation detail than anything. A single resource |
| 392 | // must be committed to the cache partition corresponding to a single |
Brianna Goldstein | d22b064 | 2022-10-11 16:30:50 | [diff] [blame] | 393 | // NetworkAnonymizationKey. This means that even though it is considered "safe" |
| 394 | // to reused cross-origin subresource prefetches for top-level navigations, we |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 395 | // can't actually do this, because the subresource is only reusable from the |
| 396 | // frame that fetched it. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 397 | // TODO(crbug.com/40256279): De-flake and re-enable. |
Dominic Farolino | 80369b2 | 2019-08-23 08:53:34 | [diff] [blame] | 398 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, |
L. David Baron | 8acd3af | 2023-02-17 17:56:04 | [diff] [blame] | 399 | DISABLED_CrossOriginSubresourceNotReusedAsNavigation) { |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 400 | // TODO(crbug.com/40093267): Remove this early-return when SplitCache is |
| 401 | // enabled by default. |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 402 | if (!IsSplitCacheEnabled()) { |
| 403 | GTEST_SKIP() << "This test is relevant only with SplitCache."; |
| 404 | } |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 405 | const char* prefetch_path = "/prefetch.html"; |
| 406 | const char* subresource_path = "/subresource.js"; |
| 407 | RegisterResponse(subresource_path, ResponseEntry("console.log('I loaded');")); |
| 408 | |
| 409 | base::RunLoop prefetch_waiter; |
| 410 | auto cross_origin_subresource_counter = RequestCounter::CreateAndMonitor( |
| 411 | cross_origin_server_.get(), subresource_path, &prefetch_waiter); |
| 412 | RegisterRequestHandler(cross_origin_server_.get()); |
| 413 | ASSERT_TRUE(cross_origin_server_->Start()); |
| 414 | |
| 415 | const GURL cross_origin_subresource_url = |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 416 | cross_origin_server_->GetURL("3p.example", subresource_path); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 417 | RegisterResponse(prefetch_path, |
| 418 | ResponseEntry(base::StringPrintf( |
| 419 | "<body><link rel='prefetch' href='%s'></body>", |
| 420 | cross_origin_subresource_url.spec().c_str()))); |
| 421 | RegisterRequestHandler(embedded_test_server()); |
| 422 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 423 | EXPECT_EQ(0, cross_origin_subresource_counter->GetRequestCount()); |
| 424 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 425 | |
| 426 | // Loading a page that prefetches the cross-origin subresource URL |
| 427 | // increments its counter. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 428 | EXPECT_TRUE( |
| 429 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 430 | prefetch_waiter.Run(); |
| 431 | EXPECT_EQ(1, cross_origin_subresource_counter->GetRequestCount()); |
| 432 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 433 | |
| 434 | // Shutdown the same-origin server. |
| 435 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 436 | // Subsequent navigation to the cross-origin subresource itself will not be |
| 437 | // reused from the cache, because the cached resource is not partitioned under |
| 438 | // the cross-origin it is served from. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 439 | EXPECT_TRUE(NavigateToURL(shell(), cross_origin_subresource_url)); |
Dominic Farolino | 56e1076 | 2019-08-06 13:33:34 | [diff] [blame] | 440 | EXPECT_EQ(2, cross_origin_subresource_counter->GetRequestCount()); |
| 441 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 442 | |
| 443 | // Shutdown the cross-origin server. |
| 444 | EXPECT_TRUE(cross_origin_server_->ShutdownAndWaitUntilComplete()); |
| 445 | } |
| 446 | |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 447 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, Simple) { |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 448 | const char* prefetch_path = "/prefetch.html"; |
| 449 | const char* target_path = "/target.html"; |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 450 | RegisterResponse( |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 451 | prefetch_path, |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 452 | ResponseEntry(base::StringPrintf( |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 453 | "<body><link rel='prefetch' href='%s'></body>", target_path))); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 454 | RegisterResponse( |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 455 | target_path, |
Domenic Denicola | 2fd4b6e | 2024-12-02 09:00:14 | [diff] [blame] | 456 | ResponseEntry("<head><title>Prefetch Target</title></head>", "text/html", |
| 457 | {{"cache-control", "public, max-age=3600"}})); |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 458 | |
| 459 | base::RunLoop prefetch_waiter; |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 460 | auto request_counter = RequestCounter::CreateAndMonitor( |
| 461 | embedded_test_server(), target_path, &prefetch_waiter); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 462 | RegisterRequestHandler(embedded_test_server()); |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 463 | ASSERT_TRUE(embedded_test_server()->Start()); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 464 | EXPECT_EQ(0, request_counter->GetRequestCount()); |
| 465 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 466 | |
| 467 | const GURL target_url = embedded_test_server()->GetURL(target_path); |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 468 | |
| 469 | // Loading a page that prefetches the target URL would increment the |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 470 | // |request_counter|. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 471 | EXPECT_TRUE( |
| 472 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 473 | prefetch_waiter.Run(); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 474 | EXPECT_EQ(1, request_counter->GetRequestCount()); |
| 475 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 476 | |
| 477 | // Shutdown the server. |
| 478 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 479 | |
| 480 | // Subsequent navigation to the target URL wouldn't hit the network for |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 481 | // the target URL. The target content should still be read correctly. |
| 482 | NavigateToURLAndWaitTitle(target_url, "Prefetch Target"); |
| 483 | } |
| 484 | |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 485 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, DoublePrefetch) { |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 486 | const char* prefetch_path = "/prefetch.html"; |
| 487 | const char* target_path = "/target.html"; |
| 488 | RegisterResponse(prefetch_path, ResponseEntry(base::StringPrintf( |
| 489 | "<body><link rel='prefetch' href='%s'>" |
| 490 | "<link rel='prefetch' href='%s'></body>", |
| 491 | target_path, target_path))); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 492 | RegisterResponse( |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 493 | target_path, |
Domenic Denicola | 2fd4b6e | 2024-12-02 09:00:14 | [diff] [blame] | 494 | ResponseEntry("<head><title>Prefetch Target</title></head>", "text/html", |
| 495 | {{"cache-control", "public, max-age=3600"}})); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 496 | |
| 497 | base::RunLoop prefetch_waiter; |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 498 | auto request_counter = RequestCounter::CreateAndMonitor( |
| 499 | embedded_test_server(), target_path, &prefetch_waiter); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 500 | RegisterRequestHandler(embedded_test_server()); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 501 | ASSERT_TRUE(embedded_test_server()->Start()); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 502 | EXPECT_EQ(0, request_counter->GetRequestCount()); |
| 503 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 504 | |
| 505 | const GURL target_url = embedded_test_server()->GetURL(target_path); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 506 | |
| 507 | // Loading a page that prefetches the target URL would increment the |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 508 | // |request_counter|, but it should hit only once. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 509 | EXPECT_TRUE( |
| 510 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 511 | prefetch_waiter.Run(); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 512 | EXPECT_EQ(1, request_counter->GetRequestCount()); |
| 513 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 514 | |
| 515 | // Shutdown the server. |
| 516 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 517 | |
| 518 | // Subsequent navigation to the target URL wouldn't hit the network for |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 519 | // the target URL. The target content should still be read correctly. |
| 520 | NavigateToURLAndWaitTitle(target_url, "Prefetch Target"); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, NoCacheAndNoStore) { |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 524 | const char* prefetch_path = "/prefetch.html"; |
| 525 | const char* nocache_path = "/target1.html"; |
| 526 | const char* nostore_path = "/target2.html"; |
| 527 | |
| 528 | RegisterResponse(prefetch_path, ResponseEntry(base::StringPrintf( |
| 529 | "<body>" |
| 530 | "<link rel='prefetch' href='%s'>" |
| 531 | "<link rel='prefetch' href='%s'></body>", |
| 532 | nocache_path, nostore_path))); |
| 533 | RegisterResponse(nocache_path, |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 534 | ResponseEntry("<head><title>NoCache Target</title></head>", |
| 535 | "text/html", {{"cache-control", "no-cache"}})); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 536 | RegisterResponse(nostore_path, |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 537 | ResponseEntry("<head><title>NoStore Target</title></head>", |
| 538 | "text/html", {{"cache-control", "no-store"}})); |
| 539 | |
| 540 | base::RunLoop nocache_waiter; |
| 541 | base::RunLoop nostore_waiter; |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 542 | auto nocache_request_counter = RequestCounter::CreateAndMonitor( |
| 543 | embedded_test_server(), nocache_path, &nocache_waiter); |
| 544 | auto nostore_request_counter = RequestCounter::CreateAndMonitor( |
| 545 | embedded_test_server(), nostore_path, &nostore_waiter); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 546 | RegisterRequestHandler(embedded_test_server()); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 547 | ASSERT_TRUE(embedded_test_server()->Start()); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 548 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 549 | |
| 550 | // Loading a page that prefetches the target URL would increment the |
| 551 | // fetch count for the both targets. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 552 | EXPECT_TRUE( |
| 553 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 554 | nocache_waiter.Run(); |
| 555 | nostore_waiter.Run(); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 556 | EXPECT_EQ(1, nocache_request_counter->GetRequestCount()); |
| 557 | EXPECT_EQ(1, nostore_request_counter->GetRequestCount()); |
| 558 | EXPECT_EQ(2, GetPrefetchURLLoaderCallCount()); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 559 | |
Domenic Denicola | 2fd4b6e | 2024-12-02 09:00:14 | [diff] [blame] | 560 | // Subsequent navigation to the no-cache URL do hit the network, because |
| 561 | // prefetch respects cache semantics. |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 562 | NavigateToURLAndWaitTitle(embedded_test_server()->GetURL(nocache_path), |
| 563 | "NoCache Target"); |
Domenic Denicola | 2fd4b6e | 2024-12-02 09:00:14 | [diff] [blame] | 564 | EXPECT_EQ(2, nocache_request_counter->GetRequestCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 565 | |
Domenic Denicola | 2fd4b6e | 2024-12-02 09:00:14 | [diff] [blame] | 566 | // Subsequent navigation to the no-store URL hit the network again, for the |
| 567 | // same reason. |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 568 | NavigateToURLAndWaitTitle(embedded_test_server()->GetURL(nostore_path), |
| 569 | "NoStore Target"); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 570 | EXPECT_EQ(2, nostore_request_counter->GetRequestCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 571 | |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 572 | EXPECT_EQ(2, GetPrefetchURLLoaderCallCount()); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, WithPreload) { |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 576 | const char* prefetch_path = "/prefetch.html"; |
| 577 | const char* target_path = "/target.html"; |
| 578 | const char* preload_path = "/preload.js"; |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 579 | RegisterResponse( |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 580 | prefetch_path, |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 581 | ResponseEntry(base::StringPrintf( |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 582 | "<body><link rel='prefetch' href='%s'></body>", target_path))); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 583 | RegisterResponse( |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 584 | target_path, |
| 585 | ResponseEntry("<head><title>Prefetch Target</title><script " |
| 586 | "src=\"./preload.js\"></script></head>", |
| 587 | "text/html", |
Domenic Denicola | 2fd4b6e | 2024-12-02 09:00:14 | [diff] [blame] | 588 | {{"link", "</preload.js>;rel=\"preload\";as=\"script\""}, |
| 589 | {"cache-control", "public, max-age=600"}})); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 590 | RegisterResponse(preload_path, |
| 591 | ResponseEntry("document.title=\"done\";", "text/javascript", |
| 592 | {{"cache-control", "public, max-age=600"}})); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 593 | |
| 594 | base::RunLoop preload_waiter; |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 595 | auto target_request_counter = |
| 596 | RequestCounter::CreateAndMonitor(embedded_test_server(), target_path); |
| 597 | auto preload_request_counter = RequestCounter::CreateAndMonitor( |
| 598 | embedded_test_server(), preload_path, &preload_waiter); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 599 | RegisterRequestHandler(embedded_test_server()); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 600 | ASSERT_TRUE(embedded_test_server()->Start()); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 601 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 602 | |
| 603 | const GURL target_url = embedded_test_server()->GetURL(target_path); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 604 | |
| 605 | // Loading a page that prefetches the target URL would increment both |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 606 | // |target_request_counter| and |preload_request_counter|. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 607 | EXPECT_TRUE( |
| 608 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 609 | preload_waiter.Run(); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 610 | EXPECT_EQ(1, target_request_counter->GetRequestCount()); |
| 611 | EXPECT_EQ(1, preload_request_counter->GetRequestCount()); |
| 612 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 613 | |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 614 | WaitUntilLoaded(embedded_test_server()->GetURL(preload_path)); |
| 615 | |
| 616 | // Shutdown the server. |
| 617 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 618 | |
| 619 | NavigateToURLAndWaitTitle(target_url, "done"); |
| 620 | } |
| 621 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 622 | // TODO(crbug.com/40256279): De-flake and re-enable. |
Matt Menke | 76af5ab | 2020-04-14 20:00:29 | [diff] [blame] | 623 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, |
L. David Baron | 8acd3af | 2023-02-17 17:56:04 | [diff] [blame] | 624 | DISABLED_CrossOriginWithPreloadHasNoSameSiteCookies) { |
Matt Menke | 76af5ab | 2020-04-14 20:00:29 | [diff] [blame] | 625 | const char* target_path = "/target.html"; |
| 626 | const char* preload_path = "/preload.js"; |
| 627 | RegisterResponse( |
| 628 | target_path, |
| 629 | ResponseEntry("<head><title>Prefetch Target</title><script " |
| 630 | "src=\"./preload.js\"></script></head>", |
| 631 | "text/html", |
| 632 | {{"link", "</preload.js>;rel=\"preload\";as=\"script\""}, |
| 633 | {"access-control-allow-origin", "*"}})); |
| 634 | RegisterResponse(preload_path, |
| 635 | ResponseEntry("document.title=\"done\";", "text/javascript", |
| 636 | {{"cache-control", "public, max-age=600"}})); |
| 637 | |
| 638 | base::RunLoop preload_waiter; |
| 639 | auto target_request_counter = |
| 640 | RequestCounter::CreateAndMonitor(cross_origin_server_.get(), target_path); |
| 641 | auto preload_request_counter = RequestCounter::CreateAndMonitor( |
| 642 | cross_origin_server_.get(), preload_path, &preload_waiter); |
| 643 | RegisterRequestHandler(cross_origin_server_.get()); |
| 644 | |
| 645 | ASSERT_TRUE(cross_origin_server_->Start()); |
| 646 | |
| 647 | const GURL cross_origin_target_url = |
| 648 | cross_origin_server_->GetURL("3p.example", target_path); |
| 649 | |
| 650 | const char* prefetch_path = "/prefetch.html"; |
| 651 | RegisterResponse(prefetch_path, |
| 652 | ResponseEntry(base::StringPrintf( |
| 653 | "<body><link rel='prefetch' href='%s' as='document' " |
| 654 | "crossorigin='anonymous'></body>", |
| 655 | cross_origin_target_url.spec().c_str()))); |
| 656 | RegisterRequestHandler(embedded_test_server()); |
| 657 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 658 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 659 | |
| 660 | URLLoaderMonitor monitor({cross_origin_target_url}); |
| 661 | |
| 662 | // Loading a page that prefetches the target URL would increment both |
| 663 | // |target_request_counter| and |preload_request_counter|. |
| 664 | EXPECT_TRUE( |
| 665 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
| 666 | preload_waiter.Run(); |
| 667 | EXPECT_EQ(1, target_request_counter->GetRequestCount()); |
| 668 | EXPECT_EQ(1, preload_request_counter->GetRequestCount()); |
| 669 | EXPECT_EQ(2, GetPrefetchURLLoaderCallCount()); |
| 670 | |
| 671 | GURL cross_origin_preload_url = |
| 672 | cross_origin_server_->GetURL("3p.example", preload_path); |
| 673 | WaitUntilLoaded(cross_origin_preload_url); |
| 674 | |
| 675 | monitor.WaitForUrls(); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 676 | std::optional<network::ResourceRequest> request = |
Matt Menke | 76af5ab | 2020-04-14 20:00:29 | [diff] [blame] | 677 | monitor.GetRequestInfo(cross_origin_target_url); |
| 678 | ASSERT_TRUE(request); |
| 679 | ASSERT_TRUE(request->site_for_cookies.IsNull()); |
| 680 | ASSERT_TRUE(request->trusted_params); |
| 681 | url::Origin cross_origin = url::Origin::Create(cross_origin_target_url); |
Matt Menke | 37c850e0 | 2020-04-20 22:17:45 | [diff] [blame] | 682 | EXPECT_TRUE(net::IsolationInfo::Create( |
Andrew Williams | 85ba9d5 | 2024-08-09 12:09:30 | [diff] [blame] | 683 | net::IsolationInfo::RequestType::kMainFrame, cross_origin, |
shivanigithub | 4e78015f59 | 2020-10-21 13:26:23 | [diff] [blame] | 684 | cross_origin, net::SiteForCookies()) |
Matt Menke | 37c850e0 | 2020-04-20 22:17:45 | [diff] [blame] | 685 | .IsEqualForTesting(request->trusted_params->isolation_info)); |
Matt Menke | 76af5ab | 2020-04-14 20:00:29 | [diff] [blame] | 686 | } |
| 687 | |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 688 | // Variants of this test: |
| 689 | // - PrefetchBrowserTest.CrossOriginWithPreloadAnonymous |
| 690 | // - PrefetchBrowserTest.CrossOriginWithPreloadCredentialled |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 691 | // TODO(crbug.com/40256279): De-flake and re-enable. |
L. David Baron | 8acd3af | 2023-02-17 17:56:04 | [diff] [blame] | 692 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, |
| 693 | DISABLED_CrossOriginWithPreloadAnonymous) { |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 694 | const char* target_path = "/target.html"; |
| 695 | const char* preload_path = "/preload.js"; |
| 696 | RegisterResponse( |
| 697 | target_path, |
| 698 | ResponseEntry("<head><title>Prefetch Target</title><script " |
| 699 | "src=\"./preload.js\"></script></head>", |
| 700 | "text/html", |
| 701 | {{"link", "</preload.js>;rel=\"preload\";as=\"script\""}, |
| 702 | {"access-control-allow-origin", "*"}})); |
| 703 | RegisterResponse(preload_path, |
| 704 | ResponseEntry("document.title=\"done\";", "text/javascript", |
| 705 | {{"cache-control", "public, max-age=600"}})); |
| 706 | |
| 707 | base::RunLoop preload_waiter; |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 708 | auto target_request_counter = |
| 709 | RequestCounter::CreateAndMonitor(cross_origin_server_.get(), target_path); |
| 710 | auto preload_request_counter = RequestCounter::CreateAndMonitor( |
| 711 | cross_origin_server_.get(), preload_path, &preload_waiter); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 712 | RegisterRequestHandler(cross_origin_server_.get()); |
Dominic Farolino | ad850d6 | 2019-09-02 10:09:57 | [diff] [blame] | 713 | base::RunLoop preload_waiter_second_request; |
| 714 | auto preload_request_counter_second_request = |
| 715 | RequestCounter::CreateAndMonitor(cross_origin_server_.get(), preload_path, |
| 716 | &preload_waiter_second_request); |
| 717 | |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 718 | ASSERT_TRUE(cross_origin_server_->Start()); |
| 719 | |
| 720 | const GURL cross_origin_target_url = |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 721 | cross_origin_server_->GetURL("3p.example", target_path); |
| 722 | |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 723 | const char* prefetch_path = "/prefetch.html"; |
Dominic Farolino | dee12624 | 2019-09-09 16:58:39 | [diff] [blame] | 724 | RegisterResponse(prefetch_path, |
| 725 | ResponseEntry(base::StringPrintf( |
| 726 | "<body><link rel='prefetch' href='%s' as='document' " |
| 727 | "crossorigin='anonymous'></body>", |
| 728 | cross_origin_target_url.spec().c_str()))); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 729 | RegisterRequestHandler(embedded_test_server()); |
| 730 | ASSERT_TRUE(embedded_test_server()->Start()); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 731 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 732 | |
| 733 | // Loading a page that prefetches the target URL would increment both |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 734 | // |target_request_counter| and |preload_request_counter|. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 735 | EXPECT_TRUE( |
| 736 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 737 | preload_waiter.Run(); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 738 | EXPECT_EQ(1, target_request_counter->GetRequestCount()); |
| 739 | EXPECT_EQ(1, preload_request_counter->GetRequestCount()); |
Dominic Farolino | 7275d735 | 2020-04-07 23:25:48 | [diff] [blame] | 740 | EXPECT_EQ(2, GetPrefetchURLLoaderCallCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 741 | |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 742 | GURL cross_origin_preload_url = |
| 743 | cross_origin_server_->GetURL("3p.example", preload_path); |
Dominic Farolino | ad850d6 | 2019-09-02 10:09:57 | [diff] [blame] | 744 | WaitUntilLoaded(cross_origin_preload_url); |
| 745 | |
| 746 | // When SplitCache is enabled and the prefetch resource and its headers are |
Andrew Williams | 43633130 | 2024-08-21 23:05:51 | [diff] [blame] | 747 | // fetched with a modified IsolationInfo, the preload header resource must |
| 748 | // not be reusable by any other origin but its parent prefetch's. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 749 | // TODO(crbug.com/40093267): When SplitCache is enabled by default, get rid of |
Dominic Farolino | ad850d6 | 2019-09-02 10:09:57 | [diff] [blame] | 750 | // the below conditional. |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 751 | if (IsSplitCacheEnabled()) { |
Dominic Farolino | ad850d6 | 2019-09-02 10:09:57 | [diff] [blame] | 752 | // Spin up another server, hosting a page with a preload header identical to |
| 753 | // the one in |target_path|. |
| 754 | const char* reuse_preload_attempt_path = "/reuse.html"; |
| 755 | RegisterResponse( |
| 756 | reuse_preload_attempt_path, |
| 757 | ResponseEntry( |
| 758 | base::StringPrintf("<head><title>Other site</title><script " |
| 759 | "src='%s'></script></head>", |
| 760 | cross_origin_preload_url.spec().c_str()), |
| 761 | "text/html", |
| 762 | {{"link", |
| 763 | base::StringPrintf("<%s>;rel=\"preload\";as=\"script\"", |
| 764 | cross_origin_preload_url.spec().c_str())}, |
| 765 | {"access-control-allow-origin", "*"}})); |
| 766 | std::unique_ptr<net::EmbeddedTestServer> other_cross_origin_server = |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 767 | std::make_unique<net::EmbeddedTestServer>(); |
Dominic Farolino | ad850d6 | 2019-09-02 10:09:57 | [diff] [blame] | 768 | RegisterRequestHandler(other_cross_origin_server.get()); |
| 769 | |
| 770 | ASSERT_TRUE(other_cross_origin_server->Start()); |
| 771 | |
| 772 | // Navigate to a page on the above-created server. A request for the same |
| 773 | // preload header fetched earlier must not be reusable, and must hit the |
| 774 | // network. |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 775 | EXPECT_TRUE(NavigateToURL( |
| 776 | shell(), other_cross_origin_server->GetURL( |
| 777 | "other3p.example", reuse_preload_attempt_path))); |
Dominic Farolino | ad850d6 | 2019-09-02 10:09:57 | [diff] [blame] | 778 | preload_waiter_second_request.Run(); |
| 779 | EXPECT_EQ(2, preload_request_counter_second_request->GetRequestCount()); |
| 780 | |
| 781 | // We won't need this server again. |
| 782 | EXPECT_TRUE(other_cross_origin_server->ShutdownAndWaitUntilComplete()); |
Andrew Williams | 43633130 | 2024-08-21 23:05:51 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | if (split_cache_test_case_ == |
| 786 | SplitCacheTestCase::kEnabledTriplePlusCredsBool) { |
| 787 | // The navigation is requested with credentials, but the prefetch is |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 788 | // requested anonymously. As a result of "SplitCacheByIncludeCredentials", |
| 789 | // those aren't considered the same for the HTTP cache. Early return. |
| 790 | // See the variant of this test in: |
Andrew Williams | 43633130 | 2024-08-21 23:05:51 | [diff] [blame] | 791 | // PrefetchBrowserTest.CrossOriginWithPreloadCredentialled |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 792 | return; |
Dominic Farolino | ad850d6 | 2019-09-02 10:09:57 | [diff] [blame] | 793 | } |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 794 | |
| 795 | // Shutdown the servers. |
| 796 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 797 | EXPECT_TRUE(cross_origin_server_->ShutdownAndWaitUntilComplete()); |
| 798 | |
Andrew Williams | 43633130 | 2024-08-21 23:05:51 | [diff] [blame] | 799 | EXPECT_TRUE(ExecJs(shell()->web_contents(), "document.title = 'not done';")); |
| 800 | |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 801 | // Subsequent navigation to the target URL wouldn't hit the network for |
| 802 | // the target URL. The target content should still be read correctly. |
| 803 | NavigateToURLAndWaitTitle(cross_origin_target_url, "done"); |
Kinuko Yasuda | 5e06da6 | 2018-02-09 10:17:40 | [diff] [blame] | 804 | } |
| 805 | |
Andrew Williams | 43633130 | 2024-08-21 23:05:51 | [diff] [blame] | 806 | // Regression test for crbug.com/357325599 - If a Link header with |
| 807 | // rel="preload" has as="document" (which is invalid), we shouldn't attempt to |
| 808 | // treat this as a rel="prefetch" as="document" and instead should just ignore |
| 809 | // the header. |
| 810 | // TODO(crbug.com/40256279): De-flake and enable. |
| 811 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, |
| 812 | DISABLED_CrossOriginWithInvalidPreloadAsDocument) { |
| 813 | const char* target_path = "/target.html"; |
| 814 | const char* preload_path = "/preload.js"; |
| 815 | RegisterResponse( |
| 816 | target_path, |
| 817 | ResponseEntry("<head><title>Prefetch Target</title><script " |
| 818 | "src=\"./preload.js\"></script></head>", |
| 819 | "text/html", |
| 820 | {{"link", "</preload.js>;rel=\"preload\";as=\"document\""}, |
| 821 | {"access-control-allow-origin", "*"}})); |
| 822 | RegisterResponse(preload_path, |
| 823 | ResponseEntry("document.title=\"done\";", "text/javascript", |
| 824 | {{"cache-control", "public, max-age=600"}})); |
| 825 | |
| 826 | auto target_request_counter = |
| 827 | RequestCounter::CreateAndMonitor(cross_origin_server_.get(), target_path); |
| 828 | auto preload_request_counter = RequestCounter::CreateAndMonitor( |
| 829 | cross_origin_server_.get(), preload_path); |
| 830 | RegisterRequestHandler(cross_origin_server_.get()); |
| 831 | base::RunLoop preload_waiter_second_request; |
| 832 | auto preload_request_counter_second_request = |
| 833 | RequestCounter::CreateAndMonitor(cross_origin_server_.get(), preload_path, |
| 834 | &preload_waiter_second_request); |
| 835 | |
| 836 | ASSERT_TRUE(cross_origin_server_->Start()); |
| 837 | |
| 838 | const GURL cross_origin_target_url = |
| 839 | cross_origin_server_->GetURL("3p.example", target_path); |
| 840 | |
| 841 | const char* prefetch_path = "/prefetch.html"; |
| 842 | RegisterResponse(prefetch_path, |
| 843 | ResponseEntry(base::StringPrintf( |
| 844 | "<body><link rel='prefetch' href='%s' as='document' " |
| 845 | "crossorigin='anonymous'></body>", |
| 846 | cross_origin_target_url.spec().c_str()))); |
| 847 | RegisterRequestHandler(embedded_test_server()); |
| 848 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 849 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 850 | |
| 851 | // Loading a page that prefetches the target URL would increment |
| 852 | // `target_request_counter` but not `preload_request_counter` because the |
| 853 | // preload header should be ignored. |
| 854 | base::test::TestFuture<void> prefetch_future; |
| 855 | RegisterPrefetchLoaderCallback(prefetch_future.GetCallback()); |
| 856 | |
| 857 | EXPECT_TRUE( |
| 858 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
| 859 | EXPECT_TRUE(prefetch_future.Wait()); |
| 860 | EXPECT_EQ(1, target_request_counter->GetRequestCount()); |
| 861 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 862 | EXPECT_EQ(0, preload_request_counter->GetRequestCount()); |
| 863 | |
| 864 | // Subsequent navigation to the target URL should result in the preloaded JS |
| 865 | // being served from the network. |
| 866 | NavigateToURLAndWaitTitle(cross_origin_target_url, "done"); |
| 867 | EXPECT_EQ(1, preload_request_counter->GetRequestCount()); |
| 868 | } |
| 869 | |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 870 | // Variants of this test: |
| 871 | // - PrefetchBrowserTest.CrossOriginWithPreloadAnonymous |
| 872 | // - PrefetchBrowserTest.CrossOriginWithPreloadCredentialled |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 873 | // TODO(crbug.com/40256279): De-flake and re-enable. |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 874 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, |
L. David Baron | 8acd3af | 2023-02-17 17:56:04 | [diff] [blame] | 875 | DISABLED_CrossOriginWithPreloadCredentialled) { |
Alex Kalugin | 96398d9 | 2021-10-18 05:17:21 | [diff] [blame] | 876 | ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); |
| 877 | const auto port = embedded_test_server()->port(); |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 878 | const char target_path[] = "/target.html"; |
| 879 | const char preload_path[] = "/preload.js"; |
| 880 | RegisterResponse( |
| 881 | target_path, |
| 882 | ResponseEntry("<head><title>Prefetch Target</title><script " |
| 883 | "src=\"./preload.js\"></script></head>", |
| 884 | "text/html", |
| 885 | { |
| 886 | { |
| 887 | "link", |
| 888 | "</preload.js>;rel=\"preload\";as=\"script\"", |
| 889 | }, |
| 890 | { |
| 891 | "Access-Control-Allow-Origin", |
Alex Kalugin | 96398d9 | 2021-10-18 05:17:21 | [diff] [blame] | 892 | "http://prefetch.com:" + base::NumberToString(port), |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 893 | }, |
| 894 | { |
| 895 | "Access-Control-Allow-Credentials", |
| 896 | "true", |
| 897 | }, |
| 898 | })); |
| 899 | RegisterResponse(preload_path, |
| 900 | ResponseEntry("document.title=\"done\";", "text/javascript", |
| 901 | {{"cache-control", "public, max-age=600"}})); |
| 902 | |
| 903 | base::RunLoop preload_waiter; |
| 904 | auto target_request_counter = |
| 905 | RequestCounter::CreateAndMonitor(cross_origin_server_.get(), target_path); |
| 906 | auto preload_request_counter = RequestCounter::CreateAndMonitor( |
| 907 | cross_origin_server_.get(), preload_path, &preload_waiter); |
| 908 | RegisterRequestHandler(cross_origin_server_.get()); |
| 909 | base::RunLoop preload_waiter_second_request; |
| 910 | auto preload_request_counter_second_request = |
| 911 | RequestCounter::CreateAndMonitor(cross_origin_server_.get(), preload_path, |
| 912 | &preload_waiter_second_request); |
| 913 | |
| 914 | ASSERT_TRUE(cross_origin_server_->Start()); |
| 915 | |
| 916 | const GURL cross_origin_target_url = |
| 917 | cross_origin_server_->GetURL("3p.example", target_path); |
| 918 | |
| 919 | const char* prefetch_path = "/prefetch.html"; |
| 920 | RegisterResponse(prefetch_path, |
| 921 | ResponseEntry(base::StringPrintf( |
| 922 | "<body><link rel='prefetch' href='%s' as='document' " |
| 923 | "crossorigin='use-credentials'></body>", |
| 924 | cross_origin_target_url.spec().c_str()))); |
| 925 | RegisterRequestHandler(embedded_test_server()); |
Alex Kalugin | 96398d9 | 2021-10-18 05:17:21 | [diff] [blame] | 926 | embedded_test_server()->StartAcceptingConnections(); |
Arthur Sonzogni | 54013f2 | 2021-09-03 14:57:12 | [diff] [blame] | 927 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 928 | |
| 929 | // Loading a page that prefetches the target URL would increment both |
| 930 | // |target_request_counter| and |preload_request_counter|. |
| 931 | EXPECT_TRUE(NavigateToURL( |
| 932 | shell(), embedded_test_server()->GetURL("prefetch.com", prefetch_path))); |
| 933 | preload_waiter.Run(); |
| 934 | EXPECT_EQ(1, target_request_counter->GetRequestCount()); |
| 935 | EXPECT_EQ(1, preload_request_counter->GetRequestCount()); |
| 936 | EXPECT_EQ(2, GetPrefetchURLLoaderCallCount()); |
| 937 | |
| 938 | GURL cross_origin_preload_url = |
| 939 | cross_origin_server_->GetURL("3p.example", preload_path); |
| 940 | WaitUntilLoaded(cross_origin_preload_url); |
| 941 | |
| 942 | // Shutdown the servers. |
| 943 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 944 | EXPECT_TRUE(cross_origin_server_->ShutdownAndWaitUntilComplete()); |
| 945 | |
| 946 | // Subsequent navigation to the target URL wouldn't hit the network for |
| 947 | // the target URL. The target content should still be read correctly. |
| 948 | NavigateToURLAndWaitTitle(cross_origin_target_url, "done"); |
| 949 | } |
| 950 | |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 951 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, SignedExchangeWithPreload) { |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 952 | const char* prefetch_path = "/prefetch.html"; |
| 953 | const char* target_sxg_path = "/target.sxg"; |
| 954 | const char* target_path = "/target.html"; |
| 955 | const char* preload_path_in_sxg = "/preload.js"; |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 956 | |
| 957 | RegisterResponse( |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 958 | prefetch_path, |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 959 | ResponseEntry(base::StringPrintf( |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 960 | "<body><link rel='prefetch' href='%s'></body>", target_sxg_path))); |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 961 | RegisterResponse( |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 962 | target_sxg_path, |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 963 | // We mock the SignedExchangeHandler, so just return a HTML content |
Kunihiko Sakamoto | d6373b2d | 2019-01-23 04:09:43 | [diff] [blame] | 964 | // as "application/signed-exchange;v=b3". |
Lukasz Anforowicz | 6d029de | 2022-04-21 20:24:10 | [diff] [blame] | 965 | ResponseEntry(MockSignedExchangeHandler::kMockSxgPrefix + |
| 966 | "<head><title>Prefetch Target (SXG)</title><script " |
| 967 | "src=\"./preload.js\"></script></head>", |
Kunihiko Sakamoto | d6373b2d | 2019-01-23 04:09:43 | [diff] [blame] | 968 | "application/signed-exchange;v=b3", |
Kouhei Ueno | 7ebbeee | 2018-12-19 09:59:35 | [diff] [blame] | 969 | {{"x-content-type-options", "nosniff"}})); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 970 | RegisterResponse(preload_path_in_sxg, |
| 971 | ResponseEntry("document.title=\"done\";", "text/javascript", |
| 972 | {{"cache-control", "public, max-age=600"}})); |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 973 | |
| 974 | base::RunLoop preload_waiter; |
| 975 | base::RunLoop prefetch_waiter; |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 976 | auto target_request_counter = RequestCounter::CreateAndMonitor( |
| 977 | embedded_test_server(), target_sxg_path, &prefetch_waiter); |
| 978 | auto preload_request_counter = RequestCounter::CreateAndMonitor( |
| 979 | embedded_test_server(), preload_path_in_sxg, &preload_waiter); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 980 | RegisterRequestHandler(embedded_test_server()); |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 981 | ASSERT_TRUE(embedded_test_server()->Start()); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 982 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 983 | |
| 984 | const GURL preload_url_in_sxg = |
| 985 | embedded_test_server()->GetURL(preload_path_in_sxg); |
| 986 | const GURL target_sxg_url = embedded_test_server()->GetURL(target_sxg_path); |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 987 | |
Tsuyoshi Horo | 2b541fe8 | 2019-04-17 07:46:47 | [diff] [blame] | 988 | MockSignedExchangeHandlerFactory factory({MockSignedExchangeHandlerParams( |
| 989 | target_sxg_url, SignedExchangeLoadResult::kSuccess, net::OK, |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 990 | GURL(embedded_test_server()->GetURL(target_path)), "text/html", |
Matt Menke | 141ff56 | 2020-04-15 01:33:23 | [diff] [blame] | 991 | {{"Link", base::StringPrintf("<%s>;rel=\"preload\";as=\"script\"", |
| 992 | preload_url_in_sxg.spec().c_str())}}, |
Tsuyoshi Horo | 242ee8f | 2019-04-26 06:37:07 | [diff] [blame] | 993 | net::SHA256HashValue({{0x00}}))}); |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 994 | ScopedSignedExchangeHandlerFactory scoped_factory(&factory); |
| 995 | |
| 996 | // Loading a page that prefetches the target URL would increment both |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 997 | // |target_request_counter| and |preload_request_counter|. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 998 | EXPECT_TRUE( |
| 999 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 1000 | prefetch_waiter.Run(); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 1001 | EXPECT_EQ(1, target_request_counter->GetRequestCount()); |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 1002 | |
Kunihiko Sakamoto | 00040e595d | 2018-07-11 01:15:55 | [diff] [blame] | 1003 | // If the header in the .sxg file is correctly extracted, we should |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 1004 | // be able to also see the preload. |
| 1005 | preload_waiter.Run(); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 1006 | EXPECT_EQ(1, preload_request_counter->GetRequestCount()); |
| 1007 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1008 | |
| 1009 | // Shutdown the server. |
| 1010 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 1011 | |
| 1012 | // Subsequent navigation to the target URL wouldn't hit the network for |
| 1013 | // the target URL. The target content should still be read correctly. |
| 1014 | NavigateToURLAndWaitTitle(target_sxg_url, "done"); |
| 1015 | } |
| 1016 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 1017 | // TODO(crbug.com/40256279): De-flake and re-enable. |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 1018 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, |
L. David Baron | 8acd3af | 2023-02-17 17:56:04 | [diff] [blame] | 1019 | DISABLED_CrossOriginSignedExchangeWithPreload) { |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1020 | const char* prefetch_path = "/prefetch.html"; |
| 1021 | const char* target_sxg_path = "/target.sxg"; |
| 1022 | const char* target_path = "/target.html"; |
| 1023 | const char* preload_path_in_sxg = "/preload.js"; |
| 1024 | |
| 1025 | RegisterResponse( |
| 1026 | target_sxg_path, |
| 1027 | // We mock the SignedExchangeHandler, so just return a HTML content |
| 1028 | // as "application/signed-exchange;v=b3". |
Lukasz Anforowicz | 6d029de | 2022-04-21 20:24:10 | [diff] [blame] | 1029 | ResponseEntry(MockSignedExchangeHandler::kMockSxgPrefix + |
| 1030 | "<head><title>Prefetch Target (SXG)</title><script " |
| 1031 | "src=\"./preload.js\"></script></head>", |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1032 | "application/signed-exchange;v=b3", |
| 1033 | {{"x-content-type-options", "nosniff"}})); |
| 1034 | RegisterResponse(preload_path_in_sxg, |
| 1035 | ResponseEntry("document.title=\"done\";", "text/javascript", |
| 1036 | {{"cache-control", "public, max-age=600"}})); |
| 1037 | |
| 1038 | base::RunLoop preload_waiter; |
| 1039 | base::RunLoop prefetch_waiter; |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 1040 | auto target_request_counter = RequestCounter::CreateAndMonitor( |
| 1041 | cross_origin_server_.get(), target_sxg_path, &prefetch_waiter); |
| 1042 | auto preload_request_counter = RequestCounter::CreateAndMonitor( |
| 1043 | cross_origin_server_.get(), preload_path_in_sxg, &preload_waiter); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1044 | RegisterRequestHandler(cross_origin_server_.get()); |
| 1045 | ASSERT_TRUE(cross_origin_server_->Start()); |
| 1046 | |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 1047 | const GURL target_sxg_url = |
| 1048 | cross_origin_server_->GetURL("3p.example", target_sxg_path); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1049 | const GURL preload_url_in_sxg = |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 1050 | cross_origin_server_->GetURL("3p.example", preload_path_in_sxg); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1051 | |
Dominic Farolino | dee12624 | 2019-09-09 16:58:39 | [diff] [blame] | 1052 | RegisterResponse( |
| 1053 | prefetch_path, |
| 1054 | ResponseEntry(base::StringPrintf( |
| 1055 | "<body><link rel='prefetch' as='document' href='%s'></body>", |
| 1056 | target_sxg_url.spec().c_str()))); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1057 | RegisterRequestHandler(embedded_test_server()); |
| 1058 | ASSERT_TRUE(embedded_test_server()->Start()); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 1059 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1060 | |
Tsuyoshi Horo | 2b541fe8 | 2019-04-17 07:46:47 | [diff] [blame] | 1061 | MockSignedExchangeHandlerFactory factory({MockSignedExchangeHandlerParams( |
| 1062 | target_sxg_url, SignedExchangeLoadResult::kSuccess, net::OK, |
Alex Turner | 53ddd03 | 2020-01-15 16:00:46 | [diff] [blame] | 1063 | GURL(cross_origin_server_->GetURL("3p.example", target_path)), |
| 1064 | "text/html", |
Matt Menke | 141ff56 | 2020-04-15 01:33:23 | [diff] [blame] | 1065 | {{"Link", base::StringPrintf("<%s>;rel=\"preload\";as=\"script\"", |
| 1066 | preload_url_in_sxg.spec().c_str())}}, |
Tsuyoshi Horo | 242ee8f | 2019-04-26 06:37:07 | [diff] [blame] | 1067 | net::SHA256HashValue({{0x00}}))}); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1068 | ScopedSignedExchangeHandlerFactory scoped_factory(&factory); |
| 1069 | |
| 1070 | // Loading a page that prefetches the target URL would increment both |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 1071 | // |target_request_counter| and |preload_request_counter|. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 1072 | EXPECT_TRUE( |
| 1073 | NavigateToURL(shell(), embedded_test_server()->GetURL(prefetch_path))); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1074 | prefetch_waiter.Run(); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 1075 | EXPECT_EQ(1, target_request_counter->GetRequestCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1076 | |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1077 | // If the header in the .sxg file is correctly extracted, we should |
| 1078 | // be able to also see the preload. |
| 1079 | preload_waiter.Run(); |
Tsuyoshi Horo | 29522493 | 2019-06-07 09:41:02 | [diff] [blame] | 1080 | EXPECT_EQ(1, preload_request_counter->GetRequestCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1081 | |
Dominic Farolino | 7275d735 | 2020-04-07 23:25:48 | [diff] [blame] | 1082 | EXPECT_EQ(2, GetPrefetchURLLoaderCallCount()); |
Tsuyoshi Horo | 342f6ea9 | 2019-03-13 05:35:47 | [diff] [blame] | 1083 | |
| 1084 | WaitUntilLoaded(preload_url_in_sxg); |
| 1085 | |
| 1086 | // Shutdown the servers. |
| 1087 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
| 1088 | EXPECT_TRUE(cross_origin_server_->ShutdownAndWaitUntilComplete()); |
| 1089 | |
| 1090 | // Subsequent navigation to the target URL wouldn't hit the network for |
| 1091 | // the target URL. The target content should still be read correctly. |
| 1092 | NavigateToURLAndWaitTitle(target_sxg_url, "done"); |
Kinuko Yasuda | 8edb9246 | 2018-02-21 15:46:57 | [diff] [blame] | 1093 | } |
| 1094 | |
Lukasz Anforowicz | a2ab868 | 2020-10-26 17:13:02 | [diff] [blame] | 1095 | IN_PROC_BROWSER_TEST_P(PrefetchBrowserTest, FileToHttp) { |
| 1096 | const char* target_path = "/target.html"; |
Domenic Denicola | 2fd4b6e | 2024-12-02 09:00:14 | [diff] [blame] | 1097 | RegisterResponse( |
| 1098 | target_path, |
| 1099 | ResponseEntry("<head><title>Prefetch Target</title></head>", |
| 1100 | // The empty content type prevents this |
| 1101 | // response from being blocked by ORB. |
| 1102 | /*content_types=*/"", |
| 1103 | {{"cache-control", "public, max-age=31536000"}})); |
Lukasz Anforowicz | a2ab868 | 2020-10-26 17:13:02 | [diff] [blame] | 1104 | |
| 1105 | base::RunLoop prefetch_waiter; |
| 1106 | auto request_counter = RequestCounter::CreateAndMonitor( |
| 1107 | embedded_test_server(), target_path, &prefetch_waiter); |
| 1108 | RegisterRequestHandler(embedded_test_server()); |
| 1109 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 1110 | EXPECT_EQ(0, request_counter->GetRequestCount()); |
| 1111 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 1112 | |
| 1113 | const GURL target_url = embedded_test_server()->GetURL(target_path); |
| 1114 | |
| 1115 | { |
| 1116 | base::ScopedAllowBlockingForTesting allow_blocking; |
| 1117 | base::ScopedTempDir temp_dir; |
| 1118 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1119 | base::FilePath file_path = temp_dir.GetPath().AppendASCII("test.html"); |
| 1120 | std::string file_content = base::StringPrintf( |
| 1121 | "<body><link rel='prefetch' as='document' href='%s'></body>", |
| 1122 | target_url.spec().c_str()); |
| 1123 | ASSERT_TRUE(base::WriteFile(file_path, file_content)); |
| 1124 | |
| 1125 | // Loading a page that prefetches the target URL would increment the |
| 1126 | // |request_counter|. |
| 1127 | GURL file_url = net::FilePathToFileURL(file_path); |
| 1128 | EXPECT_TRUE(NavigateToURL(shell(), file_url)); |
| 1129 | prefetch_waiter.Run(); |
| 1130 | EXPECT_EQ(1, request_counter->GetRequestCount()); |
| 1131 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 1132 | } |
| 1133 | |
Andrew Williams | 8260573 | 2025-03-03 18:30:55 | [diff] [blame] | 1134 | // Shutdown the server. |
| 1135 | EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); |
Lukasz Anforowicz | a2ab868 | 2020-10-26 17:13:02 | [diff] [blame] | 1136 | |
Andrew Williams | 8260573 | 2025-03-03 18:30:55 | [diff] [blame] | 1137 | // Subsequent navigation to the target URL wouldn't hit the network for |
| 1138 | // the target URL. The target content should still be read correctly. |
| 1139 | NavigateToURLAndWaitTitle(target_url, "Prefetch Target"); |
Lukasz Anforowicz | a2ab868 | 2020-10-26 17:13:02 | [diff] [blame] | 1140 | } |
| 1141 | |
Xiaochen Zhou | 4faa25c2 | 2024-06-13 17:35:49 | [diff] [blame] | 1142 | class FencedFramePrefetchTest : public PrefetchBrowserTestBase { |
| 1143 | public: |
Xiaochen Zhou | e377ec8 | 2024-06-28 14:50:53 | [diff] [blame] | 1144 | FencedFramePrefetchTest() |
| 1145 | : cross_origin_server_(net::EmbeddedTestServer::TYPE_HTTPS) {} |
Xiaochen Zhou | 4faa25c2 | 2024-06-13 17:35:49 | [diff] [blame] | 1146 | |
| 1147 | void SetUpOnMainThread() override { |
| 1148 | PrefetchBrowserTestBase::SetUpOnMainThread(); |
| 1149 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 1150 | |
| 1151 | // Set up the embedded https test server for fenced frame which requires a |
| 1152 | // secure context to load. |
| 1153 | embedded_https_test_server().SetSSLConfig( |
| 1154 | net::EmbeddedTestServer::CERT_TEST_NAMES); |
| 1155 | SetupCrossSiteRedirector(&embedded_https_test_server()); |
| 1156 | net::test_server::RegisterDefaultHandlers(&embedded_https_test_server()); |
Xiaochen Zhou | e377ec8 | 2024-06-28 14:50:53 | [diff] [blame] | 1157 | |
| 1158 | cross_origin_server()->SetSSLConfig( |
| 1159 | net::EmbeddedTestServer::CERT_TEST_NAMES); |
| 1160 | SetupCrossSiteRedirector(cross_origin_server()); |
| 1161 | net::test_server::RegisterDefaultHandlers(cross_origin_server()); |
Xiaochen Zhou | 4faa25c2 | 2024-06-13 17:35:49 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | content::test::FencedFrameTestHelper& fenced_frame_test_helper() { |
| 1165 | return fenced_frame_test_helper_; |
| 1166 | } |
| 1167 | |
Xiaochen Zhou | e377ec8 | 2024-06-28 14:50:53 | [diff] [blame] | 1168 | net::EmbeddedTestServer* cross_origin_server() { |
| 1169 | return &cross_origin_server_; |
| 1170 | } |
| 1171 | |
Xiaochen Zhou | 4faa25c2 | 2024-06-13 17:35:49 | [diff] [blame] | 1172 | private: |
| 1173 | test::FencedFrameTestHelper fenced_frame_test_helper_; |
Xiaochen Zhou | e377ec8 | 2024-06-28 14:50:53 | [diff] [blame] | 1174 | net::EmbeddedTestServer cross_origin_server_; |
Xiaochen Zhou | 4faa25c2 | 2024-06-13 17:35:49 | [diff] [blame] | 1175 | }; |
| 1176 | |
| 1177 | // Verify that prefetch works in fenced frame. |
| 1178 | IN_PROC_BROWSER_TEST_F(FencedFramePrefetchTest, BasicPrefetch) { |
| 1179 | base::RunLoop prefetch_waiter; |
| 1180 | auto request_counter = RequestCounter::CreateAndMonitor( |
| 1181 | &embedded_https_test_server(), "/image.jpg", &prefetch_waiter); |
| 1182 | |
| 1183 | RegisterRequestHandler(&embedded_https_test_server()); |
| 1184 | ASSERT_TRUE(embedded_https_test_server().Start()); |
| 1185 | EXPECT_EQ(0, request_counter->GetRequestCount()); |
| 1186 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 1187 | |
| 1188 | GURL prefetch_url = |
| 1189 | embedded_https_test_server().GetURL("a.test", "/image.jpg"); |
| 1190 | URLLoaderMonitor monitor({prefetch_url}); |
| 1191 | |
| 1192 | const GURL main_url = |
| 1193 | embedded_https_test_server().GetURL("a.test", "/title1.html"); |
| 1194 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1195 | |
| 1196 | const GURL fenced_frame_url = embedded_https_test_server().GetURL( |
| 1197 | "a.test", "/fenced_frames/title1.html"); |
| 1198 | RenderFrameHost* fenced_frame_rfh = |
| 1199 | fenced_frame_test_helper().CreateFencedFrame( |
| 1200 | shell()->web_contents()->GetPrimaryMainFrame(), fenced_frame_url); |
| 1201 | |
| 1202 | // Loading a page that prefetches the URL would increment the |
| 1203 | // |request_counter|. |
| 1204 | TestFrameNavigationObserver observer(fenced_frame_rfh); |
| 1205 | EXPECT_TRUE(ExecJs(shell()->web_contents()->GetPrimaryMainFrame(), |
| 1206 | JsReplace( |
| 1207 | R"(document.querySelector('fencedframe').config |
| 1208 | = new FencedFrameConfig($1);)", |
| 1209 | embedded_https_test_server().GetURL( |
| 1210 | "a.test", "/link_rel_prefetch.html")))); |
| 1211 | observer.WaitForCommit(); |
| 1212 | |
| 1213 | // Expect there is a prefetch request. |
| 1214 | prefetch_waiter.Run(); |
| 1215 | monitor.WaitForUrls(); |
| 1216 | std::optional<network::ResourceRequest> request = |
| 1217 | monitor.GetRequestInfo(prefetch_url); |
| 1218 | EXPECT_TRUE(request->load_flags & net::LOAD_PREFETCH); |
| 1219 | |
| 1220 | EXPECT_EQ(1, request_counter->GetRequestCount()); |
| 1221 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 1222 | |
| 1223 | // Shutdown the server. |
| 1224 | EXPECT_TRUE(embedded_https_test_server().ShutdownAndWaitUntilComplete()); |
| 1225 | } |
| 1226 | |
| 1227 | // Test that after fenced frame disables untrusted network access, prefetch |
| 1228 | // request is not allowed. |
| 1229 | IN_PROC_BROWSER_TEST_F(FencedFramePrefetchTest, NetworkCutoffDisablesPrefetch) { |
| 1230 | base::RunLoop prefetch_waiter; |
| 1231 | auto request_counter = RequestCounter::CreateAndMonitor( |
| 1232 | &embedded_https_test_server(), "/image.jpg", &prefetch_waiter); |
| 1233 | |
| 1234 | RegisterRequestHandler(&embedded_https_test_server()); |
| 1235 | ASSERT_TRUE(embedded_https_test_server().Start()); |
| 1236 | EXPECT_EQ(0, request_counter->GetRequestCount()); |
| 1237 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 1238 | |
| 1239 | GURL prefetch_url = |
| 1240 | embedded_https_test_server().GetURL("a.test", "/image.jpg"); |
| 1241 | URLLoaderMonitor monitor({prefetch_url}); |
| 1242 | |
| 1243 | const GURL main_url = |
| 1244 | embedded_https_test_server().GetURL("a.test", "/title1.html"); |
| 1245 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1246 | |
| 1247 | const GURL fenced_frame_url = embedded_https_test_server().GetURL( |
| 1248 | "a.test", "/fenced_frames/title1.html"); |
| 1249 | RenderFrameHost* fenced_frame_rfh = |
| 1250 | fenced_frame_test_helper().CreateFencedFrame( |
| 1251 | shell()->web_contents()->GetPrimaryMainFrame(), fenced_frame_url); |
| 1252 | |
| 1253 | // Loading a page that immediately disables untrusted network by calling |
| 1254 | // `window.fence.disableUntrustedNetwork()`. |
| 1255 | TestFrameNavigationObserver observer(fenced_frame_rfh); |
| 1256 | EXPECT_TRUE( |
| 1257 | ExecJs(shell()->web_contents()->GetPrimaryMainFrame(), |
| 1258 | JsReplace( |
| 1259 | R"(document.querySelector('fencedframe').config |
| 1260 | = new FencedFrameConfig($1);)", |
| 1261 | embedded_https_test_server().GetURL( |
| 1262 | "a.test", "/link_rel_prefetch_disable_network.html")))); |
| 1263 | observer.WaitForCommit(); |
| 1264 | |
| 1265 | // There should be no prefetch request because the untrusted network has been |
| 1266 | // disabled. |
| 1267 | prefetch_waiter.RunUntilIdle(); |
| 1268 | EXPECT_EQ(monitor.WaitForRequestCompletion(prefetch_url).error_code, |
| 1269 | net::ERR_NETWORK_ACCESS_REVOKED); |
| 1270 | EXPECT_EQ(0, request_counter->GetRequestCount()); |
| 1271 | |
| 1272 | // The `PrefetchURLLoader` count is 1 because the request did go through it. |
| 1273 | // It was eventually blocked by the nonce network status check in |
| 1274 | // `CorsURLLoaderFactory::CreateLoaderAndStart`. |
| 1275 | EXPECT_EQ(1, GetPrefetchURLLoaderCallCount()); |
| 1276 | |
| 1277 | // Shutdown the server. |
| 1278 | EXPECT_TRUE(embedded_https_test_server().ShutdownAndWaitUntilComplete()); |
| 1279 | } |
| 1280 | |
Xiaochen Zhou | e377ec8 | 2024-06-28 14:50:53 | [diff] [blame] | 1281 | // Similar to "PrefetchBrowserTest.CrossOriginWithPreloadCredentialled" but the |
| 1282 | // test procedure takes place within a fenced frame. |
| 1283 | // 1. Fenced frame navigates to `prefetch_path`. |
| 1284 | // 2. The response to navigation triggers a prefetch request to |
| 1285 | // `cross_origin_target_url`. |
| 1286 | // 3. The response to prefetch triggers a recursive prefetch request to |
| 1287 | // `preload_url`. |
| 1288 | IN_PROC_BROWSER_TEST_F(FencedFramePrefetchTest, |
| 1289 | CrossOriginWithPreloadCredentialled) { |
| 1290 | ASSERT_TRUE(embedded_https_test_server().InitializeAndListen()); |
| 1291 | const auto port = embedded_https_test_server().port(); |
| 1292 | const char target_path[] = "/target.html"; |
| 1293 | const char preload_path[] = "/preload.js"; |
| 1294 | |
| 1295 | // Register the response to the recursive prefetch request. |
| 1296 | RegisterResponse(preload_path, |
| 1297 | ResponseEntry(/*content=*/"document.title=\"done\";", |
| 1298 | /*content_types=*/"text/javascript", |
| 1299 | /*headers=*/ |
| 1300 | {{"cache-control", "public, max-age=600"}, |
| 1301 | {"Supports-Loading-Mode", "fenced-frame"}})); |
| 1302 | |
| 1303 | // Set up request counters. |
| 1304 | auto target_request_counter = |
| 1305 | RequestCounter::CreateAndMonitor(cross_origin_server(), target_path); |
| 1306 | |
| 1307 | base::RunLoop preload_waiter; |
| 1308 | auto preload_request_counter = RequestCounter::CreateAndMonitor( |
| 1309 | cross_origin_server(), preload_path, &preload_waiter); |
| 1310 | |
| 1311 | // Start cross origin server. |
| 1312 | RegisterRequestHandler(cross_origin_server()); |
| 1313 | ASSERT_TRUE(cross_origin_server()->Start()); |
| 1314 | |
| 1315 | // Register the response to the navigation request. |
| 1316 | const GURL cross_origin_target_url = |
| 1317 | cross_origin_server()->GetURL("b.test", target_path); |
| 1318 | const char* prefetch_path = "/prefetch.html"; |
| 1319 | RegisterResponse( |
| 1320 | prefetch_path, |
| 1321 | ResponseEntry(/*content=*/JsReplace( |
| 1322 | R"( |
| 1323 | <body> |
| 1324 | <link rel='prefetch' href=$1 as='document' |
| 1325 | crossorigin='use-credentials'> |
| 1326 | </body> |
| 1327 | )", |
| 1328 | cross_origin_target_url), |
| 1329 | /*content_types=*/"text/html", |
| 1330 | /*headers=*/{{"Supports-Loading-Mode", "fenced-frame"}})); |
| 1331 | |
| 1332 | RegisterRequestHandler(&embedded_https_test_server()); |
| 1333 | embedded_https_test_server().StartAcceptingConnections(); |
| 1334 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 1335 | |
| 1336 | // Register the response to the initial prefetch request. |
| 1337 | const GURL preload_url = |
| 1338 | cross_origin_server()->GetURL("c.test", preload_path); |
| 1339 | RegisterResponse( |
| 1340 | target_path, |
| 1341 | ResponseEntry( |
| 1342 | /*content=*/JsReplace(R"( |
| 1343 | <head> |
| 1344 | <title> |
| 1345 | Prefetch Target |
| 1346 | </title> |
| 1347 | <script src=$1></script> |
| 1348 | </head> |
| 1349 | )", |
| 1350 | preload_url), |
| 1351 | /*content_types=*/"text/html", |
| 1352 | /*headers=*/ |
| 1353 | {{ |
| 1354 | "link", |
| 1355 | base::StringPrintf("<%s>;rel=\"preload\";as=\"script\"", |
| 1356 | preload_url.spec().c_str()), |
| 1357 | }, |
| 1358 | { |
| 1359 | "Access-Control-Allow-Origin", |
| 1360 | "https://a.test:" + base::NumberToString(port), |
| 1361 | }, |
| 1362 | { |
| 1363 | "Access-Control-Allow-Credentials", |
| 1364 | "true", |
| 1365 | }, |
| 1366 | {"Supports-Loading-Mode", "fenced-frame"}})); |
| 1367 | |
| 1368 | // Create the fenced frame. |
| 1369 | const GURL main_url = |
| 1370 | embedded_https_test_server().GetURL("a.test", "/title1.html"); |
| 1371 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1372 | |
| 1373 | const GURL fenced_frame_url = embedded_https_test_server().GetURL( |
| 1374 | "a.test", "/fenced_frames/title1.html"); |
| 1375 | RenderFrameHost* fenced_frame_rfh = |
| 1376 | fenced_frame_test_helper().CreateFencedFrame( |
| 1377 | shell()->web_contents()->GetPrimaryMainFrame(), fenced_frame_url); |
| 1378 | |
| 1379 | // Loading a page that prefetches the target URL would increment both |
| 1380 | // |target_request_counter| and |preload_request_counter|. |
| 1381 | TestFrameNavigationObserver observer(fenced_frame_rfh); |
| 1382 | EXPECT_TRUE(ExecJs( |
| 1383 | shell()->web_contents()->GetPrimaryMainFrame(), |
| 1384 | JsReplace( |
| 1385 | R"(document.querySelector('fencedframe').config |
| 1386 | = new FencedFrameConfig($1);)", |
| 1387 | embedded_https_test_server().GetURL("a.test", prefetch_path)))); |
| 1388 | observer.WaitForCommit(); |
| 1389 | |
| 1390 | // Expect there are two prefetch requests: |
| 1391 | // 1. Navigation to `prefetch_path` which responses with a `link` element with |
| 1392 | // `prefetch` attribute. This triggers a prefetch request. |
| 1393 | // 2. The prefetch request from 1 to `cross_origin_target_url` gets a response |
| 1394 | // with a `link` header with `preload` attribute. This is turned into a |
| 1395 | // prefetch request because of the recursive prefetch token. |
| 1396 | preload_waiter.Run(); |
| 1397 | EXPECT_EQ(1, target_request_counter->GetRequestCount()); |
| 1398 | EXPECT_EQ(1, preload_request_counter->GetRequestCount()); |
| 1399 | EXPECT_EQ(2, GetPrefetchURLLoaderCallCount()); |
| 1400 | |
| 1401 | // Shutdown the servers. |
| 1402 | EXPECT_TRUE(embedded_https_test_server().ShutdownAndWaitUntilComplete()); |
| 1403 | EXPECT_TRUE(cross_origin_server()->ShutdownAndWaitUntilComplete()); |
| 1404 | } |
| 1405 | |
| 1406 | // Similar to FencedFramePrefetchTest.CrossOriginWithPreloadCredentialled except |
| 1407 | // the fenced frame disables its network with exemption of the first prefetch |
| 1408 | // request url. This allows the first prefetch request to go through. However, |
| 1409 | // the second prefetch request, which is changed from a preload request because |
| 1410 | // of the recursive prefetch token, is blocked. |
Xiaochen Zhou | 0c32d36 | 2024-08-06 13:00:19 | [diff] [blame] | 1411 | // TODO(crbug.com/336778624): This test is based on |
| 1412 | // PrefetchBrowserTest.CrossOriginWithPreloadCredentialled, which is flaky. Once |
| 1413 | // the flakiness is addressed, re-enable this test as well. |
Xiaochen Zhou | e377ec8 | 2024-06-28 14:50:53 | [diff] [blame] | 1414 | IN_PROC_BROWSER_TEST_F(FencedFramePrefetchTest, |
Xiaochen Zhou | 0c32d36 | 2024-08-06 13:00:19 | [diff] [blame] | 1415 | DISABLED_NetworkCutoffDisablesRecursivePrefetch) { |
Xiaochen Zhou | e377ec8 | 2024-06-28 14:50:53 | [diff] [blame] | 1416 | ASSERT_TRUE(embedded_https_test_server().InitializeAndListen()); |
| 1417 | const auto port = embedded_https_test_server().port(); |
| 1418 | const char target_path[] = "/target.html"; |
| 1419 | const char preload_path[] = "/preload.js"; |
| 1420 | |
| 1421 | // Register the response to the recursive prefetch request. |
| 1422 | RegisterResponse(preload_path, |
| 1423 | ResponseEntry(/*content=*/"document.title=\"done\";", |
| 1424 | /*content_types=*/"text/javascript", |
| 1425 | /*headers=*/ |
| 1426 | {{"cache-control", "public, max-age=600"}, |
| 1427 | {"Supports-Loading-Mode", "fenced-frame"}})); |
| 1428 | |
| 1429 | // Set up request counters. |
| 1430 | auto target_request_counter = |
| 1431 | RequestCounter::CreateAndMonitor(cross_origin_server(), target_path); |
| 1432 | auto preload_request_counter = |
| 1433 | RequestCounter::CreateAndMonitor(cross_origin_server(), preload_path); |
| 1434 | |
| 1435 | // Start cross origin server. |
| 1436 | RegisterRequestHandler(cross_origin_server()); |
| 1437 | ASSERT_TRUE(cross_origin_server()->Start()); |
| 1438 | |
| 1439 | // Register the response to the navigation request. |
| 1440 | const GURL cross_origin_target_url = |
| 1441 | cross_origin_server()->GetURL("b.test", target_path); |
| 1442 | const char* prefetch_path = "/prefetch.html"; |
| 1443 | RegisterResponse( |
| 1444 | prefetch_path, |
| 1445 | ResponseEntry(/*content=*/JsReplace( |
| 1446 | R"( |
| 1447 | <body> |
| 1448 | <link rel='prefetch' href=$1 as='document' |
| 1449 | crossorigin='use-credentials'> |
| 1450 | </body> |
| 1451 | )", |
| 1452 | cross_origin_target_url), |
| 1453 | /*content_types=*/"text/html", |
| 1454 | /*headers=*/ |
| 1455 | {{ |
| 1456 | "Access-Control-Allow-Origin", |
| 1457 | "https://a.test:" + base::NumberToString(port), |
| 1458 | }, |
| 1459 | {"Supports-Loading-Mode", "fenced-frame"}})); |
| 1460 | |
| 1461 | RegisterRequestHandler(&embedded_https_test_server()); |
| 1462 | embedded_https_test_server().StartAcceptingConnections(); |
| 1463 | EXPECT_EQ(0, GetPrefetchURLLoaderCallCount()); |
| 1464 | |
| 1465 | // Register the response to the initial prefetch request. |
| 1466 | const GURL preload_url = |
| 1467 | cross_origin_server()->GetURL("c.test", preload_path); |
| 1468 | RegisterResponse( |
| 1469 | target_path, |
| 1470 | ResponseEntry( |
| 1471 | /*content=*/JsReplace(R"( |
| 1472 | <head> |
| 1473 | <title> |
| 1474 | Prefetch Target |
| 1475 | </title> |
| 1476 | <script src=$1></script> |
| 1477 | </head> |
| 1478 | )", |
| 1479 | preload_url), |
| 1480 | /*content_types=*/"text/html", |
| 1481 | /*headers=*/ |
| 1482 | {{ |
| 1483 | "link", |
| 1484 | base::StringPrintf("<%s>;rel=\"preload\";as=\"script\"", |
| 1485 | preload_url.spec().c_str()), |
| 1486 | }, |
| 1487 | { |
| 1488 | "Access-Control-Allow-Origin", |
| 1489 | "https://a.test:" + base::NumberToString(port), |
| 1490 | }, |
| 1491 | { |
| 1492 | "Access-Control-Allow-Credentials", |
| 1493 | "true", |
| 1494 | }, |
| 1495 | {"Supports-Loading-Mode", "fenced-frame"}})); |
| 1496 | |
| 1497 | // Create the fenced frame. |
| 1498 | const GURL main_url = |
| 1499 | embedded_https_test_server().GetURL("a.test", "/title1.html"); |
| 1500 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1501 | |
| 1502 | const GURL fenced_frame_url = embedded_https_test_server().GetURL( |
| 1503 | "a.test", "/fenced_frames/title1.html"); |
| 1504 | RenderFrameHost* fenced_frame_rfh = |
| 1505 | fenced_frame_test_helper().CreateFencedFrame( |
| 1506 | shell()->web_contents()->GetPrimaryMainFrame(), fenced_frame_url); |
| 1507 | |
| 1508 | // This callback is invoked when the first `PrefetchURLLoader` is created. |
| 1509 | // This is needed because once fenced frame commits the navigation, it gets |
| 1510 | // a new nonce. The network revocation call needs to take place after the |
| 1511 | // navigation but before the prefetch request is sent. |
| 1512 | RegisterPrefetchLoaderCallback(base::BindLambdaForTesting([&]() { |
| 1513 | // Disable fenced frame untrusted network but exempt |
| 1514 | // `cross_origin_target_url`. This allows the prefetch request to this url. |
| 1515 | // Note the exemption must be done first, otherwise the in-progress prefetch |
| 1516 | // request to `cross_origin_target_url` will be blocked. |
| 1517 | RenderFrameHost* rfh = |
| 1518 | test::FencedFrameTestHelper::GetMostRecentlyAddedFencedFrame( |
| 1519 | shell()->web_contents()->GetPrimaryMainFrame()); |
| 1520 | |
| 1521 | test::ExemptUrlsFromFencedFrameNetworkRevocation(rfh, |
| 1522 | {cross_origin_target_url}); |
| 1523 | EXPECT_TRUE(ExecJs(rfh, R"( |
| 1524 | (async () => { |
| 1525 | return window.fence.disableUntrustedNetwork(); |
| 1526 | })(); |
| 1527 | )")); |
| 1528 | })); |
| 1529 | |
| 1530 | // Monitor requests to `preload_url`. |
| 1531 | URLLoaderMonitor monitor({preload_url}); |
| 1532 | |
| 1533 | // Navigate the fenced frame. |
| 1534 | TestFrameNavigationObserver observer(fenced_frame_rfh); |
| 1535 | EXPECT_TRUE(ExecJs( |
| 1536 | shell()->web_contents()->GetPrimaryMainFrame(), |
| 1537 | JsReplace( |
| 1538 | R"(document.querySelector('fencedframe').config |
| 1539 | = new FencedFrameConfig($1);)", |
| 1540 | embedded_https_test_server().GetURL("a.test", prefetch_path)))); |
| 1541 | observer.WaitForCommit(); |
| 1542 | |
| 1543 | // There should only be one prefetch request to `cross_origin_target_url`. |
| 1544 | // The recursive prefetch request is blocked because the fenced frame has |
| 1545 | // disabled its network and the request destination `preload_url` is not |
| 1546 | // exempted. |
| 1547 | EXPECT_EQ(monitor.WaitForRequestCompletion(preload_url).error_code, |
| 1548 | net::ERR_NETWORK_ACCESS_REVOKED); |
| 1549 | EXPECT_EQ(1, target_request_counter->GetRequestCount()); |
| 1550 | EXPECT_EQ(0, preload_request_counter->GetRequestCount()); |
| 1551 | |
| 1552 | // The `PrefetchURLLoader` is still called twice because the request did go |
| 1553 | // through it. The recursive prefetch request was eventually blocked by the |
| 1554 | // nonce network status check in `CorsURLLoaderFactory::CreateLoaderAndStart`. |
| 1555 | EXPECT_EQ(2, GetPrefetchURLLoaderCallCount()); |
| 1556 | |
| 1557 | // Shutdown the servers. |
| 1558 | EXPECT_TRUE(embedded_https_test_server().ShutdownAndWaitUntilComplete()); |
| 1559 | EXPECT_TRUE(cross_origin_server()->ShutdownAndWaitUntilComplete()); |
| 1560 | } |
| 1561 | |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 1562 | INSTANTIATE_TEST_SUITE_P( |
| 1563 | All, |
| 1564 | PrefetchBrowserTest, |
| 1565 | testing::ValuesIn( |
| 1566 | {SplitCacheTestCase::kDisabled, SplitCacheTestCase::kEnabledTripleKeyed, |
| 1567 | SplitCacheTestCase::kEnabledTriplePlusCredsBool, |
Andrew Williams | 8260573 | 2025-03-03 18:30:55 | [diff] [blame] | 1568 | SplitCacheTestCase::kEnabledTriplePlusCrossSiteMainFrameNavBool}), |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 1569 | [](const testing::TestParamInfo<SplitCacheTestCase>& info) { |
| 1570 | switch (info.param) { |
| 1571 | case SplitCacheTestCase::kDisabled: |
| 1572 | return "SplitCacheDisabled"; |
| 1573 | case SplitCacheTestCase::kEnabledTripleKeyed: |
| 1574 | return "SplitCacheEnabledTripleKeyed"; |
| 1575 | case SplitCacheTestCase::kEnabledTriplePlusCredsBool: |
| 1576 | return "SplitCacheEnabledTriplePlusCredsBool"; |
| 1577 | case SplitCacheTestCase::kEnabledTriplePlusCrossSiteMainFrameNavBool: |
| 1578 | return "SplitCacheEnabledTriplePlusCrossSiteMainFrameNavigationBool"; |
Andrew Williams | aeeec53b | 2024-08-20 22:32:30 | [diff] [blame] | 1579 | } |
| 1580 | }); |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 1581 | |
Kinuko Yasuda | 3e68ee7 | 2018-02-08 15:40:25 | [diff] [blame] | 1582 | } // namespace content |