Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [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 | |
avi | 933295f5 | 2017-01-03 19:50:27 | [diff] [blame] | 5 | #include <memory> |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 6 | #include <utility> |
avi | 933295f5 | 2017-01-03 19:50:27 | [diff] [blame] | 7 | #include <vector> |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 8 | |
Takashi Toyoshima | eb2ba20c5 | 2020-04-27 16:34:58 | [diff] [blame] | 9 | #include "base/command_line.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
| 11 | #include "base/functional/callback_helpers.h" |
xunjieli | 0332c19 | 2014-09-10 23:23:31 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
megjablon | d5ac7d5 | 2015-10-22 23:56:12 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Tsuyoshi Horo | ffd48736 | 2024-08-29 21:11:17 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
[email protected] | fbaccee | 2013-08-12 23:24:02 | [diff] [blame] | 16 | #include "base/strings/stringprintf.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
Guido Urdaneta | ef4e9194 | 2020-11-09 15:06:24 | [diff] [blame] | 18 | #include "base/test/bind.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 19 | #include "build/build_config.h" |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 20 | #include "content/browser/download/download_manager_impl.h" |
[email protected] | 93ddb3c | 2012-04-11 21:44:29 | [diff] [blame] | 21 | #include "content/browser/web_contents/web_contents_impl.h" |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 22 | #include "content/public/browser/browser_context.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 23 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 24 | #include "content/public/browser/browser_thread.h" |
Yoichi Osato | e040014 | 2023-05-30 19:35:40 | [diff] [blame] | 25 | #include "content/public/browser/network_service_util.h" |
Lukasz Anforowicz | 5440a8e | 2017-09-28 16:52:10 | [diff] [blame] | 26 | #include "content/public/browser/render_frame_host.h" |
Lukasz Anforowicz | 7d7da7f | 2018-07-31 00:21:59 | [diff] [blame] | 27 | #include "content/public/browser/site_isolation_policy.h" |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 28 | #include "content/public/browser/web_contents.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 29 | #include "content/public/common/content_client.h" |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 30 | #include "content/public/common/url_constants.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 31 | #include "content/public/test/browser_test.h" |
[email protected] | 35daebe0 | 2012-07-20 05:40:59 | [diff] [blame] | 32 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 33 | #include "content/public/test/content_browser_test.h" |
Scott Violet | 9986199 | 2023-02-08 01:20:12 | [diff] [blame] | 34 | #include "content/public/test/content_browser_test_content_browser_client.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 35 | #include "content/public/test/content_browser_test_utils.h" |
megjablon | d5ac7d5 | 2015-10-22 23:56:12 | [diff] [blame] | 36 | #include "content/public/test/test_navigation_observer.h" |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 37 | #include "content/public/test/test_utils.h" |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 38 | #include "content/public/test/url_loader_interceptor.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 39 | #include "content/shell/browser/shell.h" |
Matt Menke | fcbb1bd7 | 2018-01-31 21:53:12 | [diff] [blame] | 40 | #include "net/base/filename_util.h" |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 41 | #include "net/base/load_flags.h" |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 42 | #include "net/base/net_errors.h" |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 43 | #include "net/dns/mock_host_resolver.h" |
Tsuyoshi Horo | ffd48736 | 2024-08-29 21:11:17 | [diff] [blame] | 44 | #include "net/http/http_status_code.h" |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 45 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 46 | #include "net/test/embedded_test_server/http_request.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 47 | #include "net/test/embedded_test_server/http_response.h" |
xunjieli | 2906f15 | 2014-09-12 00:08:23 | [diff] [blame] | 48 | #include "net/test/url_request/url_request_failed_job.h" |
xunjieli | 0332c19 | 2014-09-10 23:23:31 | [diff] [blame] | 49 | #include "net/test/url_request/url_request_mock_http_job.h" |
John Abd-El-Malek | 21f3ea1 | 2018-03-03 00:53:12 | [diff] [blame] | 50 | #include "services/network/public/cpp/features.h" |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 51 | #include "testing/gmock/include/gmock/gmock.h" |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 52 | #include "third_party/blink/public/common/chrome_debug_urls.h" |
Minggang Wang | f6840ecf | 2019-07-29 05:15:02 | [diff] [blame] | 53 | #include "third_party/blink/public/common/loader/url_loader_throttle.h" |
megjablon | d5ac7d5 | 2015-10-22 23:56:12 | [diff] [blame] | 54 | #include "url/gurl.h" |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [diff] [blame] | 55 | |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 56 | using base::ASCIIToUTF16; |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 57 | using testing::HasSubstr; |
| 58 | using testing::Not; |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 59 | |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 60 | namespace content { |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 61 | |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 62 | class LoaderBrowserTest : public ContentBrowserTest, |
| 63 | public DownloadManager::Observer { |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [diff] [blame] | 64 | public: |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 65 | LoaderBrowserTest() : got_downloads_(false) {} |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [diff] [blame] | 66 | |
| 67 | protected: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 68 | void SetUpOnMainThread() override { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 69 | base::FilePath path = GetTestFilePath("", ""); |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 70 | GetIOThreadTaskRunner({})->PostTask( |
| 71 | FROM_HERE, |
tzik | 0f14f19 | 2017-08-15 02:43:33 | [diff] [blame] | 72 | base::BindOnce(&net::URLRequestMockHTTPJob::AddUrlHandlers, path)); |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 73 | GetIOThreadTaskRunner({})->PostTask( |
| 74 | FROM_HERE, base::BindOnce(&net::URLRequestFailedJob::AddUrlHandler)); |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 75 | host_resolver()->AddRule("*", "127.0.0.1"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 76 | } |
| 77 | |
Thanh Le | 006f2532 | 2017-09-13 17:38:04 | [diff] [blame] | 78 | void CheckTitleTest(const GURL& url, const std::string& expected_title) { |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 79 | std::u16string expected_title16(ASCIIToUTF16(expected_title)); |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 80 | TitleWatcher title_watcher(shell()->web_contents(), expected_title16); |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 81 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 82 | EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); |
| 83 | } |
| 84 | |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 85 | bool GetPopupTitle(const GURL& url, std::u16string* title) { |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 86 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 87 | |
| 88 | ShellAddedObserver new_shell_observer; |
| 89 | |
| 90 | // Create dynamic popup. |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 91 | if (!ExecJs(shell(), "OpenPopup();")) { |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 92 | return false; |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 93 | } |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 94 | |
| 95 | Shell* new_shell = new_shell_observer.GetShell(); |
| 96 | *title = new_shell->web_contents()->GetTitle(); |
| 97 | return true; |
| 98 | } |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 99 | |
| 100 | std::string GetCookies(const GURL& url) { |
Thanh Le | 006f2532 | 2017-09-13 17:38:04 | [diff] [blame] | 101 | return content::GetCookies(shell()->web_contents()->GetBrowserContext(), |
| 102 | url); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | bool got_downloads() const { return got_downloads_; } |
| 106 | |
| 107 | private: |
Takashi Toyoshima | eb2ba20c5 | 2020-04-27 16:34:58 | [diff] [blame] | 108 | void SetUp() override { |
| 109 | base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 110 | "cors_exempt_header_list", "ExemptFoo"); |
| 111 | ContentBrowserTest::SetUp(); |
| 112 | } |
| 113 | |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 114 | bool got_downloads_; |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [diff] [blame] | 115 | }; |
| 116 | |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [diff] [blame] | 117 | // Test title for content created by javascript window.open(). |
| 118 | // See http://crbug.com/5988 |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 119 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, DynamicTitle1) { |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 120 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [diff] [blame] | 121 | |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 122 | GURL url(embedded_test_server()->GetURL("/dynamic1.html")); |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 123 | std::u16string title; |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [diff] [blame] | 124 | ASSERT_TRUE(GetPopupTitle(url, &title)); |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 125 | EXPECT_TRUE( |
| 126 | base::StartsWith(title, u"My Popup Title", base::CompareCase::SENSITIVE)) |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [diff] [blame] | 127 | << "Actual title: " << title; |
| 128 | } |
| 129 | |
| 130 | // Test title for content created by javascript window.open(). |
| 131 | // See http://crbug.com/5988 |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 132 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, DynamicTitle2) { |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 133 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [diff] [blame] | 134 | |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 135 | GURL url(embedded_test_server()->GetURL("/dynamic2.html")); |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 136 | std::u16string title; |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [diff] [blame] | 137 | ASSERT_TRUE(GetPopupTitle(url, &title)); |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 138 | EXPECT_TRUE(base::StartsWith(title, u"My Dynamic Title", |
brettw | 94a2cc2 | 2015-07-01 19:26:54 | [diff] [blame] | 139 | base::CompareCase::SENSITIVE)) |
[email protected] | fac06f33 | 2011-06-14 05:30:44 | [diff] [blame] | 140 | << "Actual title: " << title; |
| 141 | } |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 142 | |
Dominic Farolino | 9c277c5 | 2019-07-16 10:56:05 | [diff] [blame] | 143 | // Tests that the renderer does not crash when issuing a stale-revalidation |
| 144 | // request when the enable_referrers renderer preference is `false`. See |
| 145 | // https://crbug.com/966140. |
| 146 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, |
| 147 | DisableReferrersStaleWhileRevalidate) { |
| 148 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 149 | WebContentsImpl* web_contents = |
| 150 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 151 | |
| 152 | // Navigate to the page that will eventually fetch a stale-revalidation |
| 153 | // request. Ensure that the renderer has not crashed. |
| 154 | ASSERT_TRUE(NavigateToURL( |
| 155 | shell(), embedded_test_server()->GetURL("/stale-while-revalidate.html"))); |
| 156 | |
Bruce Long | 1e3e1f54 | 2019-10-16 17:56:28 | [diff] [blame] | 157 | // Force-disable the |enable_referrers| preference. |
| 158 | web_contents->GetMutableRendererPrefs()->enable_referrers = false; |
| 159 | web_contents->SyncRendererPrefs(); |
Dominic Farolino | 9c277c5 | 2019-07-16 10:56:05 | [diff] [blame] | 160 | |
| 161 | // Wait for the stale-while-revalidate tests to pass by observing the page's |
| 162 | // title. If the renderer crashes, the test immediately fails. |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 163 | std::u16string expected_title = u"Pass"; |
Dominic Farolino | 9c277c5 | 2019-07-16 10:56:05 | [diff] [blame] | 164 | TitleWatcher title_watcher(web_contents, expected_title); |
| 165 | |
| 166 | // The invocation of runTest() below starts a test written in JavaScript, that |
| 167 | // after some time, creates a stale-revalidation request. The above IPC |
| 168 | // message should be handled by the renderer (thus updating its preferences), |
| 169 | // before this stale-revalidation request is sent. Technically nothing |
| 170 | // guarantees this will happen, so it is theoretically possible the test is |
| 171 | // racy, however in practice the renderer will always handle the IPC message |
| 172 | // before the stale-revalidation request. This is because the renderer is |
| 173 | // never completely blocked from the time the test starts. |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 174 | EXPECT_TRUE(ExecJs(shell(), "runTest()")); |
Dominic Farolino | 9c277c5 | 2019-07-16 10:56:05 | [diff] [blame] | 175 | ASSERT_EQ(expected_title, title_watcher.WaitAndGetTitle()); |
| 176 | } |
| 177 | |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 178 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, SniffNoContentTypeNoData) { |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 179 | // Make sure no downloads start. |
Lukasz Anforowicz | 48d8345 | 2021-05-12 02:58:20 | [diff] [blame] | 180 | shell() |
| 181 | ->web_contents() |
| 182 | ->GetBrowserContext() |
| 183 | ->GetDownloadManager() |
Thanh Le | 006f2532 | 2017-09-13 17:38:04 | [diff] [blame] | 184 | ->AddObserver(this); |
John Abd-El-Malek | 7e8aa6ab | 2018-04-25 20:21:43 | [diff] [blame] | 185 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 186 | GURL url(embedded_test_server()->GetURL("/content-sniffer-test3.html")); |
| 187 | CheckTitleTest(url, "Content Sniffer Test 3"); |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 188 | EXPECT_EQ(1u, Shell::windows().size()); |
[email protected] | 30b97df | 2012-04-21 02:15:28 | [diff] [blame] | 189 | ASSERT_FALSE(got_downloads()); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 190 | } |
| 191 | |
Matt Menke | fcbb1bd7 | 2018-01-31 21:53:12 | [diff] [blame] | 192 | // Make sure file URLs are not sniffed as HTML when they don't end in HTML. |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 193 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, DoNotSniffHTMLFromFileUrl) { |
Matt Menke | fcbb1bd7 | 2018-01-31 21:53:12 | [diff] [blame] | 194 | base::FilePath path = |
| 195 | GetTestFilePath(nullptr, "content-sniffer-test5.not-html"); |
| 196 | GURL file_url = net::FilePathToFileURL(path); |
| 197 | // If the file isn't rendered as HTML, the title will match the name of the |
| 198 | // file, rather than the contents of the file's title tag. |
| 199 | CheckTitleTest(file_url, path.BaseName().MaybeAsASCII()); |
| 200 | } |
| 201 | |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 202 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, ContentDispositionEmpty) { |
John Abd-El-Malek | 7e8aa6ab | 2018-04-25 20:21:43 | [diff] [blame] | 203 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 204 | GURL url(embedded_test_server()->GetURL("/content-disposition-empty.html")); |
| 205 | CheckTitleTest(url, "success"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 206 | } |
| 207 | |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 208 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, ContentDispositionInline) { |
John Abd-El-Malek | 7e8aa6ab | 2018-04-25 20:21:43 | [diff] [blame] | 209 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 210 | GURL url(embedded_test_server()->GetURL("/content-disposition-inline.html")); |
| 211 | CheckTitleTest(url, "success"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // Test for bug #1091358. |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 215 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, SyncXMLHttpRequest) { |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 216 | ASSERT_TRUE(embedded_test_server()->Start()); |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 217 | EXPECT_TRUE(NavigateToURL( |
| 218 | shell(), embedded_test_server()->GetURL("/sync_xmlhttprequest.html"))); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 219 | |
| 220 | // Let's check the XMLHttpRequest ran successfully. |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 221 | EXPECT_EQ(true, EvalJs(shell(), "DidSyncRequestSucceed();")); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // If this flakes, use http://crbug.com/62776. |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 225 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, SyncXMLHttpRequest_Disallowed) { |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 226 | ASSERT_TRUE(embedded_test_server()->Start()); |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 227 | EXPECT_TRUE(NavigateToURL( |
| 228 | shell(), |
| 229 | embedded_test_server()->GetURL("/sync_xmlhttprequest_disallowed.html"))); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 230 | |
| 231 | // Let's check the XMLHttpRequest ran successfully. |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 232 | EXPECT_EQ(true, EvalJs(shell(), "DidSucceed();")); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // Test for bug #1159553 -- A synchronous xhr (whose content-type is |
| 236 | // downloadable) would trigger download and hang the renderer process, |
| 237 | // if executed while navigating to a new page. |
[email protected] | ac5e5c0 | 2013-07-04 05:50:28 | [diff] [blame] | 238 | // Disabled on Mac: see http://crbug.com/56264 |
Xiaohan Wang | bf7702b9 | 2022-01-15 14:34:20 | [diff] [blame] | 239 | #if BUILDFLAG(IS_MAC) |
[email protected] | ac5e5c0 | 2013-07-04 05:50:28 | [diff] [blame] | 240 | #define MAYBE_SyncXMLHttpRequest_DuringUnload \ |
| 241 | DISABLED_SyncXMLHttpRequest_DuringUnload |
| 242 | #else |
| 243 | #define MAYBE_SyncXMLHttpRequest_DuringUnload SyncXMLHttpRequest_DuringUnload |
| 244 | #endif |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 245 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, |
[email protected] | ac5e5c0 | 2013-07-04 05:50:28 | [diff] [blame] | 246 | MAYBE_SyncXMLHttpRequest_DuringUnload) { |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 247 | ASSERT_TRUE(embedded_test_server()->Start()); |
Lukasz Anforowicz | 48d8345 | 2021-05-12 02:58:20 | [diff] [blame] | 248 | shell() |
| 249 | ->web_contents() |
| 250 | ->GetBrowserContext() |
| 251 | ->GetDownloadManager() |
Thanh Le | 006f2532 | 2017-09-13 17:38:04 | [diff] [blame] | 252 | ->AddObserver(this); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 253 | |
| 254 | CheckTitleTest( |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 255 | embedded_test_server()->GetURL("/sync_xmlhttprequest_during_unload.html"), |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 256 | "sync xhr on unload"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 257 | |
| 258 | // Navigate to a new page, to dispatch unload event and trigger xhr. |
| 259 | // (the bug would make this step hang the renderer). |
Thanh Le | 006f2532 | 2017-09-13 17:38:04 | [diff] [blame] | 260 | CheckTitleTest(embedded_test_server()->GetURL("/title2.html"), |
| 261 | "Title Of Awesomeness"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 262 | |
[email protected] | 30b97df | 2012-04-21 02:15:28 | [diff] [blame] | 263 | ASSERT_FALSE(got_downloads()); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 264 | } |
| 265 | |
tzik | 31f1ac0 | 2016-11-16 08:48:36 | [diff] [blame] | 266 | namespace { |
| 267 | |
John Abd-El-Malek | 7e8aa6ab | 2018-04-25 20:21:43 | [diff] [blame] | 268 | // Responds with a HungResponse for the specified URL to hang on the request. |
John Abd-El-Malek | ff2fb3d1 | 2019-07-29 06:32:12 | [diff] [blame] | 269 | // It crashes the process. |
Matt Menke | 7c6443c7a | 2018-06-12 19:00:56 | [diff] [blame] | 270 | // |
| 271 | // |crash_network_service_callback| crashes the network service when invoked, |
| 272 | // and must be called on the UI thread. |
tzik | 31f1ac0 | 2016-11-16 08:48:36 | [diff] [blame] | 273 | std::unique_ptr<net::test_server::HttpResponse> CancelOnRequest( |
| 274 | const std::string& relative_url, |
| 275 | int child_id, |
Matt Menke | 7c6443c7a | 2018-06-12 19:00:56 | [diff] [blame] | 276 | base::RepeatingClosure crash_network_service_callback, |
tzik | 31f1ac0 | 2016-11-16 08:48:36 | [diff] [blame] | 277 | const net::test_server::HttpRequest& request) { |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 278 | if (request.relative_url != relative_url) { |
tzik | 31f1ac0 | 2016-11-16 08:48:36 | [diff] [blame] | 279 | return nullptr; |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 280 | } |
tzik | 31f1ac0 | 2016-11-16 08:48:36 | [diff] [blame] | 281 | |
Hiroshige Hayashizaki | b5e8a12 | 2024-02-26 14:44:07 | [diff] [blame] | 282 | GetUIThreadTaskRunner({})->PostTask(FROM_HERE, |
| 283 | crash_network_service_callback); |
tzik | 31f1ac0 | 2016-11-16 08:48:36 | [diff] [blame] | 284 | |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 285 | return std::make_unique<net::test_server::HungResponse>(); |
tzik | 31f1ac0 | 2016-11-16 08:48:36 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | } // namespace |
| 289 | |
| 290 | // Tests the case where the request is cancelled by a layer above the |
| 291 | // URLRequest, which passes the error on ResourceLoader teardown, rather than in |
| 292 | // response to call to AsyncResourceHandler::OnResponseComplete. |
Xing Liu | 067ce60 | 2020-07-30 19:30:31 | [diff] [blame] | 293 | // Failed on Android M builder. See crbug/1111427. |
Xiaohan Wang | bf7702b9 | 2022-01-15 14:34:20 | [diff] [blame] | 294 | #if BUILDFLAG(IS_ANDROID) |
Xing Liu | 067ce60 | 2020-07-30 19:30:31 | [diff] [blame] | 295 | #define MAYBE_SyncXMLHttpRequest_Cancelled DISABLED_SyncXMLHttpRequest_Cancelled |
| 296 | #else |
| 297 | #define MAYBE_SyncXMLHttpRequest_Cancelled SyncXMLHttpRequest_Cancelled |
| 298 | #endif |
| 299 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, MAYBE_SyncXMLHttpRequest_Cancelled) { |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 300 | // If network service is running in-process, we can't simulate a crash. |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 301 | if (IsInProcessNetworkService()) { |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 302 | return; |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 303 | } |
Clark DuVall | c2227ae | 2018-11-28 22:26:36 | [diff] [blame] | 304 | |
Makoto Shimazu | 833e1c7 | 2019-10-09 21:14:50 | [diff] [blame] | 305 | embedded_test_server()->RegisterRequestHandler(base::BindRepeating( |
Lukasz Anforowicz | 5440a8e | 2017-09-28 16:52:10 | [diff] [blame] | 306 | &CancelOnRequest, "/hung", |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 307 | shell() |
| 308 | ->web_contents() |
| 309 | ->GetPrimaryMainFrame() |
| 310 | ->GetProcess() |
| 311 | ->GetDeprecatedID(), |
Matt Menke | 7c6443c7a | 2018-06-12 19:00:56 | [diff] [blame] | 312 | base::BindRepeating(&BrowserTestBase::SimulateNetworkServiceCrash, |
| 313 | base::Unretained(this)))); |
martijn | b362455 | 2016-11-30 08:06:09 | [diff] [blame] | 314 | |
| 315 | ASSERT_TRUE(embedded_test_server()->Start()); |
François Doray | c1afdeb | 2021-04-27 15:56:25 | [diff] [blame] | 316 | WaitForLoadStop(shell()->web_contents()); |
martijn | b362455 | 2016-11-30 08:06:09 | [diff] [blame] | 317 | |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 318 | EXPECT_TRUE(NavigateToURL( |
| 319 | shell(), |
| 320 | embedded_test_server()->GetURL("/sync_xmlhttprequest_cancelled.html"))); |
tzik | 31f1ac0 | 2016-11-16 08:48:36 | [diff] [blame] | 321 | |
tzik | 31f1ac0 | 2016-11-16 08:48:36 | [diff] [blame] | 322 | // 19 is the value of NETWORK_ERROR on DOMException. |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 323 | EXPECT_EQ(19, EvalJs(shell(), "getErrorCode();")); |
tzik | 31f1ac0 | 2016-11-16 08:48:36 | [diff] [blame] | 324 | } |
| 325 | |
benwells | 13a1ded9 | 2014-11-04 03:41:29 | [diff] [blame] | 326 | // Flaky everywhere. http://crbug.com/130404 |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 327 | // Tests that onunload is run for cross-site requests. (Bug 1114994) |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 328 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, DISABLED_CrossSiteOnunloadCookie) { |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 329 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 330 | |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 331 | GURL url = embedded_test_server()->GetURL("/onunload_cookie.html"); |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 332 | CheckTitleTest(url, "set cookie on unload"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 333 | |
| 334 | // Navigate to a new cross-site page, to dispatch unload event and set the |
| 335 | // cookie. |
tfarina | 5e7b5723 | 2015-10-17 23:37:40 | [diff] [blame] | 336 | CheckTitleTest( |
| 337 | net::URLRequestMockHTTPJob::GetMockUrl("content-sniffer-test0.html"), |
| 338 | "Content Sniffer Test 0"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 339 | |
| 340 | // Check that the cookie was set. |
| 341 | EXPECT_EQ("onunloadCookie=foo", GetCookies(url)); |
| 342 | } |
| 343 | |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 344 | // If this flakes, use http://crbug.com/130404 |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 345 | // Tests that onunload is run for cross-site requests to URLs that complete |
| 346 | // without network loads (e.g., about:blank, data URLs). |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 347 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, |
[email protected] | 91f60e5 | 2012-10-24 21:46:26 | [diff] [blame] | 348 | DISABLED_CrossSiteImmediateLoadOnunloadCookie) { |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 349 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 350 | |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 351 | GURL url = embedded_test_server()->GetURL("/onunload_cookie.html"); |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 352 | CheckTitleTest(url, "set cookie on unload"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 353 | |
| 354 | // Navigate to a cross-site page that loads immediately without making a |
| 355 | // network request. The unload event should still be run. |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 356 | EXPECT_TRUE(NavigateToURL(shell(), GURL(url::kAboutBlankURL))); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 357 | |
| 358 | // Check that the cookie was set. |
| 359 | EXPECT_EQ("onunloadCookie=foo", GetCookies(url)); |
| 360 | } |
| 361 | |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 362 | namespace { |
| 363 | |
| 364 | // Handles |request| by serving a redirect response. |
dcheng | 40ce7b38 | 2016-04-08 23:46:13 | [diff] [blame] | 365 | std::unique_ptr<net::test_server::HttpResponse> NoContentResponseHandler( |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 366 | const std::string& path, |
| 367 | const net::test_server::HttpRequest& request) { |
brettw | 94a2cc2 | 2015-07-01 19:26:54 | [diff] [blame] | 368 | if (!base::StartsWith(path, request.relative_url, |
Lei Zhang | df291f6 | 2021-04-14 17:23:44 | [diff] [blame] | 369 | base::CompareCase::SENSITIVE)) { |
| 370 | return nullptr; |
| 371 | } |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 372 | |
dcheng | 40ce7b38 | 2016-04-08 23:46:13 | [diff] [blame] | 373 | std::unique_ptr<net::test_server::BasicHttpResponse> http_response( |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 374 | new net::test_server::BasicHttpResponse); |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 375 | http_response->set_code(net::HTTP_NO_CONTENT); |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 376 | return std::move(http_response); |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | } // namespace |
| 380 | |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 381 | // Tests that the unload handler is not run for 204 responses. |
| 382 | // If this flakes use http://crbug.com/80596. |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 383 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, CrossSiteNoUnloadOn204) { |
martijn | 64a5111 | 2016-11-14 23:24:12 | [diff] [blame] | 384 | const char kNoContentPath[] = "/nocontent"; |
| 385 | embedded_test_server()->RegisterRequestHandler( |
Makoto Shimazu | 833e1c7 | 2019-10-09 21:14:50 | [diff] [blame] | 386 | base::BindRepeating(&NoContentResponseHandler, kNoContentPath)); |
martijn | 64a5111 | 2016-11-14 23:24:12 | [diff] [blame] | 387 | |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 388 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 389 | |
| 390 | // Start with a URL that sets a cookie in its unload handler. |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 391 | GURL url = embedded_test_server()->GetURL("/onunload_cookie.html"); |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 392 | CheckTitleTest(url, "set cookie on unload"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 393 | |
| 394 | // Navigate to a cross-site URL that returns a 204 No Content response. |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 395 | EXPECT_TRUE(NavigateToURLAndExpectNoCommit( |
| 396 | shell(), embedded_test_server()->GetURL(kNoContentPath))); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 397 | |
| 398 | // Check that the unload cookie was not set. |
| 399 | EXPECT_EQ("", GetCookies(url)); |
| 400 | } |
| 401 | |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 402 | // Tests that the onbeforeunload and onunload logic is short-circuited if the |
| 403 | // old renderer is gone. In that case, we don't want to wait for the old |
| 404 | // renderer to run the handlers. |
| 405 | // We need to disable this on Mac because the crash causes the OS CrashReporter |
| 406 | // process to kick in to analyze the poor dead renderer. Unfortunately, if the |
| 407 | // app isn't stripped of debug symbols, this takes about five minutes to |
| 408 | // complete and isn't conducive to quick turnarounds. As we don't currently |
| 409 | // strip the app on the build bots, this is bad times. |
Xiaohan Wang | bf7702b9 | 2022-01-15 14:34:20 | [diff] [blame] | 410 | #if BUILDFLAG(IS_MAC) |
johnme | 6e0c4eab | 2015-07-01 16:24:04 | [diff] [blame] | 411 | #define MAYBE_CrossSiteAfterCrash DISABLED_CrossSiteAfterCrash |
| 412 | #else |
| 413 | #define MAYBE_CrossSiteAfterCrash CrossSiteAfterCrash |
| 414 | #endif |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 415 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, MAYBE_CrossSiteAfterCrash) { |
[email protected] | c02f1ba | 2014-02-03 06:53:53 | [diff] [blame] | 416 | // Make sure we have a live process before trying to kill it. |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 417 | EXPECT_TRUE(NavigateToURL(shell(), GURL("about:blank"))); |
[email protected] | c02f1ba | 2014-02-03 06:53:53 | [diff] [blame] | 418 | |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 419 | // Cause the renderer to crash. |
[email protected] | 8ffad4e | 2014-01-02 23:18:26 | [diff] [blame] | 420 | RenderProcessHostWatcher crash_observer( |
| 421 | shell()->web_contents(), |
| 422 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 423 | EXPECT_FALSE(NavigateToURL(shell(), GURL(blink::kChromeUICrashURL))); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 424 | // Wait for browser to notice the renderer crash. |
| 425 | crash_observer.Wait(); |
| 426 | |
| 427 | // Navigate to a new cross-site page. The browser should not wait around for |
| 428 | // the old renderer's on{before}unload handlers to run. |
John Abd-El-Malek | 7e8aa6ab | 2018-04-25 20:21:43 | [diff] [blame] | 429 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 430 | GURL url(embedded_test_server()->GetURL("/content-sniffer-test0.html")); |
| 431 | CheckTitleTest(url, "Content Sniffer Test 0"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 432 | } |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 433 | |
| 434 | // Tests that cross-site navigations work when the new page does not go through |
| 435 | // the BufferedEventHandler (e.g., non-http{s} URLs). (Bug 1225872) |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 436 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, CrossSiteNavigationNonBuffered) { |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 437 | // Start with an HTTP page. |
John Abd-El-Malek | 7e8aa6ab | 2018-04-25 20:21:43 | [diff] [blame] | 438 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 439 | GURL url1(embedded_test_server()->GetURL("/content-sniffer-test0.html")); |
| 440 | CheckTitleTest(url1, "Content Sniffer Test 0"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 441 | |
| 442 | // Now load a file:// page, which does not use the BufferedEventHandler. |
| 443 | // Make sure that the page loads and displays a title, and doesn't get stuck. |
John Abd-El-Malek | 7e8aa6ab | 2018-04-25 20:21:43 | [diff] [blame] | 444 | GURL url2 = GetTestUrl("", "title2.html"); |
| 445 | CheckTitleTest(url2, "Title Of Awesomeness"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 446 | } |
| 447 | |
benwells | 13a1ded9 | 2014-11-04 03:41:29 | [diff] [blame] | 448 | // Flaky everywhere. http://crbug.com/130404 |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 449 | // Tests that a cross-site navigation to an error page (resulting in the link |
| 450 | // doctor page) still runs the onunload handler and can support navigations |
| 451 | // away from the link doctor page. (Bug 1235537) |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 452 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, |
benwells | 13a1ded9 | 2014-11-04 03:41:29 | [diff] [blame] | 453 | DISABLED_CrossSiteNavigationErrorPage) { |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 454 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 455 | |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 456 | GURL url(embedded_test_server()->GetURL("/onunload_cookie.html")); |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 457 | CheckTitleTest(url, "set cookie on unload"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 458 | |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 459 | // Navigate to a new cross-site URL that results in an error. |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 460 | // TODO(creis): If this causes crashes or hangs, it might be for the same |
| 461 | // reason as ErrorPageTest::DNSError. See bug 1199491 and |
| 462 | // http://crbug.com/22877. |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 463 | GURL failed_url(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 464 | std::unique_ptr<URLLoaderInterceptor> url_interceptor = |
| 465 | URLLoaderInterceptor::SetupRequestFailForURL(failed_url, |
| 466 | net::ERR_NAME_NOT_RESOLVED); |
| 467 | EXPECT_FALSE(NavigateToURL(shell(), failed_url)); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 468 | |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 469 | EXPECT_NE(u"set cookie on unload", shell()->web_contents()->GetTitle()); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 470 | |
| 471 | // Check that the cookie was set, meaning that the onunload handler ran. |
| 472 | EXPECT_EQ("onunloadCookie=foo", GetCookies(url)); |
| 473 | |
| 474 | // Check that renderer-initiated navigations still work. In a previous bug, |
| 475 | // the ResourceDispatcherHost would think that such navigations were |
| 476 | // cross-site, because we didn't clean up from the previous request. Since |
| 477 | // WebContentsImpl was in the NORMAL state, it would ignore the attempt to run |
| 478 | // the onunload handler, and the navigation would fail. We can't test by |
| 479 | // redirecting to javascript:window.location='someURL', since javascript: |
| 480 | // URLs are prohibited by policy from interacting with sensitive chrome |
| 481 | // pages of which the error page is one. Instead, use automation to kick |
| 482 | // off the navigation, and wait to see that the tab loads. |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 483 | std::u16string expected_title16(u"Title Of Awesomeness"); |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 484 | TitleWatcher title_watcher(shell()->web_contents(), expected_title16); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 485 | |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 486 | GURL test_url(embedded_test_server()->GetURL("/title2.html")); |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 487 | std::string redirect_script = |
| 488 | "window.location='" + test_url.possibly_invalid_spec() + "';" + "true;"; |
| 489 | EXPECT_EQ(true, EvalJs(shell(), redirect_script)); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 490 | EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); |
| 491 | } |
| 492 | |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 493 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, CrossSiteNavigationErrorPage2) { |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 494 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | c47329d | 2012-07-09 20:00:25 | [diff] [blame] | 495 | |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame] | 496 | GURL url(embedded_test_server()->GetURL("/title2.html")); |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 497 | CheckTitleTest(url, "Title Of Awesomeness"); |
[email protected] | c47329d | 2012-07-09 20:00:25 | [diff] [blame] | 498 | |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 499 | // Navigate to a new cross-site URL that results in an error. |
[email protected] | c47329d | 2012-07-09 20:00:25 | [diff] [blame] | 500 | // TODO(creis): If this causes crashes or hangs, it might be for the same |
| 501 | // reason as ErrorPageTest::DNSError. See bug 1199491 and |
| 502 | // http://crbug.com/22877. |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 503 | GURL failed_url(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 504 | std::unique_ptr<URLLoaderInterceptor> url_interceptor = |
| 505 | URLLoaderInterceptor::SetupRequestFailForURL(failed_url, |
| 506 | net::ERR_NAME_NOT_RESOLVED); |
[email protected] | c47329d | 2012-07-09 20:00:25 | [diff] [blame] | 507 | |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 508 | EXPECT_FALSE(NavigateToURL(shell(), failed_url)); |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 509 | EXPECT_NE(u"Title Of Awesomeness", shell()->web_contents()->GetTitle()); |
[email protected] | c47329d | 2012-07-09 20:00:25 | [diff] [blame] | 510 | |
| 511 | // Repeat navigation. We are testing that this completes. |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 512 | EXPECT_FALSE(NavigateToURL(shell(), failed_url)); |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 513 | EXPECT_NE(u"Title Of Awesomeness", shell()->web_contents()->GetTitle()); |
[email protected] | c47329d | 2012-07-09 20:00:25 | [diff] [blame] | 514 | } |
| 515 | |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 516 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, CrossOriginRedirectBlocked) { |
John Abd-El-Malek | 7e8aa6ab | 2018-04-25 20:21:43 | [diff] [blame] | 517 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 518 | GURL url( |
| 519 | embedded_test_server()->GetURL("/cross-origin-redirect-blocked.html")); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 520 | // We expect the following URL requests from this test: |
John Abd-El-Malek | 7e8aa6ab | 2018-04-25 20:21:43 | [diff] [blame] | 521 | // 1- navigation to http://127.0.0.1:[port]/cross-origin-redirect-blocked.html |
| 522 | // 2- XHR to |
| 523 | // http://127.0.0.1:[port]/server-redirect-302?http://a.com:[port]/title2.html |
| 524 | // 3- above XHR is redirected to http://a.com:[port]/title2.html which should |
| 525 | // be blocked |
| 526 | // 4- When the page notices the above request is blocked, it issues an XHR to |
| 527 | // http://127.0.0.1:[port]/title2.html |
| 528 | // 5- When the above XHR succeed, the page navigates to |
| 529 | // http://127.0.0.1:[port]/title3.html |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 530 | // |
John Abd-El-Malek | 7e8aa6ab | 2018-04-25 20:21:43 | [diff] [blame] | 531 | // If the redirect in #3 were not blocked, we'd instead see a navigation |
| 532 | // to http://a.com[port]/title2.html, and the title would be different. |
| 533 | CheckTitleTest(url, "Title Of More Awesomeness"); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | // Tests that ResourceRequestInfoImpl is updated correctly on failed |
| 537 | // requests, to prevent calling Read on a request that has already failed. |
| 538 | // See bug 40250. |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 539 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, CrossSiteFailedRequest) { |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 540 | // Visit another URL first to trigger a cross-site navigation. |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 541 | EXPECT_TRUE(NavigateToURL(shell(), GetTestUrl("", "simple_page.html"))); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 542 | |
| 543 | // Visit a URL that fails without calling ResourceDispatcherHost::Read. |
| 544 | GURL broken_url("chrome://theme"); |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 545 | EXPECT_FALSE(NavigateToURL(shell(), broken_url)); |
[email protected] | b83fbfe8 | 2012-04-21 00:05:07 | [diff] [blame] | 546 | } |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 547 | |
[email protected] | fbaccee | 2013-08-12 23:24:02 | [diff] [blame] | 548 | namespace { |
| 549 | |
dcheng | 40ce7b38 | 2016-04-08 23:46:13 | [diff] [blame] | 550 | std::unique_ptr<net::test_server::HttpResponse> HandleRedirectRequest( |
[email protected] | fbaccee | 2013-08-12 23:24:02 | [diff] [blame] | 551 | const std::string& request_path, |
| 552 | const net::test_server::HttpRequest& request) { |
brettw | 94a2cc2 | 2015-07-01 19:26:54 | [diff] [blame] | 553 | if (!base::StartsWith(request.relative_url, request_path, |
Lei Zhang | df291f6 | 2021-04-14 17:23:44 | [diff] [blame] | 554 | base::CompareCase::SENSITIVE)) { |
| 555 | return nullptr; |
| 556 | } |
[email protected] | fbaccee | 2013-08-12 23:24:02 | [diff] [blame] | 557 | |
dcheng | 40ce7b38 | 2016-04-08 23:46:13 | [diff] [blame] | 558 | std::unique_ptr<net::test_server::BasicHttpResponse> http_response( |
[email protected] | fbaccee | 2013-08-12 23:24:02 | [diff] [blame] | 559 | new net::test_server::BasicHttpResponse); |
| 560 | http_response->set_code(net::HTTP_FOUND); |
| 561 | http_response->AddCustomHeader( |
| 562 | "Location", request.relative_url.substr(request_path.length())); |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 563 | return std::move(http_response); |
[email protected] | fbaccee | 2013-08-12 23:24:02 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | } // namespace |
| 567 | |
| 568 | // Test that we update the cookie policy URLs correctly when transferring |
| 569 | // navigations. |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 570 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, CookiePolicy) { |
[email protected] | fbaccee | 2013-08-12 23:24:02 | [diff] [blame] | 571 | embedded_test_server()->RegisterRequestHandler( |
Makoto Shimazu | 833e1c7 | 2019-10-09 21:14:50 | [diff] [blame] | 572 | base::BindRepeating(&HandleRedirectRequest, "/redirect?")); |
martijn | 64a5111 | 2016-11-14 23:24:12 | [diff] [blame] | 573 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | fbaccee | 2013-08-12 23:24:02 | [diff] [blame] | 574 | |
| 575 | std::string set_cookie_url(base::StringPrintf( |
pkasting | cba1329 | 2014-11-20 03:35:21 | [diff] [blame] | 576 | "http://localhost:%u/set_cookie.html", embedded_test_server()->port())); |
[email protected] | fbaccee | 2013-08-12 23:24:02 | [diff] [blame] | 577 | GURL url(embedded_test_server()->GetURL("/redirect?" + set_cookie_url)); |
| 578 | |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 579 | std::u16string expected_title16(u"cookie set"); |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 580 | TitleWatcher title_watcher(shell()->web_contents(), expected_title16); |
| 581 | EXPECT_TRUE(NavigateToURL(shell(), url, |
| 582 | GURL(set_cookie_url) /* expected_commit_url */)); |
| 583 | EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); |
[email protected] | fbaccee | 2013-08-12 23:24:02 | [diff] [blame] | 584 | } |
| 585 | |
ohrn | 19893e6 | 2014-11-17 16:00:01 | [diff] [blame] | 586 | // Test that ui::PAGE_TRANSITION_CLIENT_REDIRECT is correctly set |
| 587 | // when encountering a meta refresh tag. |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 588 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, PageTransitionClientRedirect) { |
svaldez | c3a9a17 | 2015-11-03 22:01:33 | [diff] [blame] | 589 | ASSERT_TRUE(embedded_test_server()->Start()); |
ohrn | 19893e6 | 2014-11-17 16:00:01 | [diff] [blame] | 590 | |
ohrn | 19893e6 | 2014-11-17 16:00:01 | [diff] [blame] | 591 | NavigateToURLBlockUntilNavigationsComplete( |
Thanh Le | 006f2532 | 2017-09-13 17:38:04 | [diff] [blame] | 592 | shell(), embedded_test_server()->GetURL("/client_redirect.html"), 2); |
ohrn | 19893e6 | 2014-11-17 16:00:01 | [diff] [blame] | 593 | |
John Abd-El-Malek | ff2fb3d1 | 2019-07-29 06:32:12 | [diff] [blame] | 594 | NavigationEntry* entry = |
| 595 | shell()->web_contents()->GetController().GetLastCommittedEntry(); |
| 596 | |
| 597 | EXPECT_TRUE(entry->GetTransitionType() & ui::PAGE_TRANSITION_CLIENT_REDIRECT); |
ohrn | 19893e6 | 2014-11-17 16:00:01 | [diff] [blame] | 598 | } |
| 599 | |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 600 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, SubresourceRedirectToDataURLBlocked) { |
| 601 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 602 | |
| 603 | EXPECT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL("/echo"))); |
| 604 | |
| 605 | GURL subresource_url = embedded_test_server()->GetURL( |
| 606 | "/server-redirect?data:text/plain,redirected1"); |
| 607 | std::string script = R"((url => { |
| 608 | var xhr = new XMLHttpRequest(); |
| 609 | xhr.open('GET', url, true); |
Chris Fredrickson | b854bbf | 2023-03-27 17:27:44 | [diff] [blame] | 610 | return new Promise(resolve => { |
| 611 | xhr.onload = () => resolve("ALLOWED"); |
| 612 | xhr.onerror = () => resolve("BLOCKED"); |
| 613 | xhr.send(); |
| 614 | }); |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 615 | }))"; |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 616 | |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 617 | EXPECT_EQ("BLOCKED", |
Chris Fredrickson | b854bbf | 2023-03-27 17:27:44 | [diff] [blame] | 618 | EvalJs(shell(), script + "('" + subresource_url.spec() + "')")); |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, RedirectToDataURLBlocked) { |
| 622 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 623 | EXPECT_FALSE(NavigateToURL( |
| 624 | shell(), embedded_test_server()->GetURL( |
| 625 | "/server-redirect?data:text/plain,redirected1"))); |
| 626 | } |
| 627 | |
Clark DuVall | e558158 | 2018-10-11 22:02:26 | [diff] [blame] | 628 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, RedirectToAboutURLBlocked) { |
| 629 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 630 | EXPECT_FALSE(NavigateToURL( |
| 631 | shell(), embedded_test_server()->GetURL( |
| 632 | "/server-redirect?" + std::string(url::kAboutBlankURL)))); |
| 633 | } |
| 634 | |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 635 | namespace { |
| 636 | |
| 637 | // Creates a valid filesystem URL. |
| 638 | GURL CreateFileSystemURL(Shell* window) { |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 639 | std::string filesystem_url_string = EvalJs(window, R"( |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 640 | var blob = new Blob(['<html><body>hello</body></html>'], |
| 641 | {type: 'text/html'}); |
Chris Fredrickson | b854bbf | 2023-03-27 17:27:44 | [diff] [blame] | 642 | new Promise(resolve => { |
| 643 | window.webkitRequestFileSystem(TEMPORARY, blob.size, fs => { |
| 644 | fs.root.getFile('foo.html', {create: true}, file => { |
| 645 | file.createWriter(writer => { |
| 646 | writer.write(blob); |
| 647 | writer.onwriteend = () => { |
| 648 | resolve(file.toURL()); |
| 649 | } |
| 650 | }); |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 651 | }); |
| 652 | }); |
Chris Fredrickson | b854bbf | 2023-03-27 17:27:44 | [diff] [blame] | 653 | });)") |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 654 | .ExtractString(); |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 655 | GURL filesystem_url(filesystem_url_string); |
| 656 | EXPECT_TRUE(filesystem_url.is_valid()); |
| 657 | EXPECT_TRUE(filesystem_url.SchemeIsFileSystem()); |
| 658 | return filesystem_url; |
| 659 | } |
| 660 | |
| 661 | } // namespace |
| 662 | |
| 663 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, |
| 664 | SubresourceRedirectToFileSystemURLBlocked) { |
| 665 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 666 | |
| 667 | EXPECT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL("/echo"))); |
| 668 | |
| 669 | GURL subresource_url = embedded_test_server()->GetURL( |
| 670 | "/server-redirect?" + CreateFileSystemURL(shell()).spec()); |
| 671 | std::string script = R"((url => { |
| 672 | var xhr = new XMLHttpRequest(); |
| 673 | xhr.open('GET', url, true); |
Chris Fredrickson | b854bbf | 2023-03-27 17:27:44 | [diff] [blame] | 674 | return new Promise(resolve => { |
| 675 | xhr.onload = () => resolve("ALLOWED"); |
| 676 | xhr.onerror = () => resolve("BLOCKED"); |
| 677 | xhr.send(); |
| 678 | }); |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 679 | }))"; |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 680 | |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 681 | EXPECT_EQ("BLOCKED", |
Chris Fredrickson | b854bbf | 2023-03-27 17:27:44 | [diff] [blame] | 682 | EvalJs(shell(), script + "('" + subresource_url.spec() + "')")); |
Clark DuVall | b2680c2 | 2018-08-10 15:27:27 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, RedirectToFileSystemURLBlocked) { |
| 686 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 687 | // Need to navigate to a URL first so the filesystem can be created. |
| 688 | EXPECT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL("/echo"))); |
| 689 | |
| 690 | EXPECT_FALSE(NavigateToURL( |
| 691 | shell(), embedded_test_server()->GetURL( |
| 692 | "/server-redirect?" + CreateFileSystemURL(shell()).spec()))); |
| 693 | } |
| 694 | |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 695 | namespace { |
| 696 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 697 | struct RequestData { |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 698 | const GURL url; |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 699 | const net::SiteForCookies site_for_cookies; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 700 | const std::optional<url::Origin> initiator; |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 701 | const int load_flags; |
| 702 | const std::string referrer; |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 703 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 704 | RequestData(const GURL& url, |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 705 | const net::SiteForCookies& site_for_cookies, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 706 | const std::optional<url::Origin>& initiator, |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 707 | int load_flags, |
| 708 | const std::string& referrer) |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 709 | : url(url), |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 710 | site_for_cookies(site_for_cookies), |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 711 | initiator(initiator), |
| 712 | load_flags(load_flags), |
| 713 | referrer(referrer) {} |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 714 | }; |
| 715 | |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 716 | } // namespace |
| 717 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 718 | class RequestDataBrowserTest : public ContentBrowserTest { |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 719 | public: |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 720 | RequestDataBrowserTest() |
| 721 | : interceptor_(std::make_unique<content::URLLoaderInterceptor>( |
| 722 | base::BindRepeating(&RequestDataBrowserTest::OnRequest, |
| 723 | base::Unretained(this)))) {} |
| 724 | ~RequestDataBrowserTest() override {} |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 725 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 726 | std::vector<RequestData> data() { |
| 727 | base::AutoLock auto_lock(requests_lock_); |
| 728 | auto copy = requests_; |
| 729 | return copy; |
| 730 | } |
| 731 | |
John Abd-El-Malek | b95feaa | 2018-06-12 17:50:35 | [diff] [blame] | 732 | void WaitForRequests(size_t count) { |
| 733 | while (true) { |
| 734 | base::RunLoop run_loop; |
| 735 | { |
| 736 | base::AutoLock auto_lock(requests_lock_); |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 737 | if (requests_.size() == count) { |
John Abd-El-Malek | b95feaa | 2018-06-12 17:50:35 | [diff] [blame] | 738 | return; |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 739 | } |
John Abd-El-Malek | b95feaa | 2018-06-12 17:50:35 | [diff] [blame] | 740 | requests_closure_ = run_loop.QuitClosure(); |
| 741 | } |
| 742 | run_loop.Run(); |
| 743 | } |
| 744 | } |
| 745 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 746 | private: |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 747 | void SetUpOnMainThread() override { |
| 748 | ContentBrowserTest::SetUpOnMainThread(); |
| 749 | |
| 750 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 751 | |
jam | 8f34ea7 | 2017-04-26 17:48:55 | [diff] [blame] | 752 | host_resolver()->AddRule("*", "127.0.0.1"); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 753 | } |
| 754 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 755 | void TearDownOnMainThread() override { interceptor_.reset(); } |
| 756 | |
| 757 | bool OnRequest(URLLoaderInterceptor::RequestParams* params) { |
| 758 | RequestCreated(RequestData( |
| 759 | params->url_request.url, params->url_request.site_for_cookies, |
| 760 | params->url_request.request_initiator, params->url_request.load_flags, |
| 761 | params->url_request.referrer.spec())); |
| 762 | return false; |
| 763 | } |
| 764 | |
| 765 | void RequestCreated(RequestData data) { |
| 766 | base::AutoLock auto_lock(requests_lock_); |
| 767 | requests_.push_back(data); |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 768 | if (requests_closure_) { |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame] | 769 | std::move(requests_closure_).Run(); |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 770 | } |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | base::Lock requests_lock_; |
| 774 | std::vector<RequestData> requests_; |
danakj | f4b9e94 | 2019-11-29 15:43:04 | [diff] [blame] | 775 | base::OnceClosure requests_closure_; |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 776 | std::unique_ptr<URLLoaderInterceptor> interceptor_; |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 777 | }; |
| 778 | |
Tsuyoshi Horo | 59aea79 | 2023-05-24 05:21:43 | [diff] [blame] | 779 | IN_PROC_BROWSER_TEST_F(RequestDataBrowserTest, Basic) { |
mkwst | 47673cd | 2016-07-14 09:48:58 | [diff] [blame] | 780 | GURL top_url(embedded_test_server()->GetURL("/page_with_subresources.html")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 781 | url::Origin top_origin = url::Origin::Create(top_url); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 782 | |
| 783 | NavigateToURLBlockUntilNavigationsComplete(shell(), top_url, 1); |
Tsuyoshi Horo | 18ae360 | 2023-05-23 03:26:09 | [diff] [blame] | 784 | WaitForRequests(8u); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 785 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 786 | auto requests = data(); |
| 787 | EXPECT_EQ(8u, requests.size()); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 788 | |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 789 | // All resources loaded directly by the top-level document should have a |
| 790 | // |first_party| and |initiator| that match the URL of the top-level document. |
| 791 | // The top-level document itself doesn't have an |initiator|. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 792 | const RequestData* first_request = &requests[0]; |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 793 | EXPECT_TRUE(first_request->site_for_cookies.IsEquivalent( |
| 794 | net::SiteForCookies::FromUrl(top_url))); |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 795 | EXPECT_FALSE(first_request->initiator.has_value()); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 796 | for (size_t i = 1; i < requests.size(); i++) { |
| 797 | const RequestData* request = &requests[i]; |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 798 | EXPECT_TRUE(request->site_for_cookies.IsEquivalent( |
| 799 | net::SiteForCookies::FromOrigin(top_origin))); |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 800 | ASSERT_TRUE(request->initiator.has_value()); |
| 801 | EXPECT_EQ(top_origin, request->initiator); |
mkwst | 47673cd | 2016-07-14 09:48:58 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 805 | IN_PROC_BROWSER_TEST_F(RequestDataBrowserTest, LinkRelPrefetch) { |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 806 | GURL top_url(embedded_test_server()->GetURL("/link_rel_prefetch.html")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 807 | url::Origin top_origin = url::Origin::Create(top_url); |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 808 | |
| 809 | NavigateToURLBlockUntilNavigationsComplete(shell(), top_url, 1); |
John Abd-El-Malek | b95feaa | 2018-06-12 17:50:35 | [diff] [blame] | 810 | WaitForRequests(2u); |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 811 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 812 | auto requests = data(); |
| 813 | EXPECT_EQ(2u, requests.size()); |
| 814 | auto* request = &requests[1]; |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 815 | EXPECT_EQ(top_origin, request->initiator); |
| 816 | EXPECT_EQ(top_url, request->referrer); |
| 817 | EXPECT_TRUE(request->load_flags & net::LOAD_PREFETCH); |
| 818 | } |
| 819 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 820 | IN_PROC_BROWSER_TEST_F(RequestDataBrowserTest, LinkRelPrefetchReferrerPolicy) { |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 821 | GURL top_url(embedded_test_server()->GetURL( |
| 822 | "/link_rel_prefetch_referrer_policy.html")); |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 823 | GURL img_url(embedded_test_server()->GetURL("/image.jpg")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 824 | url::Origin top_origin = url::Origin::Create(top_url); |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 825 | |
| 826 | NavigateToURLBlockUntilNavigationsComplete(shell(), top_url, 1); |
John Abd-El-Malek | b95feaa | 2018-06-12 17:50:35 | [diff] [blame] | 827 | WaitForRequests(2u); |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 828 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 829 | auto requests = data(); |
| 830 | EXPECT_EQ(2u, requests.size()); |
| 831 | auto* main_frame_request = &requests[0]; |
| 832 | auto* image_request = &requests[1]; |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 833 | |
| 834 | // Check the main frame request. |
| 835 | EXPECT_EQ(top_url, main_frame_request->url); |
| 836 | EXPECT_FALSE(main_frame_request->initiator.has_value()); |
| 837 | |
| 838 | // Check the image request. |
| 839 | EXPECT_EQ(img_url, image_request->url); |
| 840 | EXPECT_TRUE(image_request->initiator.has_value()); |
| 841 | EXPECT_EQ(top_origin, image_request->initiator); |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 842 | // Respect the "origin" policy set by the <meta> tag. |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 843 | EXPECT_EQ(top_url.DeprecatedGetOriginAsURL().spec(), image_request->referrer); |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 844 | EXPECT_TRUE(image_request->load_flags & net::LOAD_PREFETCH); |
Benoit Lize | 0a370db7 | 2017-08-28 17:01:29 | [diff] [blame] | 845 | } |
| 846 | |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 847 | // TODO(crbug.com/40805845): Flaky on all platforms. |
Dale Curtis | aa35411 | 2021-11-19 23:30:53 | [diff] [blame] | 848 | IN_PROC_BROWSER_TEST_F(RequestDataBrowserTest, DISABLED_BasicCrossSite) { |
mkwst | 47673cd | 2016-07-14 09:48:58 | [diff] [blame] | 849 | GURL top_url(embedded_test_server()->GetURL( |
| 850 | "a.com", "/nested_page_with_subresources.html")); |
| 851 | GURL nested_url(embedded_test_server()->GetURL( |
| 852 | "not-a.com", "/page_with_subresources.html")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 853 | url::Origin top_origin = url::Origin::Create(top_url); |
| 854 | url::Origin nested_origin = url::Origin::Create(nested_url); |
mkwst | 47673cd | 2016-07-14 09:48:58 | [diff] [blame] | 855 | |
| 856 | NavigateToURLBlockUntilNavigationsComplete(shell(), top_url, 1); |
| 857 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 858 | auto requests = data(); |
| 859 | EXPECT_EQ(9u, requests.size()); |
mkwst | 47673cd | 2016-07-14 09:48:58 | [diff] [blame] | 860 | |
| 861 | // The first items loaded are the top-level and nested documents. These should |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 862 | // both have a |site_for_cookies| that matches the origin of the top-level |
| 863 | // document. The top-level document has no initiator and the nested frame is |
| 864 | // initiated by the top-level document. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 865 | EXPECT_EQ(top_url, requests[0].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 866 | EXPECT_TRUE(requests[0].site_for_cookies.IsEquivalent( |
| 867 | net::SiteForCookies::FromOrigin(top_origin))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 868 | EXPECT_FALSE(requests[0].initiator.has_value()); |
mkwst | 47673cd | 2016-07-14 09:48:58 | [diff] [blame] | 869 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 870 | EXPECT_EQ(nested_url, requests[1].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 871 | EXPECT_TRUE(requests[1].site_for_cookies.IsEquivalent( |
| 872 | net::SiteForCookies::FromOrigin(top_origin))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 873 | EXPECT_EQ(top_origin, requests[1].initiator); |
mkwst | 47673cd | 2016-07-14 09:48:58 | [diff] [blame] | 874 | |
| 875 | // The remaining items are loaded as subresources in the nested document, and |
| 876 | // should have a unique first-party, and an initiator that matches the |
| 877 | // document in which they're embedded. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 878 | for (size_t i = 2; i < requests.size(); i++) { |
| 879 | SCOPED_TRACE(requests[i].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 880 | EXPECT_TRUE(requests[i].site_for_cookies.IsNull()); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 881 | EXPECT_EQ(nested_origin, requests[i].initiator); |
mkwst | 47673cd | 2016-07-14 09:48:58 | [diff] [blame] | 882 | } |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 883 | } |
| 884 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 885 | IN_PROC_BROWSER_TEST_F(RequestDataBrowserTest, SameOriginNested) { |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 886 | GURL top_url(embedded_test_server()->GetURL("/page_with_iframe.html")); |
| 887 | GURL image_url(embedded_test_server()->GetURL("/image.jpg")); |
| 888 | GURL nested_url(embedded_test_server()->GetURL("/title1.html")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 889 | url::Origin top_origin = url::Origin::Create(top_url); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 890 | |
| 891 | NavigateToURLBlockUntilNavigationsComplete(shell(), top_url, 1); |
Tsuyoshi Horo | 18ae360 | 2023-05-23 03:26:09 | [diff] [blame] | 892 | WaitForRequests(3u); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 893 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 894 | auto requests = data(); |
| 895 | EXPECT_EQ(3u, requests.size()); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 896 | |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 897 | // User-initiated top-level navigations have a first-party that matches the |
| 898 | // URL to which they navigate. The navigation was initiated outside of a |
| 899 | // document, so there is no |initiator|. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 900 | EXPECT_EQ(top_url, requests[0].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 901 | EXPECT_TRUE(requests[0].site_for_cookies.IsEquivalent( |
| 902 | net::SiteForCookies::FromOrigin(top_origin))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 903 | EXPECT_FALSE(requests[0].initiator.has_value()); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 904 | |
| 905 | // Subresource requests have a first-party and initiator that matches the |
| 906 | // document in which they're embedded. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 907 | EXPECT_EQ(image_url, requests[1].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 908 | EXPECT_TRUE(requests[1].site_for_cookies.IsEquivalent( |
| 909 | net::SiteForCookies::FromOrigin(top_origin))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 910 | EXPECT_EQ(top_origin, requests[1].initiator); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 911 | |
| 912 | // Same-origin nested frames have a first-party and initiator that matches |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 913 | // the document in which they're embedded (since the frame is same site with |
| 914 | // toplevel). |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 915 | EXPECT_EQ(nested_url, requests[2].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 916 | EXPECT_TRUE(requests[2].site_for_cookies.IsEquivalent( |
| 917 | net::SiteForCookies::FromOrigin(top_origin))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 918 | EXPECT_EQ(top_origin, requests[2].initiator); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 919 | } |
| 920 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 921 | IN_PROC_BROWSER_TEST_F(RequestDataBrowserTest, SameOriginAuxiliary) { |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 922 | GURL top_url(embedded_test_server()->GetURL("/simple_links.html")); |
| 923 | GURL auxiliary_url(embedded_test_server()->GetURL("/title2.html")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 924 | url::Origin top_origin = url::Origin::Create(top_url); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 925 | |
| 926 | NavigateToURLBlockUntilNavigationsComplete(shell(), top_url, 1); |
| 927 | |
| 928 | ShellAddedObserver new_shell_observer; |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 929 | EXPECT_EQ(true, EvalJs(shell(), "clickSameSiteNewWindowLink();")); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 930 | Shell* new_shell = new_shell_observer.GetShell(); |
Fergal Daly | f052233 | 2020-07-18 06:09:46 | [diff] [blame] | 931 | EXPECT_TRUE(WaitForLoadStop(new_shell->web_contents())); |
Tsuyoshi Horo | 18ae360 | 2023-05-23 03:26:09 | [diff] [blame] | 932 | WaitForRequests(2u); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 933 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 934 | auto requests = data(); |
| 935 | EXPECT_EQ(2u, requests.size()); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 936 | |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 937 | // User-initiated top-level navigations have a first-party that matches the |
| 938 | // URL to which they navigate, even if they fail to load. The navigation was |
| 939 | // initiated outside of a document, so there is no |initiator|. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 940 | EXPECT_EQ(top_url, requests[0].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 941 | EXPECT_TRUE(requests[0].site_for_cookies.IsEquivalent( |
| 942 | net::SiteForCookies::FromOrigin(top_origin))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 943 | EXPECT_FALSE(requests[0].initiator.has_value()); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 944 | |
| 945 | // Auxiliary navigations have a first-party that matches the URL to which they |
| 946 | // navigate, and an initiator that matches the document that triggered them. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 947 | EXPECT_EQ(auxiliary_url, requests[1].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 948 | EXPECT_TRUE(requests[1].site_for_cookies.IsEquivalent( |
| 949 | net::SiteForCookies::FromUrl(auxiliary_url))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 950 | EXPECT_EQ(top_origin, requests[1].initiator); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 951 | } |
| 952 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 953 | IN_PROC_BROWSER_TEST_F(RequestDataBrowserTest, CrossOriginAuxiliary) { |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 954 | GURL top_url(embedded_test_server()->GetURL("/simple_links.html")); |
| 955 | GURL auxiliary_url(embedded_test_server()->GetURL("foo.com", "/title2.html")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 956 | url::Origin top_origin = url::Origin::Create(top_url); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 957 | |
| 958 | NavigateToURLBlockUntilNavigationsComplete(shell(), top_url, 1); |
| 959 | |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 960 | const char kReplacePortNumber[] = "setPortNumber(%d);"; |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 961 | uint16_t port_number = embedded_test_server()->port(); |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 962 | EXPECT_TRUE( |
| 963 | ExecJs(shell(), base::StringPrintf(kReplacePortNumber, port_number))); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 964 | |
| 965 | ShellAddedObserver new_shell_observer; |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 966 | EXPECT_EQ(true, EvalJs(shell(), "clickCrossSiteNewWindowLink();")); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 967 | Shell* new_shell = new_shell_observer.GetShell(); |
Fergal Daly | f052233 | 2020-07-18 06:09:46 | [diff] [blame] | 968 | EXPECT_TRUE(WaitForLoadStop(new_shell->web_contents())); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 969 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 970 | auto requests = data(); |
| 971 | EXPECT_EQ(2u, requests.size()); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 972 | |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 973 | // User-initiated top-level navigations have a first-party that matches the |
| 974 | // URL to which they navigate, even if they fail to load. The navigation was |
| 975 | // initiated outside of a document, so there is no initiator. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 976 | EXPECT_EQ(top_url, requests[0].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 977 | EXPECT_TRUE(requests[0].site_for_cookies.IsEquivalent( |
| 978 | net::SiteForCookies::FromOrigin(top_origin))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 979 | EXPECT_FALSE(requests[0].initiator.has_value()); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 980 | |
| 981 | // Auxiliary navigations have a first-party that matches the URL to which they |
| 982 | // navigate, and an initiator that matches the document that triggered them. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 983 | EXPECT_EQ(auxiliary_url, requests[1].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 984 | EXPECT_TRUE(requests[1].site_for_cookies.IsEquivalent( |
| 985 | net::SiteForCookies::FromUrl(auxiliary_url))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 986 | EXPECT_EQ(top_origin, requests[1].initiator); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 987 | } |
| 988 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 989 | IN_PROC_BROWSER_TEST_F(RequestDataBrowserTest, FailedNavigation) { |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 990 | // Navigating to this URL will fail, as we haven't taught the host resolver |
| 991 | // about 'a.com'. |
| 992 | GURL top_url(embedded_test_server()->GetURL("a.com", "/simple_page.html")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 993 | url::Origin top_origin = url::Origin::Create(top_url); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 994 | |
| 995 | NavigateToURLBlockUntilNavigationsComplete(shell(), top_url, 1); |
| 996 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 997 | auto requests = data(); |
| 998 | EXPECT_EQ(1u, requests.size()); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 999 | |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 1000 | // User-initiated top-level navigations have a first-party that matches the |
| 1001 | // URL to which they navigate, even if they fail to load. The navigation was |
| 1002 | // initiated outside of a document, so there is no initiator. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1003 | EXPECT_EQ(top_url, requests[0].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 1004 | EXPECT_TRUE(requests[0].site_for_cookies.IsEquivalent( |
| 1005 | net::SiteForCookies::FromOrigin(top_origin))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1006 | EXPECT_FALSE(requests[0].initiator.has_value()); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 1007 | } |
| 1008 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1009 | IN_PROC_BROWSER_TEST_F(RequestDataBrowserTest, CrossOriginNested) { |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 1010 | GURL top_url(embedded_test_server()->GetURL( |
| 1011 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 1012 | GURL top_js_url( |
| 1013 | embedded_test_server()->GetURL("a.com", "/tree_parser_util.js")); |
| 1014 | GURL nested_url(embedded_test_server()->GetURL( |
| 1015 | "b.com", "/cross_site_iframe_factory.html?b()")); |
| 1016 | GURL nested_js_url( |
| 1017 | embedded_test_server()->GetURL("b.com", "/tree_parser_util.js")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 1018 | url::Origin top_origin = url::Origin::Create(top_url); |
| 1019 | url::Origin nested_origin = url::Origin::Create(nested_url); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 1020 | |
| 1021 | NavigateToURLBlockUntilNavigationsComplete(shell(), top_url, 1); |
Tsuyoshi Horo | 18ae360 | 2023-05-23 03:26:09 | [diff] [blame] | 1022 | WaitForRequests(4u); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 1023 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1024 | auto requests = data(); |
| 1025 | EXPECT_EQ(4u, requests.size()); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 1026 | |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 1027 | // User-initiated top-level navigations have a |first-party|. The navigation |
| 1028 | // was initiated outside of a document, so there are no initiator. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1029 | EXPECT_EQ(top_url, requests[0].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 1030 | EXPECT_TRUE(requests[0].site_for_cookies.IsEquivalent( |
| 1031 | net::SiteForCookies::FromOrigin(top_origin))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1032 | EXPECT_FALSE(requests[0].initiator.has_value()); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 1033 | |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1034 | EXPECT_EQ(top_js_url, requests[1].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 1035 | EXPECT_TRUE(requests[1].site_for_cookies.IsEquivalent( |
| 1036 | net::SiteForCookies::FromOrigin(top_origin))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1037 | EXPECT_EQ(top_origin, requests[1].initiator); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 1038 | |
mkwst | 4f2cb7d | 2016-06-23 06:32:25 | [diff] [blame] | 1039 | // Cross-origin frames have a first-party and initiator that matches the URL |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 1040 | // in which they're embedded (if they are the first cross-origin thing) |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1041 | EXPECT_EQ(nested_url, requests[2].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 1042 | EXPECT_TRUE(requests[2].site_for_cookies.IsEquivalent( |
| 1043 | net::SiteForCookies::FromOrigin(top_origin))); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1044 | EXPECT_EQ(top_origin, requests[2].initiator); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 1045 | |
| 1046 | // Cross-origin subresource requests have a unique first-party, and an |
| 1047 | // initiator that matches the document in which they're embedded. |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1048 | EXPECT_EQ(nested_js_url, requests[3].url); |
Maks Orlovich | 5d02d9e0 | 2019-12-15 22:14:10 | [diff] [blame] | 1049 | EXPECT_TRUE(requests[3].site_for_cookies.IsNull()); |
John Abd-El-Malek | 2d217373 | 2018-04-25 13:40:51 | [diff] [blame] | 1050 | EXPECT_EQ(nested_origin, requests[3].initiator); |
mkwst | b1d1a95 | 2016-06-22 14:58:05 | [diff] [blame] | 1051 | } |
| 1052 | |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 1053 | // Regression test for https://crbug.com/648608. An attacker could trivially |
| 1054 | // bypass cookies SameSite=Strict protections by navigating a new window twice. |
John Abd-El-Malek | 688321299 | 2018-04-25 22:32:22 | [diff] [blame] | 1055 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 1056 | CookieSameSiteStrictOpenNewNamedWindowTwice) { |
| 1057 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 1058 | |
| 1059 | // 1) Add cookies for 'a.com', one of them with the "SameSite=Strict" option. |
| 1060 | BrowserContext* context = shell()->web_contents()->GetBrowserContext(); |
| 1061 | GURL a_url("http://a.com"); |
| 1062 | EXPECT_TRUE(SetCookie(context, a_url, "cookie_A=A; SameSite=Strict;")); |
| 1063 | EXPECT_TRUE(SetCookie(context, a_url, "cookie_B=B")); |
| 1064 | |
| 1065 | // 2) Navigate to malicious.com. |
| 1066 | EXPECT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL( |
| 1067 | "malicious.com", "/title1.html"))); |
| 1068 | |
| 1069 | // 2.1) malicious.com opens a new window to 'http://a.com/echoall'. |
| 1070 | GURL echoall_url = embedded_test_server()->GetURL("a.com", "/echoall"); |
| 1071 | std::string script = base::StringPrintf("window.open('%s', 'named_frame');", |
| 1072 | echoall_url.spec().c_str()); |
| 1073 | { |
| 1074 | TestNavigationObserver new_tab_observer(shell()->web_contents(), 1); |
| 1075 | new_tab_observer.StartWatchingNewWebContents(); |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 1076 | EXPECT_TRUE(ExecJs(shell(), script)); |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 1077 | new_tab_observer.Wait(); |
| 1078 | ASSERT_EQ(2u, Shell::windows().size()); |
| 1079 | Shell* new_shell = Shell::windows()[1]; |
| 1080 | EXPECT_TRUE(WaitForLoadStop(new_shell->web_contents())); |
| 1081 | |
| 1082 | // Only the cookie without "SameSite=Strict" should be sent. |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 1083 | std::string html_content = |
| 1084 | EvalJs(new_shell, "document.body.textContent").ExtractString(); |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 1085 | EXPECT_THAT(html_content.c_str(), Not(HasSubstr("cookie_A=A"))); |
| 1086 | EXPECT_THAT(html_content.c_str(), HasSubstr("cookie_B=B")); |
| 1087 | } |
| 1088 | |
| 1089 | // 2.2) Same as in 2.1). The difference is that the new tab will be reused. |
| 1090 | { |
| 1091 | Shell* new_shell = Shell::windows()[1]; |
| 1092 | TestNavigationObserver new_tab_observer(new_shell->web_contents(), 1); |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 1093 | EXPECT_TRUE(ExecJs(shell(), script)); |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 1094 | new_tab_observer.Wait(); |
| 1095 | ASSERT_EQ(2u, Shell::windows().size()); |
| 1096 | EXPECT_TRUE(WaitForLoadStop(new_shell->web_contents())); |
| 1097 | |
| 1098 | // Only the cookie without "SameSite=Strict" should be sent. |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 1099 | std::string html_content = |
| 1100 | EvalJs(new_shell, "document.body.textContent").ExtractString(); |
arthursonzogni | 224aa61 | 2017-12-07 15:31:51 | [diff] [blame] | 1101 | EXPECT_THAT(html_content.c_str(), Not(HasSubstr("cookie_A=A"))); |
| 1102 | EXPECT_THAT(html_content.c_str(), HasSubstr("cookie_B=B")); |
| 1103 | } |
| 1104 | } |
| 1105 | |
Minggang Wang | f6840ecf | 2019-07-29 05:15:02 | [diff] [blame] | 1106 | class URLModifyingThrottle : public blink::URLLoaderThrottle { |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1107 | public: |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1108 | URLModifyingThrottle(bool modify_start, bool modify_redirect) |
| 1109 | : modify_start_(modify_start), modify_redirect_(modify_redirect) {} |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 1110 | |
| 1111 | URLModifyingThrottle(const URLModifyingThrottle&) = delete; |
| 1112 | URLModifyingThrottle& operator=(const URLModifyingThrottle&) = delete; |
| 1113 | |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1114 | ~URLModifyingThrottle() override = default; |
| 1115 | |
| 1116 | void WillStartRequest(network::ResourceRequest* request, |
| 1117 | bool* defer) override { |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 1118 | if (!modify_start_) { |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1119 | return; |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 1120 | } |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1121 | |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1122 | GURL::Replacements replacements; |
| 1123 | replacements.SetQueryStr("foo=bar"); |
| 1124 | request->url = request->url.ReplaceComponents(replacements); |
Takashi Toyoshima | eb2ba20c5 | 2020-04-27 16:34:58 | [diff] [blame] | 1125 | request->headers.SetHeader("Foo", "BarRequest"); |
| 1126 | request->cors_exempt_headers.SetHeader("ExemptFoo", "ExemptBarRequest"); |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1127 | } |
| 1128 | |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1129 | void WillRedirectRequest( |
| 1130 | net::RedirectInfo* redirect_info, |
Lucas Furukawa Gadani | 01188659 | 2019-10-23 17:12:24 | [diff] [blame] | 1131 | const network::mojom::URLResponseHead& response_head, |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1132 | bool* defer, |
| 1133 | std::vector<std::string>* to_be_removed_request_headers, |
Takashi Toyoshima | eb2ba20c5 | 2020-04-27 16:34:58 | [diff] [blame] | 1134 | net::HttpRequestHeaders* modified_request_headers, |
| 1135 | net::HttpRequestHeaders* modified_cors_exempt_request_headers) override { |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 1136 | if (!modify_redirect_) { |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1137 | return; |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 1138 | } |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1139 | |
Takashi Toyoshima | eb2ba20c5 | 2020-04-27 16:34:58 | [diff] [blame] | 1140 | modified_request_headers->SetHeader("Foo", "BarRedirect"); |
| 1141 | modified_cors_exempt_request_headers->SetHeader("ExemptFoo", |
| 1142 | "ExemptBarRedirect"); |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1143 | |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 1144 | if (modified_redirect_url_) { |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1145 | return; // Only need to do this once. |
Solomon Kinard | ab293bae | 2024-09-19 17:13:51 | [diff] [blame] | 1146 | } |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1147 | |
| 1148 | modified_redirect_url_ = true; |
| 1149 | GURL::Replacements replacements; |
| 1150 | replacements.SetQueryStr("foo=bar"); |
| 1151 | redirect_info->new_url = |
| 1152 | redirect_info->new_url.ReplaceComponents(replacements); |
| 1153 | } |
| 1154 | |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1155 | private: |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1156 | bool modify_start_; |
| 1157 | bool modify_redirect_; |
| 1158 | bool modified_redirect_url_ = false; |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1159 | }; |
| 1160 | |
Scott Violet | 9986199 | 2023-02-08 01:20:12 | [diff] [blame] | 1161 | class ThrottleContentBrowserClient |
| 1162 | : public ContentBrowserTestContentBrowserClient { |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1163 | public: |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1164 | ThrottleContentBrowserClient(bool modify_start, bool modify_redirect) |
Scott Violet | 9986199 | 2023-02-08 01:20:12 | [diff] [blame] | 1165 | : modify_start_(modify_start), modify_redirect_(modify_redirect) {} |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 1166 | |
| 1167 | ThrottleContentBrowserClient(const ThrottleContentBrowserClient&) = delete; |
| 1168 | ThrottleContentBrowserClient& operator=(const ThrottleContentBrowserClient&) = |
| 1169 | delete; |
| 1170 | |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1171 | ~ThrottleContentBrowserClient() override {} |
| 1172 | |
| 1173 | // ContentBrowserClient overrides: |
Minggang Wang | f6840ecf | 2019-07-29 05:15:02 | [diff] [blame] | 1174 | std::vector<std::unique_ptr<blink::URLLoaderThrottle>> |
Clark DuVall | 88b09d7 | 2019-08-05 18:58:13 | [diff] [blame] | 1175 | CreateURLLoaderThrottles( |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1176 | const network::ResourceRequest& request, |
Clark DuVall | 88b09d7 | 2019-08-05 18:58:13 | [diff] [blame] | 1177 | BrowserContext* browser_context, |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1178 | const base::RepeatingCallback<WebContents*()>& wc_getter, |
| 1179 | NavigationUIData* navigation_ui_data, |
Avi Drissman | 580a3da6 | 2024-09-04 16:16:56 | [diff] [blame] | 1180 | FrameTreeNodeId frame_tree_node_id, |
Vikram Pasupathy | b53137e5 | 2024-02-01 01:46:53 | [diff] [blame] | 1181 | std::optional<int64_t> navigation_id) override { |
Minggang Wang | f6840ecf | 2019-07-29 05:15:02 | [diff] [blame] | 1182 | std::vector<std::unique_ptr<blink::URLLoaderThrottle>> throttles; |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1183 | auto throttle = |
| 1184 | std::make_unique<URLModifyingThrottle>(modify_start_, modify_redirect_); |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1185 | throttles.push_back(std::move(throttle)); |
| 1186 | return throttles; |
| 1187 | } |
| 1188 | |
| 1189 | private: |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1190 | bool modify_start_; |
| 1191 | bool modify_redirect_; |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1192 | }; |
| 1193 | |
Minggang Wang | f6840ecf | 2019-07-29 05:15:02 | [diff] [blame] | 1194 | // Ensures if a URLLoaderThrottle modifies a URL in WillStartRequest the |
| 1195 | // new request matches |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1196 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, URLLoaderThrottleStartModify) { |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1197 | base::Lock lock; |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1198 | ThrottleContentBrowserClient content_browser_client(true, false); |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1199 | |
| 1200 | std::set<GURL> urls_requested; |
John Abd-El-Malek | c460cb6 | 2018-11-08 00:29:56 | [diff] [blame] | 1201 | std::map<GURL, net::test_server::HttpRequest::HeaderMap> header_map; |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1202 | embedded_test_server()->RegisterRequestMonitor(base::BindLambdaForTesting( |
| 1203 | [&](const net::test_server::HttpRequest& request) { |
| 1204 | base::AutoLock auto_lock(lock); |
| 1205 | urls_requested.insert(request.GetURL()); |
John Abd-El-Malek | c460cb6 | 2018-11-08 00:29:56 | [diff] [blame] | 1206 | header_map[request.GetURL()] = request.headers; |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1207 | })); |
| 1208 | |
| 1209 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 1210 | |
| 1211 | GURL url = embedded_test_server()->GetURL("/simple_page.html"); |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 1212 | GURL expected_url(url.spec() + "?foo=bar"); |
| 1213 | EXPECT_TRUE( |
| 1214 | NavigateToURL(shell(), url, expected_url /* expected_commit_url */)); |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1215 | |
| 1216 | { |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1217 | base::AutoLock auto_lock(lock); |
| 1218 | ASSERT_TRUE(urls_requested.find(expected_url) != urls_requested.end()); |
Takashi Toyoshima | eb2ba20c5 | 2020-04-27 16:34:58 | [diff] [blame] | 1219 | ASSERT_TRUE(header_map[expected_url]["Foo"] == "BarRequest"); |
| 1220 | ASSERT_TRUE(header_map[expected_url]["ExemptFoo"] == "ExemptBarRequest"); |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1221 | } |
John Abd-El-Malek | c32804f | 2018-11-07 19:10:37 | [diff] [blame] | 1222 | } |
| 1223 | |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1224 | // Ensures if a URLLoaderThrottle modifies a URL and headers in |
| 1225 | // WillRedirectRequest the new request matches. |
| 1226 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, URLLoaderThrottleRedirectModify) { |
| 1227 | base::Lock lock; |
| 1228 | ThrottleContentBrowserClient content_browser_client(false, true); |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1229 | |
| 1230 | std::set<GURL> urls_requested; |
| 1231 | std::map<GURL, net::test_server::HttpRequest::HeaderMap> header_map; |
| 1232 | embedded_test_server()->RegisterRequestMonitor(base::BindLambdaForTesting( |
| 1233 | [&](const net::test_server::HttpRequest& request) { |
| 1234 | base::AutoLock auto_lock(lock); |
| 1235 | urls_requested.insert(request.GetURL()); |
| 1236 | header_map[request.GetURL()] = request.headers; |
| 1237 | })); |
| 1238 | |
| 1239 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 1240 | |
| 1241 | GURL url = |
| 1242 | embedded_test_server()->GetURL("/server-redirect?simple_page.html"); |
Alex Moshchuk | 765684f5 | 2019-09-21 01:16:41 | [diff] [blame] | 1243 | GURL expected_url = |
| 1244 | embedded_test_server()->GetURL("/simple_page.html?foo=bar"); |
| 1245 | EXPECT_TRUE( |
| 1246 | NavigateToURL(shell(), url, expected_url /* expected_commit_url */)); |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1247 | |
| 1248 | { |
| 1249 | base::AutoLock auto_lock(lock); |
Takashi Toyoshima | eb2ba20c5 | 2020-04-27 16:34:58 | [diff] [blame] | 1250 | ASSERT_EQ(header_map[expected_url]["Foo"], "BarRedirect"); |
| 1251 | ASSERT_EQ(header_map[expected_url]["ExemptFoo"], "ExemptBarRedirect"); |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1252 | ASSERT_NE(urls_requested.find(expected_url), urls_requested.end()); |
| 1253 | } |
John Abd-El-Malek | c16f673 | 2018-11-22 05:06:49 | [diff] [blame] | 1254 | } |
| 1255 | |
Tsuyoshi Horo | 1684bc0 | 2024-06-24 06:11:35 | [diff] [blame] | 1256 | class LoaderNoScriptStreamingBrowserTest : public ContentBrowserTest { |
| 1257 | public: |
| 1258 | LoaderNoScriptStreamingBrowserTest() = default; |
| 1259 | LoaderNoScriptStreamingBrowserTest( |
| 1260 | const LoaderNoScriptStreamingBrowserTest&) = delete; |
| 1261 | LoaderNoScriptStreamingBrowserTest& operator=( |
| 1262 | const LoaderNoScriptStreamingBrowserTest&) = delete; |
| 1263 | ~LoaderNoScriptStreamingBrowserTest() override = default; |
| 1264 | |
| 1265 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 1266 | command_line->AppendSwitchASCII("js-flags", "--no-script-streaming"); |
| 1267 | } |
| 1268 | }; |
| 1269 | |
| 1270 | // Regression test for https://crbug.com/348520461 |
| 1271 | // Loading a script should not cause a crash even when Script Streaming |
| 1272 | // is disabled on V8 side. |
| 1273 | IN_PROC_BROWSER_TEST_F(LoaderNoScriptStreamingBrowserTest, LoadScript) { |
| 1274 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 1275 | EXPECT_TRUE(NavigateToURL( |
| 1276 | shell(), embedded_test_server()->GetURL("/loader/blank.html"))); |
| 1277 | std::string expected_title("DONE"); |
| 1278 | std::u16string expected_title16(ASCIIToUTF16(expected_title)); |
| 1279 | TitleWatcher title_watcher(shell()->web_contents(), expected_title16); |
| 1280 | ASSERT_TRUE(ExecJs(shell(), R"( |
| 1281 | (() => { |
| 1282 | const script = document.createElement('script'); |
| 1283 | script.src = './change_title.js'; |
| 1284 | document.body.appendChild(script); |
| 1285 | })(); |
| 1286 | )")); |
| 1287 | EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); |
| 1288 | } |
| 1289 | |
Tsuyoshi Horo | ffd48736 | 2024-08-29 21:11:17 | [diff] [blame] | 1290 | // Regression test for https://crbug.com/362788339 |
| 1291 | // Tests that script can be loaded when the server responded 304 response. |
Alan Screen | b832de0 | 2024-09-25 02:05:46 | [diff] [blame] | 1292 | // TODO(crbug.com/369439037): Re-enable once flakiness is resolved for Windows |
| 1293 | // ASAN. |
| 1294 | #if BUILDFLAG(IS_WIN) && defined(ADDRESS_SANITIZER) |
| 1295 | #define MAYBE_Subresource304Response DISABLED_Subresource304Response |
| 1296 | #else |
| 1297 | #define MAYBE_Subresource304Response Subresource304Response |
| 1298 | #endif |
| 1299 | IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, MAYBE_Subresource304Response) { |
Tsuyoshi Horo | ffd48736 | 2024-08-29 21:11:17 | [diff] [blame] | 1300 | embedded_test_server()->RegisterRequestHandler(base::BindRepeating( |
| 1301 | [](const net::test_server::HttpRequest& request) |
| 1302 | -> std::unique_ptr<net::test_server::HttpResponse> { |
| 1303 | if (request.relative_url == "/test.html") { |
| 1304 | auto response = |
| 1305 | std::make_unique<net::test_server::BasicHttpResponse>(); |
| 1306 | response->set_content_type("text/html"); |
| 1307 | const size_t kScriptCount = 100; |
| 1308 | std::vector<std::string> html_strings; |
| 1309 | html_strings.emplace_back("<head><title></title><head><script>"); |
| 1310 | html_strings.emplace_back("const kScriptCount = "); |
| 1311 | html_strings.emplace_back(base::NumberToString(kScriptCount)); |
| 1312 | html_strings.emplace_back(";\n"); |
| 1313 | html_strings.emplace_back(R"( |
| 1314 | let count = 0; |
| 1315 | function done() { |
| 1316 | if (++count == kScriptCount) { |
| 1317 | document.title='Scripts Loaded'; |
| 1318 | } |
| 1319 | } |
| 1320 | )"); |
| 1321 | html_strings.emplace_back("</script>"); |
| 1322 | for (size_t i = 0; i < kScriptCount; ++i) { |
| 1323 | html_strings.emplace_back("<script src=\"./test.js?"); |
| 1324 | html_strings.emplace_back(base::NumberToString(i)); |
| 1325 | html_strings.emplace_back("\"></script>"); |
| 1326 | } |
| 1327 | response->set_content(base::StrCat(html_strings)); |
| 1328 | return response; |
| 1329 | } else if (request.relative_url.starts_with("/test.js?")) { |
| 1330 | auto response = |
| 1331 | std::make_unique<net::test_server::BasicHttpResponse>(); |
| 1332 | if (request.headers.contains("if-modified-since")) { |
| 1333 | response->set_code(net::HTTP_NOT_MODIFIED); |
| 1334 | return response; |
| 1335 | } |
| 1336 | response->set_content_type("application/javascript"); |
| 1337 | response->set_content("done();"); |
| 1338 | response->AddCustomHeader("Cache-Control", "max-age=0, no-cache"); |
| 1339 | response->AddCustomHeader("pragma", "no-cache"); |
| 1340 | response->AddCustomHeader("Last-Modified", |
| 1341 | "Wed, 20 Dec 2023 01:00:00 GMT"); |
| 1342 | return response; |
| 1343 | } |
| 1344 | return nullptr; |
| 1345 | })); |
| 1346 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 1347 | { |
| 1348 | std::u16string expected_title(u"Scripts Loaded"); |
| 1349 | TitleWatcher title_watcher(shell()->web_contents(), expected_title); |
| 1350 | EXPECT_TRUE( |
| 1351 | NavigateToURL(shell(), embedded_test_server()->GetURL("/test.html"))); |
| 1352 | EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); |
| 1353 | } |
| 1354 | { |
| 1355 | { |
| 1356 | std::u16string expected_title(u"Title Cleared"); |
| 1357 | TitleWatcher title_watcher(shell()->web_contents(), expected_title); |
| 1358 | EXPECT_EQ("Title Cleared", |
| 1359 | EvalJs(shell(), "document.title = 'Title Cleared';")); |
| 1360 | EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); |
| 1361 | } |
| 1362 | { |
| 1363 | std::u16string expected_title(u"Scripts Loaded"); |
| 1364 | TitleWatcher title_watcher(shell()->web_contents(), expected_title); |
| 1365 | shell()->Reload(); |
| 1366 | EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); |
| 1367 | } |
| 1368 | } |
| 1369 | } |
| 1370 | |
[email protected] | 854e131 | 2012-07-30 17:26:30 | [diff] [blame] | 1371 | } // namespace content |