| 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/optional.h" | 8 #include "base/optional.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 #include "extensions/browser/blocked_action_type.h" | 41 #include "extensions/browser/blocked_action_type.h" |
| 42 #include "extensions/browser/extension_system.h" | 42 #include "extensions/browser/extension_system.h" |
| 43 #include "extensions/common/extension_builder.h" | 43 #include "extensions/common/extension_builder.h" |
| 44 #include "extensions/common/features/feature.h" | 44 #include "extensions/common/features/feature.h" |
| 45 #include "extensions/test/extension_test_message_listener.h" | 45 #include "extensions/test/extension_test_message_listener.h" |
| 46 #include "extensions/test/result_catcher.h" | 46 #include "extensions/test/result_catcher.h" |
| 47 #include "net/dns/mock_host_resolver.h" | 47 #include "net/dns/mock_host_resolver.h" |
| 48 #include "net/test/embedded_test_server/embedded_test_server.h" | 48 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 49 #include "net/test/test_data_directory.h" | 49 #include "net/test/test_data_directory.h" |
| 50 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 50 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 51 #include "net/url_request/test_url_fetcher_factory.h" |
| 51 #include "net/url_request/url_fetcher.h" | 52 #include "net/url_request/url_fetcher.h" |
| 52 #include "net/url_request/url_fetcher_delegate.h" | 53 #include "net/url_request/url_fetcher_delegate.h" |
| 53 #include "net/url_request/url_request_context_getter.h" | 54 #include "net/url_request/url_request_context_getter.h" |
| 54 #include "third_party/WebKit/public/platform/WebInputEvent.h" | 55 #include "third_party/WebKit/public/platform/WebInputEvent.h" |
| 55 | 56 |
| 56 #if defined(OS_CHROMEOS) | 57 #if defined(OS_CHROMEOS) |
| 57 #include "chromeos/login/login_state.h" | 58 #include "chromeos/login/login_state.h" |
| 58 #endif // defined(OS_CHROMEOS) | 59 #endif // defined(OS_CHROMEOS) |
| 59 | 60 |
| 60 using content::WebContents; | 61 using content::WebContents; |
| (...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 TabHelper::FromWebContents(web_contents)->active_tab_permission_granter(); | 701 TabHelper::FromWebContents(web_contents)->active_tab_permission_granter(); |
| 701 ASSERT_TRUE(granter); | 702 ASSERT_TRUE(granter); |
| 702 granter->RevokeForTesting(); | 703 granter->RevokeForTesting(); |
| 703 base::RunLoop().RunUntilIdle(); | 704 base::RunLoop().RunUntilIdle(); |
| 704 PerformXhrInFrame(main_frame, kHost, port, kXhrPath); | 705 PerformXhrInFrame(main_frame, kHost, port, kXhrPath); |
| 705 EXPECT_EQ(xhr_count, | 706 EXPECT_EQ(xhr_count, |
| 706 GetWebRequestCountFromBackgroundPage(extension, profile())); | 707 GetWebRequestCountFromBackgroundPage(extension, profile())); |
| 707 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner->GetBlockedActions(extension)); | 708 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner->GetBlockedActions(extension)); |
| 708 } | 709 } |
| 709 | 710 |
| 711 // Verify that requests to clientsX.google.com are protected properly. |
| 712 // First test requests from a standard renderer and a webui renderer. |
| 713 // Then test a request from the browser process. |
| 714 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, |
| 715 WebRequestClientsGoogleComProtection) { |
| 716 ASSERT_TRUE(embedded_test_server()->Start()); |
| 717 int port = embedded_test_server()->port(); |
| 718 |
| 719 // Load an extension that registers a listener for webRequest events, and |
| 720 // wait 'til it's initialized. |
| 721 ExtensionTestMessageListener listener("ready", false); |
| 722 const Extension* extension = LoadExtension( |
| 723 test_data_dir_.AppendASCII("webrequest_clients_google_com")); |
| 724 ASSERT_TRUE(extension) << message_; |
| 725 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 726 |
| 727 // Perform requests to https://client1.google.com from renderer processes. |
| 728 |
| 729 struct TestCase { |
| 730 const char* main_frame_url; |
| 731 bool request_to_clients1_google_com_visible; |
| 732 } testcases[] = { |
| 733 {"http://www.example.com", true}, {"chrome://settings", false}, |
| 734 }; |
| 735 |
| 736 // Expected number of requests to clients1.google.com observed so far. |
| 737 int expected_requests_observed = 0; |
| 738 EXPECT_EQ(expected_requests_observed, |
| 739 GetWebRequestCountFromBackgroundPage(extension, profile())); |
| 740 |
| 741 for (const auto& testcase : testcases) { |
| 742 SCOPED_TRACE(testcase.main_frame_url); |
| 743 |
| 744 GURL url; |
| 745 if (base::StartsWith(testcase.main_frame_url, "chrome://", |
| 746 base::CompareCase::INSENSITIVE_ASCII)) { |
| 747 url = GURL(testcase.main_frame_url); |
| 748 } else { |
| 749 url = GURL(base::StringPrintf("%s:%d/simple.html", |
| 750 testcase.main_frame_url, port)); |
| 751 } |
| 752 |
| 753 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_TYPED); |
| 754 ui_test_utils::NavigateToURL(¶ms); |
| 755 |
| 756 EXPECT_EQ(expected_requests_observed, |
| 757 GetWebRequestCountFromBackgroundPage(extension, profile())); |
| 758 |
| 759 content::WebContents* web_contents = |
| 760 browser()->tab_strip_model()->GetActiveWebContents(); |
| 761 ASSERT_TRUE(web_contents); |
| 762 |
| 763 const char kRequest[] = |
| 764 "var xhr = new XMLHttpRequest();\n" |
| 765 "xhr.open('GET', 'https://clients1.google.com');\n" |
| 766 "xhr.onload = () => {window.domAutomationController.send(true);};\n" |
| 767 "xhr.onerror = () => {window.domAutomationController.send(false);};\n" |
| 768 "xhr.send();\n"; |
| 769 |
| 770 bool success = false; |
| 771 EXPECT_TRUE(ExecuteScriptAndExtractBool(web_contents->GetMainFrame(), |
| 772 kRequest, &success)); |
| 773 // Requests always fail due to cross origin nature. |
| 774 EXPECT_FALSE(success); |
| 775 |
| 776 if (testcase.request_to_clients1_google_com_visible) |
| 777 ++expected_requests_observed; |
| 778 |
| 779 EXPECT_EQ(expected_requests_observed, |
| 780 GetWebRequestCountFromBackgroundPage(extension, profile())); |
| 781 } |
| 782 |
| 783 // Perform request to https://client1.google.com from browser process. |
| 784 |
| 785 class TestURLFetcherDelegate : public net::URLFetcherDelegate { |
| 786 public: |
| 787 explicit TestURLFetcherDelegate(const base::Closure& quit_loop_func) |
| 788 : quit_loop_func_(quit_loop_func) {} |
| 789 ~TestURLFetcherDelegate() override {} |
| 790 |
| 791 void OnURLFetchComplete(const net::URLFetcher* source) override { |
| 792 EXPECT_EQ(net::HTTP_OK, source->GetResponseCode()); |
| 793 quit_loop_func_.Run(); |
| 794 } |
| 795 |
| 796 private: |
| 797 base::Closure quit_loop_func_; |
| 798 }; |
| 799 base::RunLoop run_loop; |
| 800 TestURLFetcherDelegate delegate(run_loop.QuitClosure()); |
| 801 |
| 802 net::URLFetcherImplFactory url_fetcher_impl_factory; |
| 803 net::FakeURLFetcherFactory url_fetcher_factory(&url_fetcher_impl_factory); |
| 804 url_fetcher_factory.SetFakeResponse(GURL("https://client1.google.com"), |
| 805 "hello my friend", net::HTTP_OK, |
| 806 net::URLRequestStatus::SUCCESS); |
| 807 std::unique_ptr<net::URLFetcher> fetcher = |
| 808 url_fetcher_factory.CreateURLFetcher(1, |
| 809 GURL("https://client1.google.com"), |
| 810 net::URLFetcher::GET, &delegate); |
| 811 fetcher->Start(); |
| 812 run_loop.Run(); |
| 813 |
| 814 // This request should not be observed by the extension. |
| 815 EXPECT_EQ(expected_requests_observed, |
| 816 GetWebRequestCountFromBackgroundPage(extension, profile())); |
| 817 } |
| 818 |
| 710 // Test that the webRequest events are dispatched for the WebSocket handshake | 819 // Test that the webRequest events are dispatched for the WebSocket handshake |
| 711 // requests. | 820 // requests. |
| 712 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebSocketRequest) { | 821 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebSocketRequest) { |
| 713 ASSERT_TRUE(StartEmbeddedTestServer()); | 822 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 714 ASSERT_TRUE(StartWebSocketServer(net::GetWebSocketTestDataDirectory())); | 823 ASSERT_TRUE(StartWebSocketServer(net::GetWebSocketTestDataDirectory())); |
| 715 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_websocket.html")) | 824 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_websocket.html")) |
| 716 << message_; | 825 << message_; |
| 717 } | 826 } |
| 718 | 827 |
| 719 // Test that the webRequest events are dispatched for the WebSocket handshake | 828 // Test that the webRequest events are dispatched for the WebSocket handshake |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 839 // request context. | 948 // request context. |
| 840 SCOPED_TRACE("example.com with System's request context"); | 949 SCOPED_TRACE("example.com with System's request context"); |
| 841 TestURLFetcherDelegate url_fetcher(system_context, example_url, | 950 TestURLFetcherDelegate url_fetcher(system_context, example_url, |
| 842 net::URLRequestStatus()); | 951 net::URLRequestStatus()); |
| 843 url_fetcher.SetExpectedResponse(kExampleFullContent); | 952 url_fetcher.SetExpectedResponse(kExampleFullContent); |
| 844 url_fetcher.WaitForCompletion(); | 953 url_fetcher.WaitForCompletion(); |
| 845 } | 954 } |
| 846 } | 955 } |
| 847 | 956 |
| 848 } // namespace extensions | 957 } // namespace extensions |
| OLD | NEW |