Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 #include "extensions/browser/api/web_request/web_request_api.h" | 32 #include "extensions/browser/api/web_request/web_request_api.h" |
| 33 #include "extensions/browser/blocked_action_type.h" | 33 #include "extensions/browser/blocked_action_type.h" |
| 34 #include "extensions/browser/extension_system.h" | 34 #include "extensions/browser/extension_system.h" |
| 35 #include "extensions/common/extension_builder.h" | 35 #include "extensions/common/extension_builder.h" |
| 36 #include "extensions/common/features/feature.h" | 36 #include "extensions/common/features/feature.h" |
| 37 #include "extensions/test/extension_test_message_listener.h" | 37 #include "extensions/test/extension_test_message_listener.h" |
| 38 #include "extensions/test/result_catcher.h" | 38 #include "extensions/test/result_catcher.h" |
| 39 #include "net/dns/mock_host_resolver.h" | 39 #include "net/dns/mock_host_resolver.h" |
| 40 #include "net/test/embedded_test_server/embedded_test_server.h" | 40 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 41 #include "net/test/test_data_directory.h" | 41 #include "net/test/test_data_directory.h" |
| 42 #include "net/url_request/test_url_fetcher_factory.h" | |
| 43 #include "net/url_request/url_fetcher_delegate.h" | |
| 42 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 44 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
| 43 | 45 |
| 44 #if defined(OS_CHROMEOS) | 46 #if defined(OS_CHROMEOS) |
| 45 #include "chromeos/login/login_state.h" | 47 #include "chromeos/login/login_state.h" |
| 46 #endif // defined(OS_CHROMEOS) | 48 #endif // defined(OS_CHROMEOS) |
| 47 | 49 |
| 48 using content::WebContents; | 50 using content::WebContents; |
| 49 | 51 |
| 50 namespace extensions { | 52 namespace extensions { |
| 51 | 53 |
| (...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 641 TabHelper::FromWebContents(web_contents)->active_tab_permission_granter(); | 643 TabHelper::FromWebContents(web_contents)->active_tab_permission_granter(); |
| 642 ASSERT_TRUE(granter); | 644 ASSERT_TRUE(granter); |
| 643 granter->RevokeForTesting(); | 645 granter->RevokeForTesting(); |
| 644 base::RunLoop().RunUntilIdle(); | 646 base::RunLoop().RunUntilIdle(); |
| 645 PerformXhrInFrame(main_frame, kHost, port, kXhrPath); | 647 PerformXhrInFrame(main_frame, kHost, port, kXhrPath); |
| 646 EXPECT_EQ(xhr_count, | 648 EXPECT_EQ(xhr_count, |
| 647 GetWebRequestCountFromBackgroundPage(extension, profile())); | 649 GetWebRequestCountFromBackgroundPage(extension, profile())); |
| 648 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner->GetBlockedActions(extension)); | 650 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner->GetBlockedActions(extension)); |
| 649 } | 651 } |
| 650 | 652 |
| 653 // Verify that requests to clientsX.google.com are protected properly. | |
| 654 // First test requests from a standard renderer and a webui renderer. | |
| 655 // Then test a request from the browser process. | |
| 656 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, | |
| 657 WebRequestClientsGoogleComProtection) { | |
| 658 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 659 int port = embedded_test_server()->port(); | |
| 660 | |
| 661 // Load an extension that registers a listener for webRequest events, and | |
| 662 // wait 'til it's initialized. | |
| 663 ExtensionTestMessageListener listener("ready", false); | |
| 664 const Extension* extension = LoadExtension( | |
| 665 test_data_dir_.AppendASCII("webrequest_clients_google_com")); | |
| 666 ASSERT_TRUE(extension) << message_; | |
| 667 EXPECT_TRUE(listener.WaitUntilSatisfied()); | |
| 668 | |
| 669 // Perform requests to https://client1.google.com from renderer processes. | |
| 670 | |
| 671 struct TestCase { | |
| 672 const char* main_frame_url; | |
| 673 bool request_to_clients1_google_com_visible; | |
| 674 } testcases[] = { | |
| 675 {"http://www.example.com", true}, {"chrome://settings", false}, | |
| 676 }; | |
| 677 | |
| 678 // Expected number of requests to clients1.google.com observed so far. | |
| 679 int expected_requests_observed = 0; | |
|
Devlin
2017/05/15 16:44:21
Maybe check the initial value, just for completene
battre (please use the other)
2017/05/15 17:39:22
Done.
| |
| 680 | |
| 681 for (const auto& testcase : testcases) { | |
| 682 SCOPED_TRACE(testcase.main_frame_url); | |
| 683 | |
| 684 GURL url; | |
| 685 if (base::StartsWith(testcase.main_frame_url, "chrome://", | |
| 686 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 687 url = GURL(testcase.main_frame_url); | |
| 688 } else { | |
| 689 url = GURL(base::StringPrintf("%s:%d/simple.html", | |
| 690 testcase.main_frame_url, port)); | |
| 691 } | |
| 692 | |
| 693 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_TYPED); | |
| 694 ui_test_utils::NavigateToURL(¶ms); | |
| 695 | |
| 696 EXPECT_EQ(expected_requests_observed, | |
| 697 GetWebRequestCountFromBackgroundPage(extension, profile())); | |
| 698 | |
| 699 content::WebContents* web_contents = | |
| 700 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 701 ASSERT_TRUE(web_contents); | |
| 702 ExtensionActionRunner* runner = | |
| 703 ExtensionActionRunner::GetForWebContents(web_contents); | |
| 704 ASSERT_TRUE(runner); | |
|
Devlin
2017/05/15 16:44:21
Used?
battre (please use the other)
2017/05/15 17:39:22
Done.
| |
| 705 | |
| 706 const char kRequest[] = | |
| 707 "var xhr = new XMLHttpRequest();\n" | |
| 708 "xhr.open('GET', 'https://clients1.google.com');\n" | |
| 709 "xhr.onload = () => {window.domAutomationController.send(true);};\n" | |
| 710 "xhr.onerror = () => {window.domAutomationController.send(false);};\n" | |
| 711 "xhr.send();\n"; | |
| 712 | |
| 713 bool success = false; | |
| 714 EXPECT_TRUE(ExecuteScriptAndExtractBool(web_contents->GetMainFrame(), | |
| 715 kRequest, &success)); | |
| 716 // Requests always fail due to cross origin nature. | |
| 717 EXPECT_FALSE(success); | |
|
mmenke
2017/05/11 18:39:45
If the requests are failing due to CORS, does that
Devlin
2017/05/15 16:44:21
We actually notify the extension *before* the requ
mmenke
2017/05/15 16:47:26
CORS blocks requests from even reaching the networ
Devlin
2017/05/15 17:03:35
Hmm, interesting... Why does this succeed in inter
mmenke
2017/05/15 17:09:51
My guess is that it intercepts the CORS request.
battre (please use the other)
2017/05/15 17:39:22
My understanding is that Chrome sends the request
mmenke
2017/05/15 18:31:16
I was concenred that we wouldn't be making a reque
| |
| 718 | |
| 719 if (testcase.request_to_clients1_google_com_visible) | |
| 720 ++expected_requests_observed; | |
| 721 | |
| 722 EXPECT_EQ(expected_requests_observed, | |
| 723 GetWebRequestCountFromBackgroundPage(extension, profile())); | |
| 724 } | |
| 725 | |
| 726 // Perform request to https://client1.google.com from browser process. | |
| 727 | |
| 728 class TestURLFetcherDelegate : public net::URLFetcherDelegate { | |
|
Devlin
2017/05/15 16:44:21
I don't know if we have a specific rule about clas
battre (please use the other)
2017/05/15 17:39:22
I actually copied this from code in Chrome. I like
Devlin
2017/05/15 18:40:39
Heh fair enough. I have a TODO around here somewh
| |
| 729 public: | |
| 730 explicit TestURLFetcherDelegate(const base::Closure& quit_loop_func) | |
| 731 : quit_loop_func_(quit_loop_func) {} | |
| 732 ~TestURLFetcherDelegate() override {} | |
| 733 | |
| 734 void OnURLFetchComplete(const net::URLFetcher* source) override { | |
| 735 EXPECT_EQ(net::HTTP_OK, source->GetResponseCode()); | |
| 736 quit_loop_func_.Run(); | |
| 737 } | |
| 738 | |
| 739 private: | |
| 740 base::Closure quit_loop_func_; | |
| 741 }; | |
| 742 base::RunLoop run_loop; | |
| 743 TestURLFetcherDelegate delegate(run_loop.QuitClosure()); | |
| 744 | |
| 745 net::URLFetcherImplFactory url_fetcher_impl_factory; | |
| 746 net::FakeURLFetcherFactory url_fetcher_factory(&url_fetcher_factory); | |
| 747 url_fetcher_factory.SetFakeResponse(GURL("https://client1.google.com"), | |
| 748 "hello my friend", net::HTTP_OK, | |
| 749 net::URLRequestStatus::SUCCESS); | |
| 750 std::unique_ptr<net::URLFetcher> fetcher = | |
| 751 url_fetcher_factory.CreateURLFetcher(1, | |
| 752 GURL("https://client1.google.com"), | |
| 753 net::URLFetcher::GET, &delegate); | |
| 754 fetcher->Start(); | |
| 755 run_loop.Run(); | |
| 756 | |
| 757 // This request should not be observed by the extension. | |
| 758 EXPECT_EQ(expected_requests_observed, | |
| 759 GetWebRequestCountFromBackgroundPage(extension, profile())); | |
| 760 } | |
| 761 | |
| 651 // Test that the webRequest events are dispatched for the WebSocket handshake | 762 // Test that the webRequest events are dispatched for the WebSocket handshake |
| 652 // requests. | 763 // requests. |
| 653 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebSocketRequest) { | 764 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebSocketRequest) { |
| 654 ASSERT_TRUE(StartEmbeddedTestServer()); | 765 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 655 ASSERT_TRUE(StartWebSocketServer(net::GetWebSocketTestDataDirectory())); | 766 ASSERT_TRUE(StartWebSocketServer(net::GetWebSocketTestDataDirectory())); |
| 656 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_websocket.html")) | 767 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_websocket.html")) |
| 657 << message_; | 768 << message_; |
| 658 } | 769 } |
| 659 | 770 |
| 660 // Test that the webRequest events are dispatched for the WebSocket handshake | 771 // Test that the webRequest events are dispatched for the WebSocket handshake |
| 661 // requests when authenrication is requested by server. | 772 // requests when authenrication is requested by server. |
| 662 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, | 773 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, |
| 663 WebSocketRequestAuthRequired) { | 774 WebSocketRequestAuthRequired) { |
| 664 ASSERT_TRUE(StartEmbeddedTestServer()); | 775 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 665 ASSERT_TRUE(StartWebSocketServer(net::GetWebSocketTestDataDirectory(), true)); | 776 ASSERT_TRUE(StartWebSocketServer(net::GetWebSocketTestDataDirectory(), true)); |
| 666 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_websocket_auth.html")) | 777 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_websocket_auth.html")) |
| 667 << message_; | 778 << message_; |
| 668 } | 779 } |
| 669 | 780 |
| 670 } // namespace extensions | 781 } // namespace extensions |
| OLD | NEW |