[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 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 5 | #include "base/macros.h" |
| 6 | #include "build/build_config.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 7 | #include "content/browser/frame_host/frame_tree.h" |
| 8 | #include "content/browser/frame_host/frame_tree_node.h" |
| 9 | #include "content/browser/renderer_host/render_view_host_impl.h" |
| 10 | #include "content/browser/web_contents/web_contents_impl.h" |
| 11 | #include "content/public/browser/notification_service.h" |
| 12 | #include "content/public/browser/notification_types.h" |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 13 | #include "content/public/common/content_switches.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 14 | #include "content/public/common/url_constants.h" |
| 15 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 16 | #include "content/public/test/content_browser_test.h" |
| 17 | #include "content/public/test/content_browser_test_utils.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 18 | #include "content/public/test/test_navigation_observer.h" |
| 19 | #include "content/public/test/test_utils.h" |
| 20 | #include "content/shell/browser/shell.h" |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 21 | #include "content/shell/common/shell_switches.h" |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 22 | #include "content/test/content_browser_test_utils_internal.h" |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 23 | #include "content/test/test_frame_navigation_observer.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 24 | #include "net/dns/mock_host_resolver.h" |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 25 | #include "net/test/embedded_test_server/embedded_test_server.h" |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame] | 26 | #include "third_party/WebKit/public/web/WebSandboxFlags.h" |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 27 | #include "url/url_constants.h" |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 28 | |
kjellander | 4badba2 | 2015-03-19 11:47:07 | [diff] [blame] | 29 | // For fine-grained suppression on flaky tests. |
| 30 | #if defined(OS_WIN) |
| 31 | #include "base/win/windows_version.h" |
| 32 | #endif |
| 33 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 34 | namespace content { |
| 35 | |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | std::string GetOriginFromRenderer(FrameTreeNode* node) { |
| 39 | std::string origin; |
| 40 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 41 | node, "window.domAutomationController.send(document.origin);", &origin)); |
| 42 | return origin; |
| 43 | } |
| 44 | |
| 45 | } // namespace |
| 46 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 47 | class FrameTreeBrowserTest : public ContentBrowserTest { |
| 48 | public: |
| 49 | FrameTreeBrowserTest() {} |
| 50 | |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 51 | void SetUpOnMainThread() override { |
| 52 | host_resolver()->AddRule("*", "127.0.0.1"); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 53 | SetupCrossSiteRedirector(embedded_test_server()); |
martijn | 12d2dad | 2016-11-11 10:59:27 | [diff] [blame^] | 54 | ASSERT_TRUE(embedded_test_server()->Start()); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 57 | private: |
| 58 | DISALLOW_COPY_AND_ASSIGN(FrameTreeBrowserTest); |
| 59 | }; |
| 60 | |
| 61 | // Ensures FrameTree correctly reflects page structure during navigations. |
| 62 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeShape) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 63 | GURL base_url = embedded_test_server()->GetURL("A.com", "/site_isolation/"); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 64 | |
| 65 | // Load doc without iframes. Verify FrameTree just has root. |
| 66 | // Frame tree: |
| 67 | // Site-A Root |
| 68 | NavigateToURL(shell(), base_url.Resolve("blank.html")); |
| 69 | FrameTreeNode* root = |
| 70 | static_cast<WebContentsImpl*>(shell()->web_contents())-> |
| 71 | GetFrameTree()->root(); |
| 72 | EXPECT_EQ(0U, root->child_count()); |
| 73 | |
| 74 | // Add 2 same-site frames. Verify 3 nodes in tree with proper names. |
| 75 | // Frame tree: |
| 76 | // Site-A Root -- Site-A frame1 |
| 77 | // \-- Site-A frame2 |
| 78 | WindowedNotificationObserver observer1( |
| 79 | content::NOTIFICATION_LOAD_STOP, |
| 80 | content::Source<NavigationController>( |
| 81 | &shell()->web_contents()->GetController())); |
| 82 | NavigateToURL(shell(), base_url.Resolve("frames-X-X.html")); |
| 83 | observer1.Wait(); |
| 84 | ASSERT_EQ(2U, root->child_count()); |
| 85 | EXPECT_EQ(0U, root->child_at(0)->child_count()); |
| 86 | EXPECT_EQ(0U, root->child_at(1)->child_count()); |
| 87 | } |
| 88 | |
| 89 | // TODO(ajwong): Talk with nasko and merge this functionality with |
| 90 | // FrameTreeShape. |
| 91 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeShape2) { |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 92 | NavigateToURL(shell(), |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 93 | embedded_test_server()->GetURL("/frame_tree/top.html")); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 94 | |
| 95 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 96 | FrameTreeNode* root = wc->GetFrameTree()->root(); |
| 97 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 98 | // Check that the root node is properly created. |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 99 | ASSERT_EQ(3UL, root->child_count()); |
| 100 | EXPECT_EQ(std::string(), root->frame_name()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 101 | |
| 102 | ASSERT_EQ(2UL, root->child_at(0)->child_count()); |
| 103 | EXPECT_STREQ("1-1-name", root->child_at(0)->frame_name().c_str()); |
| 104 | |
| 105 | // Verify the deepest node exists and has the right name. |
| 106 | ASSERT_EQ(2UL, root->child_at(2)->child_count()); |
| 107 | EXPECT_EQ(1UL, root->child_at(2)->child_at(1)->child_count()); |
| 108 | EXPECT_EQ(0UL, root->child_at(2)->child_at(1)->child_at(0)->child_count()); |
[email protected] | b0cd7a95 | 2014-03-08 11:44:06 | [diff] [blame] | 109 | EXPECT_STREQ("3-1-name", |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 110 | root->child_at(2)->child_at(1)->child_at(0)->frame_name().c_str()); |
| 111 | |
| 112 | // Navigate to about:blank, which should leave only the root node of the frame |
| 113 | // tree in the browser process. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 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) { |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 124 | NavigateToURL(shell(), |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 125 | 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(); |
| 129 | RenderFrameHostImpl* rfh = |
| 130 | static_cast<RenderFrameHostImpl*>(rvh->GetMainFrame()); |
| 131 | EXPECT_TRUE(rvh->IsRenderViewLive()); |
| 132 | EXPECT_TRUE(rfh->IsRenderFrameLive()); |
| 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); |
| 138 | NavigateToURL(shell(), GURL(kChromeUICrashURL)); |
| 139 | crash_observer.Wait(); |
| 140 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 141 | // The frame tree should be cleared. |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 142 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 143 | FrameTreeNode* root = wc->GetFrameTree()->root(); |
| 144 | EXPECT_EQ(0UL, root->child_count()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 145 | |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 146 | // Ensure the view and frame aren't live anymore. |
| 147 | EXPECT_FALSE(rvh->IsRenderViewLive()); |
| 148 | EXPECT_FALSE(rfh->IsRenderFrameLive()); |
| 149 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 150 | // Navigate to a new URL. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 151 | GURL url(embedded_test_server()->GetURL("/title1.html")); |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 152 | NavigateToURL(shell(), url); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 153 | EXPECT_EQ(0UL, root->child_count()); |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 154 | EXPECT_EQ(url, root->current_url()); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 155 | |
| 156 | // Ensure the view and frame are live again. |
| 157 | EXPECT_TRUE(rvh->IsRenderViewLive()); |
| 158 | EXPECT_TRUE(rfh->IsRenderFrameLive()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Test that we can navigate away if the previous renderer doesn't clean up its |
| 162 | // child frames. |
pauljensen | 58ee2ea2 | 2015-01-26 17:45:53 | [diff] [blame] | 163 | // Flaky on Mac. http://crbug.com/452018 |
| 164 | #if defined(OS_MACOSX) |
| 165 | #define MAYBE_NavigateWithLeftoverFrames DISABLED_NavigateWithLeftoverFrames |
| 166 | #else |
| 167 | #define MAYBE_NavigateWithLeftoverFrames NavigateWithLeftoverFrames |
| 168 | #endif |
| 169 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, MAYBE_NavigateWithLeftoverFrames) { |
kjellander | 4badba2 | 2015-03-19 11:47:07 | [diff] [blame] | 170 | #if defined(OS_WIN) |
| 171 | // Flaky on XP bot http://crbug.com/468713 |
| 172 | if (base::win::GetVersion() <= base::win::VERSION_XP) |
| 173 | return; |
| 174 | #endif |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 175 | GURL base_url = embedded_test_server()->GetURL("A.com", "/site_isolation/"); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 176 | |
| 177 | NavigateToURL(shell(), |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 178 | embedded_test_server()->GetURL("/frame_tree/top.html")); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 179 | |
| 180 | // Hang the renderer so that it doesn't send any FrameDetached messages. |
| 181 | // (This navigation will never complete, so don't wait for it.) |
| 182 | shell()->LoadURL(GURL(kChromeUIHangURL)); |
| 183 | |
| 184 | // Check that the frame tree still has children. |
| 185 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 186 | FrameTreeNode* root = wc->GetFrameTree()->root(); |
| 187 | ASSERT_EQ(3UL, root->child_count()); |
| 188 | |
| 189 | // Navigate to a new URL. We use LoadURL because NavigateToURL will try to |
| 190 | // wait for the previous navigation to stop. |
| 191 | TestNavigationObserver tab_observer(wc, 1); |
| 192 | shell()->LoadURL(base_url.Resolve("blank.html")); |
| 193 | tab_observer.Wait(); |
| 194 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 195 | // The frame tree should now be cleared. |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 196 | EXPECT_EQ(0UL, root->child_count()); |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 197 | } |
| 198 | |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 199 | // Ensure that IsRenderFrameLive is true for main frames and same-site iframes. |
| 200 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, IsRenderFrameLive) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 201 | GURL main_url(embedded_test_server()->GetURL("/frame_tree/top.html")); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 202 | NavigateToURL(shell(), main_url); |
| 203 | |
| 204 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 205 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 206 | ->GetFrameTree()->root(); |
| 207 | |
| 208 | // The root and subframe should each have a live RenderFrame. |
| 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 | // Load a same-site page into iframe and it should still be live. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 215 | GURL http_url(embedded_test_server()->GetURL("/title1.html")); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 216 | NavigateFrameToURL(root->child_at(0), http_url); |
| 217 | EXPECT_TRUE( |
| 218 | root->current_frame_host()->render_view_host()->IsRenderViewLive()); |
| 219 | EXPECT_TRUE(root->current_frame_host()->IsRenderFrameLive()); |
| 220 | EXPECT_TRUE(root->child_at(0)->current_frame_host()->IsRenderFrameLive()); |
| 221 | } |
| 222 | |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 223 | // Ensure that origins are correctly set on navigations. |
| 224 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, OriginSetOnNavigation) { |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 225 | GURL about_blank(url::kAboutBlankURL); |
| 226 | GURL main_url( |
| 227 | embedded_test_server()->GetURL("a.com", "/frame_tree/top.html")); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 228 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 229 | WebContents* contents = shell()->web_contents(); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 230 | |
| 231 | // 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] | 232 | FrameTreeNode* root = |
| 233 | static_cast<WebContentsImpl*>(contents)->GetFrameTree()->root(); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 234 | |
| 235 | // Extra '/' is added because the replicated origin is serialized in RFC 6454 |
| 236 | // format, which dictates no trailing '/', whereas GURL::GetOrigin does put a |
| 237 | // '/' at the end. |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 238 | EXPECT_EQ(main_url.GetOrigin().spec(), |
| 239 | root->current_origin().Serialize() + '/'); |
| 240 | EXPECT_EQ( |
| 241 | main_url.GetOrigin().spec(), |
| 242 | root->current_frame_host()->GetLastCommittedOrigin().Serialize() + '/'); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 243 | |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 244 | // The iframe is inititially same-origin. |
| 245 | EXPECT_TRUE( |
| 246 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
| 247 | root->child_at(0)->current_frame_host()->GetLastCommittedOrigin())); |
| 248 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
| 249 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize(), |
| 250 | GetOriginFromRenderer(root->child_at(0))); |
| 251 | |
| 252 | // Navigate the iframe cross-origin. |
| 253 | GURL frame_url(embedded_test_server()->GetURL("b.com", "/title1.html")); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 254 | NavigateFrameToURL(root->child_at(0), frame_url); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 255 | EXPECT_EQ(frame_url, root->child_at(0)->current_url()); |
| 256 | EXPECT_EQ(frame_url.GetOrigin().spec(), |
| 257 | root->child_at(0)->current_origin().Serialize() + '/'); |
| 258 | EXPECT_FALSE( |
| 259 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
| 260 | root->child_at(0)->current_frame_host()->GetLastCommittedOrigin())); |
| 261 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
| 262 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize(), |
| 263 | GetOriginFromRenderer(root->child_at(0))); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 264 | |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 265 | // Parent-initiated about:blank navigation should inherit the parent's a.com |
| 266 | // origin. |
| 267 | NavigateIframeToURL(contents, "1-1-id", about_blank); |
| 268 | EXPECT_EQ(about_blank, root->child_at(0)->current_url()); |
| 269 | EXPECT_EQ(main_url.GetOrigin().spec(), |
| 270 | root->child_at(0)->current_origin().Serialize() + '/'); |
| 271 | EXPECT_EQ(root->current_frame_host()->GetLastCommittedOrigin().Serialize(), |
| 272 | root->child_at(0) |
| 273 | ->current_frame_host() |
| 274 | ->GetLastCommittedOrigin() |
| 275 | .Serialize()); |
| 276 | EXPECT_TRUE( |
| 277 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
| 278 | root->child_at(0)->current_frame_host()->GetLastCommittedOrigin())); |
| 279 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
| 280 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize(), |
| 281 | GetOriginFromRenderer(root->child_at(0))); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 282 | |
| 283 | GURL data_url("data:text/html,foo"); |
| 284 | EXPECT_TRUE(NavigateToURL(shell(), data_url)); |
| 285 | |
| 286 | // Navigating to a data URL should set a unique origin. This is represented |
| 287 | // as "null" per RFC 6454. |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 288 | EXPECT_EQ("null", root->current_origin().Serialize()); |
| 289 | EXPECT_TRUE(contents->GetMainFrame()->GetLastCommittedOrigin().unique()); |
| 290 | EXPECT_EQ("null", GetOriginFromRenderer(root)); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 291 | |
| 292 | // Re-navigating to a normal URL should update the origin. |
| 293 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
nick | 1466c84 | 2015-11-25 20:08:06 | [diff] [blame] | 294 | EXPECT_EQ(main_url.GetOrigin().spec(), |
| 295 | root->current_origin().Serialize() + '/'); |
| 296 | EXPECT_EQ( |
| 297 | main_url.GetOrigin().spec(), |
| 298 | contents->GetMainFrame()->GetLastCommittedOrigin().Serialize() + '/'); |
| 299 | EXPECT_FALSE(contents->GetMainFrame()->GetLastCommittedOrigin().unique()); |
| 300 | EXPECT_EQ(root->current_origin().Serialize(), GetOriginFromRenderer(root)); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 301 | } |
| 302 | |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 303 | // Tests a cross-origin navigation to a blob URL. The main frame initiates this |
| 304 | // navigation on its grandchild. It should wind up in the main frame's process. |
| 305 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, NavigateGrandchildToBlob) { |
| 306 | WebContents* contents = shell()->web_contents(); |
| 307 | FrameTreeNode* root = |
| 308 | static_cast<WebContentsImpl*>(contents)->GetFrameTree()->root(); |
| 309 | |
| 310 | // First, snapshot the FrameTree for a normal A(B(A)) case where all frames |
| 311 | // are served over http. The blob test should result in the same structure. |
| 312 | EXPECT_TRUE(NavigateToURL( |
| 313 | shell(), embedded_test_server()->GetURL( |
| 314 | "a.com", "/cross_site_iframe_factory.html?a(b(a))"))); |
| 315 | std::string reference_tree = FrameTreeVisualizer().DepictFrameTree(root); |
| 316 | |
| 317 | GURL main_url(embedded_test_server()->GetURL( |
| 318 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 319 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 320 | |
| 321 | // The root node will initiate the navigation; its grandchild node will be the |
| 322 | // target of the navigation. |
| 323 | FrameTreeNode* target = root->child_at(0)->child_at(0); |
| 324 | |
| 325 | std::string blob_url_string; |
creis | b2d5f1f3 | 2016-11-07 23:25:05 | [diff] [blame] | 326 | RenderFrameDeletedObserver deleted_observer(target->current_frame_host()); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 327 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 328 | root, |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 329 | "function receiveMessage(event) {" |
| 330 | " document.body.appendChild(document.createTextNode(event.data));" |
| 331 | " domAutomationController.send(event.source.location.href);" |
| 332 | "}" |
| 333 | "window.addEventListener('message', receiveMessage, false);" |
| 334 | "var blob = new Blob([" |
| 335 | " '<html><body><div>This is blob content.</div><script>" |
| 336 | " window.parent.parent.postMessage(\"HI\", document.origin);" |
| 337 | " </script></body></html>'], {type: 'text/html'});" |
| 338 | "var blob_url = URL.createObjectURL(blob);" |
| 339 | "frames[0][0].location.href = blob_url;", |
| 340 | &blob_url_string)); |
creis | b2d5f1f3 | 2016-11-07 23:25:05 | [diff] [blame] | 341 | // Wait for the RenderFrame to go away, if this will be cross-process. |
| 342 | if (AreAllSitesIsolatedForTesting()) |
| 343 | deleted_observer.WaitUntilDeleted(); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 344 | EXPECT_EQ(GURL(blob_url_string), target->current_url()); |
| 345 | EXPECT_EQ(url::kBlobScheme, target->current_url().scheme()); |
| 346 | EXPECT_FALSE(target->current_origin().unique()); |
| 347 | EXPECT_EQ("a.com", target->current_origin().host()); |
| 348 | EXPECT_EQ(url::kHttpScheme, target->current_origin().scheme()); |
| 349 | |
| 350 | std::string document_body; |
| 351 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 352 | target, |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 353 | "domAutomationController.send(document.body.children[0].innerHTML);", |
| 354 | &document_body)); |
| 355 | EXPECT_EQ("This is blob content.", document_body); |
| 356 | EXPECT_EQ(reference_tree, FrameTreeVisualizer().DepictFrameTree(root)); |
| 357 | } |
| 358 | |
| 359 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, NavigateChildToAboutBlank) { |
| 360 | GURL main_url(embedded_test_server()->GetURL( |
| 361 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 362 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 363 | WebContentsImpl* contents = |
| 364 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 365 | |
| 366 | // The leaf node (c.com) will be navigated. Its parent node (b.com) will |
| 367 | // initiate the navigation. |
| 368 | FrameTreeNode* target = |
| 369 | contents->GetFrameTree()->root()->child_at(0)->child_at(0); |
| 370 | FrameTreeNode* initiator = target->parent(); |
| 371 | |
| 372 | // Give the target a name. |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 373 | EXPECT_TRUE(ExecuteScript(target, "window.name = 'target';")); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 374 | |
| 375 | // Use window.open(about:blank), then poll the document for access. |
| 376 | std::string about_blank_origin; |
| 377 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 378 | initiator, |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 379 | "var didNavigate = false;" |
| 380 | "var intervalID = setInterval(function() {" |
| 381 | " if (!didNavigate) {" |
| 382 | " didNavigate = true;" |
| 383 | " window.open('about:blank', 'target');" |
| 384 | " }" |
| 385 | " // Poll the document until it doesn't throw a SecurityError.\n" |
| 386 | " try {" |
| 387 | " frames[0].document.write('Hi from ' + document.domain);" |
| 388 | " } catch (e) { return; }" |
| 389 | " clearInterval(intervalID);" |
| 390 | " domAutomationController.send(frames[0].document.origin);" |
| 391 | "}, 16);", |
| 392 | &about_blank_origin)); |
| 393 | EXPECT_EQ(GURL(url::kAboutBlankURL), target->current_url()); |
| 394 | EXPECT_EQ(url::kAboutScheme, target->current_url().scheme()); |
| 395 | EXPECT_FALSE(target->current_origin().unique()); |
| 396 | EXPECT_EQ("b.com", target->current_origin().host()); |
| 397 | EXPECT_EQ(url::kHttpScheme, target->current_origin().scheme()); |
| 398 | EXPECT_EQ(target->current_origin().Serialize(), about_blank_origin); |
| 399 | |
| 400 | std::string document_body; |
| 401 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 402 | target, "domAutomationController.send(document.body.innerHTML);", |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 403 | &document_body)); |
| 404 | EXPECT_EQ("Hi from b.com", document_body); |
| 405 | } |
| 406 | |
| 407 | // Nested iframes, three origins: A(B(C)). Frame A navigates C to about:blank |
| 408 | // (via window.open). This should wind up in A's origin per the spec. Test fails |
| 409 | // because of http://crbug.com/564292 |
| 410 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, |
| 411 | DISABLED_NavigateGrandchildToAboutBlank) { |
| 412 | GURL main_url(embedded_test_server()->GetURL( |
| 413 | "a.com", "/cross_site_iframe_factory.html?a(b(c))")); |
| 414 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 415 | WebContentsImpl* contents = |
| 416 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 417 | |
| 418 | // The leaf node (c.com) will be navigated. Its grandparent node (a.com) will |
| 419 | // initiate the navigation. |
| 420 | FrameTreeNode* target = |
| 421 | contents->GetFrameTree()->root()->child_at(0)->child_at(0); |
| 422 | FrameTreeNode* initiator = target->parent()->parent(); |
| 423 | |
| 424 | // Give the target a name. |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 425 | EXPECT_TRUE(ExecuteScript(target, "window.name = 'target';")); |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 426 | |
| 427 | // Use window.open(about:blank), then poll the document for access. |
| 428 | std::string about_blank_origin; |
| 429 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 430 | initiator, |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 431 | "var didNavigate = false;" |
| 432 | "var intervalID = setInterval(function() {" |
| 433 | " if (!didNavigate) {" |
| 434 | " didNavigate = true;" |
| 435 | " window.open('about:blank', 'target');" |
| 436 | " }" |
| 437 | " // May raise a SecurityError, that's expected.\n" |
| 438 | " frames[0][0].document.write('Hi from ' + document.domain);" |
| 439 | " clearInterval(intervalID);" |
| 440 | " domAutomationController.send(frames[0][0].document.origin);" |
| 441 | "}, 16);", |
| 442 | &about_blank_origin)); |
| 443 | EXPECT_EQ(GURL(url::kAboutBlankURL), target->current_url()); |
| 444 | EXPECT_EQ(url::kAboutScheme, target->current_url().scheme()); |
| 445 | EXPECT_FALSE(target->current_origin().unique()); |
| 446 | EXPECT_EQ("a.com", target->current_origin().host()); |
| 447 | EXPECT_EQ(url::kHttpScheme, target->current_origin().scheme()); |
| 448 | EXPECT_EQ(target->current_origin().Serialize(), about_blank_origin); |
| 449 | |
| 450 | std::string document_body; |
| 451 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 452 | target, "domAutomationController.send(document.body.innerHTML);", |
nick | 9f34e289 | 2016-01-12 21:01:07 | [diff] [blame] | 453 | &document_body)); |
| 454 | EXPECT_EQ("Hi from a.com", document_body); |
| 455 | } |
| 456 | |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 457 | // Ensures that iframe with srcdoc is always put in the same origin as its |
| 458 | // parent frame. |
| 459 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, ChildFrameWithSrcdoc) { |
| 460 | GURL main_url(embedded_test_server()->GetURL( |
| 461 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 462 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 463 | WebContentsImpl* contents = |
| 464 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 465 | FrameTreeNode* root = contents->GetFrameTree()->root(); |
| 466 | EXPECT_EQ(1U, root->child_count()); |
| 467 | |
| 468 | FrameTreeNode* child = root->child_at(0); |
| 469 | std::string frame_origin; |
| 470 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 471 | child, "domAutomationController.send(document.origin);", &frame_origin)); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 472 | EXPECT_TRUE( |
| 473 | child->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
| 474 | url::Origin(GURL(frame_origin)))); |
| 475 | EXPECT_FALSE( |
| 476 | root->current_frame_host()->GetLastCommittedOrigin().IsSameOriginWith( |
| 477 | url::Origin(GURL(frame_origin)))); |
| 478 | |
| 479 | // Create a new iframe with srcdoc and add it to the main frame. It should |
| 480 | // be created in the same SiteInstance as the parent. |
| 481 | { |
| 482 | std::string script("var f = document.createElement('iframe');" |
| 483 | "f.srcdoc = 'some content';" |
| 484 | "document.body.appendChild(f)"); |
| 485 | TestNavigationObserver observer(shell()->web_contents()); |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 486 | EXPECT_TRUE(ExecuteScript(root, script)); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 487 | EXPECT_EQ(2U, root->child_count()); |
| 488 | observer.Wait(); |
| 489 | |
| 490 | EXPECT_EQ(GURL(url::kAboutBlankURL), root->child_at(1)->current_url()); |
| 491 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 492 | root->child_at(1), "domAutomationController.send(document.origin);", |
| 493 | &frame_origin)); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 494 | EXPECT_EQ(root->current_frame_host()->GetLastCommittedURL().GetOrigin(), |
| 495 | GURL(frame_origin)); |
| 496 | EXPECT_NE(child->current_frame_host()->GetLastCommittedURL().GetOrigin(), |
| 497 | GURL(frame_origin)); |
| 498 | } |
| 499 | |
| 500 | // Set srcdoc on the existing cross-site frame. It should navigate the frame |
| 501 | // back to the origin of the parent. |
| 502 | { |
| 503 | std::string script("var f = document.getElementById('child-0');" |
| 504 | "f.srcdoc = 'some content';"); |
| 505 | TestNavigationObserver observer(shell()->web_contents()); |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 506 | EXPECT_TRUE(ExecuteScript(root, script)); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 507 | observer.Wait(); |
| 508 | |
| 509 | EXPECT_EQ(GURL(url::kAboutBlankURL), child->current_url()); |
| 510 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 511 | child, "domAutomationController.send(document.origin);", |
| 512 | &frame_origin)); |
nasko | faa01fb | 2016-04-30 01:04:17 | [diff] [blame] | 513 | EXPECT_EQ(root->current_frame_host()->GetLastCommittedURL().GetOrigin(), |
| 514 | GURL(frame_origin)); |
| 515 | } |
| 516 | } |
| 517 | |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 518 | // Ensure that sandbox flags are correctly set when child frames are created. |
| 519 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, SandboxFlagsSetForChildFrames) { |
| 520 | GURL main_url(embedded_test_server()->GetURL("/sandboxed_frames.html")); |
| 521 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 522 | |
| 523 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 524 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 525 | ->GetFrameTree()->root(); |
| 526 | |
| 527 | // Verify that sandbox flags are set properly for all FrameTreeNodes. |
| 528 | // First frame is completely sandboxed; second frame uses "allow-scripts", |
| 529 | // which resets both SandboxFlags::Scripts and |
| 530 | // SandboxFlags::AutomaticFeatures bits per blink::parseSandboxPolicy(), and |
| 531 | // third frame has "allow-scripts allow-same-origin". |
alexmos | 6e94010 | 2016-01-19 22:47:25 | [diff] [blame] | 532 | EXPECT_EQ(blink::WebSandboxFlags::None, root->effective_sandbox_flags()); |
| 533 | EXPECT_EQ(blink::WebSandboxFlags::All, |
| 534 | root->child_at(0)->effective_sandbox_flags()); |
| 535 | EXPECT_EQ(blink::WebSandboxFlags::All & ~blink::WebSandboxFlags::Scripts & |
| 536 | ~blink::WebSandboxFlags::AutomaticFeatures, |
| 537 | root->child_at(1)->effective_sandbox_flags()); |
| 538 | EXPECT_EQ(blink::WebSandboxFlags::All & ~blink::WebSandboxFlags::Scripts & |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame] | 539 | ~blink::WebSandboxFlags::AutomaticFeatures & |
alexmos | 6e94010 | 2016-01-19 22:47:25 | [diff] [blame] | 540 | ~blink::WebSandboxFlags::Origin, |
| 541 | root->child_at(2)->effective_sandbox_flags()); |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 542 | |
| 543 | // Sandboxed frames should set a unique origin unless they have the |
| 544 | // "allow-same-origin" directive. |
alexmos | 6e94010 | 2016-01-19 22:47:25 | [diff] [blame] | 545 | EXPECT_EQ("null", root->child_at(0)->current_origin().Serialize()); |
| 546 | EXPECT_EQ("null", root->child_at(1)->current_origin().Serialize()); |
| 547 | EXPECT_EQ(main_url.GetOrigin().spec(), |
| 548 | root->child_at(2)->current_origin().Serialize() + "/"); |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 549 | |
| 550 | // Navigating to a different URL should not clear sandbox flags. |
| 551 | GURL frame_url(embedded_test_server()->GetURL("/title1.html")); |
| 552 | NavigateFrameToURL(root->child_at(0), frame_url); |
alexmos | 6e94010 | 2016-01-19 22:47:25 | [diff] [blame] | 553 | EXPECT_EQ(blink::WebSandboxFlags::All, |
| 554 | root->child_at(0)->effective_sandbox_flags()); |
alexmos | f832a2f | 2015-01-27 22:44:03 | [diff] [blame] | 555 | } |
| 556 | |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 557 | // Ensure that a popup opened from a subframe sets its opener to the subframe's |
| 558 | // FrameTreeNode, and that the opener is cleared if the subframe is destroyed. |
| 559 | IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, SubframeOpenerSetForNewWindow) { |
| 560 | GURL main_url(embedded_test_server()->GetURL("/frame_tree/top.html")); |
| 561 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 562 | |
| 563 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 564 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 565 | ->GetFrameTree() |
| 566 | ->root(); |
| 567 | |
| 568 | // Open a new window from a subframe. |
| 569 | ShellAddedObserver new_shell_observer; |
| 570 | GURL popup_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 571 | EXPECT_TRUE(ExecuteScript(root->child_at(0), |
alexmos | e201c7cd | 2015-06-10 17:14:21 | [diff] [blame] | 572 | "window.open('" + popup_url.spec() + "');")); |
| 573 | Shell* new_shell = new_shell_observer.GetShell(); |
| 574 | WebContents* new_contents = new_shell->web_contents(); |
| 575 | WaitForLoadStop(new_contents); |
| 576 | |
| 577 | // Check that the new window's opener points to the correct subframe on |
| 578 | // original window. |
| 579 | FrameTreeNode* popup_root = |
| 580 | static_cast<WebContentsImpl*>(new_contents)->GetFrameTree()->root(); |
| 581 | EXPECT_EQ(root->child_at(0), popup_root->opener()); |
| 582 | |
| 583 | // Close the original window. This should clear the new window's opener. |
| 584 | shell()->Close(); |
| 585 | EXPECT_EQ(nullptr, popup_root->opener()); |
| 586 | } |
| 587 | |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 588 | class CrossProcessFrameTreeBrowserTest : public ContentBrowserTest { |
| 589 | public: |
| 590 | CrossProcessFrameTreeBrowserTest() {} |
| 591 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 592 | void SetUpCommandLine(base::CommandLine* command_line) override { |
nick | d30fd96 | 2015-07-27 21:51:08 | [diff] [blame] | 593 | IsolateAllSitesForTesting(command_line); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 594 | } |
| 595 | |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 596 | void SetUpOnMainThread() override { |
| 597 | host_resolver()->AddRule("*", "127.0.0.1"); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 598 | SetupCrossSiteRedirector(embedded_test_server()); |
martijn | 12d2dad | 2016-11-11 10:59:27 | [diff] [blame^] | 599 | ASSERT_TRUE(embedded_test_server()->Start()); |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 600 | } |
| 601 | |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 602 | private: |
| 603 | DISALLOW_COPY_AND_ASSIGN(CrossProcessFrameTreeBrowserTest); |
| 604 | }; |
| 605 | |
| 606 | // Ensure that we can complete a cross-process subframe navigation. |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 607 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
nasko | 83fe5dc | 2015-07-01 16:34:19 | [diff] [blame] | 608 | CreateCrossProcessSubframeProxies) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 609 | GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html")); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 610 | NavigateToURL(shell(), main_url); |
| 611 | |
| 612 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 613 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 614 | ->GetFrameTree()->root(); |
| 615 | |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 616 | // There should not be a proxy for the root's own SiteInstance. |
| 617 | SiteInstance* root_instance = root->current_frame_host()->GetSiteInstance(); |
| 618 | EXPECT_FALSE(root->render_manager()->GetRenderFrameProxyHost(root_instance)); |
| 619 | |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 620 | // Load same-site page into iframe. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 621 | GURL http_url(embedded_test_server()->GetURL("/title1.html")); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 622 | NavigateFrameToURL(root->child_at(0), http_url); |
| 623 | |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 624 | // Load cross-site page into iframe. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 625 | GURL cross_site_url( |
| 626 | embedded_test_server()->GetURL("foo.com", "/title2.html")); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 627 | NavigateFrameToURL(root->child_at(0), cross_site_url); |
| 628 | |
| 629 | // Ensure that we have created a new process for the subframe. |
nasko | e6edde3 | 2014-10-17 15:36:48 | [diff] [blame] | 630 | ASSERT_EQ(2U, root->child_count()); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 631 | FrameTreeNode* child = root->child_at(0); |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 632 | SiteInstance* child_instance = child->current_frame_host()->GetSiteInstance(); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 633 | RenderViewHost* rvh = child->current_frame_host()->render_view_host(); |
| 634 | RenderProcessHost* rph = child->current_frame_host()->GetProcess(); |
| 635 | |
| 636 | EXPECT_NE(shell()->web_contents()->GetRenderViewHost(), rvh); |
[email protected] | 14522ce4 | 2014-08-08 18:56:16 | [diff] [blame] | 637 | EXPECT_NE(shell()->web_contents()->GetSiteInstance(), child_instance); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 638 | EXPECT_NE(shell()->web_contents()->GetRenderProcessHost(), rph); |
| 639 | |
| 640 | // 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] | 641 | EXPECT_TRUE(root->render_manager()->GetRenderFrameProxyHost(child_instance)); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 642 | |
| 643 | // 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] | 644 | EXPECT_TRUE(child->render_manager()->GetRenderFrameProxyHost(root_instance)); |
| 645 | |
| 646 | // The nodes should not have proxies for their own SiteInstance. |
| 647 | EXPECT_FALSE(root->render_manager()->GetRenderFrameProxyHost(root_instance)); |
| 648 | EXPECT_FALSE( |
| 649 | child->render_manager()->GetRenderFrameProxyHost(child_instance)); |
creis | e42f2a5 | 2014-09-18 18:14:57 | [diff] [blame] | 650 | |
| 651 | // Ensure that the RenderViews and RenderFrames are all live. |
| 652 | EXPECT_TRUE( |
| 653 | root->current_frame_host()->render_view_host()->IsRenderViewLive()); |
| 654 | EXPECT_TRUE( |
| 655 | child->current_frame_host()->render_view_host()->IsRenderViewLive()); |
| 656 | EXPECT_TRUE(root->current_frame_host()->IsRenderFrameLive()); |
| 657 | EXPECT_TRUE(root->child_at(0)->current_frame_host()->IsRenderFrameLive()); |
[email protected] | 82307f6b | 2014-08-07 03:30:12 | [diff] [blame] | 658 | } |
| 659 | |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 660 | IN_PROC_BROWSER_TEST_F(CrossProcessFrameTreeBrowserTest, |
| 661 | OriginSetOnCrossProcessNavigations) { |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 662 | GURL main_url(embedded_test_server()->GetURL("/site_per_process_main.html")); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 663 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 664 | |
| 665 | // It is safe to obtain the root frame tree node here, as it doesn't change. |
| 666 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 667 | ->GetFrameTree()->root(); |
| 668 | |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 669 | EXPECT_EQ(root->current_origin().Serialize() + '/', |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 670 | main_url.GetOrigin().spec()); |
| 671 | |
| 672 | // First frame is an about:blank frame. Check that its origin is correctly |
| 673 | // inherited from the parent. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 674 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize() + '/', |
| 675 | main_url.GetOrigin().spec()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 676 | |
| 677 | // Second frame loads a same-site page. Its origin should also be the same |
| 678 | // as the parent. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 679 | EXPECT_EQ(root->child_at(1)->current_origin().Serialize() + '/', |
| 680 | main_url.GetOrigin().spec()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 681 | |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 682 | // Load cross-site page into the first frame. |
alexmos | 478dcbb | 2014-12-10 21:24:46 | [diff] [blame] | 683 | GURL cross_site_url( |
| 684 | embedded_test_server()->GetURL("foo.com", "/title2.html")); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 685 | NavigateFrameToURL(root->child_at(0), cross_site_url); |
| 686 | |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 687 | EXPECT_EQ(root->child_at(0)->current_origin().Serialize() + '/', |
| 688 | cross_site_url.GetOrigin().spec()); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 689 | |
| 690 | // The root's origin shouldn't have changed. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 691 | EXPECT_EQ(root->current_origin().Serialize() + '/', |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 692 | main_url.GetOrigin().spec()); |
| 693 | |
| 694 | GURL data_url("data:text/html,foo"); |
| 695 | NavigateFrameToURL(root->child_at(1), data_url); |
| 696 | |
| 697 | // Navigating to a data URL should set a unique origin. This is represented |
| 698 | // as "null" per RFC 6454. |
creis | abdd2bd6 | 2015-11-21 01:14:58 | [diff] [blame] | 699 | EXPECT_EQ(root->child_at(1)->current_origin().Serialize(), "null"); |
alexmos | bc7eafa | 2014-12-06 01:38:09 | [diff] [blame] | 700 | } |
| 701 | |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 702 | // FrameTreeBrowserTest variant where we isolate http://*.is, Iceland's top |
| 703 | // level domain. This is an analogue to --isolate-extensions that we use inside |
| 704 | // of content_browsertests, where extensions don't exist. Iceland, like an |
| 705 | // extension process, is a special place with magical powers; we want to protect |
| 706 | // it from outsiders. |
| 707 | class IsolateIcelandFrameTreeBrowserTest : public ContentBrowserTest { |
| 708 | public: |
| 709 | IsolateIcelandFrameTreeBrowserTest() {} |
| 710 | |
| 711 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 712 | command_line->AppendSwitchASCII(switches::kIsolateSitesForTesting, "*.is"); |
| 713 | } |
| 714 | |
| 715 | void SetUpOnMainThread() override { |
| 716 | host_resolver()->AddRule("*", "127.0.0.1"); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 717 | SetupCrossSiteRedirector(embedded_test_server()); |
martijn | 12d2dad | 2016-11-11 10:59:27 | [diff] [blame^] | 718 | ASSERT_TRUE(embedded_test_server()->Start()); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | private: |
| 722 | DISALLOW_COPY_AND_ASSIGN(IsolateIcelandFrameTreeBrowserTest); |
| 723 | }; |
| 724 | |
| 725 | // Regression test for https://crbug.com/644966 |
| 726 | IN_PROC_BROWSER_TEST_F(IsolateIcelandFrameTreeBrowserTest, |
| 727 | ProcessSwitchForIsolatedBlob) { |
| 728 | // blink suppresses navigations to blob URLs of origins different from the |
| 729 | // frame initiating the navigation. We disable those checks for this test, to |
| 730 | // test what happens in a compromise scenario. |
| 731 | base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 732 | switches::kDisableWebSecurity); |
| 733 | |
| 734 | // Set up an iframe. |
| 735 | WebContents* contents = shell()->web_contents(); |
| 736 | FrameTreeNode* root = |
| 737 | static_cast<WebContentsImpl*>(contents)->GetFrameTree()->root(); |
| 738 | GURL main_url(embedded_test_server()->GetURL( |
nick | 07fd7e1 | 2016-09-12 18:54:06 | [diff] [blame] | 739 | "a.com", "/cross_site_iframe_factory.html?a(a)")); |
nick | db193a1 | 2016-09-09 23:09:23 | [diff] [blame] | 740 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 741 | |
| 742 | // The navigation targets an invalid blob url; that's intentional to trigger |
| 743 | // an error response. The response should commit in a process dedicated to |
| 744 | // http://b.is. |
| 745 | std::string result; |
| 746 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 747 | root, |
| 748 | "var iframe_element = document.getElementsByTagName('iframe')[0];" |
| 749 | "iframe_element.onload = () => {" |
| 750 | " domAutomationController.send('done');" |
| 751 | "};" |
| 752 | "iframe_element.src = 'blob:http://b.is:2932/';", |
| 753 | &result)); |
| 754 | WaitForLoadStop(contents); |
| 755 | |
| 756 | // Make sure we did a process transfer back to "b.is". |
| 757 | EXPECT_EQ( |
| 758 | " Site A ------------ proxies for B\n" |
| 759 | " +--Site B ------- proxies for A\n" |
| 760 | "Where A = http://a.com/\n" |
| 761 | " B = http://b.is/", |
| 762 | FrameTreeVisualizer().DepictFrameTree(root)); |
| 763 | } |
| 764 | |
[email protected] | 5f96f5a6 | 2014-01-10 00:05:11 | [diff] [blame] | 765 | } // namespace content |