Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: content/browser/site_per_process_browsertest.cc

Issue 1957783002: Replicate Content-Security-Policy into remote frame proxies. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebasing + small self-review tweaks (comments + assert->dcheck). Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/site_per_process_browsertest.h" 5 #include "content/browser/site_per_process_browsertest.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 #include "content/public/common/url_constants.h" 48 #include "content/public/common/url_constants.h"
49 #include "content/public/test/browser_test_utils.h" 49 #include "content/public/test/browser_test_utils.h"
50 #include "content/public/test/content_browser_test_utils.h" 50 #include "content/public/test/content_browser_test_utils.h"
51 #include "content/public/test/test_navigation_observer.h" 51 #include "content/public/test/test_navigation_observer.h"
52 #include "content/public/test/test_utils.h" 52 #include "content/public/test/test_utils.h"
53 #include "content/test/content_browser_test_utils_internal.h" 53 #include "content/test/content_browser_test_utils_internal.h"
54 #include "content/test/test_frame_navigation_observer.h" 54 #include "content/test/test_frame_navigation_observer.h"
55 #include "ipc/ipc_security_test_util.h" 55 #include "ipc/ipc_security_test_util.h"
56 #include "net/dns/mock_host_resolver.h" 56 #include "net/dns/mock_host_resolver.h"
57 #include "net/test/embedded_test_server/embedded_test_server.h" 57 #include "net/test/embedded_test_server/embedded_test_server.h"
58 #include "testing/gtest/include/gtest/gtest.h"
58 #include "third_party/WebKit/public/web/WebInputEvent.h" 59 #include "third_party/WebKit/public/web/WebInputEvent.h"
59 #include "third_party/WebKit/public/web/WebSandboxFlags.h" 60 #include "third_party/WebKit/public/web/WebSandboxFlags.h"
60 #include "ui/display/display_switches.h" 61 #include "ui/display/display_switches.h"
61 #include "ui/events/event.h" 62 #include "ui/events/event.h"
62 #include "ui/events/event_utils.h" 63 #include "ui/events/event_utils.h"
63 #include "ui/gfx/geometry/point.h" 64 #include "ui/gfx/geometry/point.h"
64 65
65 #if defined(USE_AURA) 66 #if defined(USE_AURA)
66 #include "content/browser/renderer_host/render_widget_host_view_aura.h" 67 #include "content/browser/renderer_host/render_widget_host_view_aura.h"
67 #endif 68 #endif
(...skipping 6257 matching lines...) Expand 10 before | Expand all | Expand 10 after
6325 // the blocked page is seen as cross-origin. However, those flags shouldn't 6326 // the blocked page is seen as cross-origin. However, those flags shouldn't
6326 // affect future navigations for a frame. Verify this for the above 6327 // affect future navigations for a frame. Verify this for the above
6327 // navigation. 6328 // navigation.
6328 EXPECT_EQ(c_url.GetOrigin().spec(), 6329 EXPECT_EQ(c_url.GetOrigin().spec(),
6329 root->child_at(0)->current_origin().Serialize() + "/"); 6330 root->child_at(0)->current_origin().Serialize() + "/");
6330 EXPECT_EQ(blink::WebSandboxFlags::None, 6331 EXPECT_EQ(blink::WebSandboxFlags::None,
6331 root->child_at(0)->effective_sandbox_flags()); 6332 root->child_at(0)->effective_sandbox_flags());
6332 } 6333 }
6333 } 6334 }
6334 6335
6336 // Test that a cross-origin frame's navigation can be blocked by CSP frame-src.
6337 // In this version of a test, CSP comes from HTTP headers.
6338 IN_PROC_BROWSER_TEST_F(SitePerProcessBrowserTest,
6339 CrossSiteIframeBlockedByParentCSPFromHeaders) {
6340 GURL main_url(
6341 embedded_test_server()->GetURL("a.com", "/frame-src-self-and-b.html"));
6342 EXPECT_TRUE(NavigateToURL(shell(), main_url));
6343
6344 FrameTreeNode* root = web_contents()->GetFrameTree()->root();
6345
6346 // Sanity-check that the test page has the expected shape for testing.
6347 EXPECT_FALSE(root->child_at(0)->HasSameOrigin(*root));
6348 EXPECT_EQ(embedded_test_server()->GetURL("b.com", "/title2.html"),
6349 root->child_at(0)->current_url());
alexmos 2016/05/16 22:31:56 Perhaps we could also check root->current_replicat
Łukasz Anforowicz 2016/05/16 23:12:51 Done. This sounds like a good idea. I guess one
alexmos 2016/05/16 23:38:56 Thanks! One final thought I just had is that it m
Łukasz Anforowicz 2016/05/17 00:19:43 Done (here and for the srcdoc test).
6350
6351 // Monitor subframe's load events via main frame's title.
6352 EXPECT_TRUE(ExecuteScript(shell()->web_contents(),
6353 "document.querySelector('iframe').onload = "
6354 " function() { document.title = 'loaded'; };"));
6355 EXPECT_TRUE(
6356 ExecuteScript(shell()->web_contents(), "document.title = 'not loaded';"));
6357 base::string16 expected_title(base::UTF8ToUTF16("loaded"));
6358 TitleWatcher title_watcher(shell()->web_contents(), expected_title);
6359
6360 // Try to navigate the subframe to a blocked URL.
6361 TestNavigationObserver load_observer(shell()->web_contents());
6362 GURL blocked_url = embedded_test_server()->GetURL("c.com", "/title3.html");
6363 EXPECT_TRUE(
6364 ExecuteScript(root->child_at(0)->current_frame_host(),
6365 "window.location.href = '" + blocked_url.spec() + "';"));
6366
6367 // The blocked frame should still fire a load event in its parent's process.
6368 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
6369
6370 // Check that the current RenderFrameHost has stopped loading.
6371 if (root->child_at(0)->current_frame_host()->is_loading()) {
6372 ADD_FAILURE() << "Blocked RenderFrameHost shouldn't be loading anything";
6373 load_observer.Wait();
6374 }
6375
6376 // The blocked frame should stay at the old location.
6377 EXPECT_EQ(embedded_test_server()->GetURL("b.com", "/title2.html"),
6378 root->child_at(0)->current_url());
6379
6380 // The blocked frame should keep the old title.
6381 std::string frame_title;
6382 EXPECT_TRUE(ExecuteScriptAndExtractString(
6383 root->child_at(0)->current_frame_host(),
6384 "domAutomationController.send(document.title)", &frame_title));
6385 EXPECT_EQ("Title Of Awesomeness", frame_title);
6386 }
6387
6388 // Test that a cross-origin frame's navigation can be blocked by CSP frame-src.
6389 // In this version of a test, CSP comes from a <meta> element added after the
6390 // page has already loaded.
6391 IN_PROC_BROWSER_TEST_F(SitePerProcessBrowserTest,
6392 CrossSiteIframeBlockedByParentCSPFromMeta) {
6393 GURL main_url(embedded_test_server()->GetURL(
6394 "a.com", "/cross_site_iframe_factory.html?a(a)"));
6395 EXPECT_TRUE(NavigateToURL(shell(), main_url));
6396
6397 FrameTreeNode* root = web_contents()->GetFrameTree()->root();
6398
6399 // Navigate the subframe to a location we will disallow in the future.
6400 NavigateFrameToURL(root->child_at(0),
6401 embedded_test_server()->GetURL("b.com", "/title2.html"));
alexmos 2016/05/16 22:31:56 nit: consider defining the b.com URL in a var and
Łukasz Anforowicz 2016/05/16 23:12:51 Done.
6402
6403 // Add frame-src CSP via a new <meta> element.
6404 EXPECT_TRUE(ExecuteScript(
6405 shell()->web_contents(),
6406 "var meta = document.createElement('meta');"
6407 "meta.httpEquiv = 'Content-Security-Policy';"
6408 "meta.content = 'frame-src https://a.com:*';"
6409 "document.getElementsByTagName('head')[0].appendChild(meta);"));
6410
6411 // Sanity-check that the test page has the expected shape for testing.
6412 // (the CSP should not have an effect on the already loaded frames).
6413 EXPECT_FALSE(root->child_at(0)->HasSameOrigin(*root));
6414 EXPECT_EQ(embedded_test_server()->GetURL("b.com", "/title2.html"),
6415 root->child_at(0)->current_url());
6416
6417 // Monitor subframe's load events via main frame's title.
6418 EXPECT_TRUE(ExecuteScript(shell()->web_contents(),
6419 "document.querySelector('iframe').onload = "
6420 " function() { document.title = 'loaded'; };"));
6421 EXPECT_TRUE(
6422 ExecuteScript(shell()->web_contents(), "document.title = 'not loaded';"));
6423 base::string16 expected_title(base::UTF8ToUTF16("loaded"));
6424 TitleWatcher title_watcher(shell()->web_contents(), expected_title);
6425
6426 // Try to navigate the subframe to a blocked URL.
6427 TestNavigationObserver load_observer2(shell()->web_contents());
6428 GURL blocked_url = embedded_test_server()->GetURL("c.com", "/title3.html");
6429 EXPECT_TRUE(
6430 ExecuteScript(root->child_at(0)->current_frame_host(),
6431 "window.location.href = '" + blocked_url.spec() + "';"));
6432
6433 // The blocked frame should still fire a load event in its parent's process.
6434 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
6435
6436 // Check that the current RenderFrameHost has stopped loading.
6437 if (root->child_at(0)->current_frame_host()->is_loading()) {
6438 ADD_FAILURE() << "Blocked RenderFrameHost shouldn't be loading anything";
6439 load_observer2.Wait();
6440 }
6441
6442 // The blocked frame should stay at the old location.
6443 EXPECT_EQ(embedded_test_server()->GetURL("b.com", "/title2.html"),
6444 root->child_at(0)->current_url());
6445
6446 // The blocked frame should keep the old title.
6447 std::string frame_title;
6448 EXPECT_TRUE(ExecuteScriptAndExtractString(
6449 root->child_at(0)->current_frame_host(),
6450 "domAutomationController.send(document.title)", &frame_title));
6451 EXPECT_EQ("Title Of Awesomeness", frame_title);
6452 }
6453
6454 // Test that a cross-origin frame's navigation can be blocked by CSP frame-src.
6455 // In this version of a test, CSP is inherited by srcdoc iframe from a parent
6456 // that declared CSP via HTTP headers. Cross-origin frame navigating to a
6457 // blocked location is a child of the srcdoc iframe.
6458 IN_PROC_BROWSER_TEST_F(SitePerProcessBrowserTest,
6459 CrossSiteIframeBlockedByCSPInheritedBySrcDocParent) {
6460 GURL main_url(
6461 embedded_test_server()->GetURL("a.com", "/frame-src-self-and-b.html"));
6462 EXPECT_TRUE(NavigateToURL(shell(), main_url));
6463
6464 FrameTreeNode* root = web_contents()->GetFrameTree()->root();
6465 FrameTreeNode* srcdoc_frame = root->child_at(1);
6466 EXPECT_TRUE(srcdoc_frame != nullptr);
6467 FrameTreeNode* navigating_frame = srcdoc_frame->child_at(0);
6468 EXPECT_TRUE(navigating_frame != nullptr);
6469
6470 // Sanity-check that the test page has the expected shape for testing.
6471 // (the CSP should not have an effect on the already loaded frames).
6472 EXPECT_TRUE(srcdoc_frame->HasSameOrigin(*root));
6473 EXPECT_FALSE(srcdoc_frame->HasSameOrigin(*navigating_frame));
6474 EXPECT_EQ(embedded_test_server()->GetURL("b.com", "/title2.html"),
6475 navigating_frame->current_url());
6476
6477 // Monitor navigating_frame's load events via srcdoc_frame posting
6478 // a message to the parent frame.
6479 EXPECT_TRUE(
6480 ExecuteScript(root->current_frame_host(),
6481 "window.addEventListener('message', function(event) {"
6482 " document.title = event.data;"
6483 "});"));
6484 EXPECT_TRUE(ExecuteScript(
6485 srcdoc_frame->current_frame_host(),
6486 "document.querySelector('iframe').onload = "
6487 " function() { window.top.postMessage('loaded', '*'); };"));
6488 EXPECT_TRUE(
6489 ExecuteScript(shell()->web_contents(), "document.title = 'not loaded';"));
6490 base::string16 expected_title(base::UTF8ToUTF16("loaded"));
6491 TitleWatcher title_watcher(shell()->web_contents(), expected_title);
6492
6493 // Try to navigate the subframe to a blocked URL.
6494 TestNavigationObserver load_observer2(shell()->web_contents());
6495 GURL blocked_url = embedded_test_server()->GetURL("c.com", "/title3.html");
6496 EXPECT_TRUE(
6497 ExecuteScript(navigating_frame->current_frame_host(),
6498 "window.location.href = '" + blocked_url.spec() + "';"));
6499
6500 // The blocked frame should still fire a load event in its parent's process.
6501 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
6502
6503 // Check that the current RenderFrameHost has stopped loading.
6504 if (navigating_frame->current_frame_host()->is_loading()) {
6505 ADD_FAILURE() << "Blocked RenderFrameHost shouldn't be loading anything";
6506 load_observer2.Wait();
6507 }
6508
6509 // The blocked frame should stay at the old location.
6510 EXPECT_EQ(embedded_test_server()->GetURL("b.com", "/title2.html"),
6511 navigating_frame->current_url());
6512
6513 // The blocked frame should keep the old title.
6514 std::string frame_title;
6515 EXPECT_TRUE(ExecuteScriptAndExtractString(
6516 navigating_frame->current_frame_host(),
6517 "domAutomationController.send(document.title)", &frame_title));
6518 EXPECT_EQ("Title Of Awesomeness", frame_title);
6519 }
6520
6335 IN_PROC_BROWSER_TEST_F(SitePerProcessBrowserTest, ScreenCoordinates) { 6521 IN_PROC_BROWSER_TEST_F(SitePerProcessBrowserTest, ScreenCoordinates) {
6336 GURL main_url(embedded_test_server()->GetURL( 6522 GURL main_url(embedded_test_server()->GetURL(
6337 "a.com", "/cross_site_iframe_factory.html?a(b)")); 6523 "a.com", "/cross_site_iframe_factory.html?a(b)"));
6338 NavigateToURL(shell(), main_url); 6524 NavigateToURL(shell(), main_url);
6339 6525
6340 FrameTreeNode* root = web_contents()->GetFrameTree()->root(); 6526 FrameTreeNode* root = web_contents()->GetFrameTree()->root();
6341 FrameTreeNode* child = root->child_at(0); 6527 FrameTreeNode* child = root->child_at(0);
6342 6528
6343 const char* properties[] = {"screenX", "screenY", "outerWidth", 6529 const char* properties[] = {"screenX", "screenY", "outerWidth",
6344 "outerHeight"}; 6530 "outerHeight"};
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
6712 6898
6713 EXPECT_EQ( 6899 EXPECT_EQ(
6714 " Site A ------------ proxies for B\n" 6900 " Site A ------------ proxies for B\n"
6715 " +--Site B ------- proxies for A\n" 6901 " +--Site B ------- proxies for A\n"
6716 "Where A = http://a.com/\n" 6902 "Where A = http://a.com/\n"
6717 " B = http://b.com/", 6903 " B = http://b.com/",
6718 DepictFrameTree(root)); 6904 DepictFrameTree(root));
6719 } 6905 }
6720 6906
6721 } // namespace content 6907 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/frame_host/render_frame_host_manager.cc ('k') | content/common/content_param_traits_macros.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698