[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ken Rockot | 86bea1c | 2017-05-16 06:21:35 | [diff] [blame] | 5 | #include "base/command_line.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 6 | #include "base/macros.h" |
Aaron Colwell | e953e56 | 2019-07-24 16:47:36 | [diff] [blame] | 7 | #include "base/strings/stringprintf.h" |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 8 | #include "base/test/scoped_feature_list.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 9 | #include "build/build_config.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 10 | #include "content/browser/frame_host/frame_tree.h" |
| 11 | #include "content/browser/frame_host/frame_tree_node.h" |
Lukasz Anforowicz | 9e0ce4e | 2017-09-28 19:09:15 | [diff] [blame] | 12 | #include "content/browser/frame_host/render_frame_host_impl.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 13 | #include "content/browser/renderer_host/render_view_host_impl.h" |
| 14 | #include "content/browser/web_contents/web_contents_impl.h" |
| 15 | #include "content/public/browser/notification_service.h" |
| 16 | #include "content/public/browser/notification_types.h" |
Annie Sullivan | 35977ad | 2019-04-30 21:04:28 | [diff] [blame] | 17 | #include "content/public/common/content_features.h" |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 18 | #include "content/public/common/content_switches.h" |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 19 | #include "content/public/common/origin_util.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 20 | #include "content/public/common/url_constants.h" |
| 21 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 22 | #include "content/public/test/content_browser_test.h" |
| 23 | #include "content/public/test/content_browser_test_utils.h" |
John Abd-El-Malek | 3fea4c4 | 2019-01-10 01:15:42 | [diff] [blame] | 24 | #include "content/public/test/test_frame_navigation_observer.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 25 | #include "content/public/test/test_navigation_observer.h" |
| 26 | #include "content/public/test/test_utils.h" |
| 27 | #include "content/shell/browser/shell.h" |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 28 | #include "content/shell/common/shell_switches.h" |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 29 | #include "content/test/content_browser_test_utils_internal.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 30 | #include "net/dns/mock_host_resolver.h" |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 31 | #include "net/test/embedded_test_server/embedded_test_server.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 32 | #include "third_party/blink/public/common/frame/sandbox_flags.h" |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 33 | #include "url/url_constants.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 34 | |
| 35 | namespace content { |
| 36 | |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 37 | namespace { |
| 38 | |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 39 | EvalJsResult GetOriginFromRenderer(FrameTreeNode* node) { |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 40 | return EvalJs(node, "self.origin"); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | } // namespace |
| 44 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 45 | class FrameTreeBrowserTest : public ContentBrowserTest { |
| 46 | public: |
| 47 | FrameTreeBrowserTest() {} |
| 48 | |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 49 | void SetUpOnMainThread() override { |
| 50 | host_resolver()->AddRule("*", "127.0.0.1"); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 51 | SetupCrossSiteRedirector(embedded_test_server()); |
martijn | 12d2dad | 2016-11-11 10:59:27 | [diff] [blame] | 52 | ASSERT_TRUE(embedded_test_server()->Start()); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 53 | } |
| 54 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 55 | private: |
| 56 | DISALLOW_COPY_AND_ASSIGN(FrameTreeBrowserTest); |
| 57 | }; |
| 58 | |
| 59 | // Ensures FrameTree correctly reflects page structure during navigations. |
| 60 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeShape) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 61 | GURL base_url = embedded_test_server()->GetURL("A.com", "/site_isolation/"); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 62 | |
| 63 | // Load doc without iframes. Verify FrameTree just has root. |
| 64 | // Frame tree: |
| 65 | // Site-A Root |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 66 | EXPECT_TRUE(NavigateToURL(shell(), base_url.Resolve("blank.html"))); |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 67 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 68 | ->GetFrameTree() |
| 69 | ->root(); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 70 | EXPECT_EQ(0U, root->child_count()); |
| 71 | |
| 72 | // Add 2 same-site frames. Verify 3 nodes in tree with proper names. |
| 73 | // Frame tree: |
| 74 | // Site-A Root -- Site-A frame1 |
| 75 | // \-- Site-A frame2 |
| 76 | WindowedNotificationObserver observer1( |
| 77 | content::NOTIFICATION_LOAD_STOP, |
| 78 | content::Source<NavigationController>( |
| 79 | &shell()->web_contents()->GetController())); |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 80 | EXPECT_TRUE(NavigateToURL(shell(), base_url.Resolve("frames-X-X.html"))); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 81 | observer1.Wait(); |
| 82 | ASSERT_EQ(2U, root->child_count()); |
| 83 | EXPECT_EQ(0U, root->child_at(0)->child_count()); |
| 84 | EXPECT_EQ(0U, root->child_at(1)->child_count()); |
| 85 | } |
| 86 | |
| 87 | // TODO(ajwong): Talk with nasko and merge this functionality with |
| 88 | // FrameTreeShape. |
| 89 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeShape2) { |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 90 | EXPECT_TRUE(NavigateToURL( |
| 91 | shell(), embedded_test_server()->GetURL("/frame_tree/top.html"))); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 92 | |
| 93 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 94 | FrameTreeNode* root = wc->GetFrameTree()->root(); |
| 95 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 96 | // Check that the root node is properly created. |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 97 | ASSERT_EQ(3UL, root->child_count()); |
| 98 | EXPECT_EQ(std::string(), root->frame_name()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 99 | |
| 100 | ASSERT_EQ(2UL, root->child_at(0)->child_count()); |
| 101 | EXPECT_STREQ("1-1-name", root->child_at(0)->frame_name().c_str()); |
| 102 | |
| 103 | // Verify the deepest node exists and has the right name. |
| 104 | ASSERT_EQ(2UL, root->child_at(2)->child_count()); |
| 105 | EXPECT_EQ(1UL, root->child_at(2)->child_at(1)->child_count()); |
| 106 | EXPECT_EQ(0UL, root->child_at(2)->child_at(1)->child_at(0)->child_count()); |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 107 | EXPECT_STREQ( |
| 108 | "3-1-name", |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 109 | root->child_at(2)->child_at(1)->child_at(0)->frame_name().c_str()); |
| 110 | |
| 111 | // Navigate to about:blank, which should leave only the root node of the frame |
| 112 | // tree in the browser process. |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 113 | EXPECT_TRUE( |
| 114 | NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"))); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 115 | |
| 116 | root = wc->GetFrameTree()->root(); |
| 117 | EXPECT_EQ(0UL, root->child_count()); |
| 118 | EXPECT_EQ(std::string(), root->frame_name()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // Test that we can navigate away if the previous renderer doesn't clean up its |
| 122 | // child frames. |
| 123 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeAfterCrash) { |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 124 | EXPECT_TRUE(NavigateToURL( |
| 125 | shell(), embedded_test_server()->GetURL("/frame_tree/top.html"))); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 126 | |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 127 | // Ensure the view and frame are live. |
| 128 | RenderViewHost* rvh = shell()->web_contents()->GetRenderViewHost(); |
arthursonzogni | f4c4a4a | 2019-08-08 18:54:44 | [diff] [blame] | 129 | RenderFrameHostImpl* rfh1 = |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 130 | static_cast<RenderFrameHostImpl*>(rvh->GetMainFrame()); |
| 131 | EXPECT_TRUE(rvh->IsRenderViewLive()); |
arthursonzogni | f4c4a4a | 2019-08-08 18:54:44 | [diff] [blame] | 132 | EXPECT_TRUE(rfh1->IsRenderFrameLive()); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 133 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 134 | // Crash the renderer so that it doesn't send any FrameDetached messages. |
| 135 | RenderProcessHostWatcher crash_observer( |
| 136 | shell()->web_contents(), |
| 137 | RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
Wez | 0abfbf51 | 2018-03-03 01:54:45 | [diff] [blame] | 138 | ASSERT_TRUE( |
| 139 | shell()->web_contents()->GetMainFrame()->GetProcess()->Shutdown(0)); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 140 | crash_observer.Wait(); |
| 141 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 142 | // The frame tree should be cleared. |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 143 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 144 | FrameTreeNode* root = wc->GetFrameTree()->root(); |
| 145 | EXPECT_EQ(0UL, root->child_count()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 146 | |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 147 | // Ensure the view and frame aren't live anymore. |
| 148 | EXPECT_FALSE(rvh->IsRenderViewLive()); |
arthursonzogni | f4c4a4a | 2019-08-08 18:54:44 | [diff] [blame] | 149 | EXPECT_FALSE(rfh1->IsRenderFrameLive()); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 150 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 151 | // Navigate to a new URL. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 152 | GURL url(embedded_test_server()->GetURL("/title1.html")); |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 153 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 154 | EXPECT_EQ(0UL, root->child_count()); |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 155 | EXPECT_EQ(url, root->current_url()); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 156 | |
arthursonzogni | f4c4a4a | 2019-08-08 18:54:44 | [diff] [blame] | 157 | RenderFrameHostImpl* rfh2 = root->current_frame_host(); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 158 | // Ensure the view and frame are live again. |
| 159 | EXPECT_TRUE(rvh->IsRenderViewLive()); |
arthursonzogni | f4c4a4a | 2019-08-08 18:54:44 | [diff] [blame] | 160 | EXPECT_TRUE(rfh2->IsRenderFrameLive()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Test that we can navigate away if the previous renderer doesn't clean up its |
| 164 | // child frames. |
Nasko Oskov | 31c45ff | 2019-10-16 01:15:19 | [diff] [blame] | 165 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, NavigateWithLeftoverFrames) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 166 | GURL base_url = embedded_test_server()->GetURL("A.com", "/site_isolation/"); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 167 | |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 168 | EXPECT_TRUE(NavigateToURL( |
| 169 | shell(), embedded_test_server()->GetURL("/frame_tree/top.html"))); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 170 | |
| 171 | // Hang the renderer so that it doesn't send any FrameDetached messages. |
| 172 | // (This navigation will never complete, so don't wait for it.) |
| 173 | shell()->LoadURL(GURL(kChromeUIHangURL)); |
| 174 | |
| 175 | // Check that the frame tree still has children. |
| 176 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 177 | FrameTreeNode* root = wc->GetFrameTree()->root(); |
| 178 | ASSERT_EQ(3UL, root->child_count()); |
| 179 | |
| 180 | // Navigate to a new URL. We use LoadURL because NavigateToURL will try to |
| 181 | // wait for the previous navigation to stop. |
| 182 | TestNavigationObserver tab_observer(wc, 1); |
| 183 | shell()->LoadURL(base_url.Resolve("blank.html")); |
| 184 | tab_observer.Wait(); |
| 185 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 186 | // The frame tree should now be cleared. |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 187 | EXPECT_EQ(0UL, root->child_count()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 188 | } |
| 189 | |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 190 | // Ensure that IsRenderFrameLive is true for main frames and same-site iframes. |
| 191 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, IsRenderFrameLive) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 192 | GURL main_url(embedded_test_server()->GetURL("/frame_tree/top.html")); |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 193 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 194 | |
| 195 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 196 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 197 | ->GetFrameTree() |
| 198 | ->root(); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 199 | |
| 200 | // The root and subframe should each have a live RenderFrame. |
| 201 | EXPECT_TRUE( |
| 202 | root->current_frame_host()->render_view_host()->IsRenderViewLive()); |
| 203 | EXPECT_TRUE(root->current_frame_host()->IsRenderFrameLive()); |
| 204 | EXPECT_TRUE(root->child_at(0)->current_frame_host()->IsRenderFrameLive()); |
| 205 | |
| 206 | // Load a same-site page into iframe and it should still be live. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 207 | GURL http_url(embedded_test_server()->GetURL("/title1.html")); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 208 | NavigateFrameToURL(root->child_at(0), http_url); |
| 209 | EXPECT_TRUE( |
| 210 | root->current_frame_host()->render_view_host()->IsRenderViewLive()); |
| 211 | EXPECT_TRUE(root->current_frame_host()->IsRenderFrameLive()); |
| 212 | EXPECT_TRUE(root->child_at(0)->current_frame_host()->IsRenderFrameLive()); |
| 213 | } |
| 214 | |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 215 | // Ensure that origins are correctly set on navigations. |
| 216 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, OriginSetOnNavigation) { |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 217 | GURL about_blank(url::kAboutBlankURL); |
| 218 | GURL main_url( |
| 219 | embedded_test_server()->GetURL("a.com", "/frame_tree/top.html")); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 220 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 221 | WebContents* contents = shell()->web_contents(); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 222 | |
| 223 | // 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] | 224 | FrameTreeNode* root = |
| 225 | static_cast<WebContentsImpl*>(contents)->GetFrameTree()->root(); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 226 | |
| 227 | // Extra '/' is added because the replicated origin is serialized in RFC 6454 |
| 228 | // format, which dictates no trailing '/', whereas GURL::GetOrigin does put a |
| 229 | // '/' at the end. |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 230 | EXPECT_EQ(main_url.GetOrigin().spec(), |
| 231 | root->current_origin().Serialize() + '/'); |
| 232 | EXPECT_EQ( |
| 233 | main_url.GetOrigin().spec(), |
| 234 | root->current_frame_host()->GetLastCommittedOrigin().Serialize() + '/'); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 235 | |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 236 | // The iframe is inititially same-origin. |
| 237 | EXPECT_TRUE( |
| 238 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
| 239 | root->child_at(0)->current_frame_host()->GetLastCommittedOrigin())); |
| 240 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
| 241 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize(), |
| 242 | GetOriginFromRenderer(root->child_at(0))); |
| 243 | |
| 244 | // Navigate the iframe cross-origin. |
| 245 | GURL frame_url(embedded_test_server()->GetURL("b.com", "/title1.html")); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 246 | NavigateFrameToURL(root->child_at(0), frame_url); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 247 | EXPECT_EQ(frame_url, root->child_at(0)->current_url()); |
| 248 | EXPECT_EQ(frame_url.GetOrigin().spec(), |
| 249 | root->child_at(0)->current_origin().Serialize() + '/'); |
| 250 | EXPECT_FALSE( |
| 251 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
| 252 | root->child_at(0)->current_frame_host()->GetLastCommittedOrigin())); |
| 253 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
| 254 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize(), |
| 255 | GetOriginFromRenderer(root->child_at(0))); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 256 | |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 257 | // Parent-initiated about:blank navigation should inherit the parent's a.com |
| 258 | // origin. |
| 259 | NavigateIframeToURL(contents, "1-1-id", about_blank); |
| 260 | EXPECT_EQ(about_blank, root->child_at(0)->current_url()); |
| 261 | EXPECT_EQ(main_url.GetOrigin().spec(), |
| 262 | root->child_at(0)->current_origin().Serialize() + '/'); |
| 263 | EXPECT_EQ(root->current_frame_host()->GetLastCommittedOrigin().Serialize(), |
| 264 | root->child_at(0) |
| 265 | ->current_frame_host() |
| 266 | ->GetLastCommittedOrigin() |
| 267 | .Serialize()); |
| 268 | EXPECT_TRUE( |
| 269 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
| 270 | root->child_at(0)->current_frame_host()->GetLastCommittedOrigin())); |
| 271 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
| 272 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize(), |
| 273 | GetOriginFromRenderer(root->child_at(0))); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 274 | |
| 275 | GURL data_url("data:text/html,foo"); |
| 276 | EXPECT_TRUE(NavigateToURL(shell(), data_url)); |
| 277 | |
| 278 | // Navigating to a data URL should set a unique origin. This is represented |
| 279 | // as "null" per RFC 6454. |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 280 | EXPECT_EQ("null", root->current_origin().Serialize()); |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 281 | EXPECT_TRUE(contents->GetMainFrame()->GetLastCommittedOrigin().opaque()); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 282 | EXPECT_EQ("null", GetOriginFromRenderer(root)); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 283 | |
| 284 | // Re-navigating to a normal URL should update the origin. |
| 285 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 286 | EXPECT_EQ(main_url.GetOrigin().spec(), |
| 287 | root->current_origin().Serialize() + '/'); |
| 288 | EXPECT_EQ( |
| 289 | main_url.GetOrigin().spec(), |
| 290 | contents->GetMainFrame()->GetLastCommittedOrigin().Serialize() + '/'); |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 291 | EXPECT_FALSE(contents->GetMainFrame()->GetLastCommittedOrigin().opaque()); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 292 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 293 | } |
| 294 | |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 295 | // Tests a cross-origin navigation to a blob URL. The main frame initiates this |
| 296 | // navigation on its grandchild. It should wind up in the main frame's process. |
| 297 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, NavigateGrandchildToBlob) { |
| 298 | WebContents* contents = shell()->web_contents(); |
| 299 | FrameTreeNode* root = |
| 300 | static_cast<WebContentsImpl*>(contents)->GetFrameTree()->root(); |
| 301 | |
| 302 | // First, snapshot the FrameTree for a normal A(B(A)) case where all frames |
| 303 | // are served over http. The blob test should result in the same structure. |
| 304 | EXPECT_TRUE(NavigateToURL( |
| 305 | shell(), embedded_test_server()->GetURL( |
| 306 | "a.com", "/cross_site_iframe_factory.html?a(b(a))"))); |
| 307 | std::string reference_tree = FrameTreeVisualizer().DepictFrameTree(root); |
| 308 | |
| 309 | GURL main_url(embedded_test_server()->GetURL( |
| 310 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 311 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 312 | |
| 313 | // The root node will initiate the navigation; its grandchild node will be the |
| 314 | // target of the navigation. |
| 315 | FrameTreeNode* target = root->child_at(0)->child_at(0); |
| 316 | |
creis | b2d5f1f3 | 2016-11-07 23:25:05 | [diff] [blame] | 317 | RenderFrameDeletedObserver deleted_observer(target->current_frame_host()); |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 318 | std::string html = |
| 319 | "<html><body><div>This is blob content.</div>" |
| 320 | "<script>" |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 321 | "window.parent.parent.postMessage('HI', self.origin);" |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 322 | "</script></body></html>"; |
| 323 | std::string script = JsReplace( |
| 324 | "new Promise((resolve) => {" |
| 325 | " window.addEventListener('message', resolve, false);" |
| 326 | " var blob = new Blob([$1], {type: 'text/html'});" |
| 327 | " var blob_url = URL.createObjectURL(blob);" |
| 328 | " frames[0][0].location.href = blob_url;" |
| 329 | "}).then((event) => {" |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 330 | " document.body.appendChild(document.createTextNode(event.data));" |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 331 | " return event.source.location.href;" |
| 332 | "});", |
| 333 | html); |
| 334 | std::string blob_url_string = EvalJs(root, script).ExtractString(); |
creis | b2d5f1f3 | 2016-11-07 23:25:05 | [diff] [blame] | 335 | // Wait for the RenderFrame to go away, if this will be cross-process. |
| 336 | if (AreAllSitesIsolatedForTesting()) |
| 337 | deleted_observer.WaitUntilDeleted(); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 338 | EXPECT_EQ(GURL(blob_url_string), target->current_url()); |
| 339 | EXPECT_EQ(url::kBlobScheme, target->current_url().scheme()); |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 340 | EXPECT_FALSE(target->current_origin().opaque()); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 341 | EXPECT_EQ("a.com", target->current_origin().host()); |
| 342 | EXPECT_EQ(url::kHttpScheme, target->current_origin().scheme()); |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 343 | EXPECT_EQ("This is blob content.", |
| 344 | EvalJs(target, "document.body.children[0].innerHTML")); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 345 | EXPECT_EQ(reference_tree, FrameTreeVisualizer().DepictFrameTree(root)); |
| 346 | } |
| 347 | |
| 348 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, NavigateChildToAboutBlank) { |
| 349 | GURL main_url(embedded_test_server()->GetURL( |
| 350 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 351 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 352 | WebContentsImpl* contents = |
| 353 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 354 | |
| 355 | // The leaf node (c.com) will be navigated. Its parent node (b.com) will |
| 356 | // initiate the navigation. |
| 357 | FrameTreeNode* target = |
| 358 | contents->GetFrameTree()->root()->child_at(0)->child_at(0); |
| 359 | FrameTreeNode* initiator = target->parent(); |
| 360 | |
| 361 | // Give the target a name. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 362 | EXPECT_TRUE(ExecJs(target, "window.name = 'target';")); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 363 | |
| 364 | // Use window.open(about:blank), then poll the document for access. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 365 | EvalJsResult about_blank_origin = EvalJs( |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 366 | initiator, |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 367 | "new Promise(resolve => {" |
| 368 | " var didNavigate = false;" |
| 369 | " var intervalID = setInterval(function() {" |
| 370 | " if (!didNavigate) {" |
| 371 | " didNavigate = true;" |
| 372 | " window.open('about:blank', 'target');" |
| 373 | " }" |
| 374 | " // Poll the document until it doesn't throw a SecurityError.\n" |
| 375 | " try {" |
| 376 | " frames[0].document.write('Hi from ' + document.domain);" |
| 377 | " } catch (e) { return; }" |
| 378 | " clearInterval(intervalID);" |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 379 | " resolve(frames[0].self.origin);" |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 380 | " }, 16);" |
| 381 | "});"); |
| 382 | EXPECT_EQ(target->current_origin(), about_blank_origin); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 383 | EXPECT_EQ(GURL(url::kAboutBlankURL), target->current_url()); |
| 384 | EXPECT_EQ(url::kAboutScheme, target->current_url().scheme()); |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 385 | EXPECT_FALSE(target->current_origin().opaque()); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 386 | EXPECT_EQ("b.com", target->current_origin().host()); |
| 387 | EXPECT_EQ(url::kHttpScheme, target->current_origin().scheme()); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 388 | |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 389 | EXPECT_EQ("Hi from b.com", EvalJs(target, "document.body.innerHTML")); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Nested iframes, three origins: A(B(C)). Frame A navigates C to about:blank |
| 393 | // (via window.open). This should wind up in A's origin per the spec. Test fails |
| 394 | // because of http://crbug.com/564292 |
| 395 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, |
| 396 | DISABLED_NavigateGrandchildToAboutBlank) { |
| 397 | GURL main_url(embedded_test_server()->GetURL( |
| 398 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 399 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 400 | WebContentsImpl* contents = |
| 401 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 402 | |
| 403 | // The leaf node (c.com) will be navigated. Its grandparent node (a.com) will |
| 404 | // initiate the navigation. |
| 405 | FrameTreeNode* target = |
| 406 | contents->GetFrameTree()->root()->child_at(0)->child_at(0); |
| 407 | FrameTreeNode* initiator = target->parent()->parent(); |
| 408 | |
| 409 | // Give the target a name. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 410 | EXPECT_TRUE(ExecJs(target, "window.name = 'target';")); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 411 | |
| 412 | // Use window.open(about:blank), then poll the document for access. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 413 | EvalJsResult about_blank_origin = |
| 414 | EvalJs(initiator, |
| 415 | "new Promise((resolve) => {" |
| 416 | " var didNavigate = false;" |
| 417 | " var intervalID = setInterval(() => {" |
| 418 | " if (!didNavigate) {" |
| 419 | " didNavigate = true;" |
| 420 | " window.open('about:blank', 'target');" |
| 421 | " }" |
| 422 | " // May raise a SecurityError, that's expected.\n" |
| 423 | " try {" |
| 424 | " frames[0][0].document.write('Hi from ' + document.domain);" |
| 425 | " } catch (e) { return; }" |
| 426 | " clearInterval(intervalID);" |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 427 | " resolve(frames[0][0].self.origin);" |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 428 | " }, 16);" |
| 429 | "});"); |
| 430 | EXPECT_EQ(target->current_origin(), about_blank_origin); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 431 | EXPECT_EQ(GURL(url::kAboutBlankURL), target->current_url()); |
| 432 | EXPECT_EQ(url::kAboutScheme, target->current_url().scheme()); |
Chris Palmer | ab5e5b5 | 2018-09-28 19:19:30 | [diff] [blame] | 433 | EXPECT_FALSE(target->current_origin().opaque()); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 434 | EXPECT_EQ("a.com", target->current_origin().host()); |
| 435 | EXPECT_EQ(url::kHttpScheme, target->current_origin().scheme()); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 436 | |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 437 | EXPECT_EQ("Hi from a.com", EvalJs(target, "document.body.innerHTML")); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 438 | } |
| 439 | |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 440 | // Tests a cross-origin navigation to a data: URL. The main frame initiates this |
| 441 | // navigation on its grandchild. It should wind up in the main frame's process |
| 442 | // and have precursor origin of the main frame origin. |
| 443 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, NavigateGrandchildToDataUrl) { |
| 444 | GURL main_url(embedded_test_server()->GetURL( |
| 445 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 446 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 447 | WebContentsImpl* contents = |
| 448 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 449 | |
| 450 | // The leaf node (c.com) will be navigated. Its grandparent node (a.com) will |
| 451 | // initiate the navigation. |
| 452 | FrameTreeNode* target = |
| 453 | contents->GetFrameTree()->root()->child_at(0)->child_at(0); |
| 454 | FrameTreeNode* initiator = target->parent()->parent(); |
| 455 | |
| 456 | // Give the target a name. |
| 457 | EXPECT_TRUE(ExecJs(target, "window.name = 'target';")); |
| 458 | |
| 459 | // Navigate the target frame through the initiator frame. |
| 460 | { |
| 461 | TestFrameNavigationObserver observer(target); |
| 462 | EXPECT_TRUE( |
| 463 | ExecJs(initiator, "window.open('data:text/html,content', 'target');")); |
| 464 | observer.Wait(); |
| 465 | } |
| 466 | |
| 467 | url::Origin original_target_origin = |
| 468 | target->current_frame_host()->GetLastCommittedOrigin(); |
| 469 | EXPECT_TRUE(original_target_origin.opaque()); |
| 470 | EXPECT_EQ(original_target_origin.GetTupleOrPrecursorTupleIfOpaque(), |
| 471 | url::SchemeHostPort(main_url)); |
| 472 | |
| 473 | // Navigate the grandchild frame again cross-process to foo.com, then |
| 474 | // go back in session history. The origin for the data: URL must be preserved. |
| 475 | { |
| 476 | TestFrameNavigationObserver observer(target); |
| 477 | EXPECT_TRUE(ExecJs(target, JsReplace("window.location = $1", |
| 478 | embedded_test_server()->GetURL( |
| 479 | "foo.com", "/title2.html")))); |
| 480 | observer.Wait(); |
| 481 | } |
| 482 | EXPECT_NE(original_target_origin, |
| 483 | target->current_frame_host()->GetLastCommittedOrigin()); |
| 484 | { |
| 485 | TestFrameNavigationObserver observer(target); |
| 486 | contents->GetController().GoBack(); |
| 487 | observer.Wait(); |
| 488 | } |
| 489 | |
| 490 | url::Origin target_origin = |
| 491 | target->current_frame_host()->GetLastCommittedOrigin(); |
| 492 | EXPECT_TRUE(target_origin.opaque()); |
| 493 | EXPECT_EQ(target_origin.GetTupleOrPrecursorTupleIfOpaque(), |
| 494 | url::SchemeHostPort(main_url)); |
| 495 | EXPECT_EQ(target_origin, original_target_origin); |
| 496 | } |
| 497 | |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 498 | // Ensures that iframe with srcdoc is always put in the same origin as its |
| 499 | // parent frame. |
| 500 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, ChildFrameWithSrcdoc) { |
| 501 | GURL main_url(embedded_test_server()->GetURL( |
| 502 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 503 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 504 | WebContentsImpl* contents = |
| 505 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 506 | FrameTreeNode* root = contents->GetFrameTree()->root(); |
| 507 | EXPECT_EQ(1U, root->child_count()); |
| 508 | |
| 509 | FrameTreeNode* child = root->child_at(0); |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 510 | std::string frame_origin = EvalJs(child, "self.origin;").ExtractString(); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 511 | EXPECT_TRUE( |
| 512 | child->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 513 | url::Origin::Create(GURL(frame_origin)))); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 514 | EXPECT_FALSE( |
| 515 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 516 | url::Origin::Create(GURL(frame_origin)))); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 517 | |
| 518 | // Create a new iframe with srcdoc and add it to the main frame. It should |
| 519 | // be created in the same SiteInstance as the parent. |
| 520 | { |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 521 | std::string script( |
| 522 | "var f = document.createElement('iframe');" |
| 523 | "f.srcdoc = 'some content';" |
| 524 | "document.body.appendChild(f)"); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 525 | TestNavigationObserver observer(shell()->web_contents()); |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 526 | EXPECT_TRUE(ExecJs(root, script)); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 527 | EXPECT_EQ(2U, root->child_count()); |
| 528 | observer.Wait(); |
| 529 | |
Lukasz Anforowicz | 42d3d07f | 2019-06-19 01:06:42 | [diff] [blame] | 530 | EXPECT_TRUE(root->child_at(1)->current_url().IsAboutSrcdoc()); |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 531 | EvalJsResult frame_origin = EvalJs(root->child_at(1), "self.origin"); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 532 | EXPECT_EQ(root->current_frame_host()->GetLastCommittedURL().GetOrigin(), |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 533 | GURL(frame_origin.ExtractString())); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 534 | EXPECT_NE(child->current_frame_host()->GetLastCommittedURL().GetOrigin(), |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 535 | GURL(frame_origin.ExtractString())); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | // Set srcdoc on the existing cross-site frame. It should navigate the frame |
| 539 | // back to the origin of the parent. |
| 540 | { |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 541 | std::string script( |
| 542 | "var f = document.getElementById('child-0');" |
| 543 | "f.srcdoc = 'some content';"); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 544 | TestNavigationObserver observer(shell()->web_contents()); |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 545 | EXPECT_TRUE(ExecJs(root, script)); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 546 | observer.Wait(); |
| 547 | |
Lukasz Anforowicz | 42d3d07f | 2019-06-19 01:06:42 | [diff] [blame] | 548 | EXPECT_TRUE(child->current_url().IsAboutSrcdoc()); |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 549 | EXPECT_EQ( |
| 550 | url::Origin::Create(root->current_frame_host()->GetLastCommittedURL()) |
| 551 | .Serialize(), |
Philip Jägenstedt | 67302a2 | 2018-09-14 09:58:05 | [diff] [blame] | 552 | EvalJs(child, "self.origin")); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 556 | // Ensure that sandbox flags are correctly set in the main frame when set by |
| 557 | // Content-Security-Policy header. |
| 558 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, SandboxFlagsSetForMainFrame) { |
| 559 | GURL main_url(embedded_test_server()->GetURL("/csp_sandboxed_frame.html")); |
| 560 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 561 | |
| 562 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 563 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 564 | ->GetFrameTree() |
| 565 | ->root(); |
| 566 | |
| 567 | // Verify that sandbox flags are set properly for the root FrameTreeNode and |
| 568 | // RenderFrameHost. Root frame is sandboxed with "allow-scripts". |
| 569 | EXPECT_EQ(blink::WebSandboxFlags::kNone, |
| 570 | root->effective_frame_policy().sandbox_flags); |
| 571 | EXPECT_EQ(blink::WebSandboxFlags::kAll & ~blink::WebSandboxFlags::kScripts & |
| 572 | ~blink::WebSandboxFlags::kAutomaticFeatures, |
| 573 | root->active_sandbox_flags()); |
| 574 | EXPECT_EQ(root->active_sandbox_flags(), |
| 575 | root->current_frame_host()->active_sandbox_flags()); |
| 576 | |
| 577 | // Verify that child frames inherit sandbox flags from the root. First frame |
| 578 | // has no explicitly set flags of its own, and should inherit those from the |
| 579 | // root. Second frame is completely sandboxed. |
| 580 | EXPECT_EQ(blink::WebSandboxFlags::kAll & ~blink::WebSandboxFlags::kScripts & |
| 581 | ~blink::WebSandboxFlags::kAutomaticFeatures, |
| 582 | root->child_at(0)->effective_frame_policy().sandbox_flags); |
| 583 | EXPECT_EQ(blink::WebSandboxFlags::kAll & ~blink::WebSandboxFlags::kScripts & |
| 584 | ~blink::WebSandboxFlags::kAutomaticFeatures, |
| 585 | root->child_at(0)->active_sandbox_flags()); |
| 586 | EXPECT_EQ(root->child_at(0)->active_sandbox_flags(), |
| 587 | root->child_at(0)->current_frame_host()->active_sandbox_flags()); |
| 588 | EXPECT_EQ(blink::WebSandboxFlags::kAll, |
| 589 | root->child_at(1)->effective_frame_policy().sandbox_flags); |
| 590 | EXPECT_EQ(blink::WebSandboxFlags::kAll, |
| 591 | root->child_at(1)->active_sandbox_flags()); |
| 592 | EXPECT_EQ(root->child_at(1)->active_sandbox_flags(), |
| 593 | root->child_at(1)->current_frame_host()->active_sandbox_flags()); |
| 594 | |
| 595 | // Navigating the main frame to a different URL should clear sandbox flags. |
| 596 | GURL unsandboxed_url(embedded_test_server()->GetURL("/title1.html")); |
| 597 | NavigateFrameToURL(root, unsandboxed_url); |
| 598 | |
| 599 | // Verify that sandbox flags are cleared properly for the root FrameTreeNode |
| 600 | // and RenderFrameHost. |
| 601 | EXPECT_EQ(blink::WebSandboxFlags::kNone, |
| 602 | root->effective_frame_policy().sandbox_flags); |
| 603 | EXPECT_EQ(blink::WebSandboxFlags::kNone, root->active_sandbox_flags()); |
| 604 | EXPECT_EQ(blink::WebSandboxFlags::kNone, |
| 605 | root->current_frame_host()->active_sandbox_flags()); |
| 606 | } |
| 607 | |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 608 | // Ensure that sandbox flags are correctly set when child frames are created. |
| 609 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, SandboxFlagsSetForChildFrames) { |
| 610 | GURL main_url(embedded_test_server()->GetURL("/sandboxed_frames.html")); |
| 611 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 612 | |
| 613 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 614 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 615 | ->GetFrameTree() |
| 616 | ->root(); |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 617 | |
| 618 | // Verify that sandbox flags are set properly for all FrameTreeNodes. |
| 619 | // First frame is completely sandboxed; second frame uses "allow-scripts", |
| 620 | // which resets both SandboxFlags::Scripts and |
| 621 | // SandboxFlags::AutomaticFeatures bits per blink::parseSandboxPolicy(), and |
| 622 | // third frame has "allow-scripts allow-same-origin". |
Ian Clelland | cdc4f31 | 2017-10-13 22:24:12 | [diff] [blame] | 623 | EXPECT_EQ(blink::WebSandboxFlags::kNone, |
| 624 | root->effective_frame_policy().sandbox_flags); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 625 | EXPECT_EQ(blink::WebSandboxFlags::kAll, |
Ian Clelland | cdc4f31 | 2017-10-13 22:24:12 | [diff] [blame] | 626 | root->child_at(0)->effective_frame_policy().sandbox_flags); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 627 | EXPECT_EQ(blink::WebSandboxFlags::kAll & ~blink::WebSandboxFlags::kScripts & |
| 628 | ~blink::WebSandboxFlags::kAutomaticFeatures, |
Ian Clelland | cdc4f31 | 2017-10-13 22:24:12 | [diff] [blame] | 629 | root->child_at(1)->effective_frame_policy().sandbox_flags); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 630 | EXPECT_EQ(blink::WebSandboxFlags::kAll & ~blink::WebSandboxFlags::kScripts & |
| 631 | ~blink::WebSandboxFlags::kAutomaticFeatures & |
| 632 | ~blink::WebSandboxFlags::kOrigin, |
Ian Clelland | cdc4f31 | 2017-10-13 22:24:12 | [diff] [blame] | 633 | root->child_at(2)->effective_frame_policy().sandbox_flags); |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 634 | |
| 635 | // Sandboxed frames should set a unique origin unless they have the |
| 636 | // "allow-same-origin" directive. |
alexmos | 6e94010 | 2016-01-19 22:47:25 | [diff] [blame] | 637 | EXPECT_EQ("null", root->child_at(0)->current_origin().Serialize()); |
| 638 | EXPECT_EQ("null", root->child_at(1)->current_origin().Serialize()); |
| 639 | EXPECT_EQ(main_url.GetOrigin().spec(), |
| 640 | root->child_at(2)->current_origin().Serialize() + "/"); |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 641 | |
| 642 | // Navigating to a different URL should not clear sandbox flags. |
| 643 | GURL frame_url(embedded_test_server()->GetURL("/title1.html")); |
| 644 | NavigateFrameToURL(root->child_at(0), frame_url); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 645 | EXPECT_EQ(blink::WebSandboxFlags::kAll, |
Ian Clelland | cdc4f31 | 2017-10-13 22:24:12 | [diff] [blame] | 646 | root->child_at(0)->effective_frame_policy().sandbox_flags); |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 647 | } |
| 648 | |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 649 | // Ensure that sandbox flags are correctly set in the child frames when set by |
| 650 | // Content-Security-Policy header, and in combination with the sandbox iframe |
| 651 | // attribute. |
| 652 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, |
| 653 | SandboxFlagsSetByCSPForChildFrames) { |
| 654 | GURL main_url(embedded_test_server()->GetURL("/sandboxed_frames_csp.html")); |
| 655 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 656 | |
| 657 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 658 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 659 | ->GetFrameTree() |
| 660 | ->root(); |
| 661 | |
| 662 | // Verify that sandbox flags are set properly for all FrameTreeNodes. |
| 663 | // First frame has no iframe sandbox flags, but the framed document is served |
| 664 | // with a CSP header which sets "allow-scripts", "allow-popups" and |
| 665 | // "allow-pointer-lock". |
| 666 | // Second frame is sandboxed with "allow-scripts", "allow-pointer-lock" and |
| 667 | // "allow-orientation-lock", and the framed document is also served with a CSP |
| 668 | // header which uses "allow-popups" and "allow-pointer-lock". The resulting |
| 669 | // sandbox for the frame should only have "allow-pointer-lock". |
| 670 | EXPECT_EQ(blink::WebSandboxFlags::kNone, |
| 671 | root->effective_frame_policy().sandbox_flags); |
| 672 | EXPECT_EQ(blink::WebSandboxFlags::kNone, root->active_sandbox_flags()); |
| 673 | EXPECT_EQ(root->active_sandbox_flags(), |
| 674 | root->current_frame_host()->active_sandbox_flags()); |
| 675 | EXPECT_EQ(blink::WebSandboxFlags::kNone, |
| 676 | root->child_at(0)->effective_frame_policy().sandbox_flags); |
| 677 | EXPECT_EQ(blink::WebSandboxFlags::kAll & ~blink::WebSandboxFlags::kScripts & |
| 678 | ~blink::WebSandboxFlags::kAutomaticFeatures & |
| 679 | ~blink::WebSandboxFlags::kPopups & |
| 680 | ~blink::WebSandboxFlags::kPointerLock, |
| 681 | root->child_at(0)->active_sandbox_flags()); |
| 682 | EXPECT_EQ(root->child_at(0)->active_sandbox_flags(), |
| 683 | root->child_at(0)->current_frame_host()->active_sandbox_flags()); |
| 684 | EXPECT_EQ(blink::WebSandboxFlags::kAll & ~blink::WebSandboxFlags::kScripts & |
| 685 | ~blink::WebSandboxFlags::kAutomaticFeatures & |
| 686 | ~blink::WebSandboxFlags::kPointerLock & |
| 687 | ~blink::WebSandboxFlags::kOrientationLock, |
| 688 | root->child_at(1)->effective_frame_policy().sandbox_flags); |
| 689 | EXPECT_EQ(blink::WebSandboxFlags::kAll & ~blink::WebSandboxFlags::kScripts & |
| 690 | ~blink::WebSandboxFlags::kAutomaticFeatures & |
| 691 | ~blink::WebSandboxFlags::kPointerLock, |
| 692 | root->child_at(1)->active_sandbox_flags()); |
| 693 | EXPECT_EQ(root->child_at(1)->active_sandbox_flags(), |
| 694 | root->child_at(1)->current_frame_host()->active_sandbox_flags()); |
| 695 | |
| 696 | // Navigating to a different URL *should* clear CSP-set sandbox flags, but |
| 697 | // should retain those flags set by the frame owner. |
| 698 | GURL frame_url(embedded_test_server()->GetURL("/title1.html")); |
| 699 | |
| 700 | NavigateFrameToURL(root->child_at(0), frame_url); |
| 701 | EXPECT_EQ(blink::WebSandboxFlags::kNone, |
| 702 | root->child_at(0)->effective_frame_policy().sandbox_flags); |
| 703 | EXPECT_EQ(blink::WebSandboxFlags::kNone, |
| 704 | root->child_at(0)->active_sandbox_flags()); |
| 705 | EXPECT_EQ(root->child_at(0)->active_sandbox_flags(), |
| 706 | root->child_at(0)->current_frame_host()->active_sandbox_flags()); |
| 707 | |
| 708 | NavigateFrameToURL(root->child_at(1), frame_url); |
| 709 | EXPECT_EQ(blink::WebSandboxFlags::kAll & ~blink::WebSandboxFlags::kScripts & |
| 710 | ~blink::WebSandboxFlags::kAutomaticFeatures & |
| 711 | ~blink::WebSandboxFlags::kPointerLock & |
| 712 | ~blink::WebSandboxFlags::kOrientationLock, |
| 713 | root->child_at(1)->effective_frame_policy().sandbox_flags); |
| 714 | EXPECT_EQ(blink::WebSandboxFlags::kAll & ~blink::WebSandboxFlags::kScripts & |
| 715 | ~blink::WebSandboxFlags::kAutomaticFeatures & |
| 716 | ~blink::WebSandboxFlags::kPointerLock & |
| 717 | ~blink::WebSandboxFlags::kOrientationLock, |
| 718 | root->child_at(1)->active_sandbox_flags()); |
| 719 | EXPECT_EQ(root->child_at(1)->active_sandbox_flags(), |
| 720 | root->child_at(1)->current_frame_host()->active_sandbox_flags()); |
| 721 | } |
| 722 | |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 723 | // Ensure that a popup opened from a subframe sets its opener to the subframe's |
| 724 | // FrameTreeNode, and that the opener is cleared if the subframe is destroyed. |
| 725 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, SubframeOpenerSetForNewWindow) { |
| 726 | GURL main_url(embedded_test_server()->GetURL("/frame_tree/top.html")); |
| 727 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 728 | |
| 729 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 730 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 731 | ->GetFrameTree() |
| 732 | ->root(); |
| 733 | |
| 734 | // Open a new window from a subframe. |
| 735 | ShellAddedObserver new_shell_observer; |
| 736 | GURL popup_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 737 | EXPECT_TRUE( |
| 738 | ExecJs(root->child_at(0), JsReplace("window.open($1);", popup_url))); |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 739 | Shell* new_shell = new_shell_observer.GetShell(); |
| 740 | WebContents* new_contents = new_shell->web_contents(); |
| 741 | WaitForLoadStop(new_contents); |
| 742 | |
| 743 | // Check that the new window's opener points to the correct subframe on |
| 744 | // original window. |
| 745 | FrameTreeNode* popup_root = |
| 746 | static_cast<WebContentsImpl*>(new_contents)->GetFrameTree()->root(); |
| 747 | EXPECT_EQ(root->child_at(0), popup_root->opener()); |
| 748 | |
| 749 | // Close the original window. This should clear the new window's opener. |
| 750 | shell()->Close(); |
| 751 | EXPECT_EQ(nullptr, popup_root->opener()); |
| 752 | } |
| 753 | |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 754 | // Tests that the user activation bits get cleared when a same-site document is |
| 755 | // installed in the frame. |
| 756 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, |
| 757 | ClearUserActivationForNewDocument) { |
| 758 | GURL main_url(embedded_test_server()->GetURL("/frame_tree/top.html")); |
| 759 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 760 | |
| 761 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 762 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 763 | ->GetFrameTree() |
| 764 | ->root(); |
| 765 | |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 766 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 767 | EXPECT_FALSE(root->HasTransientUserActivation()); |
| 768 | |
| 769 | // Set the user activation bits. |
| 770 | root->UpdateUserActivationState( |
| 771 | blink::UserActivationUpdateType::kNotifyActivation); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 772 | EXPECT_TRUE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 773 | EXPECT_TRUE(root->HasTransientUserActivation()); |
| 774 | |
| 775 | // Install a new same-site document to check the clearing of user activation |
| 776 | // bits. |
| 777 | GURL url(embedded_test_server()->GetURL("/title1.html")); |
| 778 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
| 779 | |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 780 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 781 | EXPECT_FALSE(root->HasTransientUserActivation()); |
| 782 | } |
| 783 | |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 784 | class CrossProcessFrameTreeBrowserTest : public ContentBrowserTest { |
| 785 | public: |
| 786 | CrossProcessFrameTreeBrowserTest() {} |
| 787 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 788 | void SetUpCommandLine(base::CommandLine* command_line) override { |
nick | d30fd96 | 2015-07-27 21:51:08 | [diff] [blame] | 789 | IsolateAllSitesForTesting(command_line); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 790 | } |
| 791 | |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 792 | void SetUpOnMainThread() override { |
| 793 | host_resolver()->AddRule("*", "127.0.0.1"); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 794 | SetupCrossSiteRedirector(embedded_test_server()); |
martijn | 12d2dad | 2016-11-11 10:59:27 | [diff] [blame] | 795 | ASSERT_TRUE(embedded_test_server()->Start()); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 796 | } |
| 797 | |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 798 | private: |
| 799 | DISALLOW_COPY_AND_ASSIGN(CrossProcessFrameTreeBrowserTest); |
| 800 | }; |
| 801 | |
| 802 | // Ensure that we can complete a cross-process subframe navigation. |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 803 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
nasko | 83fe5dc | 2015-07-01 16:34:19 | [diff] [blame] | 804 | CreateCrossProcessSubframeProxies) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 805 | GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html")); |
Alex Moshchuk | aa95adf5 | 2019-08-13 00:02:02 | [diff] [blame] | 806 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 807 | |
| 808 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 809 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 810 | ->GetFrameTree() |
| 811 | ->root(); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 812 | |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 813 | // There should not be a proxy for the root's own SiteInstance. |
| 814 | SiteInstance* root_instance = root->current_frame_host()->GetSiteInstance(); |
| 815 | EXPECT_FALSE(root->render_manager()->GetRenderFrameProxyHost(root_instance)); |
| 816 | |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 817 | // Load same-site page into iframe. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 818 | GURL http_url(embedded_test_server()->GetURL("/title1.html")); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 819 | NavigateFrameToURL(root->child_at(0), http_url); |
| 820 | |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 821 | // Load cross-site page into iframe. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 822 | GURL cross_site_url( |
| 823 | embedded_test_server()->GetURL("foo.com", "/title2.html")); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 824 | NavigateFrameToURL(root->child_at(0), cross_site_url); |
| 825 | |
| 826 | // Ensure that we have created a new process for the subframe. |
nasko | e6edde3 | 2014-10-17 15:36:48 | [diff] [blame] | 827 | ASSERT_EQ(2U, root->child_count()); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 828 | FrameTreeNode* child = root->child_at(0); |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 829 | SiteInstance* child_instance = child->current_frame_host()->GetSiteInstance(); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 830 | RenderViewHost* rvh = child->current_frame_host()->render_view_host(); |
| 831 | RenderProcessHost* rph = child->current_frame_host()->GetProcess(); |
| 832 | |
| 833 | EXPECT_NE(shell()->web_contents()->GetRenderViewHost(), rvh); |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 834 | EXPECT_NE(shell()->web_contents()->GetSiteInstance(), child_instance); |
Lukasz Anforowicz | 9e0ce4e | 2017-09-28 19:09:15 | [diff] [blame] | 835 | EXPECT_NE(shell()->web_contents()->GetMainFrame()->GetProcess(), rph); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 836 | |
| 837 | // Ensure that the root node has a proxy for the child node's SiteInstance. |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 838 | EXPECT_TRUE(root->render_manager()->GetRenderFrameProxyHost(child_instance)); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 839 | |
| 840 | // Also ensure that the child has a proxy for the root node's SiteInstance. |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 841 | EXPECT_TRUE(child->render_manager()->GetRenderFrameProxyHost(root_instance)); |
| 842 | |
| 843 | // The nodes should not have proxies for their own SiteInstance. |
| 844 | EXPECT_FALSE(root->render_manager()->GetRenderFrameProxyHost(root_instance)); |
| 845 | EXPECT_FALSE( |
| 846 | child->render_manager()->GetRenderFrameProxyHost(child_instance)); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 847 | |
| 848 | // Ensure that the RenderViews and RenderFrames are all live. |
| 849 | EXPECT_TRUE( |
| 850 | root->current_frame_host()->render_view_host()->IsRenderViewLive()); |
| 851 | EXPECT_TRUE( |
| 852 | child->current_frame_host()->render_view_host()->IsRenderViewLive()); |
| 853 | EXPECT_TRUE(root->current_frame_host()->IsRenderFrameLive()); |
| 854 | EXPECT_TRUE(root->child_at(0)->current_frame_host()->IsRenderFrameLive()); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 855 | } |
| 856 | |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 857 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 858 | OriginSetOnNavigations) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 859 | GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html")); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 860 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 861 | |
| 862 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 863 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Aran Gilman | 37d1163 | 2019-10-08 23:07:15 | [diff] [blame] | 864 | ->GetFrameTree() |
| 865 | ->root(); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 866 | |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 867 | EXPECT_EQ(root->current_origin().Serialize() + '/', |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 868 | main_url.GetOrigin().spec()); |
| 869 | |
| 870 | // First frame is an about:blank frame. Check that its origin is correctly |
| 871 | // inherited from the parent. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 872 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize() + '/', |
| 873 | main_url.GetOrigin().spec()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 874 | |
| 875 | // Second frame loads a same-site page. Its origin should also be the same |
| 876 | // as the parent. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 877 | EXPECT_EQ(root->child_at(1)->current_origin().Serialize() + '/', |
| 878 | main_url.GetOrigin().spec()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 879 | |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 880 | // Load cross-site page into the first frame. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 881 | GURL cross_site_url( |
| 882 | embedded_test_server()->GetURL("foo.com", "/title2.html")); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 883 | NavigateFrameToURL(root->child_at(0), cross_site_url); |
| 884 | |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 885 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize() + '/', |
| 886 | cross_site_url.GetOrigin().spec()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 887 | |
| 888 | // The root's origin shouldn't have changed. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 889 | EXPECT_EQ(root->current_origin().Serialize() + '/', |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 890 | main_url.GetOrigin().spec()); |
| 891 | |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 892 | { |
| 893 | GURL data_url("data:text/html,foo"); |
| 894 | TestNavigationObserver observer(shell()->web_contents()); |
| 895 | EXPECT_TRUE( |
| 896 | ExecJs(root->child_at(1), JsReplace("window.location = $1", data_url))); |
| 897 | observer.Wait(); |
| 898 | } |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 899 | |
| 900 | // Navigating to a data URL should set a unique origin. This is represented |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 901 | // as "null" per RFC 6454. A frame navigating itself to a data: URL does not |
| 902 | // require a process transfer, but should retain the original origin |
| 903 | // as its precursor. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 904 | EXPECT_EQ(root->child_at(1)->current_origin().Serialize(), "null"); |
Nasko Oskov | 03912101 | 2019-01-11 00:21:32 | [diff] [blame] | 905 | EXPECT_TRUE(root->child_at(1)->current_origin().opaque()); |
| 906 | ASSERT_EQ( |
| 907 | url::SchemeHostPort(main_url), |
| 908 | root->child_at(1)->current_origin().GetTupleOrPrecursorTupleIfOpaque()) |
| 909 | << "Expected the precursor origin to be preserved; should be the " |
| 910 | "initiator of a data: navigation."; |
| 911 | |
| 912 | // Adding an <iframe sandbox srcdoc=> frame should result in a unique origin |
| 913 | // that is different-origin from its data: URL parent. |
| 914 | { |
| 915 | TestNavigationObserver observer(shell()->web_contents()); |
| 916 | |
| 917 | ASSERT_EQ(0U, root->child_at(1)->child_count()); |
| 918 | EXPECT_TRUE( |
| 919 | ExecJs(root->child_at(1), JsReplace( |
| 920 | R"( |
| 921 | var iframe = document.createElement('iframe'); |
| 922 | iframe.setAttribute('sandbox', 'allow-scripts'); |
| 923 | iframe.srcdoc = $1; |
| 924 | document.body.appendChild(iframe); |
| 925 | )", |
| 926 | "<html><body>This sandboxed doc should " |
| 927 | "be different-origin.</body></html>"))); |
| 928 | observer.Wait(); |
| 929 | ASSERT_EQ(1U, root->child_at(1)->child_count()); |
| 930 | } |
| 931 | |
| 932 | url::Origin root_origin = root->current_origin(); |
| 933 | url::Origin child_1 = root->child_at(1)->current_origin(); |
| 934 | url::Origin child_1_0 = root->child_at(1)->child_at(0)->current_origin(); |
| 935 | EXPECT_FALSE(root_origin.opaque()); |
| 936 | EXPECT_TRUE(child_1.opaque()); |
| 937 | EXPECT_TRUE(child_1_0.opaque()); |
| 938 | EXPECT_NE(child_1, child_1_0); |
| 939 | EXPECT_EQ(url::SchemeHostPort(main_url), |
| 940 | root_origin.GetTupleOrPrecursorTupleIfOpaque()); |
| 941 | EXPECT_EQ(url::SchemeHostPort(main_url), |
| 942 | child_1.GetTupleOrPrecursorTupleIfOpaque()); |
| 943 | EXPECT_EQ(url::SchemeHostPort(main_url), |
| 944 | child_1_0.GetTupleOrPrecursorTupleIfOpaque()); |
| 945 | |
| 946 | { |
| 947 | TestNavigationObserver observer(shell()->web_contents()); |
| 948 | |
| 949 | ASSERT_EQ(1U, root->child_at(1)->child_count()); |
| 950 | EXPECT_TRUE( |
| 951 | ExecJs(root->child_at(1), JsReplace( |
| 952 | R"( |
| 953 | var iframe = document.createElement('iframe'); |
| 954 | iframe.srcdoc = $1; |
| 955 | document.body.appendChild(iframe); |
| 956 | )", |
| 957 | "<html><body>This srcdoc document should " |
| 958 | "be same-origin.</body></html>"))); |
| 959 | observer.Wait(); |
| 960 | ASSERT_EQ(2U, root->child_at(1)->child_count()); |
| 961 | } |
| 962 | EXPECT_EQ(root_origin, root->current_origin()); |
| 963 | EXPECT_EQ(child_1, root->child_at(1)->current_origin()); |
| 964 | EXPECT_EQ(child_1_0, root->child_at(1)->child_at(0)->current_origin()); |
| 965 | url::Origin child_1_1 = root->child_at(1)->child_at(1)->current_origin(); |
| 966 | EXPECT_EQ(child_1, child_1_1); |
| 967 | EXPECT_NE(child_1_0, child_1_1); |
| 968 | |
| 969 | { |
| 970 | TestNavigationObserver observer(shell()->web_contents()); |
| 971 | |
| 972 | ASSERT_EQ(2U, root->child_at(1)->child_count()); |
| 973 | EXPECT_TRUE( |
| 974 | ExecJs(root->child_at(1), JsReplace( |
| 975 | R"( |
| 976 | var iframe = document.createElement('iframe'); |
| 977 | iframe.src = 'data:text/html;base64,' + btoa($1); |
| 978 | document.body.appendChild(iframe); |
| 979 | )", |
| 980 | "<html><body>This data: doc should be " |
| 981 | "different-origin.</body></html>"))); |
| 982 | observer.Wait(); |
| 983 | ASSERT_EQ(3U, root->child_at(1)->child_count()); |
| 984 | } |
| 985 | EXPECT_EQ(root_origin, root->current_origin()); |
| 986 | EXPECT_EQ(child_1, root->child_at(1)->current_origin()); |
| 987 | EXPECT_EQ(child_1_0, root->child_at(1)->child_at(0)->current_origin()); |
| 988 | EXPECT_EQ(child_1_1, root->child_at(1)->child_at(1)->current_origin()); |
| 989 | url::Origin child_1_2 = root->child_at(1)->child_at(2)->current_origin(); |
| 990 | EXPECT_NE(child_1, child_1_2); |
| 991 | EXPECT_NE(child_1_0, child_1_2); |
| 992 | EXPECT_NE(child_1_1, child_1_2); |
| 993 | EXPECT_EQ(url::SchemeHostPort(main_url), |
| 994 | child_1_2.GetTupleOrPrecursorTupleIfOpaque()); |
| 995 | |
| 996 | // If the parent navigates its child to a data URL, it should transfer |
| 997 | // to the parent's process, and the precursor origin should track the |
| 998 | // parent's origin. |
| 999 | { |
| 1000 | GURL data_url("data:text/html,foo2"); |
| 1001 | TestNavigationObserver observer(shell()->web_contents()); |
| 1002 | EXPECT_TRUE(ExecJs(root, JsReplace("frames[0].location = $1", data_url))); |
| 1003 | observer.Wait(); |
| 1004 | EXPECT_EQ(data_url, root->child_at(0)->current_url()); |
| 1005 | } |
| 1006 | |
| 1007 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize(), "null"); |
| 1008 | EXPECT_TRUE(root->child_at(0)->current_origin().opaque()); |
| 1009 | EXPECT_EQ( |
| 1010 | url::SchemeHostPort(main_url), |
| 1011 | root->child_at(0)->current_origin().GetTupleOrPrecursorTupleIfOpaque()); |
| 1012 | EXPECT_EQ(root->current_frame_host()->GetProcess(), |
| 1013 | root->child_at(0)->current_frame_host()->GetProcess()); |
| 1014 | } |
| 1015 | |
| 1016 | // Test to verify that a blob: URL that is created by a unique opaque origin |
| 1017 | // will correctly set the origin_to_commit on a session history navigation. |
| 1018 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
| 1019 | OriginForBlobUrlsFromUniqueOpaqueOrigin) { |
| 1020 | // Start off with a navigation to data: URL in the main frame. It should |
| 1021 | // result in a unique opaque origin without any precursor information. |
| 1022 | GURL data_url("data:text/html,foo<iframe id='child' src='" + |
| 1023 | embedded_test_server()->GetURL("/title1.html").spec() + |
| 1024 | "'></iframe>"); |
| 1025 | EXPECT_TRUE(NavigateToURL(shell(), data_url)); |
| 1026 | |
| 1027 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 1028 | ->GetFrameTree() |
| 1029 | ->root(); |
| 1030 | EXPECT_TRUE(root->current_origin().opaque()); |
| 1031 | EXPECT_TRUE( |
| 1032 | root->current_origin().GetTupleOrPrecursorTupleIfOpaque().IsInvalid()); |
| 1033 | EXPECT_EQ(1UL, root->child_count()); |
| 1034 | FrameTreeNode* child = root->child_at(0); |
| 1035 | |
| 1036 | // Create a blob: URL and navigate the child frame to it. |
| 1037 | std::string html = "<html><body>This is blob content.</body></html>"; |
| 1038 | std::string script = JsReplace( |
| 1039 | "var blob = new Blob([$1], {type: 'text/html'});" |
| 1040 | "var blob_url = URL.createObjectURL(blob);" |
| 1041 | "document.getElementById('child').src = blob_url;" |
| 1042 | "blob_url;", |
| 1043 | html); |
| 1044 | GURL blob_url; |
| 1045 | { |
| 1046 | TestFrameNavigationObserver observer(child); |
| 1047 | blob_url = GURL(EvalJs(root, script).ExtractString()); |
| 1048 | observer.Wait(); |
| 1049 | EXPECT_EQ(blob_url, child->current_frame_host()->GetLastCommittedURL()); |
| 1050 | } |
| 1051 | |
| 1052 | // We expect the frame to have committed in an opaque origin which contains |
| 1053 | // the same precursor information - none. |
| 1054 | url::Origin blob_origin = child->current_origin(); |
| 1055 | EXPECT_TRUE(blob_origin.opaque()); |
| 1056 | EXPECT_EQ(root->current_origin().GetTupleOrPrecursorTupleIfOpaque(), |
| 1057 | blob_origin.GetTupleOrPrecursorTupleIfOpaque()); |
| 1058 | |
| 1059 | // Navigate the frame away to any web URL. |
| 1060 | { |
| 1061 | GURL url(embedded_test_server()->GetURL("/title2.html")); |
| 1062 | TestFrameNavigationObserver observer(child); |
| 1063 | EXPECT_TRUE(ExecJs(child, JsReplace("window.location = $1", url))); |
| 1064 | observer.Wait(); |
| 1065 | EXPECT_EQ(url, child->current_frame_host()->GetLastCommittedURL()); |
| 1066 | } |
| 1067 | EXPECT_FALSE(child->current_origin().opaque()); |
| 1068 | EXPECT_TRUE(shell()->web_contents()->GetController().CanGoBack()); |
| 1069 | EXPECT_EQ(3, shell()->web_contents()->GetController().GetEntryCount()); |
| 1070 | EXPECT_EQ( |
| 1071 | 2, shell()->web_contents()->GetController().GetLastCommittedEntryIndex()); |
| 1072 | |
| 1073 | // Verify the blob URL still exists in the main frame, which keeps it alive |
| 1074 | // allowing a session history navigation back to succeed. |
| 1075 | EXPECT_EQ(blob_url, GURL(EvalJs(root, "blob_url;").ExtractString())); |
| 1076 | |
| 1077 | // Now navigate back in session history. It should successfully go back to |
| 1078 | // the blob: URL. |
| 1079 | { |
| 1080 | TestFrameNavigationObserver observer(child); |
| 1081 | shell()->web_contents()->GetController().GoBack(); |
| 1082 | observer.Wait(); |
| 1083 | } |
| 1084 | EXPECT_EQ(blob_url, child->current_frame_host()->GetLastCommittedURL()); |
| 1085 | EXPECT_TRUE(child->current_origin().opaque()); |
| 1086 | EXPECT_EQ(blob_origin, child->current_origin()); |
| 1087 | EXPECT_EQ(root->current_origin().GetTupleOrPrecursorTupleIfOpaque(), |
| 1088 | child->current_origin().GetTupleOrPrecursorTupleIfOpaque()); |
| 1089 | } |
| 1090 | |
| 1091 | // Test to verify that about:blank iframe, which is a child of a sandboxed |
| 1092 | // iframe is not considered same origin, but precursor information is preserved |
| 1093 | // in its origin. |
| 1094 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
| 1095 | AboutBlankSubframeInSandboxedFrame) { |
| 1096 | // Start off by navigating to a page with sandboxed iframe, which allows |
| 1097 | // script execution. |
| 1098 | GURL main_url( |
| 1099 | embedded_test_server()->GetURL("/sandboxed_main_frame_script.html")); |
| 1100 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1101 | |
| 1102 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 1103 | ->GetFrameTree() |
| 1104 | ->root(); |
| 1105 | EXPECT_EQ(1UL, root->child_count()); |
| 1106 | FrameTreeNode* child = root->child_at(0); |
| 1107 | |
| 1108 | // Navigate the frame to data: URL to cause it to have an opaque origin that |
| 1109 | // is derived from the |main_url| origin. |
| 1110 | GURL data_url("data:text/html,<html><body>foo</body></html>"); |
| 1111 | { |
| 1112 | TestFrameNavigationObserver observer(child); |
| 1113 | EXPECT_TRUE(ExecJs(root, JsReplace("frames[0].location = $1", data_url))); |
| 1114 | observer.Wait(); |
| 1115 | EXPECT_EQ(data_url, child->current_frame_host()->GetLastCommittedURL()); |
| 1116 | } |
| 1117 | |
| 1118 | // Add an about:blank iframe to the data: frame, which should not inherit the |
| 1119 | // origin, but should preserve the precursor information. |
| 1120 | { |
| 1121 | EXPECT_TRUE(ExecJs(child, |
| 1122 | "var f = document.createElement('iframe');" |
| 1123 | "document.body.appendChild(f);")); |
| 1124 | } |
| 1125 | EXPECT_EQ(1UL, child->child_count()); |
| 1126 | FrameTreeNode* grandchild = child->child_at(0); |
| 1127 | |
| 1128 | EXPECT_TRUE(grandchild->current_origin().opaque()); |
| 1129 | EXPECT_EQ(GURL(url::kAboutBlankURL), |
| 1130 | grandchild->current_frame_host()->GetLastCommittedURL()); |
| 1131 | |
| 1132 | // The origin of the data: document should have precursor information matching |
| 1133 | // the main frame origin. |
| 1134 | EXPECT_EQ(root->current_origin().GetTupleOrPrecursorTupleIfOpaque(), |
| 1135 | child->current_origin().GetTupleOrPrecursorTupleIfOpaque()); |
| 1136 | |
| 1137 | // The same should hold also for the about:blank subframe of the data: frame. |
| 1138 | EXPECT_EQ(root->current_origin().GetTupleOrPrecursorTupleIfOpaque(), |
| 1139 | grandchild->current_origin().GetTupleOrPrecursorTupleIfOpaque()); |
| 1140 | |
| 1141 | // The about:blank document should not be able to access its parent, as they |
| 1142 | // are considered cross origin due to the sandbox flags on the parent. |
| 1143 | EXPECT_FALSE(ExecJs(grandchild, "window.parent.foo = 'bar';")); |
| 1144 | EXPECT_NE(child->current_origin(), grandchild->current_origin()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 1145 | } |
| 1146 | |
Ian Clelland | 5cbaaf8 | 2017-11-27 22:00:03 | [diff] [blame] | 1147 | // Ensure that a popup opened from a sandboxed main frame inherits sandbox flags |
| 1148 | // from its opener. |
| 1149 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
| 1150 | SandboxFlagsSetForNewWindow) { |
| 1151 | GURL main_url( |
| 1152 | embedded_test_server()->GetURL("/sandboxed_main_frame_script.html")); |
| 1153 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1154 | |
| 1155 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 1156 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 1157 | ->GetFrameTree() |
| 1158 | ->root(); |
| 1159 | |
| 1160 | // Open a new window from the main frame. |
| 1161 | GURL popup_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
| 1162 | Shell* new_shell = OpenPopup(root->current_frame_host(), popup_url, ""); |
| 1163 | EXPECT_TRUE(new_shell); |
| 1164 | WebContents* new_contents = new_shell->web_contents(); |
| 1165 | |
| 1166 | // Check that the new window's sandbox flags correctly reflect the opener's |
| 1167 | // flags. Main frame sets allow-popups, allow-pointer-lock and allow-scripts. |
| 1168 | FrameTreeNode* popup_root = |
| 1169 | static_cast<WebContentsImpl*>(new_contents)->GetFrameTree()->root(); |
| 1170 | blink::WebSandboxFlags main_frame_sandbox_flags = |
| 1171 | root->current_frame_host()->active_sandbox_flags(); |
| 1172 | EXPECT_EQ(blink::WebSandboxFlags::kAll & ~blink::WebSandboxFlags::kPopups & |
| 1173 | ~blink::WebSandboxFlags::kPointerLock & |
| 1174 | ~blink::WebSandboxFlags::kScripts & |
| 1175 | ~blink::WebSandboxFlags::kAutomaticFeatures, |
| 1176 | main_frame_sandbox_flags); |
| 1177 | |
| 1178 | EXPECT_EQ(main_frame_sandbox_flags, |
| 1179 | popup_root->effective_frame_policy().sandbox_flags); |
| 1180 | EXPECT_EQ(main_frame_sandbox_flags, popup_root->active_sandbox_flags()); |
| 1181 | EXPECT_EQ(main_frame_sandbox_flags, |
| 1182 | popup_root->current_frame_host()->active_sandbox_flags()); |
| 1183 | } |
| 1184 | |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1185 | // Tests that the user activation bits get cleared when a cross-site document is |
| 1186 | // installed in the frame. |
| 1187 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
| 1188 | ClearUserActivationForNewDocument) { |
| 1189 | GURL main_url(embedded_test_server()->GetURL("/frame_tree/top.html")); |
| 1190 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1191 | |
| 1192 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 1193 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 1194 | ->GetFrameTree() |
| 1195 | ->root(); |
| 1196 | |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1197 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1198 | EXPECT_FALSE(root->HasTransientUserActivation()); |
| 1199 | |
| 1200 | // Set the user activation bits. |
| 1201 | root->UpdateUserActivationState( |
| 1202 | blink::UserActivationUpdateType::kNotifyActivation); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1203 | EXPECT_TRUE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1204 | EXPECT_TRUE(root->HasTransientUserActivation()); |
| 1205 | |
| 1206 | // Install a new cross-site document to check the clearing of user activation |
| 1207 | // bits. |
| 1208 | GURL cross_site_url( |
| 1209 | embedded_test_server()->GetURL("foo.com", "/title2.html")); |
| 1210 | EXPECT_TRUE(NavigateToURL(shell(), cross_site_url)); |
| 1211 | |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1212 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Shivani Sharma | c4f56158 | 2018-11-15 15:58:39 | [diff] [blame] | 1213 | EXPECT_FALSE(root->HasTransientUserActivation()); |
| 1214 | } |
| 1215 | |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1216 | // FrameTreeBrowserTest variant where we isolate http://*.is, Iceland's top |
nasko | abed2a5 | 2017-05-03 05:10:17 | [diff] [blame] | 1217 | // level domain. This is an analogue to isolating extensions, which we can use |
| 1218 | // inside content_browsertests, where extensions don't exist. Iceland, like an |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1219 | // extension process, is a special place with magical powers; we want to protect |
| 1220 | // it from outsiders. |
| 1221 | class IsolateIcelandFrameTreeBrowserTest : public ContentBrowserTest { |
| 1222 | public: |
| 1223 | IsolateIcelandFrameTreeBrowserTest() {} |
| 1224 | |
| 1225 | void SetUpCommandLine(base::CommandLine* command_line) override { |
Lukasz Anforowicz | e7c87d1 | 2018-11-03 02:53:34 | [diff] [blame] | 1226 | // Blink suppresses navigations to blob URLs of origins different from the |
alexis.menard | 83f1b6d | 2017-05-17 19:37:33 | [diff] [blame] | 1227 | // frame initiating the navigation. We disable those checks for this test, |
| 1228 | // to test what happens in a compromise scenario. |
| 1229 | command_line->AppendSwitch(switches::kDisableWebSecurity); |
Lukasz Anforowicz | e7c87d1 | 2018-11-03 02:53:34 | [diff] [blame] | 1230 | |
| 1231 | // ProcessSwitchForIsolatedBlob test below requires that one of URLs used in |
Lukasz Anforowicz | 2542093 | 2018-12-18 20:59:22 | [diff] [blame] | 1232 | // the test (blob:http://b.is/) belongs to an isolated origin. |
| 1233 | command_line->AppendSwitchASCII(switches::kIsolateOrigins, "http://b.is/"); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | void SetUpOnMainThread() override { |
| 1237 | host_resolver()->AddRule("*", "127.0.0.1"); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1238 | SetupCrossSiteRedirector(embedded_test_server()); |
martijn | 12d2dad | 2016-11-11 10:59:27 | [diff] [blame] | 1239 | ASSERT_TRUE(embedded_test_server()->Start()); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | private: |
| 1243 | DISALLOW_COPY_AND_ASSIGN(IsolateIcelandFrameTreeBrowserTest); |
| 1244 | }; |
| 1245 | |
| 1246 | // Regression test for https://crbug.com/644966 |
| 1247 | IN_PROC_BROWSER_TEST_F(IsolateIcelandFrameTreeBrowserTest, |
| 1248 | ProcessSwitchForIsolatedBlob) { |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1249 | // Set up an iframe. |
| 1250 | WebContents* contents = shell()->web_contents(); |
| 1251 | FrameTreeNode* root = |
| 1252 | static_cast<WebContentsImpl*>(contents)->GetFrameTree()->root(); |
| 1253 | GURL main_url(embedded_test_server()->GetURL( |
nick | 07fd7e1 | 2016-09-12 18:54:06 | [diff] [blame] | 1254 | "a.com", "/cross_site_iframe_factory.html?a(a)")); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1255 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1256 | |
| 1257 | // The navigation targets an invalid blob url; that's intentional to trigger |
| 1258 | // an error response. The response should commit in a process dedicated to |
Lukasz Anforowicz | 2542093 | 2018-12-18 20:59:22 | [diff] [blame] | 1259 | // http://b.is. |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 1260 | EXPECT_EQ( |
| 1261 | "done", |
| 1262 | EvalJs( |
| 1263 | root, |
| 1264 | "new Promise((resolve) => {" |
| 1265 | " var iframe_element = document.getElementsByTagName('iframe')[0];" |
| 1266 | " iframe_element.onload = () => resolve('done');" |
Lukasz Anforowicz | 2542093 | 2018-12-18 20:59:22 | [diff] [blame] | 1267 | " iframe_element.src = 'blob:http://b.is/';" |
Nick Carter | b7e7131 | 2018-08-03 23:36:13 | [diff] [blame] | 1268 | "});")); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1269 | WaitForLoadStop(contents); |
| 1270 | |
| 1271 | // Make sure we did a process transfer back to "b.is". |
Aaron Colwell | e953e56 | 2019-07-24 16:47:36 | [diff] [blame] | 1272 | const std::string kExpectedSiteURL = |
| 1273 | AreDefaultSiteInstancesEnabled() |
| 1274 | ? SiteInstanceImpl::GetDefaultSiteURL().spec() |
| 1275 | : "http://a.com/"; |
| 1276 | EXPECT_EQ(base::StringPrintf(" Site A ------------ proxies for B\n" |
| 1277 | " +--Site B ------- proxies for A\n" |
| 1278 | "Where A = %s\n" |
| 1279 | " B = http://b.is/", |
| 1280 | kExpectedSiteURL.c_str()), |
| 1281 | FrameTreeVisualizer().DepictFrameTree(root)); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 1282 | } |
| 1283 | |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1284 | #if !defined(OS_ANDROID) |
| 1285 | // Test transferring the user activation state between nodes in the frame tree. |
| 1286 | class TransferUserActivationFrameTreeBrowserTest : public ContentBrowserTest { |
| 1287 | public: |
| 1288 | TransferUserActivationFrameTreeBrowserTest() {} |
| 1289 | |
| 1290 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 1291 | feature_list_.InitWithFeatures( |
| 1292 | {features::kUserActivationPostMessageTransfer, |
Mustaq Ahmed | e5f1256 | 2019-10-30 18:02:03 | [diff] [blame] | 1293 | features::kUserActivationSameOriginVisibility}, |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1294 | {}); |
| 1295 | } |
| 1296 | |
| 1297 | void SetUpOnMainThread() override { |
| 1298 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 1299 | SetupCrossSiteRedirector(embedded_test_server()); |
| 1300 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 1301 | } |
| 1302 | |
| 1303 | private: |
| 1304 | base::test::ScopedFeatureList feature_list_; |
| 1305 | |
| 1306 | DISALLOW_COPY_AND_ASSIGN(TransferUserActivationFrameTreeBrowserTest); |
| 1307 | }; |
| 1308 | |
| 1309 | IN_PROC_BROWSER_TEST_F(TransferUserActivationFrameTreeBrowserTest, |
Lan Wei | c927a41 | 2019-05-09 18:37:38 | [diff] [blame] | 1310 | PostMessageTransferActivationCrossOrigin) { |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1311 | GURL main_url(embedded_test_server()->GetURL( |
| 1312 | "a.com", "/cross_site_iframe_factory.html?a(b,c)")); |
| 1313 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1314 | |
| 1315 | WebContentsImpl* contents = |
| 1316 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 1317 | FrameTreeNode* root = contents->GetFrameTree()->root(); |
| 1318 | FrameTreeNode* child1 = root->child_at(0); |
| 1319 | FrameTreeNode* child2 = root->child_at(1); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1320 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1321 | EXPECT_FALSE(root->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1322 | EXPECT_FALSE(child1->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1323 | EXPECT_FALSE(child1->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1324 | EXPECT_FALSE(child2->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1325 | EXPECT_FALSE(child2->HasTransientUserActivation()); |
| 1326 | |
| 1327 | // Activate the root frame. |
| 1328 | EXPECT_TRUE(ExecuteScript(shell(), "")); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1329 | EXPECT_TRUE(root->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1330 | EXPECT_TRUE(root->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1331 | EXPECT_FALSE(child1->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1332 | EXPECT_FALSE(child1->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1333 | EXPECT_FALSE(child2->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1334 | EXPECT_FALSE(child2->HasTransientUserActivation()); |
| 1335 | |
| 1336 | // Post a message from the root frame to the child frame with |
| 1337 | // |transferUserActivation| true to transfer the user activation state from |
| 1338 | // root to child. |
| 1339 | EXPECT_TRUE(ExecuteScriptWithoutUserGesture( |
| 1340 | child1->current_frame_host(), |
| 1341 | "window.addEventListener('message', function(event) {\n" |
| 1342 | " domAutomationController.send('done-' + event.data);\n" |
| 1343 | "});")); |
| 1344 | DOMMessageQueue msg_queue; |
| 1345 | std::string actual_test_reply; |
| 1346 | EXPECT_TRUE(ExecuteScriptWithoutUserGesture( |
| 1347 | shell(), |
| 1348 | "window.frames[0].postMessage('Hello', " |
| 1349 | "{targetOrigin: '*', transferUserActivation: " |
| 1350 | "true})")); |
| 1351 | while (msg_queue.WaitForMessage(&actual_test_reply)) { |
| 1352 | if (actual_test_reply == "\"done-Hello\"") |
| 1353 | break; |
| 1354 | } |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1355 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1356 | EXPECT_FALSE(root->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1357 | EXPECT_TRUE(child1->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1358 | EXPECT_TRUE(child1->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1359 | EXPECT_FALSE(child2->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1360 | EXPECT_FALSE(child2->HasTransientUserActivation()); |
| 1361 | |
| 1362 | // Post a message from one child to another child in a different origin with |
| 1363 | // |transferUserActivation| true to transfer the user activation state from |
| 1364 | // one child to another child node in the frame tree. |
| 1365 | EXPECT_TRUE(ExecuteScriptWithoutUserGesture( |
| 1366 | child2->current_frame_host(), |
| 1367 | "window.addEventListener('message', function(event) {\n" |
| 1368 | " domAutomationController.send('done-' + event.data);\n" |
| 1369 | "});")); |
| 1370 | EXPECT_TRUE(ExecuteScriptWithoutUserGesture( |
| 1371 | child1->current_frame_host(), |
| 1372 | "window.parent.frames[1].postMessage('Hey', " |
| 1373 | "{targetOrigin: '*', transferUserActivation: " |
| 1374 | "true})")); |
| 1375 | while (msg_queue.WaitForMessage(&actual_test_reply)) { |
| 1376 | if (actual_test_reply == "\"done-Hey\"") |
| 1377 | break; |
| 1378 | } |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1379 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1380 | EXPECT_FALSE(root->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1381 | EXPECT_FALSE(child1->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1382 | EXPECT_FALSE(child1->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1383 | EXPECT_TRUE(child2->HasStickyUserActivation()); |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1384 | EXPECT_TRUE(child2->HasTransientUserActivation()); |
| 1385 | } |
Lan Wei | c927a41 | 2019-05-09 18:37:38 | [diff] [blame] | 1386 | |
| 1387 | IN_PROC_BROWSER_TEST_F(TransferUserActivationFrameTreeBrowserTest, |
| 1388 | PostMessageTransferActivationSameOrigin) { |
| 1389 | GURL main_url(embedded_test_server()->GetURL( |
| 1390 | "a.com", "/cross_site_iframe_factory.html?a(b(b))")); |
| 1391 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1392 | |
| 1393 | WebContentsImpl* contents = |
| 1394 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 1395 | FrameTreeNode* root = contents->GetFrameTree()->root(); |
| 1396 | FrameTreeNode* child1 = root->child_at(0); |
| 1397 | FrameTreeNode* child2 = child1->child_at(0); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1398 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Lan Wei | c927a41 | 2019-05-09 18:37:38 | [diff] [blame] | 1399 | EXPECT_FALSE(root->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1400 | EXPECT_FALSE(child1->HasStickyUserActivation()); |
Lan Wei | c927a41 | 2019-05-09 18:37:38 | [diff] [blame] | 1401 | EXPECT_FALSE(child1->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1402 | EXPECT_FALSE(child2->HasStickyUserActivation()); |
Lan Wei | c927a41 | 2019-05-09 18:37:38 | [diff] [blame] | 1403 | EXPECT_FALSE(child2->HasTransientUserActivation()); |
| 1404 | |
| 1405 | // Activate the root frame and transfer the user activation state from root |
| 1406 | // to child1. |
| 1407 | std::string message_event_listener = |
| 1408 | "window.addEventListener('message', function(event) {\n" |
| 1409 | " domAutomationController.send('done-' + event.data);\n" |
| 1410 | "});"; |
| 1411 | EXPECT_TRUE(ExecuteScript(shell(), "")); |
| 1412 | EXPECT_TRUE(ExecuteScriptWithoutUserGesture(child1->current_frame_host(), |
| 1413 | message_event_listener)); |
| 1414 | DOMMessageQueue msg_queue; |
| 1415 | std::string actual_test_reply; |
| 1416 | EXPECT_TRUE(ExecuteScriptWithoutUserGesture( |
| 1417 | shell(), |
| 1418 | "window.frames[0].postMessage('Hello', " |
| 1419 | "{targetOrigin: '*', transferUserActivation: true})")); |
| 1420 | while (msg_queue.WaitForMessage(&actual_test_reply)) { |
| 1421 | if (actual_test_reply == "\"done-Hello\"") |
| 1422 | break; |
| 1423 | } |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1424 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Lan Wei | c927a41 | 2019-05-09 18:37:38 | [diff] [blame] | 1425 | EXPECT_FALSE(root->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1426 | EXPECT_TRUE(child1->HasStickyUserActivation()); |
Lan Wei | c927a41 | 2019-05-09 18:37:38 | [diff] [blame] | 1427 | EXPECT_TRUE(child1->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1428 | EXPECT_FALSE(child2->HasStickyUserActivation()); |
Lan Wei | c927a41 | 2019-05-09 18:37:38 | [diff] [blame] | 1429 | EXPECT_FALSE(child2->HasTransientUserActivation()); |
| 1430 | |
| 1431 | // Post a message from child1 to its child child2 in the same origin with |
| 1432 | // |transferUserActivation| true to transfer the user activation state from |
| 1433 | // one node to its child node in the frame tree. |
| 1434 | EXPECT_TRUE(ExecuteScriptWithoutUserGesture(child2->current_frame_host(), |
| 1435 | message_event_listener)); |
| 1436 | EXPECT_TRUE(ExecuteScriptWithoutUserGesture( |
| 1437 | child1->current_frame_host(), |
| 1438 | "window.frames[0].postMessage('Hey', " |
| 1439 | "{targetOrigin: '*', transferUserActivation: true})")); |
| 1440 | while (msg_queue.WaitForMessage(&actual_test_reply)) { |
| 1441 | if (actual_test_reply == "\"done-Hey\"") |
| 1442 | break; |
| 1443 | } |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1444 | EXPECT_FALSE(root->HasStickyUserActivation()); |
Lan Wei | c927a41 | 2019-05-09 18:37:38 | [diff] [blame] | 1445 | EXPECT_FALSE(root->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1446 | EXPECT_FALSE(child1->HasStickyUserActivation()); |
Lan Wei | c927a41 | 2019-05-09 18:37:38 | [diff] [blame] | 1447 | EXPECT_FALSE(child1->HasTransientUserActivation()); |
Mustaq Ahmed | 4baa9a6e8 | 2019-12-20 23:43:46 | [diff] [blame^] | 1448 | EXPECT_TRUE(child2->HasStickyUserActivation()); |
Lan Wei | c927a41 | 2019-05-09 18:37:38 | [diff] [blame] | 1449 | EXPECT_TRUE(child2->HasTransientUserActivation()); |
| 1450 | } |
Lan Wei | 3c2cbad | 2019-03-29 21:53:53 | [diff] [blame] | 1451 | #endif |
| 1452 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 1453 | } // namespace content |