Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [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 | |
Thomas Lukaszewicz | 00d3927 | 2024-08-21 23:54:10 | [diff] [blame^] | 5 | #include "content/browser/renderer_host/frame_tree.h" |
| 6 | |
Ken Rockot | 86bea1c | 2017-05-16 06:21:35 | [diff] [blame] | 7 | #include "base/command_line.h" |
Ari Chivukula | 5d15efb | 2023-01-21 04:33:52 | [diff] [blame] | 8 | #include "base/strings/strcat.h" |
Aaron Colwell | e953e56 | 2019-07-24 16:47:36 | [diff] [blame] | 9 | #include "base/strings/stringprintf.h" |
shivanigithub | f33e469c | 2021-08-02 13:52:20 | [diff] [blame] | 10 | #include "base/synchronization/lock.h" |
Thomas Lukaszewicz | 00d3927 | 2024-08-21 23:54:10 | [diff] [blame^] | 11 | #include "base/test/run_until.h" |
shivanigithub | 93878d0 | 2021-06-15 11:37:53 | [diff] [blame] | 12 | #include "base/test/scoped_feature_list.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 13 | #include "build/build_config.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 14 | #include "content/browser/renderer_host/frame_tree_node.h" |
Yao Xiao | 6e1f7d3 | 2022-01-07 03:28:40 | [diff] [blame] | 15 | #include "content/browser/renderer_host/navigation_request.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 16 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 17 | #include "content/browser/renderer_host/render_view_host_impl.h" |
| 18 | #include "content/browser/web_contents/web_contents_impl.h" |
Thomas Lukaszewicz | 00d3927 | 2024-08-21 23:54:10 | [diff] [blame^] | 19 | #include "content/common/content_navigation_policy.h" |
shivanigithub | f33e469c | 2021-08-02 13:52:20 | [diff] [blame] | 20 | #include "content/public/browser/browser_thread.h" |
Thomas Lukaszewicz | 00d3927 | 2024-08-21 23:54:10 | [diff] [blame^] | 21 | #include "content/public/browser/dedicated_worker_service.h" |
Dave Tapuska | 87696ae | 2021-11-18 18:48:31 | [diff] [blame] | 22 | #include "content/public/browser/navigation_handle.h" |
Nasko Oskov | 0401d81 | 2021-02-05 22:20:08 | [diff] [blame] | 23 | #include "content/public/browser/site_isolation_policy.h" |
Annie Sullivan | 35977ad | 2019-04-30 21:04:28 | [diff] [blame] | 24 | #include "content/public/common/content_features.h" |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 25 | #include "content/public/common/content_switches.h" |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 26 | #include "content/public/common/origin_util.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 27 | #include "content/public/common/url_constants.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 28 | #include "content/public/test/browser_test.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 29 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 30 | #include "content/public/test/content_browser_test.h" |
| 31 | #include "content/public/test/content_browser_test_utils.h" |
Ari Chivukula | 5d15efb | 2023-01-21 04:33:52 | [diff] [blame] | 32 | #include "content/public/test/content_mock_cert_verifier.h" |
John Abd-El-Malek | 3fea4c4 | 2019-01-10 01:15:42 | [diff] [blame] | 33 | #include "content/public/test/test_frame_navigation_observer.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 34 | #include "content/public/test/test_navigation_observer.h" |
| 35 | #include "content/public/test/test_utils.h" |
| 36 | #include "content/shell/browser/shell.h" |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 37 | #include "content/shell/common/shell_switches.h" |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 38 | #include "content/test/content_browser_test_utils_internal.h" |
David Sanders | c9ef300 | 2022-02-02 15:27:01 | [diff] [blame] | 39 | #include "net/base/features.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 40 | #include "net/dns/mock_host_resolver.h" |
Nan Lin | ea8e9d20 | 2022-04-13 18:31:01 | [diff] [blame] | 41 | #include "net/test/embedded_test_server/controllable_http_response.h" |
Dave Tapuska | 708c273 | 2022-09-23 22:47:49 | [diff] [blame] | 42 | #include "net/test/embedded_test_server/default_handlers.h" |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 43 | #include "net/test/embedded_test_server/embedded_test_server.h" |
shivanigithub | f33e469c | 2021-08-02 13:52:20 | [diff] [blame] | 44 | #include "net/test/embedded_test_server/http_request.h" |
Ari Chivukula | 5d15efb | 2023-01-21 04:33:52 | [diff] [blame] | 45 | #include "services/network/public/cpp/network_switches.h" |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 46 | #include "services/network/public/cpp/web_sandbox_flags.h" |
| 47 | #include "services/network/public/mojom/web_sandbox_flags.mojom-shared.h" |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 48 | #include "third_party/blink/public/common/chrome_debug_urls.h" |
shivanigithub | 93878d0 | 2021-06-15 11:37:53 | [diff] [blame] | 49 | #include "third_party/blink/public/common/features.h" |
Ari Chivukula | 5d15efb | 2023-01-21 04:33:52 | [diff] [blame] | 50 | #include "third_party/blink/public/common/storage_key/storage_key.h" |
Thomas Lukaszewicz | 00d3927 | 2024-08-21 23:54:10 | [diff] [blame^] | 51 | #include "third_party/blink/public/common/switches.h" |
Antonio Gomes | 4b2c513 | 2020-01-16 11:49:48 | [diff] [blame] | 52 | #include "third_party/blink/public/mojom/frame/user_activation_update_types.mojom.h" |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 53 | #include "url/url_constants.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 54 | |
| 55 | namespace content { |
| 56 | |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 57 | namespace { |
| 58 | |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 59 | EvalJsResult GetOriginFromRenderer(FrameTreeNode* node) { |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 60 | return EvalJs(node, "self.origin"); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 61 | } |
| 62 | |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 63 | // Expect that frame_name, id and src match the node's values. |
| 64 | void ExpectAttributesEq(FrameTreeNode* node, |
| 65 | const std::string& frame_name, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 66 | const std::optional<std::string> id, |
| 67 | const std::optional<std::string> src) { |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 68 | EXPECT_EQ(frame_name, node->frame_name()); |
| 69 | EXPECT_EQ(id, node->html_id()); |
| 70 | EXPECT_EQ(src, node->html_src()); |
| 71 | } |
| 72 | |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 73 | } // namespace |
| 74 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 75 | class FrameTreeBrowserTest : public ContentBrowserTest { |
| 76 | public: |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 77 | FrameTreeBrowserTest() = default; |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 78 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 79 | FrameTreeBrowserTest(const FrameTreeBrowserTest&) = delete; |
| 80 | FrameTreeBrowserTest& operator=(const FrameTreeBrowserTest&) = delete; |
| 81 | |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 82 | void SetUpOnMainThread() override { |
| 83 | host_resolver()->AddRule("*", "127.0.0.1"); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 84 | SetupCrossSiteRedirector(embedded_test_server()); |
martijn | 12d2dad | 2016-11-11 10:59:27 | [diff] [blame] | 85 | ASSERT_TRUE(embedded_test_server()->Start()); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 86 | } |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | // Ensures FrameTree correctly reflects page structure during navigations. |
| 90 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeShape) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 91 | GURL base_url = embedded_test_server()->GetURL("A.com", "/site_isolation/"); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 92 | |
| 93 | // Load doc without iframes. Verify FrameTree just has root. |
| 94 | // Frame tree: |
| 95 | // Site-A Root |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 96 | EXPECT_TRUE(NavigateToURL(shell(), base_url.Resolve("blank.html"))); |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 97 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 98 | ->GetPrimaryFrameTree() |
| 99 | .root(); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 100 | EXPECT_EQ(0U, root->child_count()); |
| 101 | |
| 102 | // Add 2 same-site frames. Verify 3 nodes in tree with proper names. |
| 103 | // Frame tree: |
| 104 | // Site-A Root -- Site-A frame1 |
| 105 | // \-- Site-A frame2 |
[email protected] | 71bc140b | 2022-03-14 23:45:01 | [diff] [blame] | 106 | LoadStopObserver observer1(shell()->web_contents()); |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 107 | EXPECT_TRUE(NavigateToURL(shell(), base_url.Resolve("frames-X-X.html"))); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 108 | observer1.Wait(); |
| 109 | ASSERT_EQ(2U, root->child_count()); |
| 110 | EXPECT_EQ(0U, root->child_at(0)->child_count()); |
| 111 | EXPECT_EQ(0U, root->child_at(1)->child_count()); |
| 112 | } |
| 113 | |
| 114 | // TODO(ajwong): Talk with nasko and merge this functionality with |
| 115 | // FrameTreeShape. |
| 116 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeShape2) { |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 117 | EXPECT_TRUE(NavigateToURL( |
| 118 | shell(), embedded_test_server()->GetURL("/frame_tree/top.html"))); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 119 | |
| 120 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 121 | FrameTreeNode* root = wc->GetPrimaryFrameTree().root(); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 122 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 123 | // Check that the root node is properly created. |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 124 | ASSERT_EQ(3UL, root->child_count()); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 125 | ExpectAttributesEq(root, std::string(), std::nullopt, std::nullopt); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 126 | |
| 127 | ASSERT_EQ(2UL, root->child_at(0)->child_count()); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 128 | ExpectAttributesEq(root->child_at(0), "1-1-name", "1-1-id", "1-1.html"); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 129 | |
| 130 | // Verify the deepest node exists and has the right name. |
| 131 | ASSERT_EQ(2UL, root->child_at(2)->child_count()); |
| 132 | EXPECT_EQ(1UL, root->child_at(2)->child_at(1)->child_count()); |
| 133 | EXPECT_EQ(0UL, root->child_at(2)->child_at(1)->child_at(0)->child_count()); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 134 | ExpectAttributesEq(root->child_at(2)->child_at(1)->child_at(0), "3-1-name", |
| 135 | "3-1-id", "3-1.html"); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 136 | |
| 137 | // Navigate to about:blank, which should leave only the root node of the frame |
| 138 | // tree in the browser process. |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 139 | EXPECT_TRUE( |
| 140 | NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"))); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 141 | |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 142 | root = wc->GetPrimaryFrameTree().root(); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 143 | EXPECT_EQ(0UL, root->child_count()); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 144 | ExpectAttributesEq(root, std::string(), std::nullopt, std::nullopt); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Frame attributes of iframe elements are correctly tracked in FrameTree. |
| 148 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeAttributesUpdate) { |
| 149 | EXPECT_TRUE(NavigateToURL( |
| 150 | shell(), embedded_test_server()->GetURL("/frame_tree/top.html"))); |
| 151 | |
| 152 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 153 | FrameTreeNode* root = wc->GetPrimaryFrameTree().root(); |
| 154 | |
| 155 | // Check that the root node is properly created. |
| 156 | ASSERT_EQ(3UL, root->child_count()); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 157 | ExpectAttributesEq(root, std::string(), std::nullopt, std::nullopt); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 158 | |
| 159 | ASSERT_EQ(2UL, root->child_at(0)->child_count()); |
| 160 | ExpectAttributesEq(root->child_at(0), "1-1-name", "1-1-id", "1-1.html"); |
| 161 | |
| 162 | // Change id, name and src of the iframe. |
| 163 | EXPECT_TRUE(ExecJs(root->current_frame_host(), R"( |
| 164 | let iframe = document.getElementById('1-1-id'); |
| 165 | iframe.id = '1-1-updated-id'; |
| 166 | iframe.name = '1-1-updated-name'; |
| 167 | iframe.src = '1-1-updated.html'; |
| 168 | )")); |
| 169 | // |html_name()| gets updated whenever the name attribute gets updated. |
| 170 | EXPECT_EQ("1-1-updated-name", root->child_at(0)->html_name()); |
| 171 | ExpectAttributesEq(root->child_at(0), "1-1-name", "1-1-updated-id", |
| 172 | "1-1-updated.html"); |
| 173 | } |
| 174 | |
| 175 | // Ensures that frames' name attributes and their updates are tracked in |
| 176 | // |html_name()| and window.name and its updates are tracked in |frame_name()|. |
| 177 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameNameVSWindowName) { |
| 178 | EXPECT_TRUE(NavigateToURL( |
| 179 | shell(), embedded_test_server()->GetURL("/frame_tree/top.html"))); |
| 180 | |
| 181 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 182 | FrameTreeNode* root = wc->GetPrimaryFrameTree().root(); |
| 183 | |
| 184 | // Check that the root node is properly created. |
| 185 | ASSERT_EQ(3UL, root->child_count()); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 186 | EXPECT_EQ(std::nullopt, root->html_name()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 187 | EXPECT_EQ(std::string(), root->frame_name()); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 188 | |
| 189 | ASSERT_EQ(2UL, root->child_at(0)->child_count()); |
| 190 | EXPECT_EQ("1-1-name", root->child_at(0)->html_name()); |
| 191 | EXPECT_EQ("1-1-name", root->child_at(0)->frame_name()); |
| 192 | |
| 193 | // Change the name attribute of the iframe. |
| 194 | EXPECT_TRUE(ExecJs(root->current_frame_host(), R"( |
| 195 | let iframe = document.getElementById('1-1-id'); |
| 196 | iframe.name = '1-1-updated-name'; |
| 197 | )")); |
| 198 | // |html_name()| gets updated whenever the name attribute gets updated. |
| 199 | EXPECT_EQ("1-1-updated-name", root->child_at(0)->html_name()); |
| 200 | // |frame_name()| stays the same. |
| 201 | EXPECT_EQ("1-1-name", root->child_at(0)->frame_name()); |
| 202 | |
| 203 | // Change the window.name of the iframe. |
| 204 | EXPECT_TRUE(ExecJs(root->current_frame_host(), R"( |
| 205 | let iframe = document.getElementById('1-1-id'); |
| 206 | iframe.contentWindow.name = '1-1-updated-name-2'; |
| 207 | )")); |
| 208 | // |html_name()| stays the same. |
| 209 | EXPECT_EQ("1-1-updated-name", root->child_at(0)->html_name()); |
| 210 | // |frame_name()| gets updated. |
| 211 | EXPECT_EQ("1-1-updated-name-2", root->child_at(0)->frame_name()); |
| 212 | } |
| 213 | |
| 214 | // Ensures that long attributes are cut down to the max length. |
| 215 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, LongAttributesCutDown) { |
| 216 | EXPECT_TRUE(NavigateToURL( |
| 217 | shell(), embedded_test_server()->GetURL("/frame_tree/top.html"))); |
| 218 | |
| 219 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 220 | FrameTreeNode* root = wc->GetPrimaryFrameTree().root(); |
| 221 | |
| 222 | // Check that the root node is properly created. |
| 223 | ASSERT_EQ(3UL, root->child_count()); |
| 224 | ASSERT_EQ(2UL, root->child_at(0)->child_count()); |
| 225 | EXPECT_EQ("1-1-name", root->child_at(0)->html_name()); |
| 226 | |
| 227 | // Change the name attribute of the iframe. |
| 228 | EXPECT_TRUE(ExecJs(root->current_frame_host(), R"( |
| 229 | let iframe = document.getElementById('1-1-id'); |
| 230 | iframe.id += 'a'.repeat(1200); |
| 231 | iframe.name += 'b'.repeat(1200); |
| 232 | iframe.src += 'c'.repeat(1200) + '.html'; |
| 233 | )")); |
| 234 | // Long attribute is cut down to the maximum length. |
Yuzu Saijo | dc870f9 | 2023-01-20 03:39:11 | [diff] [blame] | 235 | EXPECT_EQ(1024UL, root->child_at(0)->html_id()->size()); |
| 236 | EXPECT_EQ(1024UL, root->child_at(0)->html_name()->size()); |
| 237 | EXPECT_EQ(1024UL, root->child_at(0)->html_src()->size()); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // Insert a frame into the frame tree and ensure that the inserted frame's |
| 241 | // attributes are correctly captured. |
| 242 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, InsertFrameInTree) { |
| 243 | EXPECT_TRUE(NavigateToURL( |
| 244 | shell(), embedded_test_server()->GetURL("/frame_tree/top.html"))); |
| 245 | |
| 246 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 247 | FrameTreeNode* root = wc->GetPrimaryFrameTree().root(); |
| 248 | |
| 249 | // Check that the root node is properly created. |
| 250 | ASSERT_EQ(3UL, root->child_count()); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 251 | ExpectAttributesEq(root, std::string(), std::nullopt, std::nullopt); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 252 | |
| 253 | ASSERT_EQ(2UL, root->child_at(0)->child_count()); |
| 254 | ExpectAttributesEq(root->child_at(0), "1-1-name", "1-1-id", "1-1.html"); |
| 255 | |
| 256 | // Insert a child iframe. |
| 257 | EXPECT_TRUE(ExecJs(root->current_frame_host(), R"( |
| 258 | let new_iframe = document.createElement('iframe'); |
| 259 | new_iframe.id = '1-1-child-id'; |
| 260 | new_iframe.src = '1-1-child.html'; |
| 261 | new_iframe.name = '1-1-child-name'; |
| 262 | |
| 263 | document.body.appendChild(new_iframe); |
| 264 | )")); |
| 265 | // Check that the new iframe is inserted and their attributes are correct. |
| 266 | ASSERT_EQ(4UL, root->child_count()); |
| 267 | ExpectAttributesEq(root->child_at(3), "1-1-child-name", "1-1-child-id", |
| 268 | "1-1-child.html"); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // Test that we can navigate away if the previous renderer doesn't clean up its |
| 272 | // child frames. |
| 273 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeAfterCrash) { |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 274 | EXPECT_TRUE(NavigateToURL( |
| 275 | shell(), embedded_test_server()->GetURL("/frame_tree/top.html"))); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 276 | |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 277 | // Ensure the view and frame are live. |
Lingqi Chi | 641aeee | 2021-09-08 00:32:58 | [diff] [blame] | 278 | RenderFrameHostImpl* rfh1 = static_cast<RenderFrameHostImpl*>( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 279 | shell()->web_contents()->GetPrimaryMainFrame()); |
Kevin McNee | ec16230 | 2022-04-14 16:00:28 | [diff] [blame] | 280 | RenderViewHostImpl* rvh = rfh1->render_view_host(); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 281 | EXPECT_TRUE(rvh->IsRenderViewLive()); |
arthursonzogni | f4c4a4a | 2019-08-08 18:54:44 | [diff] [blame] | 282 | EXPECT_TRUE(rfh1->IsRenderFrameLive()); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 283 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 284 | // Crash the renderer so that it doesn't send any FrameDetached messages. |
| 285 | RenderProcessHostWatcher crash_observer( |
| 286 | shell()->web_contents(), |
| 287 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
Wez | 0abfbf51 | 2018-03-03 01:54:45 | [diff] [blame] | 288 | ASSERT_TRUE( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 289 | shell()->web_contents()->GetPrimaryMainFrame()->GetProcess()->Shutdown( |
| 290 | 0)); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 291 | crash_observer.Wait(); |
| 292 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 293 | // The frame tree should be cleared. |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 294 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 295 | FrameTreeNode* root = wc->GetPrimaryFrameTree().root(); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 296 | EXPECT_EQ(0UL, root->child_count()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 297 | |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 298 | // Ensure the view and frame aren't live anymore. |
| 299 | EXPECT_FALSE(rvh->IsRenderViewLive()); |
arthursonzogni | f4c4a4a | 2019-08-08 18:54:44 | [diff] [blame] | 300 | EXPECT_FALSE(rfh1->IsRenderFrameLive()); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 301 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 302 | // Navigate to a new URL. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 303 | GURL url(embedded_test_server()->GetURL("/title1.html")); |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 304 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 305 | EXPECT_EQ(0UL, root->child_count()); |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 306 | EXPECT_EQ(url, root->current_url()); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 307 | |
arthursonzogni | f4c4a4a | 2019-08-08 18:54:44 | [diff] [blame] | 308 | RenderFrameHostImpl* rfh2 = root->current_frame_host(); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 309 | // Ensure the view and frame are live again. |
| 310 | EXPECT_TRUE(rvh->IsRenderViewLive()); |
arthursonzogni | f4c4a4a | 2019-08-08 18:54:44 | [diff] [blame] | 311 | EXPECT_TRUE(rfh2->IsRenderFrameLive()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 312 | } |
| 313 | |
Thomas Lukaszewicz | 00d3927 | 2024-08-21 23:54:10 | [diff] [blame^] | 314 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, DiscardFrameTree) { |
| 315 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 316 | FrameTree& frame_tree = wc->GetPrimaryFrameTree(); |
| 317 | FrameTreeNode* root = frame_tree.root(); |
| 318 | |
| 319 | EXPECT_TRUE(NavigateToURL( |
| 320 | shell(), embedded_test_server()->GetURL("/frame_tree/top.html"))); |
| 321 | EXPECT_FALSE(wc->GetController().NeedsReload()); |
| 322 | EXPECT_EQ(3UL, root->child_count()); |
| 323 | |
| 324 | // Ensure the view and frame are live. |
| 325 | RenderFrameHostImplWrapper initial_rfh(wc->GetPrimaryMainFrame()); |
| 326 | RenderViewHostImpl* initial_rvh = initial_rfh->render_view_host(); |
| 327 | EXPECT_TRUE(initial_rvh->IsRenderViewLive()); |
| 328 | EXPECT_TRUE(initial_rfh->IsRenderFrameLive()); |
| 329 | |
| 330 | const auto get_child_isn = [&](RenderFrameHostImpl* rfh, int child_pos) { |
| 331 | return rfh->child_at(child_pos) |
| 332 | ->current_frame_host() |
| 333 | ->last_committed_frame_entry() |
| 334 | ->item_sequence_number(); |
| 335 | }; |
| 336 | const int initial_nav_entry_id = |
| 337 | wc->GetController().GetLastCommittedEntry()->GetUniqueID(); |
| 338 | const int64_t initial_isn = |
| 339 | initial_rfh->last_committed_frame_entry()->item_sequence_number(); |
| 340 | const int64_t initial_isn_child1 = get_child_isn(initial_rfh.get(), 0); |
| 341 | const int64_t initial_isn_child2 = get_child_isn(initial_rfh.get(), 1); |
| 342 | const int64_t initial_isn_child3 = get_child_isn(initial_rfh.get(), 2); |
| 343 | |
| 344 | // Discard the frame tree, wait until all child frames have been cleared away. |
| 345 | EXPECT_FALSE(root->was_discarded()); |
| 346 | frame_tree.Discard(); |
| 347 | ASSERT_TRUE( |
| 348 | base::test::RunUntil([&]() { return 0u == root->child_count(); })); |
| 349 | |
| 350 | // The root rfh and rvh should remain unchanged however the child frames |
| 351 | // should have been cleared. |
| 352 | EXPECT_EQ(initial_nav_entry_id, |
| 353 | wc->GetController().GetLastCommittedEntry()->GetUniqueID()); |
| 354 | EXPECT_EQ(initial_isn, |
| 355 | initial_rfh->last_committed_frame_entry()->item_sequence_number()); |
| 356 | |
| 357 | EXPECT_TRUE(root->was_discarded()); |
| 358 | EXPECT_TRUE(wc->GetController().NeedsReload()); |
| 359 | EXPECT_EQ(initial_rfh.get(), wc->GetPrimaryMainFrame()); |
| 360 | EXPECT_EQ(initial_rvh, wc->GetPrimaryMainFrame()->render_view_host()); |
| 361 | EXPECT_TRUE(initial_rvh->IsRenderViewLive()); |
| 362 | EXPECT_TRUE(initial_rfh->IsRenderFrameLive()); |
| 363 | EXPECT_EQ(0u, root->child_count()); |
| 364 | |
| 365 | // Reload the frame tree. Child frames should be reloaded and the root rfh and |
| 366 | // rvh should have changed. |
| 367 | wc->GetController().LoadIfNecessary(); |
| 368 | EXPECT_TRUE(WaitForLoadStop(wc)); |
| 369 | |
| 370 | RenderFrameHostImplWrapper final_rfh(wc->GetPrimaryMainFrame()); |
| 371 | RenderViewHostImpl* final_rvh = final_rfh->render_view_host(); |
| 372 | EXPECT_EQ(initial_nav_entry_id, |
| 373 | wc->GetController().GetLastCommittedEntry()->GetUniqueID()); |
| 374 | EXPECT_EQ(initial_isn, |
| 375 | final_rfh->last_committed_frame_entry()->item_sequence_number()); |
| 376 | EXPECT_EQ(initial_isn_child1, get_child_isn(final_rfh.get(), 0)); |
| 377 | EXPECT_EQ(initial_isn_child2, get_child_isn(final_rfh.get(), 1)); |
| 378 | EXPECT_EQ(initial_isn_child3, get_child_isn(final_rfh.get(), 2)); |
| 379 | |
| 380 | EXPECT_FALSE(root->was_discarded()); |
| 381 | EXPECT_FALSE(wc->GetController().NeedsReload()); |
| 382 | EXPECT_EQ(3u, root->child_count()); |
| 383 | EXPECT_NE(initial_rfh.get(), final_rfh.get()); |
| 384 | EXPECT_NE(initial_rvh, final_rvh); |
| 385 | EXPECT_TRUE(final_rvh->IsRenderViewLive()); |
| 386 | EXPECT_TRUE(final_rfh->IsRenderFrameLive()); |
| 387 | } |
| 388 | |
| 389 | class DedicatedWorkerObserver : public DedicatedWorkerService::Observer { |
| 390 | public: |
| 391 | explicit DedicatedWorkerObserver(DedicatedWorkerService* worker_service) { |
| 392 | scoped_context_observation_.Observe(worker_service); |
| 393 | } |
| 394 | DedicatedWorkerObserver(const DedicatedWorkerObserver&) = delete; |
| 395 | DedicatedWorkerObserver& operator=(const DedicatedWorkerObserver&) = delete; |
| 396 | ~DedicatedWorkerObserver() override = default; |
| 397 | |
| 398 | void WaitForCreated() { |
| 399 | if (!is_created_) { |
| 400 | run_loop_.emplace(); |
| 401 | run_loop_->Run(); |
| 402 | run_loop_.reset(); |
| 403 | } |
| 404 | } |
| 405 | void WaitForDestroyed() { |
| 406 | if (!is_destroyed_) { |
| 407 | run_loop_.emplace(); |
| 408 | run_loop_->Run(); |
| 409 | run_loop_.reset(); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // DedicatedWorkerService::Observer: |
| 414 | void OnBeforeWorkerDestroyed(const blink::DedicatedWorkerToken& worker_token, |
| 415 | DedicatedWorkerCreator creator) override { |
| 416 | is_destroyed_ = true; |
| 417 | if (run_loop_.has_value()) { |
| 418 | run_loop_->Quit(); |
| 419 | } |
| 420 | } |
| 421 | void OnWorkerCreated(const blink::DedicatedWorkerToken& worker_token, |
| 422 | int worker_process_id, |
| 423 | const url::Origin& security_origin, |
| 424 | DedicatedWorkerCreator creator) override { |
| 425 | is_created_ = true; |
| 426 | if (run_loop_.has_value()) { |
| 427 | run_loop_->Quit(); |
| 428 | } |
| 429 | } |
| 430 | void OnFinalResponseURLDetermined( |
| 431 | const blink::DedicatedWorkerToken& worker_token, |
| 432 | const GURL& url) override {} |
| 433 | |
| 434 | private: |
| 435 | bool is_created_ = false; |
| 436 | bool is_destroyed_ = false; |
| 437 | std::optional<base::RunLoop> run_loop_; |
| 438 | base::ScopedObservation<DedicatedWorkerService, |
| 439 | DedicatedWorkerService::Observer> |
| 440 | scoped_context_observation_{this}; |
| 441 | }; |
| 442 | |
| 443 | class DedicatedWorkerFrameTreeBrowserTest : public FrameTreeBrowserTest { |
| 444 | public: |
| 445 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 446 | FrameTreeBrowserTest::SetUpCommandLine(command_line); |
| 447 | command_line->AppendSwitchASCII(blink::switches::kJavaScriptFlags, |
| 448 | "--expose-gc"); |
| 449 | } |
| 450 | }; |
| 451 | |
| 452 | IN_PROC_BROWSER_TEST_F(DedicatedWorkerFrameTreeBrowserTest, |
| 453 | DiscardClearsDedicatedWorkers) { |
| 454 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 455 | FrameTree& frame_tree = wc->GetPrimaryFrameTree(); |
| 456 | FrameTreeNode* root = frame_tree.root(); |
| 457 | |
| 458 | EXPECT_TRUE( |
| 459 | NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"))); |
| 460 | |
| 461 | // Navigate to a page and register a dedicated worker. |
| 462 | DedicatedWorkerObserver worker_observer(root->current_frame_host() |
| 463 | ->GetStoragePartition() |
| 464 | ->GetDedicatedWorkerService()); |
| 465 | EXPECT_TRUE(EvalJs(shell(), "const worker = new Worker('/workers/empty.js');") |
| 466 | .error.empty()); |
| 467 | worker_observer.WaitForCreated(); |
| 468 | |
| 469 | // Discard the rfh, the associated worker should be cleared. |
| 470 | EXPECT_FALSE(root->was_discarded()); |
| 471 | EXPECT_FALSE(wc->GetController().NeedsReload()); |
| 472 | frame_tree.Discard(); |
| 473 | EXPECT_TRUE(root->was_discarded()); |
| 474 | EXPECT_TRUE(wc->GetController().NeedsReload()); |
| 475 | // Trigger GC to cleanup the worker in the renderer. |
| 476 | EXPECT_TRUE(EvalJs(shell(), "window.gc();").error.empty()); |
| 477 | worker_observer.WaitForDestroyed(); |
| 478 | } |
| 479 | |
| 480 | // TODO(347770670): Consider restricting script access to discarded documents |
| 481 | // from related documents. |
| 482 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, DiscardedFrameAllowsScriptAccess) { |
| 483 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 484 | FrameTree& frame_tree = wc->GetPrimaryFrameTree(); |
| 485 | |
| 486 | const GURL main_url(embedded_test_server()->GetURL("/title1.html")); |
| 487 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 488 | |
| 489 | // Open a popup using window.open. |
| 490 | const GURL popup_url(embedded_test_server()->GetURL("/title2.html")); |
| 491 | Shell* new_shell = OpenPopup(shell(), popup_url, "foo"); |
| 492 | EXPECT_TRUE(new_shell); |
| 493 | |
| 494 | // Assert the opened window is able to script its opener. |
| 495 | EXPECT_EQ("foo", EvalJs(new_shell, "window.name;")); |
| 496 | EXPECT_TRUE(EvalJs(new_shell, "window.opener.name = 'bar';").error.empty()); |
| 497 | EXPECT_EQ("bar", EvalJs(shell(), "window.name;")); |
| 498 | |
| 499 | frame_tree.Discard(); |
| 500 | EXPECT_TRUE(frame_tree.root()->was_discarded()); |
| 501 | |
| 502 | // After a discard operation the opened window should should still be able to |
| 503 | // script its opener. |
| 504 | EXPECT_TRUE(EvalJs(new_shell, "window.opener.name = 'bar2';").error.empty()); |
| 505 | EXPECT_EQ("bar2", EvalJs(shell(), "window.name;")); |
| 506 | |
| 507 | // After a reload the opened window should still be able to script its opener. |
| 508 | wc->GetController().LoadIfNecessary(); |
| 509 | EXPECT_TRUE(WaitForLoadStop(wc)); |
| 510 | EXPECT_TRUE(EvalJs(new_shell, "window.opener.name = 'bar3';").error.empty()); |
| 511 | EXPECT_EQ("bar3", EvalJs(shell(), "window.name;")); |
| 512 | } |
| 513 | |
| 514 | class FrameTreeBrowserTestWithBFCache : public FrameTreeBrowserTest { |
| 515 | public: |
| 516 | FrameTreeBrowserTestWithBFCache() { |
| 517 | feature_list_.InitWithFeaturesAndParameters( |
| 518 | {{features::kBackForwardCache, {}}, |
| 519 | {kBackForwardCacheNoTimeEviction, {}}}, |
| 520 | // Allow BackForwardCache for all devices regardless of their memory. |
| 521 | {features::kBackForwardCacheMemoryControls}); |
| 522 | EXPECT_TRUE(IsBackForwardCacheEnabled()); |
| 523 | } |
| 524 | |
| 525 | private: |
| 526 | base::test::ScopedFeatureList feature_list_; |
| 527 | }; |
| 528 | |
| 529 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTestWithBFCache, DiscardClearsBFCache) { |
| 530 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 531 | FrameTree& frame_tree = wc->GetPrimaryFrameTree(); |
| 532 | BackForwardCacheImpl& back_forward_cache = |
| 533 | wc->GetController().GetBackForwardCache(); |
| 534 | |
| 535 | // The BFCache should start empty. |
| 536 | EXPECT_TRUE(back_forward_cache.GetEntries().empty()); |
| 537 | |
| 538 | const GURL url1(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 539 | const GURL url2(embedded_test_server()->GetURL("b.com", "/title2.html")); |
| 540 | |
| 541 | // Navigate to url1. |
| 542 | EXPECT_TRUE(NavigateToURL(shell(), url1)); |
| 543 | RenderFrameHostImplWrapper rfh_a(wc->GetPrimaryMainFrame()); |
| 544 | RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a.get()); |
| 545 | |
| 546 | // Navigate to url2, the frame hosting url1 should be moved to the BFCache. |
| 547 | EXPECT_TRUE(NavigateToURL(shell(), url2)); |
| 548 | EXPECT_FALSE(delete_observer_rfh_a.deleted()); |
| 549 | EXPECT_TRUE(rfh_a->IsInBackForwardCache()); |
| 550 | EXPECT_EQ(1u, back_forward_cache.GetEntries().size()); |
| 551 | |
| 552 | // Discard the frame tree, the BFCache should have been cleared. |
| 553 | frame_tree.Discard(); |
| 554 | EXPECT_TRUE(frame_tree.root()->was_discarded()); |
| 555 | back_forward_cache.PostTaskToDestroyEvictedFrames(); |
| 556 | ASSERT_TRUE(base::test::RunUntil( |
| 557 | [&]() { return back_forward_cache.GetEntries().empty(); })); |
| 558 | EXPECT_TRUE(delete_observer_rfh_a.deleted()); |
| 559 | } |
| 560 | |
| 561 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTestWithBFCache, |
| 562 | DiscardedFrameDoesNotEnterBFCache) { |
| 563 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 564 | FrameTree& frame_tree = wc->GetPrimaryFrameTree(); |
| 565 | BackForwardCacheImpl& back_forward_cache = |
| 566 | wc->GetController().GetBackForwardCache(); |
| 567 | |
| 568 | // The BFCache should start empty. |
| 569 | EXPECT_TRUE(back_forward_cache.GetEntries().empty()); |
| 570 | |
| 571 | const GURL url1(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 572 | const GURL url2(embedded_test_server()->GetURL("b.com", "/title2.html")); |
| 573 | |
| 574 | // Navigate to url1. |
| 575 | EXPECT_TRUE(NavigateToURL(shell(), url1)); |
| 576 | RenderFrameHostImplWrapper rfh_a(wc->GetPrimaryMainFrame()); |
| 577 | RenderFrameDeletedObserver delete_observer_rfh_a(rfh_a.get()); |
| 578 | |
| 579 | // Discard the frame tree. |
| 580 | frame_tree.Discard(); |
| 581 | EXPECT_TRUE(frame_tree.root()->was_discarded()); |
| 582 | EXPECT_FALSE(delete_observer_rfh_a.deleted()); |
| 583 | |
| 584 | // Navigate to url2, the frame hosting url1 should not be moved to the |
| 585 | // BFCache. |
| 586 | EXPECT_TRUE(NavigateToURL(shell(), url2)); |
| 587 | EXPECT_FALSE(frame_tree.root()->was_discarded()); |
| 588 | EXPECT_TRUE(back_forward_cache.GetEntries().empty()); |
| 589 | ASSERT_TRUE( |
| 590 | base::test::RunUntil([&]() { return delete_observer_rfh_a.deleted(); })); |
| 591 | } |
| 592 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 593 | // Test that we can navigate away if the previous renderer doesn't clean up its |
| 594 | // child frames. |
Nasko Oskov | 31c45ff | 2019-10-16 01:15:19 | [diff] [blame] | 595 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, NavigateWithLeftoverFrames) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 596 | GURL base_url = embedded_test_server()->GetURL("A.com", "/site_isolation/"); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 597 | |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 598 | EXPECT_TRUE(NavigateToURL( |
| 599 | shell(), embedded_test_server()->GetURL("/frame_tree/top.html"))); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 600 | |
| 601 | // Hang the renderer so that it doesn't send any FrameDetached messages. |
| 602 | // (This navigation will never complete, so don't wait for it.) |
Gyuyoung Kim | 107c2a0 | 2021-04-13 01:49:30 | [diff] [blame] | 603 | shell()->LoadURL(GURL(blink::kChromeUIHangURL)); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 604 | |
| 605 | // Check that the frame tree still has children. |
| 606 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 607 | FrameTreeNode* root = wc->GetPrimaryFrameTree().root(); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 608 | ASSERT_EQ(3UL, root->child_count()); |
| 609 | |
| 610 | // Navigate to a new URL. We use LoadURL because NavigateToURL will try to |
| 611 | // wait for the previous navigation to stop. |
| 612 | TestNavigationObserver tab_observer(wc, 1); |
| 613 | shell()->LoadURL(base_url.Resolve("blank.html")); |
| 614 | tab_observer.Wait(); |
| 615 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 616 | // The frame tree should now be cleared. |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 617 | EXPECT_EQ(0UL, root->child_count()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 618 | } |
| 619 | |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 620 | // Ensure that IsRenderFrameLive is true for main frames and same-site iframes. |
| 621 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, IsRenderFrameLive) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 622 | GURL main_url(embedded_test_server()->GetURL("/frame_tree/top.html")); |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 623 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 624 | |
| 625 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 626 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 627 | ->GetPrimaryFrameTree() |
| 628 | .root(); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 629 | |
| 630 | // The root and subframe should each have a live RenderFrame. |
| 631 | EXPECT_TRUE( |
| 632 | root->current_frame_host()->render_view_host()->IsRenderViewLive()); |
| 633 | EXPECT_TRUE(root->current_frame_host()->IsRenderFrameLive()); |
| 634 | EXPECT_TRUE(root->child_at(0)->current_frame_host()->IsRenderFrameLive()); |
| 635 | |
| 636 | // Load a same-site page into iframe and it should still be live. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 637 | GURL http_url(embedded_test_server()->GetURL("/title1.html")); |
Lukasz Anforowicz | 69c25dfd | 2020-11-12 21:50:20 | [diff] [blame] | 638 | EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), http_url)); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 639 | EXPECT_TRUE( |
| 640 | root->current_frame_host()->render_view_host()->IsRenderViewLive()); |
| 641 | EXPECT_TRUE(root->current_frame_host()->IsRenderFrameLive()); |
| 642 | EXPECT_TRUE(root->child_at(0)->current_frame_host()->IsRenderFrameLive()); |
| 643 | } |
| 644 | |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 645 | // Ensure that origins are correctly set on navigations. |
| 646 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, OriginSetOnNavigation) { |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 647 | GURL about_blank(url::kAboutBlankURL); |
| 648 | GURL main_url( |
| 649 | embedded_test_server()->GetURL("a.com", "/frame_tree/top.html")); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 650 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 651 | WebContents* contents = shell()->web_contents(); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 652 | |
| 653 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 654 | FrameTreeNode* root = |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 655 | static_cast<WebContentsImpl*>(contents)->GetPrimaryFrameTree().root(); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 656 | |
| 657 | // Extra '/' is added because the replicated origin is serialized in RFC 6454 |
| 658 | // format, which dictates no trailing '/', whereas GURL::GetOrigin does put a |
| 659 | // '/' at the end. |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 660 | EXPECT_EQ(main_url.DeprecatedGetOriginAsURL().spec(), |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 661 | root->current_origin().Serialize() + '/'); |
| 662 | EXPECT_EQ( |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 663 | main_url.DeprecatedGetOriginAsURL().spec(), |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 664 | root->current_frame_host()->GetLastCommittedOrigin().Serialize() + '/'); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 665 | |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 666 | // The iframe is inititially same-origin. |
| 667 | EXPECT_TRUE( |
| 668 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
| 669 | root->child_at(0)->current_frame_host()->GetLastCommittedOrigin())); |
| 670 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
| 671 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize(), |
| 672 | GetOriginFromRenderer(root->child_at(0))); |
| 673 | |
| 674 | // Navigate the iframe cross-origin. |
| 675 | GURL frame_url(embedded_test_server()->GetURL("b.com", "/title1.html")); |
Lukasz Anforowicz | 69c25dfd | 2020-11-12 21:50:20 | [diff] [blame] | 676 | EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), frame_url)); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 677 | EXPECT_EQ(frame_url, root->child_at(0)->current_url()); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 678 | EXPECT_EQ(frame_url.DeprecatedGetOriginAsURL().spec(), |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 679 | root->child_at(0)->current_origin().Serialize() + '/'); |
| 680 | EXPECT_FALSE( |
| 681 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
| 682 | root->child_at(0)->current_frame_host()->GetLastCommittedOrigin())); |
| 683 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
| 684 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize(), |
| 685 | GetOriginFromRenderer(root->child_at(0))); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 686 | |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 687 | // Parent-initiated about:blank navigation should inherit the parent's a.com |
| 688 | // origin. |
| 689 | NavigateIframeToURL(contents, "1-1-id", about_blank); |
| 690 | EXPECT_EQ(about_blank, root->child_at(0)->current_url()); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 691 | EXPECT_EQ(main_url.DeprecatedGetOriginAsURL().spec(), |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 692 | root->child_at(0)->current_origin().Serialize() + '/'); |
| 693 | EXPECT_EQ(root->current_frame_host()->GetLastCommittedOrigin().Serialize(), |
| 694 | root->child_at(0) |
| 695 | ->current_frame_host() |
| 696 | ->GetLastCommittedOrigin() |
| 697 | .Serialize()); |
| 698 | EXPECT_TRUE( |
| 699 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
| 700 | root->child_at(0)->current_frame_host()->GetLastCommittedOrigin())); |
| 701 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
| 702 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize(), |
| 703 | GetOriginFromRenderer(root->child_at(0))); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 704 | |
| 705 | GURL data_url("data:text/html,foo"); |
| 706 | EXPECT_TRUE(NavigateToURL(shell(), data_url)); |
| 707 | |
| 708 | // Navigating to a data URL should set a unique origin. This is represented |
| 709 | // as "null" per RFC 6454. |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 710 | EXPECT_EQ("null", root->current_origin().Serialize()); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 711 | EXPECT_TRUE( |
| 712 | contents->GetPrimaryMainFrame()->GetLastCommittedOrigin().opaque()); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 713 | EXPECT_EQ("null", GetOriginFromRenderer(root)); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 714 | |
| 715 | // Re-navigating to a normal URL should update the origin. |
| 716 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 717 | EXPECT_EQ(main_url.DeprecatedGetOriginAsURL().spec(), |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 718 | root->current_origin().Serialize() + '/'); |
| 719 | EXPECT_EQ( |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 720 | main_url.DeprecatedGetOriginAsURL().spec(), |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 721 | contents->GetPrimaryMainFrame()->GetLastCommittedOrigin().Serialize() + |
| 722 | '/'); |
| 723 | EXPECT_FALSE( |
| 724 | contents->GetPrimaryMainFrame()->GetLastCommittedOrigin().opaque()); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 725 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 726 | } |
| 727 | |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 728 | // Tests a cross-origin navigation to a blob URL. The main frame initiates this |
| 729 | // navigation on its grandchild. It should wind up in the main frame's process. |
| 730 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, NavigateGrandchildToBlob) { |
| 731 | WebContents* contents = shell()->web_contents(); |
| 732 | FrameTreeNode* root = |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 733 | static_cast<WebContentsImpl*>(contents)->GetPrimaryFrameTree().root(); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 734 | |
| 735 | // First, snapshot the FrameTree for a normal A(B(A)) case where all frames |
| 736 | // are served over http. The blob test should result in the same structure. |
| 737 | EXPECT_TRUE(NavigateToURL( |
| 738 | shell(), embedded_test_server()->GetURL( |
| 739 | "a.com", "/cross_site_iframe_factory.html?a(b(a))"))); |
Fergal Daly | 79f4429 | 2020-12-01 02:30:48 | [diff] [blame] | 740 | std::string reference_tree = DepictFrameTree(*root); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 741 | |
| 742 | GURL main_url(embedded_test_server()->GetURL( |
| 743 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 744 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 745 | |
| 746 | // The root node will initiate the navigation; its grandchild node will be the |
| 747 | // target of the navigation. |
| 748 | FrameTreeNode* target = root->child_at(0)->child_at(0); |
| 749 | |
creis | b2d5f1f3 | 2016-11-07 23:25:05 | [diff] [blame] | 750 | RenderFrameDeletedObserver deleted_observer(target->current_frame_host()); |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 751 | std::string html = |
| 752 | "<html><body><div>This is blob content.</div>" |
| 753 | "<script>" |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 754 | "window.parent.parent.postMessage('HI', self.origin);" |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 755 | "</script></body></html>"; |
| 756 | std::string script = JsReplace( |
| 757 | "new Promise((resolve) => {" |
| 758 | " window.addEventListener('message', resolve, false);" |
| 759 | " var blob = new Blob([$1], {type: 'text/html'});" |
| 760 | " var blob_url = URL.createObjectURL(blob);" |
| 761 | " frames[0][0].location.href = blob_url;" |
| 762 | "}).then((event) => {" |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 763 | " document.body.appendChild(document.createTextNode(event.data));" |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 764 | " return event.source.location.href;" |
| 765 | "});", |
| 766 | html); |
| 767 | std::string blob_url_string = EvalJs(root, script).ExtractString(); |
creis | b2d5f1f3 | 2016-11-07 23:25:05 | [diff] [blame] | 768 | // Wait for the RenderFrame to go away, if this will be cross-process. |
| 769 | if (AreAllSitesIsolatedForTesting()) |
| 770 | deleted_observer.WaitUntilDeleted(); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 771 | EXPECT_EQ(GURL(blob_url_string), target->current_url()); |
| 772 | EXPECT_EQ(url::kBlobScheme, target->current_url().scheme()); |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 773 | EXPECT_FALSE(target->current_origin().opaque()); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 774 | EXPECT_EQ("a.com", target->current_origin().host()); |
| 775 | EXPECT_EQ(url::kHttpScheme, target->current_origin().scheme()); |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 776 | EXPECT_EQ("This is blob content.", |
| 777 | EvalJs(target, "document.body.children[0].innerHTML")); |
Fergal Daly | 79f4429 | 2020-12-01 02:30:48 | [diff] [blame] | 778 | EXPECT_EQ(reference_tree, DepictFrameTree(*root)); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, NavigateChildToAboutBlank) { |
| 782 | GURL main_url(embedded_test_server()->GetURL( |
| 783 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 784 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 785 | WebContentsImpl* contents = |
| 786 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 787 | |
| 788 | // The leaf node (c.com) will be navigated. Its parent node (b.com) will |
| 789 | // initiate the navigation. |
| 790 | FrameTreeNode* target = |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 791 | contents->GetPrimaryFrameTree().root()->child_at(0)->child_at(0); |
Harkiran Bolaria | 4eaae307 | 2021-09-21 10:45:48 | [diff] [blame] | 792 | RenderFrameHost* initiator_rfh = target->parent(); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 793 | |
| 794 | // Give the target a name. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 795 | EXPECT_TRUE(ExecJs(target, "window.name = 'target';")); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 796 | |
| 797 | // Use window.open(about:blank), then poll the document for access. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 798 | EvalJsResult about_blank_origin = EvalJs( |
Harkiran Bolaria | 4eaae307 | 2021-09-21 10:45:48 | [diff] [blame] | 799 | initiator_rfh, |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 800 | "new Promise(resolve => {" |
| 801 | " var didNavigate = false;" |
| 802 | " var intervalID = setInterval(function() {" |
| 803 | " if (!didNavigate) {" |
| 804 | " didNavigate = true;" |
| 805 | " window.open('about:blank', 'target');" |
| 806 | " }" |
| 807 | " // Poll the document until it doesn't throw a SecurityError.\n" |
| 808 | " try {" |
| 809 | " frames[0].document.write('Hi from ' + document.domain);" |
| 810 | " } catch (e) { return; }" |
| 811 | " clearInterval(intervalID);" |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 812 | " resolve(frames[0].self.origin);" |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 813 | " }, 16);" |
| 814 | "});"); |
| 815 | EXPECT_EQ(target->current_origin(), about_blank_origin); |
Nate Chapin | ab67b04 | 2021-05-04 21:00:51 | [diff] [blame] | 816 | EXPECT_EQ(GURL(url::kAboutBlankURL), target->current_url()); |
| 817 | EXPECT_EQ(url::kAboutScheme, target->current_url().scheme()); |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 818 | EXPECT_FALSE(target->current_origin().opaque()); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 819 | EXPECT_EQ("b.com", target->current_origin().host()); |
| 820 | EXPECT_EQ(url::kHttpScheme, target->current_origin().scheme()); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 821 | |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 822 | EXPECT_EQ("Hi from b.com", EvalJs(target, "document.body.innerHTML")); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | // Nested iframes, three origins: A(B(C)). Frame A navigates C to about:blank |
| 826 | // (via window.open). This should wind up in A's origin per the spec. Test fails |
| 827 | // because of http://crbug.com/564292 |
| 828 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, |
| 829 | DISABLED_NavigateGrandchildToAboutBlank) { |
| 830 | GURL main_url(embedded_test_server()->GetURL( |
| 831 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 832 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 833 | WebContentsImpl* contents = |
| 834 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 835 | |
| 836 | // The leaf node (c.com) will be navigated. Its grandparent node (a.com) will |
| 837 | // initiate the navigation. |
| 838 | FrameTreeNode* target = |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 839 | contents->GetPrimaryFrameTree().root()->child_at(0)->child_at(0); |
Harkiran Bolaria | 4eaae307 | 2021-09-21 10:45:48 | [diff] [blame] | 840 | RenderFrameHost* initiator_rfh = target->parent()->GetParent(); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 841 | |
| 842 | // Give the target a name. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 843 | EXPECT_TRUE(ExecJs(target, "window.name = 'target';")); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 844 | |
| 845 | // Use window.open(about:blank), then poll the document for access. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 846 | EvalJsResult about_blank_origin = |
Harkiran Bolaria | 4eaae307 | 2021-09-21 10:45:48 | [diff] [blame] | 847 | EvalJs(initiator_rfh, |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 848 | "new Promise((resolve) => {" |
| 849 | " var didNavigate = false;" |
| 850 | " var intervalID = setInterval(() => {" |
| 851 | " if (!didNavigate) {" |
| 852 | " didNavigate = true;" |
| 853 | " window.open('about:blank', 'target');" |
| 854 | " }" |
| 855 | " // May raise a SecurityError, that's expected.\n" |
| 856 | " try {" |
| 857 | " frames[0][0].document.write('Hi from ' + document.domain);" |
| 858 | " } catch (e) { return; }" |
| 859 | " clearInterval(intervalID);" |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 860 | " resolve(frames[0][0].self.origin);" |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 861 | " }, 16);" |
| 862 | "});"); |
| 863 | EXPECT_EQ(target->current_origin(), about_blank_origin); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 864 | EXPECT_EQ(GURL(url::kAboutBlankURL), target->current_url()); |
| 865 | EXPECT_EQ(url::kAboutScheme, target->current_url().scheme()); |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 866 | EXPECT_FALSE(target->current_origin().opaque()); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 867 | EXPECT_EQ("a.com", target->current_origin().host()); |
| 868 | EXPECT_EQ(url::kHttpScheme, target->current_origin().scheme()); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 869 | |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 870 | EXPECT_EQ("Hi from a.com", EvalJs(target, "document.body.innerHTML")); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 871 | } |
| 872 | |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 873 | // Tests a cross-origin navigation to a data: URL. The main frame initiates this |
| 874 | // navigation on its grandchild. It should wind up in the main frame's process |
| 875 | // and have precursor origin of the main frame origin. |
| 876 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, NavigateGrandchildToDataUrl) { |
| 877 | GURL main_url(embedded_test_server()->GetURL( |
| 878 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 879 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 880 | WebContentsImpl* contents = |
| 881 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 882 | |
| 883 | // The leaf node (c.com) will be navigated. Its grandparent node (a.com) will |
| 884 | // initiate the navigation. |
| 885 | FrameTreeNode* target = |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 886 | contents->GetPrimaryFrameTree().root()->child_at(0)->child_at(0); |
Harkiran Bolaria | 4eaae307 | 2021-09-21 10:45:48 | [diff] [blame] | 887 | RenderFrameHostImpl* initiator_rfh = target->parent()->GetParent(); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 888 | |
| 889 | // Give the target a name. |
| 890 | EXPECT_TRUE(ExecJs(target, "window.name = 'target';")); |
| 891 | |
| 892 | // Navigate the target frame through the initiator frame. |
| 893 | { |
| 894 | TestFrameNavigationObserver observer(target); |
Harkiran Bolaria | 4eaae307 | 2021-09-21 10:45:48 | [diff] [blame] | 895 | EXPECT_TRUE(ExecJs(initiator_rfh, |
| 896 | "window.open('data:text/html,content', 'target');")); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 897 | observer.Wait(); |
| 898 | } |
| 899 | |
| 900 | url::Origin original_target_origin = |
| 901 | target->current_frame_host()->GetLastCommittedOrigin(); |
| 902 | EXPECT_TRUE(original_target_origin.opaque()); |
| 903 | EXPECT_EQ(original_target_origin.GetTupleOrPrecursorTupleIfOpaque(), |
| 904 | url::SchemeHostPort(main_url)); |
| 905 | |
| 906 | // Navigate the grandchild frame again cross-process to foo.com, then |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 907 | // go back in session history. The frame should commit a new opaque origin, |
| 908 | // but it will still have the same precursor origin (the main frame origin). |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 909 | { |
| 910 | TestFrameNavigationObserver observer(target); |
| 911 | EXPECT_TRUE(ExecJs(target, JsReplace("window.location = $1", |
| 912 | embedded_test_server()->GetURL( |
| 913 | "foo.com", "/title2.html")))); |
| 914 | observer.Wait(); |
| 915 | } |
| 916 | EXPECT_NE(original_target_origin, |
| 917 | target->current_frame_host()->GetLastCommittedOrigin()); |
| 918 | { |
| 919 | TestFrameNavigationObserver observer(target); |
| 920 | contents->GetController().GoBack(); |
| 921 | observer.Wait(); |
| 922 | } |
| 923 | |
| 924 | url::Origin target_origin = |
| 925 | target->current_frame_host()->GetLastCommittedOrigin(); |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 926 | EXPECT_NE(target_origin, original_target_origin); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 927 | EXPECT_TRUE(target_origin.opaque()); |
| 928 | EXPECT_EQ(target_origin.GetTupleOrPrecursorTupleIfOpaque(), |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 929 | original_target_origin.GetTupleOrPrecursorTupleIfOpaque()); |
| 930 | EXPECT_EQ(target_origin.GetTupleOrPrecursorTupleIfOpaque(), |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 931 | url::SchemeHostPort(main_url)); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 932 | } |
| 933 | |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 934 | // Ensures that iframe with srcdoc is always put in the same origin as its |
| 935 | // parent frame. |
| 936 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, ChildFrameWithSrcdoc) { |
| 937 | GURL main_url(embedded_test_server()->GetURL( |
| 938 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 939 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 940 | WebContentsImpl* contents = |
| 941 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 942 | FrameTreeNode* root = contents->GetPrimaryFrameTree().root(); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 943 | EXPECT_EQ(1U, root->child_count()); |
| 944 | |
| 945 | FrameTreeNode* child = root->child_at(0); |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 946 | std::string frame_origin = EvalJs(child, "self.origin;").ExtractString(); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 947 | EXPECT_TRUE( |
| 948 | child->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 949 | url::Origin::Create(GURL(frame_origin)))); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 950 | EXPECT_FALSE( |
| 951 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 952 | url::Origin::Create(GURL(frame_origin)))); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 953 | |
| 954 | // Create a new iframe with srcdoc and add it to the main frame. It should |
| 955 | // be created in the same SiteInstance as the parent. |
| 956 | { |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 957 | std::string script( |
| 958 | "var f = document.createElement('iframe');" |
| 959 | "f.srcdoc = 'some content';" |
| 960 | "document.body.appendChild(f)"); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 961 | TestNavigationObserver observer(shell()->web_contents()); |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 962 | EXPECT_TRUE(ExecJs(root, script)); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 963 | EXPECT_EQ(2U, root->child_count()); |
| 964 | observer.Wait(); |
| 965 | |
Lukasz Anforowicz | 42d3d07f | 2019-06-19 01:06:42 | [diff] [blame] | 966 | EXPECT_TRUE(root->child_at(1)->current_url().IsAboutSrcdoc()); |
Peter Kasting | eb8c3ce | 2021-08-20 04:39:35 | [diff] [blame] | 967 | EvalJsResult js_result = EvalJs(root->child_at(1), "self.origin"); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 968 | EXPECT_EQ(root->current_frame_host() |
| 969 | ->GetLastCommittedURL() |
| 970 | .DeprecatedGetOriginAsURL(), |
Peter Kasting | eb8c3ce | 2021-08-20 04:39:35 | [diff] [blame] | 971 | GURL(js_result.ExtractString())); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 972 | EXPECT_NE(child->current_frame_host() |
| 973 | ->GetLastCommittedURL() |
| 974 | .DeprecatedGetOriginAsURL(), |
Peter Kasting | eb8c3ce | 2021-08-20 04:39:35 | [diff] [blame] | 975 | GURL(js_result.ExtractString())); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | // Set srcdoc on the existing cross-site frame. It should navigate the frame |
| 979 | // back to the origin of the parent. |
| 980 | { |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 981 | std::string script( |
| 982 | "var f = document.getElementById('child-0');" |
| 983 | "f.srcdoc = 'some content';"); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 984 | TestNavigationObserver observer(shell()->web_contents()); |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 985 | EXPECT_TRUE(ExecJs(root, script)); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 986 | observer.Wait(); |
| 987 | |
Lukasz Anforowicz | 42d3d07f | 2019-06-19 01:06:42 | [diff] [blame] | 988 | EXPECT_TRUE(child->current_url().IsAboutSrcdoc()); |
Avi Drissman | a3144c58 | 2021-10-14 17:00:26 | [diff] [blame] | 989 | EXPECT_EQ(root->current_frame_host()->GetLastCommittedOrigin().Serialize(), |
| 990 | EvalJs(child, "self.origin")); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 994 | // Ensure that sandbox flags are correctly set in the main frame when set by |
| 995 | // Content-Security-Policy header. |
| 996 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, SandboxFlagsSetForMainFrame) { |
| 997 | GURL main_url(embedded_test_server()->GetURL("/csp_sandboxed_frame.html")); |
| 998 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 999 | |
| 1000 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 1001 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1002 | ->GetPrimaryFrameTree() |
| 1003 | .root(); |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1004 | |
| 1005 | // Verify that sandbox flags are set properly for the root FrameTreeNode and |
| 1006 | // RenderFrameHost. Root frame is sandboxed with "allow-scripts". |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1007 | EXPECT_EQ(network::mojom::WebSandboxFlags::kNone, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1008 | root->effective_frame_policy().sandbox_flags); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1009 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll & |
| 1010 | ~network::mojom::WebSandboxFlags::kScripts & |
| 1011 | ~network::mojom::WebSandboxFlags::kAutomaticFeatures, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1012 | root->active_sandbox_flags()); |
| 1013 | EXPECT_EQ(root->active_sandbox_flags(), |
| 1014 | root->current_frame_host()->active_sandbox_flags()); |
| 1015 | |
| 1016 | // Verify that child frames inherit sandbox flags from the root. First frame |
| 1017 | // has no explicitly set flags of its own, and should inherit those from the |
| 1018 | // root. Second frame is completely sandboxed. |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1019 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll & |
| 1020 | ~network::mojom::WebSandboxFlags::kScripts & |
| 1021 | ~network::mojom::WebSandboxFlags::kAutomaticFeatures, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1022 | root->child_at(0)->effective_frame_policy().sandbox_flags); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1023 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll & |
| 1024 | ~network::mojom::WebSandboxFlags::kScripts & |
| 1025 | ~network::mojom::WebSandboxFlags::kAutomaticFeatures, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1026 | root->child_at(0)->active_sandbox_flags()); |
| 1027 | EXPECT_EQ(root->child_at(0)->active_sandbox_flags(), |
| 1028 | root->child_at(0)->current_frame_host()->active_sandbox_flags()); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1029 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1030 | root->child_at(1)->effective_frame_policy().sandbox_flags); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1031 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1032 | root->child_at(1)->active_sandbox_flags()); |
| 1033 | EXPECT_EQ(root->child_at(1)->active_sandbox_flags(), |
| 1034 | root->child_at(1)->current_frame_host()->active_sandbox_flags()); |
| 1035 | |
| 1036 | // Navigating the main frame to a different URL should clear sandbox flags. |
| 1037 | GURL unsandboxed_url(embedded_test_server()->GetURL("/title1.html")); |
Lukasz Anforowicz | 69c25dfd | 2020-11-12 21:50:20 | [diff] [blame] | 1038 | EXPECT_TRUE(NavigateToURLFromRenderer(root, unsandboxed_url)); |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1039 | |
| 1040 | // Verify that sandbox flags are cleared properly for the root FrameTreeNode |
| 1041 | // and RenderFrameHost. |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1042 | EXPECT_EQ(network::mojom::WebSandboxFlags::kNone, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1043 | root->effective_frame_policy().sandbox_flags); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1044 | EXPECT_EQ(network::mojom::WebSandboxFlags::kNone, |
| 1045 | root->active_sandbox_flags()); |
| 1046 | EXPECT_EQ(network::mojom::WebSandboxFlags::kNone, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1047 | root->current_frame_host()->active_sandbox_flags()); |
| 1048 | } |
| 1049 | |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 1050 | // Ensure that sandbox flags are correctly set when child frames are created. |
| 1051 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, SandboxFlagsSetForChildFrames) { |
| 1052 | GURL main_url(embedded_test_server()->GetURL("/sandboxed_frames.html")); |
| 1053 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1054 | |
| 1055 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 1056 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1057 | ->GetPrimaryFrameTree() |
| 1058 | .root(); |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 1059 | |
| 1060 | // Verify that sandbox flags are set properly for all FrameTreeNodes. |
| 1061 | // First frame is completely sandboxed; second frame uses "allow-scripts", |
| 1062 | // which resets both SandboxFlags::Scripts and |
| 1063 | // SandboxFlags::AutomaticFeatures bits per blink::parseSandboxPolicy(), and |
| 1064 | // third frame has "allow-scripts allow-same-origin". |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1065 | EXPECT_EQ(network::mojom::WebSandboxFlags::kNone, |
Ian Clelland | cdc4f31 | 2017-10-13 22:24:12 | [diff] [blame] | 1066 | root->effective_frame_policy().sandbox_flags); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1067 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll, |
Ian Clelland | cdc4f31 | 2017-10-13 22:24:12 | [diff] [blame] | 1068 | root->child_at(0)->effective_frame_policy().sandbox_flags); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1069 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll & |
| 1070 | ~network::mojom::WebSandboxFlags::kScripts & |
| 1071 | ~network::mojom::WebSandboxFlags::kAutomaticFeatures, |
Ian Clelland | cdc4f31 | 2017-10-13 22:24:12 | [diff] [blame] | 1072 | root->child_at(1)->effective_frame_policy().sandbox_flags); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1073 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll & |
| 1074 | ~network::mojom::WebSandboxFlags::kScripts & |
| 1075 | ~network::mojom::WebSandboxFlags::kAutomaticFeatures & |
| 1076 | ~network::mojom::WebSandboxFlags::kOrigin, |
Ian Clelland | cdc4f31 | 2017-10-13 22:24:12 | [diff] [blame] | 1077 | root->child_at(2)->effective_frame_policy().sandbox_flags); |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 1078 | |
| 1079 | // Sandboxed frames should set a unique origin unless they have the |
| 1080 | // "allow-same-origin" directive. |
alexmos | 6e94010 | 2016-01-19 22:47:25 | [diff] [blame] | 1081 | EXPECT_EQ("null", root->child_at(0)->current_origin().Serialize()); |
| 1082 | EXPECT_EQ("null", root->child_at(1)->current_origin().Serialize()); |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 1083 | EXPECT_EQ(main_url.DeprecatedGetOriginAsURL().spec(), |
alexmos | 6e94010 | 2016-01-19 22:47:25 | [diff] [blame] | 1084 | root->child_at(2)->current_origin().Serialize() + "/"); |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 1085 | |
| 1086 | // Navigating to a different URL should not clear sandbox flags. |
| 1087 | GURL frame_url(embedded_test_server()->GetURL("/title1.html")); |
| 1088 | NavigateFrameToURL(root->child_at(0), frame_url); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1089 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll, |
Ian Clelland | cdc4f31 | 2017-10-13 22:24:12 | [diff] [blame] | 1090 | root->child_at(0)->effective_frame_policy().sandbox_flags); |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 1091 | } |
| 1092 | |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1093 | // Ensure that sandbox flags are correctly set in the child frames when set by |
| 1094 | // Content-Security-Policy header, and in combination with the sandbox iframe |
| 1095 | // attribute. |
| 1096 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, |
| 1097 | SandboxFlagsSetByCSPForChildFrames) { |
| 1098 | GURL main_url(embedded_test_server()->GetURL("/sandboxed_frames_csp.html")); |
| 1099 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1100 | |
| 1101 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 1102 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1103 | ->GetPrimaryFrameTree() |
| 1104 | .root(); |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1105 | |
| 1106 | // Verify that sandbox flags are set properly for all FrameTreeNodes. |
| 1107 | // First frame has no iframe sandbox flags, but the framed document is served |
| 1108 | // with a CSP header which sets "allow-scripts", "allow-popups" and |
| 1109 | // "allow-pointer-lock". |
| 1110 | // Second frame is sandboxed with "allow-scripts", "allow-pointer-lock" and |
| 1111 | // "allow-orientation-lock", and the framed document is also served with a CSP |
| 1112 | // header which uses "allow-popups" and "allow-pointer-lock". The resulting |
| 1113 | // sandbox for the frame should only have "allow-pointer-lock". |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1114 | EXPECT_EQ(network::mojom::WebSandboxFlags::kNone, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1115 | root->effective_frame_policy().sandbox_flags); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1116 | EXPECT_EQ(network::mojom::WebSandboxFlags::kNone, |
| 1117 | root->active_sandbox_flags()); |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1118 | EXPECT_EQ(root->active_sandbox_flags(), |
| 1119 | root->current_frame_host()->active_sandbox_flags()); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1120 | EXPECT_EQ(network::mojom::WebSandboxFlags::kNone, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1121 | root->child_at(0)->effective_frame_policy().sandbox_flags); |
Arthur Sonzogni | 4ba3104f | 2022-03-09 09:04:39 | [diff] [blame] | 1122 | EXPECT_EQ( |
| 1123 | network::mojom::WebSandboxFlags::kAll & |
| 1124 | ~network::mojom::WebSandboxFlags::kAutomaticFeatures & |
| 1125 | ~network::mojom::WebSandboxFlags::kPointerLock & |
| 1126 | ~network::mojom::WebSandboxFlags::kPopups & |
| 1127 | ~network::mojom::WebSandboxFlags::kScripts & |
| 1128 | ~network::mojom::WebSandboxFlags::kTopNavigationToCustomProtocols, |
| 1129 | root->child_at(0)->active_sandbox_flags()); |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1130 | EXPECT_EQ(root->child_at(0)->active_sandbox_flags(), |
| 1131 | root->child_at(0)->current_frame_host()->active_sandbox_flags()); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1132 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll & |
| 1133 | ~network::mojom::WebSandboxFlags::kScripts & |
| 1134 | ~network::mojom::WebSandboxFlags::kAutomaticFeatures & |
| 1135 | ~network::mojom::WebSandboxFlags::kPointerLock & |
| 1136 | ~network::mojom::WebSandboxFlags::kOrientationLock, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1137 | root->child_at(1)->effective_frame_policy().sandbox_flags); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1138 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll & |
| 1139 | ~network::mojom::WebSandboxFlags::kScripts & |
| 1140 | ~network::mojom::WebSandboxFlags::kAutomaticFeatures & |
| 1141 | ~network::mojom::WebSandboxFlags::kPointerLock, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1142 | root->child_at(1)->active_sandbox_flags()); |
| 1143 | EXPECT_EQ(root->child_at(1)->active_sandbox_flags(), |
| 1144 | root->child_at(1)->current_frame_host()->active_sandbox_flags()); |
| 1145 | |
| 1146 | // Navigating to a different URL *should* clear CSP-set sandbox flags, but |
| 1147 | // should retain those flags set by the frame owner. |
| 1148 | GURL frame_url(embedded_test_server()->GetURL("/title1.html")); |
| 1149 | |
Lukasz Anforowicz | 69c25dfd | 2020-11-12 21:50:20 | [diff] [blame] | 1150 | EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), frame_url)); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1151 | EXPECT_EQ(network::mojom::WebSandboxFlags::kNone, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1152 | root->child_at(0)->effective_frame_policy().sandbox_flags); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1153 | EXPECT_EQ(network::mojom::WebSandboxFlags::kNone, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1154 | root->child_at(0)->active_sandbox_flags()); |
| 1155 | EXPECT_EQ(root->child_at(0)->active_sandbox_flags(), |
| 1156 | root->child_at(0)->current_frame_host()->active_sandbox_flags()); |
| 1157 | |
Lukasz Anforowicz | 69c25dfd | 2020-11-12 21:50:20 | [diff] [blame] | 1158 | EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(1), frame_url)); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1159 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll & |
| 1160 | ~network::mojom::WebSandboxFlags::kScripts & |
| 1161 | ~network::mojom::WebSandboxFlags::kAutomaticFeatures & |
| 1162 | ~network::mojom::WebSandboxFlags::kPointerLock & |
| 1163 | ~network::mojom::WebSandboxFlags::kOrientationLock, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1164 | root->child_at(1)->effective_frame_policy().sandbox_flags); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1165 | EXPECT_EQ(network::mojom::WebSandboxFlags::kAll & |
| 1166 | ~network::mojom::WebSandboxFlags::kScripts & |
| 1167 | ~network::mojom::WebSandboxFlags::kAutomaticFeatures & |
| 1168 | ~network::mojom::WebSandboxFlags::kPointerLock & |
| 1169 | ~network::mojom::WebSandboxFlags::kOrientationLock, |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1170 | root->child_at(1)->active_sandbox_flags()); |
| 1171 | EXPECT_EQ(root->child_at(1)->active_sandbox_flags(), |
| 1172 | root->child_at(1)->current_frame_host()->active_sandbox_flags()); |
| 1173 | } |
| 1174 | |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 1175 | // Ensure that a popup opened from a subframe sets its opener to the subframe's |
| 1176 | // FrameTreeNode, and that the opener is cleared if the subframe is destroyed. |
| 1177 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, SubframeOpenerSetForNewWindow) { |
| 1178 | GURL main_url(embedded_test_server()->GetURL("/frame_tree/top.html")); |
| 1179 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1180 | |
| 1181 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 1182 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1183 | ->GetPrimaryFrameTree() |
| 1184 | .root(); |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 1185 | |
| 1186 | // Open a new window from a subframe. |
| 1187 | ShellAddedObserver new_shell_observer; |
| 1188 | GURL popup_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 1189 | EXPECT_TRUE( |
| 1190 | ExecJs(root->child_at(0), JsReplace("window.open($1);", popup_url))); |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 1191 | Shell* new_shell = new_shell_observer.GetShell(); |
| 1192 | WebContents* new_contents = new_shell->web_contents(); |
Fergal Daly | f052233 | 2020-07-18 06:09:46 | [diff] [blame] | 1193 | EXPECT_TRUE(WaitForLoadStop(new_contents)); |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 1194 | |
| 1195 | // Check that the new window's opener points to the correct subframe on |
| 1196 | // original window. |
| 1197 | FrameTreeNode* popup_root = |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1198 | static_cast<WebContentsImpl*>(new_contents)->GetPrimaryFrameTree().root(); |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 1199 | EXPECT_EQ(root->child_at(0), popup_root->opener()); |
| 1200 | |
| 1201 | // Close the original window. This should clear the new window's opener. |
| 1202 | shell()->Close(); |
| 1203 | EXPECT_EQ(nullptr, popup_root->opener()); |
| 1204 | } |
| 1205 | |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1206 | // Tests that the user activation bits get cleared when a same-site document is |
| 1207 | // installed in the frame. |
| 1208 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, |
| 1209 | ClearUserActivationForNewDocument) { |
| 1210 | GURL main_url(embedded_test_server()->GetURL("/frame_tree/top.html")); |
| 1211 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1212 | |
| 1213 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 1214 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1215 | ->GetPrimaryFrameTree() |
| 1216 | .root(); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1217 | |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame] | 1218 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1219 | EXPECT_FALSE(root->HasTransientUserActivation()); |
| 1220 | |
| 1221 | // Set the user activation bits. |
| 1222 | root->UpdateUserActivationState( |
Mustaq Ahmed | dc195e5b | 2020-08-04 18:45:11 | [diff] [blame] | 1223 | blink::mojom::UserActivationUpdateType::kNotifyActivation, |
| 1224 | blink::mojom::UserActivationNotificationType::kTest); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame] | 1225 | EXPECT_TRUE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1226 | EXPECT_TRUE(root->HasTransientUserActivation()); |
| 1227 | |
| 1228 | // Install a new same-site document to check the clearing of user activation |
| 1229 | // bits. |
| 1230 | GURL url(embedded_test_server()->GetURL("/title1.html")); |
| 1231 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
| 1232 | |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame] | 1233 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1234 | EXPECT_FALSE(root->HasTransientUserActivation()); |
| 1235 | } |
| 1236 | |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1237 | class CrossProcessFrameTreeBrowserTest : public ContentBrowserTest { |
| 1238 | public: |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 1239 | CrossProcessFrameTreeBrowserTest() = default; |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1240 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 1241 | CrossProcessFrameTreeBrowserTest(const CrossProcessFrameTreeBrowserTest&) = |
| 1242 | delete; |
| 1243 | CrossProcessFrameTreeBrowserTest& operator=( |
| 1244 | const CrossProcessFrameTreeBrowserTest&) = delete; |
| 1245 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 1246 | void SetUpCommandLine(base::CommandLine* command_line) override { |
nick | d30fd96 | 2015-07-27 21:51:08 | [diff] [blame] | 1247 | IsolateAllSitesForTesting(command_line); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1248 | } |
| 1249 | |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 1250 | void SetUpOnMainThread() override { |
| 1251 | host_resolver()->AddRule("*", "127.0.0.1"); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 1252 | SetupCrossSiteRedirector(embedded_test_server()); |
martijn | 12d2dad | 2016-11-11 10:59:27 | [diff] [blame] | 1253 | ASSERT_TRUE(embedded_test_server()->Start()); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 1254 | } |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1255 | }; |
| 1256 | |
| 1257 | // Ensure that we can complete a cross-process subframe navigation. |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1258 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
nasko | 83fe5dc | 2015-07-01 16:34:19 | [diff] [blame] | 1259 | CreateCrossProcessSubframeProxies) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 1260 | GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html")); |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 1261 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1262 | |
| 1263 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 1264 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1265 | ->GetPrimaryFrameTree() |
| 1266 | .root(); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1267 | |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 1268 | // There should not be a proxy for the root's own SiteInstance. |
Sharon Yang | 7424bda | 2021-11-04 20:27:43 | [diff] [blame] | 1269 | SiteInstanceImpl* root_instance = |
| 1270 | root->current_frame_host()->GetSiteInstance(); |
Harkiran Bolaria | d22a1dca | 2022-02-22 17:01:12 | [diff] [blame] | 1271 | EXPECT_FALSE(root->current_frame_host() |
| 1272 | ->browsing_context_state() |
| 1273 | ->GetRenderFrameProxyHost(root_instance->group())); |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 1274 | |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1275 | // Load same-site page into iframe. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 1276 | GURL http_url(embedded_test_server()->GetURL("/title1.html")); |
Lukasz Anforowicz | 69c25dfd | 2020-11-12 21:50:20 | [diff] [blame] | 1277 | EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), http_url)); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1278 | |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1279 | // Load cross-site page into iframe. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 1280 | GURL cross_site_url( |
| 1281 | embedded_test_server()->GetURL("foo.com", "/title2.html")); |
Lukasz Anforowicz | 69c25dfd | 2020-11-12 21:50:20 | [diff] [blame] | 1282 | EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), cross_site_url)); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1283 | |
| 1284 | // Ensure that we have created a new process for the subframe. |
nasko | e6edde3 | 2014-10-17 15:36:48 | [diff] [blame] | 1285 | ASSERT_EQ(2U, root->child_count()); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1286 | FrameTreeNode* child = root->child_at(0); |
Sharon Yang | 7424bda | 2021-11-04 20:27:43 | [diff] [blame] | 1287 | SiteInstanceImpl* child_instance = |
| 1288 | child->current_frame_host()->GetSiteInstance(); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1289 | RenderViewHost* rvh = child->current_frame_host()->render_view_host(); |
| 1290 | RenderProcessHost* rph = child->current_frame_host()->GetProcess(); |
| 1291 | |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1292 | EXPECT_NE(shell()->web_contents()->GetPrimaryMainFrame()->GetRenderViewHost(), |
| 1293 | rvh); |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 1294 | EXPECT_NE(shell()->web_contents()->GetSiteInstance(), child_instance); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1295 | EXPECT_NE(shell()->web_contents()->GetPrimaryMainFrame()->GetProcess(), rph); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1296 | |
| 1297 | // Ensure that the root node has a proxy for the child node's SiteInstance. |
Harkiran Bolaria | d22a1dca | 2022-02-22 17:01:12 | [diff] [blame] | 1298 | EXPECT_TRUE(root->current_frame_host() |
| 1299 | ->browsing_context_state() |
| 1300 | ->GetRenderFrameProxyHost(child_instance->group())); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1301 | |
| 1302 | // Also ensure that the child has a proxy for the root node's SiteInstance. |
Harkiran Bolaria | d22a1dca | 2022-02-22 17:01:12 | [diff] [blame] | 1303 | EXPECT_TRUE(child->current_frame_host() |
| 1304 | ->browsing_context_state() |
| 1305 | ->GetRenderFrameProxyHost(root_instance->group())); |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 1306 | |
| 1307 | // The nodes should not have proxies for their own SiteInstance. |
Harkiran Bolaria | d22a1dca | 2022-02-22 17:01:12 | [diff] [blame] | 1308 | EXPECT_FALSE(root->current_frame_host() |
| 1309 | ->browsing_context_state() |
| 1310 | ->GetRenderFrameProxyHost(root_instance->group())); |
| 1311 | EXPECT_FALSE(child->current_frame_host() |
| 1312 | ->browsing_context_state() |
| 1313 | ->GetRenderFrameProxyHost(child_instance->group())); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 1314 | |
| 1315 | // Ensure that the RenderViews and RenderFrames are all live. |
| 1316 | EXPECT_TRUE( |
| 1317 | root->current_frame_host()->render_view_host()->IsRenderViewLive()); |
| 1318 | EXPECT_TRUE( |
| 1319 | child->current_frame_host()->render_view_host()->IsRenderViewLive()); |
| 1320 | EXPECT_TRUE(root->current_frame_host()->IsRenderFrameLive()); |
| 1321 | EXPECT_TRUE(root->child_at(0)->current_frame_host()->IsRenderFrameLive()); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 1322 | } |
| 1323 | |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1324 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 1325 | OriginSetOnNavigations) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 1326 | GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html")); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1327 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1328 | |
| 1329 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 1330 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1331 | ->GetPrimaryFrameTree() |
| 1332 | .root(); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1333 | |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 1334 | EXPECT_EQ(root->current_origin().Serialize() + '/', |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 1335 | main_url.DeprecatedGetOriginAsURL().spec()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1336 | |
| 1337 | // First frame is an about:blank frame. Check that its origin is correctly |
| 1338 | // inherited from the parent. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 1339 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize() + '/', |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 1340 | main_url.DeprecatedGetOriginAsURL().spec()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1341 | |
| 1342 | // Second frame loads a same-site page. Its origin should also be the same |
| 1343 | // as the parent. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 1344 | EXPECT_EQ(root->child_at(1)->current_origin().Serialize() + '/', |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 1345 | main_url.DeprecatedGetOriginAsURL().spec()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1346 | |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1347 | // Load cross-site page into the first frame. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 1348 | GURL cross_site_url( |
| 1349 | embedded_test_server()->GetURL("foo.com", "/title2.html")); |
Lukasz Anforowicz | 69c25dfd | 2020-11-12 21:50:20 | [diff] [blame] | 1350 | EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), cross_site_url)); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1351 | |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 1352 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize() + '/', |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 1353 | cross_site_url.DeprecatedGetOriginAsURL().spec()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1354 | |
| 1355 | // The root's origin shouldn't have changed. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 1356 | EXPECT_EQ(root->current_origin().Serialize() + '/', |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 1357 | main_url.DeprecatedGetOriginAsURL().spec()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1358 | |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 1359 | { |
| 1360 | GURL data_url("data:text/html,foo"); |
| 1361 | TestNavigationObserver observer(shell()->web_contents()); |
| 1362 | EXPECT_TRUE( |
| 1363 | ExecJs(root->child_at(1), JsReplace("window.location = $1", data_url))); |
| 1364 | observer.Wait(); |
| 1365 | } |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1366 | |
| 1367 | // Navigating to a data URL should set a unique origin. This is represented |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 1368 | // as "null" per RFC 6454. A frame navigating itself to a data: URL does not |
| 1369 | // require a process transfer, but should retain the original origin |
| 1370 | // as its precursor. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 1371 | EXPECT_EQ(root->child_at(1)->current_origin().Serialize(), "null"); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 1372 | EXPECT_TRUE(root->child_at(1)->current_origin().opaque()); |
| 1373 | ASSERT_EQ( |
| 1374 | url::SchemeHostPort(main_url), |
| 1375 | root->child_at(1)->current_origin().GetTupleOrPrecursorTupleIfOpaque()) |
| 1376 | << "Expected the precursor origin to be preserved; should be the " |
| 1377 | "initiator of a data: navigation."; |
| 1378 | |
| 1379 | // Adding an <iframe sandbox srcdoc=> frame should result in a unique origin |
| 1380 | // that is different-origin from its data: URL parent. |
| 1381 | { |
| 1382 | TestNavigationObserver observer(shell()->web_contents()); |
| 1383 | |
| 1384 | ASSERT_EQ(0U, root->child_at(1)->child_count()); |
| 1385 | EXPECT_TRUE( |
| 1386 | ExecJs(root->child_at(1), JsReplace( |
| 1387 | R"( |
| 1388 | var iframe = document.createElement('iframe'); |
| 1389 | iframe.setAttribute('sandbox', 'allow-scripts'); |
| 1390 | iframe.srcdoc = $1; |
| 1391 | document.body.appendChild(iframe); |
| 1392 | )", |
| 1393 | "<html><body>This sandboxed doc should " |
| 1394 | "be different-origin.</body></html>"))); |
| 1395 | observer.Wait(); |
| 1396 | ASSERT_EQ(1U, root->child_at(1)->child_count()); |
| 1397 | } |
| 1398 | |
| 1399 | url::Origin root_origin = root->current_origin(); |
| 1400 | url::Origin child_1 = root->child_at(1)->current_origin(); |
| 1401 | url::Origin child_1_0 = root->child_at(1)->child_at(0)->current_origin(); |
| 1402 | EXPECT_FALSE(root_origin.opaque()); |
| 1403 | EXPECT_TRUE(child_1.opaque()); |
| 1404 | EXPECT_TRUE(child_1_0.opaque()); |
| 1405 | EXPECT_NE(child_1, child_1_0); |
| 1406 | EXPECT_EQ(url::SchemeHostPort(main_url), |
| 1407 | root_origin.GetTupleOrPrecursorTupleIfOpaque()); |
| 1408 | EXPECT_EQ(url::SchemeHostPort(main_url), |
| 1409 | child_1.GetTupleOrPrecursorTupleIfOpaque()); |
| 1410 | EXPECT_EQ(url::SchemeHostPort(main_url), |
| 1411 | child_1_0.GetTupleOrPrecursorTupleIfOpaque()); |
| 1412 | |
| 1413 | { |
| 1414 | TestNavigationObserver observer(shell()->web_contents()); |
| 1415 | |
| 1416 | ASSERT_EQ(1U, root->child_at(1)->child_count()); |
| 1417 | EXPECT_TRUE( |
| 1418 | ExecJs(root->child_at(1), JsReplace( |
| 1419 | R"( |
| 1420 | var iframe = document.createElement('iframe'); |
| 1421 | iframe.srcdoc = $1; |
| 1422 | document.body.appendChild(iframe); |
| 1423 | )", |
| 1424 | "<html><body>This srcdoc document should " |
| 1425 | "be same-origin.</body></html>"))); |
| 1426 | observer.Wait(); |
| 1427 | ASSERT_EQ(2U, root->child_at(1)->child_count()); |
| 1428 | } |
| 1429 | EXPECT_EQ(root_origin, root->current_origin()); |
| 1430 | EXPECT_EQ(child_1, root->child_at(1)->current_origin()); |
| 1431 | EXPECT_EQ(child_1_0, root->child_at(1)->child_at(0)->current_origin()); |
| 1432 | url::Origin child_1_1 = root->child_at(1)->child_at(1)->current_origin(); |
| 1433 | EXPECT_EQ(child_1, child_1_1); |
| 1434 | EXPECT_NE(child_1_0, child_1_1); |
| 1435 | |
| 1436 | { |
| 1437 | TestNavigationObserver observer(shell()->web_contents()); |
| 1438 | |
| 1439 | ASSERT_EQ(2U, root->child_at(1)->child_count()); |
| 1440 | EXPECT_TRUE( |
| 1441 | ExecJs(root->child_at(1), JsReplace( |
| 1442 | R"( |
| 1443 | var iframe = document.createElement('iframe'); |
| 1444 | iframe.src = 'data:text/html;base64,' + btoa($1); |
| 1445 | document.body.appendChild(iframe); |
| 1446 | )", |
| 1447 | "<html><body>This data: doc should be " |
| 1448 | "different-origin.</body></html>"))); |
| 1449 | observer.Wait(); |
| 1450 | ASSERT_EQ(3U, root->child_at(1)->child_count()); |
| 1451 | } |
| 1452 | EXPECT_EQ(root_origin, root->current_origin()); |
| 1453 | EXPECT_EQ(child_1, root->child_at(1)->current_origin()); |
| 1454 | EXPECT_EQ(child_1_0, root->child_at(1)->child_at(0)->current_origin()); |
| 1455 | EXPECT_EQ(child_1_1, root->child_at(1)->child_at(1)->current_origin()); |
| 1456 | url::Origin child_1_2 = root->child_at(1)->child_at(2)->current_origin(); |
| 1457 | EXPECT_NE(child_1, child_1_2); |
| 1458 | EXPECT_NE(child_1_0, child_1_2); |
| 1459 | EXPECT_NE(child_1_1, child_1_2); |
| 1460 | EXPECT_EQ(url::SchemeHostPort(main_url), |
| 1461 | child_1_2.GetTupleOrPrecursorTupleIfOpaque()); |
| 1462 | |
| 1463 | // If the parent navigates its child to a data URL, it should transfer |
| 1464 | // to the parent's process, and the precursor origin should track the |
| 1465 | // parent's origin. |
| 1466 | { |
| 1467 | GURL data_url("data:text/html,foo2"); |
| 1468 | TestNavigationObserver observer(shell()->web_contents()); |
| 1469 | EXPECT_TRUE(ExecJs(root, JsReplace("frames[0].location = $1", data_url))); |
| 1470 | observer.Wait(); |
| 1471 | EXPECT_EQ(data_url, root->child_at(0)->current_url()); |
| 1472 | } |
| 1473 | |
| 1474 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize(), "null"); |
| 1475 | EXPECT_TRUE(root->child_at(0)->current_origin().opaque()); |
| 1476 | EXPECT_EQ( |
| 1477 | url::SchemeHostPort(main_url), |
| 1478 | root->child_at(0)->current_origin().GetTupleOrPrecursorTupleIfOpaque()); |
| 1479 | EXPECT_EQ(root->current_frame_host()->GetProcess(), |
| 1480 | root->child_at(0)->current_frame_host()->GetProcess()); |
| 1481 | } |
| 1482 | |
| 1483 | // Test to verify that a blob: URL that is created by a unique opaque origin |
| 1484 | // will correctly set the origin_to_commit on a session history navigation. |
| 1485 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
| 1486 | OriginForBlobUrlsFromUniqueOpaqueOrigin) { |
| 1487 | // Start off with a navigation to data: URL in the main frame. It should |
| 1488 | // result in a unique opaque origin without any precursor information. |
| 1489 | GURL data_url("data:text/html,foo<iframe id='child' src='" + |
| 1490 | embedded_test_server()->GetURL("/title1.html").spec() + |
| 1491 | "'></iframe>"); |
| 1492 | EXPECT_TRUE(NavigateToURL(shell(), data_url)); |
| 1493 | |
| 1494 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1495 | ->GetPrimaryFrameTree() |
| 1496 | .root(); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 1497 | EXPECT_TRUE(root->current_origin().opaque()); |
Nasko Oskov | 5511938 | 2020-01-17 18:22:18 | [diff] [blame] | 1498 | EXPECT_FALSE( |
| 1499 | root->current_origin().GetTupleOrPrecursorTupleIfOpaque().IsValid()); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 1500 | EXPECT_EQ(1UL, root->child_count()); |
| 1501 | FrameTreeNode* child = root->child_at(0); |
| 1502 | |
| 1503 | // Create a blob: URL and navigate the child frame to it. |
| 1504 | std::string html = "<html><body>This is blob content.</body></html>"; |
| 1505 | std::string script = JsReplace( |
| 1506 | "var blob = new Blob([$1], {type: 'text/html'});" |
| 1507 | "var blob_url = URL.createObjectURL(blob);" |
| 1508 | "document.getElementById('child').src = blob_url;" |
| 1509 | "blob_url;", |
| 1510 | html); |
| 1511 | GURL blob_url; |
| 1512 | { |
| 1513 | TestFrameNavigationObserver observer(child); |
| 1514 | blob_url = GURL(EvalJs(root, script).ExtractString()); |
| 1515 | observer.Wait(); |
| 1516 | EXPECT_EQ(blob_url, child->current_frame_host()->GetLastCommittedURL()); |
| 1517 | } |
| 1518 | |
| 1519 | // We expect the frame to have committed in an opaque origin which contains |
| 1520 | // the same precursor information - none. |
| 1521 | url::Origin blob_origin = child->current_origin(); |
| 1522 | EXPECT_TRUE(blob_origin.opaque()); |
| 1523 | EXPECT_EQ(root->current_origin().GetTupleOrPrecursorTupleIfOpaque(), |
| 1524 | blob_origin.GetTupleOrPrecursorTupleIfOpaque()); |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 1525 | EXPECT_FALSE( |
| 1526 | child->current_origin().GetTupleOrPrecursorTupleIfOpaque().IsValid()); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 1527 | |
| 1528 | // Navigate the frame away to any web URL. |
| 1529 | { |
| 1530 | GURL url(embedded_test_server()->GetURL("/title2.html")); |
| 1531 | TestFrameNavigationObserver observer(child); |
| 1532 | EXPECT_TRUE(ExecJs(child, JsReplace("window.location = $1", url))); |
| 1533 | observer.Wait(); |
| 1534 | EXPECT_EQ(url, child->current_frame_host()->GetLastCommittedURL()); |
| 1535 | } |
| 1536 | EXPECT_FALSE(child->current_origin().opaque()); |
| 1537 | EXPECT_TRUE(shell()->web_contents()->GetController().CanGoBack()); |
| 1538 | EXPECT_EQ(3, shell()->web_contents()->GetController().GetEntryCount()); |
| 1539 | EXPECT_EQ( |
| 1540 | 2, shell()->web_contents()->GetController().GetLastCommittedEntryIndex()); |
| 1541 | |
| 1542 | // Verify the blob URL still exists in the main frame, which keeps it alive |
| 1543 | // allowing a session history navigation back to succeed. |
| 1544 | EXPECT_EQ(blob_url, GURL(EvalJs(root, "blob_url;").ExtractString())); |
| 1545 | |
| 1546 | // Now navigate back in session history. It should successfully go back to |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 1547 | // the blob: URL. The child frame won't be reusing the exact same origin it |
| 1548 | // used before, but it will commit a new opaque origin which will still have |
| 1549 | // no precursor information. |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 1550 | { |
| 1551 | TestFrameNavigationObserver observer(child); |
| 1552 | shell()->web_contents()->GetController().GoBack(); |
| 1553 | observer.Wait(); |
| 1554 | } |
| 1555 | EXPECT_EQ(blob_url, child->current_frame_host()->GetLastCommittedURL()); |
| 1556 | EXPECT_TRUE(child->current_origin().opaque()); |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 1557 | EXPECT_NE(blob_origin, child->current_origin()); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 1558 | EXPECT_EQ(root->current_origin().GetTupleOrPrecursorTupleIfOpaque(), |
| 1559 | child->current_origin().GetTupleOrPrecursorTupleIfOpaque()); |
Rakina Zata Amni | c736785 | 2022-11-07 17:10:40 | [diff] [blame] | 1560 | EXPECT_FALSE( |
| 1561 | child->current_origin().GetTupleOrPrecursorTupleIfOpaque().IsValid()); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | // Test to verify that about:blank iframe, which is a child of a sandboxed |
| 1565 | // iframe is not considered same origin, but precursor information is preserved |
| 1566 | // in its origin. |
| 1567 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
| 1568 | AboutBlankSubframeInSandboxedFrame) { |
| 1569 | // Start off by navigating to a page with sandboxed iframe, which allows |
| 1570 | // script execution. |
| 1571 | GURL main_url( |
| 1572 | embedded_test_server()->GetURL("/sandboxed_main_frame_script.html")); |
| 1573 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1574 | |
| 1575 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1576 | ->GetPrimaryFrameTree() |
| 1577 | .root(); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 1578 | EXPECT_EQ(1UL, root->child_count()); |
| 1579 | FrameTreeNode* child = root->child_at(0); |
| 1580 | |
| 1581 | // Navigate the frame to data: URL to cause it to have an opaque origin that |
| 1582 | // is derived from the |main_url| origin. |
| 1583 | GURL data_url("data:text/html,<html><body>foo</body></html>"); |
| 1584 | { |
| 1585 | TestFrameNavigationObserver observer(child); |
| 1586 | EXPECT_TRUE(ExecJs(root, JsReplace("frames[0].location = $1", data_url))); |
| 1587 | observer.Wait(); |
| 1588 | EXPECT_EQ(data_url, child->current_frame_host()->GetLastCommittedURL()); |
| 1589 | } |
| 1590 | |
| 1591 | // Add an about:blank iframe to the data: frame, which should not inherit the |
| 1592 | // origin, but should preserve the precursor information. |
| 1593 | { |
| 1594 | EXPECT_TRUE(ExecJs(child, |
| 1595 | "var f = document.createElement('iframe');" |
| 1596 | "document.body.appendChild(f);")); |
| 1597 | } |
| 1598 | EXPECT_EQ(1UL, child->child_count()); |
| 1599 | FrameTreeNode* grandchild = child->child_at(0); |
| 1600 | |
| 1601 | EXPECT_TRUE(grandchild->current_origin().opaque()); |
| 1602 | EXPECT_EQ(GURL(url::kAboutBlankURL), |
| 1603 | grandchild->current_frame_host()->GetLastCommittedURL()); |
| 1604 | |
| 1605 | // The origin of the data: document should have precursor information matching |
| 1606 | // the main frame origin. |
| 1607 | EXPECT_EQ(root->current_origin().GetTupleOrPrecursorTupleIfOpaque(), |
| 1608 | child->current_origin().GetTupleOrPrecursorTupleIfOpaque()); |
| 1609 | |
| 1610 | // The same should hold also for the about:blank subframe of the data: frame. |
| 1611 | EXPECT_EQ(root->current_origin().GetTupleOrPrecursorTupleIfOpaque(), |
| 1612 | grandchild->current_origin().GetTupleOrPrecursorTupleIfOpaque()); |
| 1613 | |
| 1614 | // The about:blank document should not be able to access its parent, as they |
| 1615 | // are considered cross origin due to the sandbox flags on the parent. |
| 1616 | EXPECT_FALSE(ExecJs(grandchild, "window.parent.foo = 'bar';")); |
| 1617 | EXPECT_NE(child->current_origin(), grandchild->current_origin()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1618 | } |
| 1619 | |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1620 | // Ensure that a popup opened from a sandboxed main frame inherits sandbox flags |
| 1621 | // from its opener. |
| 1622 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
| 1623 | SandboxFlagsSetForNewWindow) { |
| 1624 | GURL main_url( |
| 1625 | embedded_test_server()->GetURL("/sandboxed_main_frame_script.html")); |
| 1626 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1627 | |
| 1628 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 1629 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1630 | ->GetPrimaryFrameTree() |
| 1631 | .root(); |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1632 | |
| 1633 | // Open a new window from the main frame. |
| 1634 | GURL popup_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
| 1635 | Shell* new_shell = OpenPopup(root->current_frame_host(), popup_url, ""); |
| 1636 | EXPECT_TRUE(new_shell); |
| 1637 | WebContents* new_contents = new_shell->web_contents(); |
| 1638 | |
| 1639 | // Check that the new window's sandbox flags correctly reflect the opener's |
| 1640 | // flags. Main frame sets allow-popups, allow-pointer-lock and allow-scripts. |
| 1641 | FrameTreeNode* popup_root = |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1642 | static_cast<WebContentsImpl*>(new_contents)->GetPrimaryFrameTree().root(); |
arthursonzogni | b93a447 | 2020-04-10 07:38:00 | [diff] [blame] | 1643 | network::mojom::WebSandboxFlags main_frame_sandbox_flags = |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1644 | root->current_frame_host()->active_sandbox_flags(); |
Arthur Sonzogni | 4ba3104f | 2022-03-09 09:04:39 | [diff] [blame] | 1645 | EXPECT_EQ( |
| 1646 | network::mojom::WebSandboxFlags::kAll & |
| 1647 | ~network::mojom::WebSandboxFlags::kAutomaticFeatures & |
| 1648 | ~network::mojom::WebSandboxFlags::kPointerLock & |
| 1649 | ~network::mojom::WebSandboxFlags::kPopups & |
| 1650 | ~network::mojom::WebSandboxFlags::kScripts & |
| 1651 | ~network::mojom::WebSandboxFlags::kTopNavigationToCustomProtocols, |
| 1652 | main_frame_sandbox_flags); |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1653 | |
| 1654 | EXPECT_EQ(main_frame_sandbox_flags, |
| 1655 | popup_root->effective_frame_policy().sandbox_flags); |
| 1656 | EXPECT_EQ(main_frame_sandbox_flags, popup_root->active_sandbox_flags()); |
| 1657 | EXPECT_EQ(main_frame_sandbox_flags, |
| 1658 | popup_root->current_frame_host()->active_sandbox_flags()); |
| 1659 | } |
| 1660 | |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1661 | // Tests that the user activation bits get cleared when a cross-site document is |
| 1662 | // installed in the frame. |
| 1663 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
| 1664 | ClearUserActivationForNewDocument) { |
| 1665 | GURL main_url(embedded_test_server()->GetURL("/frame_tree/top.html")); |
| 1666 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1667 | |
| 1668 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 1669 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1670 | ->GetPrimaryFrameTree() |
| 1671 | .root(); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1672 | |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame] | 1673 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1674 | EXPECT_FALSE(root->HasTransientUserActivation()); |
| 1675 | |
| 1676 | // Set the user activation bits. |
| 1677 | root->UpdateUserActivationState( |
Mustaq Ahmed | dc195e5b | 2020-08-04 18:45:11 | [diff] [blame] | 1678 | blink::mojom::UserActivationUpdateType::kNotifyActivation, |
| 1679 | blink::mojom::UserActivationNotificationType::kTest); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame] | 1680 | EXPECT_TRUE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1681 | EXPECT_TRUE(root->HasTransientUserActivation()); |
| 1682 | |
| 1683 | // Install a new cross-site document to check the clearing of user activation |
| 1684 | // bits. |
| 1685 | GURL cross_site_url( |
| 1686 | embedded_test_server()->GetURL("foo.com", "/title2.html")); |
| 1687 | EXPECT_TRUE(NavigateToURL(shell(), cross_site_url)); |
| 1688 | |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame] | 1689 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1690 | EXPECT_FALSE(root->HasTransientUserActivation()); |
| 1691 | } |
| 1692 | |
Dave Tapuska | 708c273 | 2022-09-23 22:47:49 | [diff] [blame] | 1693 | class BrowserContextGroupSwapFrameTreeBrowserTest : public ContentBrowserTest { |
| 1694 | public: |
| 1695 | BrowserContextGroupSwapFrameTreeBrowserTest() |
| 1696 | : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {} |
| 1697 | |
| 1698 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 1699 | IsolateAllSitesForTesting(command_line); |
| 1700 | } |
| 1701 | |
| 1702 | void SetUpOnMainThread() override { |
| 1703 | ContentBrowserTest::SetUpOnMainThread(); |
| 1704 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 1705 | https_server_.ServeFilesFromSourceDirectory(GetTestDataFilePath()); |
| 1706 | https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES); |
| 1707 | net::test_server::RegisterDefaultHandlers(&https_server_); |
| 1708 | ASSERT_TRUE(https_server_.Start()); |
| 1709 | } |
| 1710 | |
| 1711 | net::EmbeddedTestServer* https_server() { return &https_server_; } |
| 1712 | |
| 1713 | public: |
| 1714 | net::EmbeddedTestServer https_server_; |
| 1715 | }; |
| 1716 | |
| 1717 | // Force a race between when the RenderViewHostImpl's main frame is running |
| 1718 | // the unload handlers and when a new navigation occurs that tries to |
| 1719 | // reuse a RenderViewHostImpl. |
| 1720 | IN_PROC_BROWSER_TEST_F(BrowserContextGroupSwapFrameTreeBrowserTest, |
| 1721 | NavigateAndGoBack) { |
| 1722 | GURL main_url(https_server()->GetURL("a.test", "/title1.html")); |
| 1723 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1724 | |
| 1725 | auto* web_contents = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 1726 | web_contents->GetPrimaryMainFrame()->DoNotDeleteForTesting(); |
| 1727 | DisableBFCacheForRFHForTesting( |
| 1728 | web_contents->GetPrimaryFrameTree().root()->current_frame_host()); |
| 1729 | |
| 1730 | // Load a page with COOP set to force the browsing context group swap |
| 1731 | // and clears out old proxies. |
| 1732 | GURL new_main_url(https_server()->GetURL( |
| 1733 | "b.test", "/set-header?Cross-Origin-Opener-Policy: same-origin")); |
| 1734 | |
| 1735 | EXPECT_TRUE(NavigateToURL(shell(), new_main_url)); |
| 1736 | |
| 1737 | TestNavigationObserver back_load_observer(web_contents); |
| 1738 | web_contents->GetController().GoBack(); |
| 1739 | back_load_observer.Wait(); |
| 1740 | } |
| 1741 | |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1742 | // FrameTreeBrowserTest variant where we isolate http://*.is, Iceland's top |
nasko | abed2a5 | 2017-05-03 05:10:17 | [diff] [blame] | 1743 | // level domain. This is an analogue to isolating extensions, which we can use |
| 1744 | // inside content_browsertests, where extensions don't exist. Iceland, like an |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1745 | // extension process, is a special place with magical powers; we want to protect |
| 1746 | // it from outsiders. |
| 1747 | class IsolateIcelandFrameTreeBrowserTest : public ContentBrowserTest { |
| 1748 | public: |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 1749 | IsolateIcelandFrameTreeBrowserTest() = default; |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1750 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 1751 | IsolateIcelandFrameTreeBrowserTest( |
| 1752 | const IsolateIcelandFrameTreeBrowserTest&) = delete; |
| 1753 | IsolateIcelandFrameTreeBrowserTest& operator=( |
| 1754 | const IsolateIcelandFrameTreeBrowserTest&) = delete; |
| 1755 | |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1756 | void SetUpCommandLine(base::CommandLine* command_line) override { |
Lukasz Anforowicz | e7c87d1 | 2018-11-03 02:53:34 | [diff] [blame] | 1757 | // Blink suppresses navigations to blob URLs of origins different from the |
alexis.menard | 83f1b6d | 2017-05-17 19:37:33 | [diff] [blame] | 1758 | // frame initiating the navigation. We disable those checks for this test, |
| 1759 | // to test what happens in a compromise scenario. |
| 1760 | command_line->AppendSwitch(switches::kDisableWebSecurity); |
Lukasz Anforowicz | e7c87d1 | 2018-11-03 02:53:34 | [diff] [blame] | 1761 | |
| 1762 | // ProcessSwitchForIsolatedBlob test below requires that one of URLs used in |
Lukasz Anforowicz | 2542093 | 2018-12-18 20:59:22 | [diff] [blame] | 1763 | // the test (blob:http://b.is/) belongs to an isolated origin. |
| 1764 | command_line->AppendSwitchASCII(switches::kIsolateOrigins, "http://b.is/"); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | void SetUpOnMainThread() override { |
| 1768 | host_resolver()->AddRule("*", "127.0.0.1"); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1769 | SetupCrossSiteRedirector(embedded_test_server()); |
martijn | 12d2dad | 2016-11-11 10:59:27 | [diff] [blame] | 1770 | ASSERT_TRUE(embedded_test_server()->Start()); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1771 | } |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1772 | }; |
| 1773 | |
| 1774 | // Regression test for https://crbug.com/644966 |
| 1775 | IN_PROC_BROWSER_TEST_F(IsolateIcelandFrameTreeBrowserTest, |
| 1776 | ProcessSwitchForIsolatedBlob) { |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1777 | // Set up an iframe. |
| 1778 | WebContents* contents = shell()->web_contents(); |
| 1779 | FrameTreeNode* root = |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1780 | static_cast<WebContentsImpl*>(contents)->GetPrimaryFrameTree().root(); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1781 | GURL main_url(embedded_test_server()->GetURL( |
nick | 07fd7e1 | 2016-09-12 18:54:06 | [diff] [blame] | 1782 | "a.com", "/cross_site_iframe_factory.html?a(a)")); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1783 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1784 | |
| 1785 | // The navigation targets an invalid blob url; that's intentional to trigger |
| 1786 | // an error response. The response should commit in a process dedicated to |
Nasko Oskov | 0401d81 | 2021-02-05 22:20:08 | [diff] [blame] | 1787 | // http://b.is or error pages, depending on policy. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 1788 | EXPECT_EQ( |
| 1789 | "done", |
| 1790 | EvalJs( |
| 1791 | root, |
| 1792 | "new Promise((resolve) => {" |
| 1793 | " var iframe_element = document.getElementsByTagName('iframe')[0];" |
| 1794 | " iframe_element.onload = () => resolve('done');" |
Lukasz Anforowicz | 2542093 | 2018-12-18 20:59:22 | [diff] [blame] | 1795 | " iframe_element.src = 'blob:http://b.is/';" |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 1796 | "});")); |
Fergal Daly | f052233 | 2020-07-18 06:09:46 | [diff] [blame] | 1797 | EXPECT_TRUE(WaitForLoadStop(contents)); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1798 | |
| 1799 | // Make sure we did a process transfer back to "b.is". |
Aaron Colwell | e953e56 | 2019-07-24 16:47:36 | [diff] [blame] | 1800 | const std::string kExpectedSiteURL = |
| 1801 | AreDefaultSiteInstancesEnabled() |
| 1802 | ? SiteInstanceImpl::GetDefaultSiteURL().spec() |
| 1803 | : "http://a.com/"; |
Nasko Oskov | 0401d81 | 2021-02-05 22:20:08 | [diff] [blame] | 1804 | const std::string kExpectedSubframeSiteURL = |
| 1805 | SiteIsolationPolicy::IsErrorPageIsolationEnabled(/*in_main_frame*/ false) |
| 1806 | ? "chrome-error://chromewebdata/" |
| 1807 | : "http://b.is/"; |
Aaron Colwell | e953e56 | 2019-07-24 16:47:36 | [diff] [blame] | 1808 | EXPECT_EQ(base::StringPrintf(" Site A ------------ proxies for B\n" |
| 1809 | " +--Site B ------- proxies for A\n" |
| 1810 | "Where A = %s\n" |
Nasko Oskov | 0401d81 | 2021-02-05 22:20:08 | [diff] [blame] | 1811 | " B = %s", |
| 1812 | kExpectedSiteURL.c_str(), |
| 1813 | kExpectedSubframeSiteURL.c_str()), |
Fergal Daly | 79f4429 | 2020-12-01 02:30:48 | [diff] [blame] | 1814 | DepictFrameTree(*root)); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1815 | } |
| 1816 | |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1817 | class FrameTreeCredentiallessIframeBrowserTest : public FrameTreeBrowserTest { |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1818 | public: |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1819 | FrameTreeCredentiallessIframeBrowserTest() = default; |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1820 | |
| 1821 | void SetUpCommandLine(base::CommandLine* command_line) override { |
Antonio Sartori | 753cd6d | 2021-07-23 08:34:55 | [diff] [blame] | 1822 | command_line->AppendSwitch(switches::kEnableBlinkTestFeatures); |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1823 | } |
| 1824 | }; |
| 1825 | |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1826 | // Tests the mojo propagation of the 'credentialless' attribute to the browser. |
| 1827 | IN_PROC_BROWSER_TEST_F(FrameTreeCredentiallessIframeBrowserTest, |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1828 | AttributeIsPropagatedToBrowser) { |
| 1829 | GURL main_url(embedded_test_server()->GetURL("/hello.html")); |
| 1830 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1831 | |
| 1832 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1833 | ->GetPrimaryFrameTree() |
| 1834 | .root(); |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1835 | |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1836 | // Not setting the attribute => the iframe is not credentialless. |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1837 | EXPECT_TRUE(ExecJs(root, |
| 1838 | "var f = document.createElement('iframe');" |
| 1839 | "document.body.appendChild(f);")); |
| 1840 | EXPECT_EQ(1U, root->child_count()); |
Miyoung Shin | c9ff481 | 2023-01-05 08:58:05 | [diff] [blame] | 1841 | EXPECT_FALSE(root->child_at(0)->Credentialless()); |
Yifan Luo | e1a2e05d | 2022-01-12 16:47:59 | [diff] [blame] | 1842 | EXPECT_EQ(false, EvalJs(root->child_at(0)->current_frame_host(), |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1843 | "window.credentialless")); |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1844 | |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1845 | // Setting the attribute on the iframe element makes the iframe |
| 1846 | // credentialless. |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1847 | EXPECT_TRUE(ExecJs(root, |
| 1848 | "var d = document.createElement('div');" |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1849 | "d.innerHTML = '<iframe credentialless></iframe>';" |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1850 | "document.body.appendChild(d);")); |
| 1851 | EXPECT_EQ(2U, root->child_count()); |
Miyoung Shin | c9ff481 | 2023-01-05 08:58:05 | [diff] [blame] | 1852 | EXPECT_TRUE(root->child_at(1)->Credentialless()); |
Yifan Luo | 40086747 | 2022-05-19 09:34:20 | [diff] [blame] | 1853 | EXPECT_EQ(true, EvalJs(root->child_at(1)->current_frame_host(), |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1854 | "window.credentialless")); |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1855 | |
| 1856 | // Setting the attribute via javascript works. |
| 1857 | EXPECT_TRUE(ExecJs(root, |
| 1858 | "var g = document.createElement('iframe');" |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1859 | "g.credentialless = true;" |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1860 | "document.body.appendChild(g);")); |
| 1861 | EXPECT_EQ(3U, root->child_count()); |
Miyoung Shin | c9ff481 | 2023-01-05 08:58:05 | [diff] [blame] | 1862 | EXPECT_TRUE(root->child_at(2)->Credentialless()); |
Yifan Luo | 40086747 | 2022-05-19 09:34:20 | [diff] [blame] | 1863 | EXPECT_EQ(true, EvalJs(root->child_at(2)->current_frame_host(), |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1864 | "window.credentialless")); |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1865 | |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1866 | EXPECT_TRUE(ExecJs(root, "g.credentialless = false;")); |
Miyoung Shin | c9ff481 | 2023-01-05 08:58:05 | [diff] [blame] | 1867 | EXPECT_FALSE(root->child_at(2)->Credentialless()); |
Yifan Luo | 40086747 | 2022-05-19 09:34:20 | [diff] [blame] | 1868 | EXPECT_EQ(true, EvalJs(root->child_at(2)->current_frame_host(), |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1869 | "window.credentialless")); |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1870 | |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1871 | EXPECT_TRUE(ExecJs(root, "g.credentialless = true;")); |
Miyoung Shin | c9ff481 | 2023-01-05 08:58:05 | [diff] [blame] | 1872 | EXPECT_TRUE(root->child_at(2)->Credentialless()); |
Yifan Luo | 40086747 | 2022-05-19 09:34:20 | [diff] [blame] | 1873 | EXPECT_EQ(true, EvalJs(root->child_at(2)->current_frame_host(), |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 1874 | "window.credentialless")); |
Antonio Sartori | 5abc8de | 2021-07-13 08:42:47 | [diff] [blame] | 1875 | } |
| 1876 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 1877 | } // namespace content |