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

Side by Side Diff: chrome/browser/extensions/api/web_request/web_request_apitest.cc

Issue 2495353003: chrome.webRequest support for ExtensionSettings (Closed)
Patch Set: Log which webpages the embedded test server has served & query them. Created 3 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 "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"
11 #include "chrome/browser/extensions/active_tab_permission_granter.h" 11 #include "chrome/browser/extensions/active_tab_permission_granter.h"
12 #include "chrome/browser/extensions/extension_action_runner.h" 12 #include "chrome/browser/extensions/extension_action_runner.h"
13 #include "chrome/browser/extensions/extension_apitest.h" 13 #include "chrome/browser/extensions/extension_apitest.h"
14 #include "chrome/browser/extensions/extension_service.h" 14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_with_management_policy_apitest.h"
15 #include "chrome/browser/extensions/tab_helper.h" 16 #include "chrome/browser/extensions/tab_helper.h"
16 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/search_engines/template_url_service_factory.h" 18 #include "chrome/browser/search_engines/template_url_service_factory.h"
18 #include "chrome/browser/ui/browser.h" 19 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_navigator_params.h" 20 #include "chrome/browser/ui/browser_navigator_params.h"
20 #include "chrome/browser/ui/login/login_handler.h" 21 #include "chrome/browser/ui/login/login_handler.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h" 22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/common/extensions/extension_process_policy.h" 23 #include "chrome/common/extensions/extension_process_policy.h"
23 #include "chrome/test/base/search_test_utils.h" 24 #include "chrome/test/base/search_test_utils.h"
24 #include "chrome/test/base/ui_test_utils.h" 25 #include "chrome/test/base/ui_test_utils.h"
26 #include "chromeos/login/scoped_test_public_session_login_state.h"
25 #include "content/public/browser/notification_registrar.h" 27 #include "content/public/browser/notification_registrar.h"
26 #include "content/public/browser/notification_service.h" 28 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/render_frame_host.h" 29 #include "content/public/browser/render_frame_host.h"
28 #include "content/public/browser/render_view_host.h" 30 #include "content/public/browser/render_view_host.h"
29 #include "content/public/browser/render_widget_host.h" 31 #include "content/public/browser/render_widget_host.h"
30 #include "content/public/browser/web_contents.h" 32 #include "content/public/browser/web_contents.h"
31 #include "content/public/test/browser_test_utils.h" 33 #include "content/public/test/browser_test_utils.h"
32 #include "extensions/browser/api/web_request/web_request_api.h" 34 #include "extensions/browser/api/web_request/web_request_api.h"
33 #include "extensions/browser/blocked_action_type.h" 35 #include "extensions/browser/blocked_action_type.h"
34 #include "extensions/browser/extension_system.h" 36 #include "extensions/browser/extension_system.h"
35 #include "extensions/common/extension_builder.h" 37 #include "extensions/common/extension_builder.h"
36 #include "extensions/common/features/feature.h" 38 #include "extensions/common/features/feature.h"
37 #include "extensions/test/extension_test_message_listener.h" 39 #include "extensions/test/extension_test_message_listener.h"
38 #include "extensions/test/result_catcher.h" 40 #include "extensions/test/result_catcher.h"
39 #include "net/dns/mock_host_resolver.h" 41 #include "net/dns/mock_host_resolver.h"
40 #include "net/test/embedded_test_server/embedded_test_server.h" 42 #include "net/test/embedded_test_server/embedded_test_server.h"
43 #include "net/test/embedded_test_server/http_request.h"
41 #include "net/test/test_data_directory.h" 44 #include "net/test/test_data_directory.h"
42 #include "third_party/WebKit/public/platform/WebInputEvent.h" 45 #include "third_party/WebKit/public/platform/WebInputEvent.h"
43 46
44 #if defined(OS_CHROMEOS) 47 #if defined(OS_CHROMEOS)
45 #include "chromeos/login/login_state.h" 48 #include "chromeos/login/login_state.h"
46 #endif // defined(OS_CHROMEOS) 49 #endif // defined(OS_CHROMEOS)
47 50
48 using content::WebContents; 51 using content::WebContents;
49 52
50 namespace extensions { 53 namespace extensions {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 if (!ExecuteScriptAndExtractInt( 120 if (!ExecuteScriptAndExtractInt(
118 host->host_contents(), 121 host->host_contents(),
119 "window.domAutomationController.send(window.webRequestCount)", 122 "window.domAutomationController.send(window.webRequestCount)",
120 &count)) 123 &count))
121 return -1; 124 return -1;
122 return count; 125 return count;
123 } 126 }
124 127
125 } // namespace 128 } // namespace
126 129
127 class ExtensionWebRequestApiTest : public ExtensionApiTest { 130 class ExtensionWebRequestApiTest : public ExtensionApiTestWithManagementPolicy {
128 public: 131 public:
129 void SetUpInProcessBrowserTestFixture() override { 132 void SetUpInProcessBrowserTestFixture() override {
130 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); 133 ExtensionApiTestWithManagementPolicy::SetUpInProcessBrowserTestFixture();
131 host_resolver()->AddRule("*", "127.0.0.1"); 134 host_resolver()->AddRule("*", "127.0.0.1");
132 } 135 }
133 136
134 void RunPermissionTest( 137 void RunPermissionTest(
135 const char* extension_directory, 138 const char* extension_directory,
136 bool load_extension_with_incognito_permission, 139 bool load_extension_with_incognito_permission,
137 bool wait_for_extension_loaded_in_incognito, 140 bool wait_for_extension_loaded_in_incognito,
138 const char* expected_content_regular_window, 141 const char* expected_content_regular_window,
139 const char* exptected_content_incognito_window); 142 const char* exptected_content_incognito_window);
140 }; 143 };
(...skipping 22 matching lines...) Expand all
163 } 166 }
164 167
165 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebRequestTypes) { 168 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebRequestTypes) {
166 ASSERT_TRUE(StartEmbeddedTestServer()); 169 ASSERT_TRUE(StartEmbeddedTestServer());
167 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_types.html")) << message_; 170 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_types.html")) << message_;
168 } 171 }
169 172
170 #if defined(OS_CHROMEOS) 173 #if defined(OS_CHROMEOS)
171 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebRequestPublicSession) { 174 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebRequestPublicSession) {
172 ASSERT_TRUE(StartEmbeddedTestServer()); 175 ASSERT_TRUE(StartEmbeddedTestServer());
173 // Set Public Session state. 176 chromeos::ScopedTestPublicSessionLoginState login_state;
174 chromeos::LoginState::Get()->SetLoggedInState(
175 chromeos::LoginState::LOGGED_IN_ACTIVE,
176 chromeos::LoginState::LOGGED_IN_USER_PUBLIC_ACCOUNT);
177 // Disable a CHECK while doing api tests. 177 // Disable a CHECK while doing api tests.
178 WebRequestPermissions::AllowAllExtensionLocationsInPublicSessionForTesting( 178 WebRequestPermissions::AllowAllExtensionLocationsInPublicSessionForTesting(
179 true); 179 true);
180 ASSERT_TRUE(RunExtensionSubtest("webrequest_public_session", "test.html")) << 180 ASSERT_TRUE(RunExtensionSubtest("webrequest_public_session", "test.html")) <<
181 message_; 181 message_;
182 WebRequestPermissions::AllowAllExtensionLocationsInPublicSessionForTesting( 182 WebRequestPermissions::AllowAllExtensionLocationsInPublicSessionForTesting(
183 false); 183 false);
184 } 184 }
185 #endif // defined(OS_CHROMEOS) 185 #endif // defined(OS_CHROMEOS)
186 186
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 // Test that the webRequest events are dispatched for the WebSocket handshake 660 // Test that the webRequest events are dispatched for the WebSocket handshake
661 // requests when authenrication is requested by server. 661 // requests when authenrication is requested by server.
662 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, 662 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
663 WebSocketRequestAuthRequired) { 663 WebSocketRequestAuthRequired) {
664 ASSERT_TRUE(StartEmbeddedTestServer()); 664 ASSERT_TRUE(StartEmbeddedTestServer());
665 ASSERT_TRUE(StartWebSocketServer(net::GetWebSocketTestDataDirectory(), true)); 665 ASSERT_TRUE(StartWebSocketServer(net::GetWebSocketTestDataDirectory(), true));
666 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_websocket_auth.html")) 666 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_websocket_auth.html"))
667 << message_; 667 << message_;
668 } 668 }
669 669
670 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, policyBlockByOrigin) {
671 // Browse to protected example.com page, which loads JS of unprotected
672 // domain. We try to block this external JS in the extension. Since
673 // the JS is loaded from a protected Origin, we succeed if the request
674 // isn't blocked.
675
676 // Set enterprise policy to block modification of requests to or
677 // from (origin) example.com/no*. This should NOT match the URL so the request
678 // CAN NOT be viewed or modified
679 {
680 ExtensionManagementPolicyUpdater pref(&policy_provider_);
681 pref.AddRuntimeBlockedHost("*", "*://example.com/no*");
682 }
683 // Set auto confirm UI flag.
684 PermissionsRequestFunction::SetAutoConfirmForTests(true);
685 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true);
686
687 ASSERT_TRUE(StartEmbeddedTestServer());
688
689 LoadExtension(test_data_dir_.AppendASCII("webrequest/policy_blocked"));
690
691 // Listen to verify extension sees the web request.
692 ExtensionTestMessageListener before_request_listener("protected_origin",
693 false);
694
695 // Wait until all remote Javascript files have been blocked / pulled down.
696 ui_test_utils::NavigateToURLWithDisposition(
697 browser(),
698 embedded_test_server()->GetURL(
699 "example.com",
700 "/extensions/api_test/webrequest/policy_blocked/ref_remote_js.html"),
701 WindowOpenDisposition::CURRENT_TAB,
702 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
703
704 // The webRequest was seen by the extension
705 EXPECT_TRUE(before_request_listener.was_satisfied());
706
707 // Clear the list of domains the server has seen
708 ClearRequestLog();
709
710 // Set enterprise policy to block modification of requests to or
711 // from (origin) example.com/e*. This SHOULD match the URL so the request
712 // SHOULD be modifyable, in this case
713 {
714 ExtensionManagementPolicyUpdater pref(&policy_provider_);
715 pref.AddRuntimeBlockedHost("*", "*://example.com/e*");
716 }
717
718 // Listen in case extension sees the web requst
719 ExtensionTestMessageListener before_request_listener2("protected_origin",
720 false);
721
722 // Wait until all remote Javascript files have been pulled down
723 ui_test_utils::NavigateToURLWithDisposition(
724 browser(),
725 embedded_test_server()->GetURL(
726 "example.com",
727 "/extensions/api_test/webrequest/policy_blocked/ref_remote_js.html"),
728 WindowOpenDisposition::CURRENT_TAB,
729 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
730
731 // The server saw a request for the remote Javascript file
732 EXPECT_TRUE(BrowsedTo("example2.com"));
733
734 // The webRequest was hidden from the extension
735 EXPECT_FALSE(before_request_listener2.was_satisfied());
736
737 // We need to test again to make sure non-protected URLs are still visible
738 ClearRequestLog();
739
740 // Wait until all remote Javascript files have been pulled down
741 ui_test_utils::NavigateToURLWithDisposition(
742 browser(),
743 embedded_test_server()->GetURL(
744 "not_blocked_example.com",
745 "/extensions/api_test/webrequest/policy_blocked/ref_remote_js.html"),
746 WindowOpenDisposition::CURRENT_TAB,
747 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
748
749 // The server saw a request for the remote Javascript file
750 EXPECT_TRUE(BrowsedTo("example2.com"));
751
752 // The webRequest was visible from the extension
753 EXPECT_TRUE(before_request_listener.was_satisfied());
754 }
755
756 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, policyBlockByUrl) {
757 // Set enterprise policy to block modification of requests to or
758 // from (origin) example.com.
759 {
760 ExtensionManagementPolicyUpdater pref(&policy_provider_);
761 pref.AddRuntimeBlockedHost("*", "*://example.com/*");
762 }
763 // Set auto confirm UI flag.
764 PermissionsRequestFunction::SetAutoConfirmForTests(true);
765 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true);
766
767 ASSERT_TRUE(StartEmbeddedTestServer());
768
769 LoadExtension(test_data_dir_.AppendASCII("webrequest/policy_blocked"));
770
771 // Listen in case extension sees the web requst
772 ExtensionTestMessageListener before_request_listener("protected_url", false);
773
774 // Wait until page fully loads
775 ui_test_utils::NavigateToURLWithDisposition(
776 browser(),
777 embedded_test_server()->GetURL(
778 "example.com",
779 "/extensions/api_test/webrequest/policy_blocked/protected_url.html"),
780 WindowOpenDisposition::CURRENT_TAB,
781 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
782
783 // The server saw a request for the protected site
784 EXPECT_TRUE(BrowsedTo("example.com"));
785
786 // The webRequest was hidden from the extension
787 EXPECT_FALSE(before_request_listener.was_satisfied());
788
789 // Wait until page fully loads
790 ui_test_utils::NavigateToURLWithDisposition(
791 browser(),
792 embedded_test_server()->GetURL(
793 "not_blocked_example.com",
794 "/extensions/api_test/webrequest/policy_blocked/protected_url.html"),
795 WindowOpenDisposition::CURRENT_TAB,
796 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
797
798 // The server saw a request for the protected site
799 EXPECT_TRUE(BrowsedTo("not_blocked_example.com"));
800
801 // The webRequest was visible from the extension
802 EXPECT_TRUE(before_request_listener.was_satisfied());
803 }
804
805 // Tests that webRequest respects hosts protected by ExtensionSettings policy.
806 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
807 WebRequestProtectedByPolicy) {
808 FeatureSwitch::ScopedOverride enable_scripts_require_action(
809 FeatureSwitch::scripts_require_action(), true);
810 {
811 ExtensionManagementPolicyUpdater pref(&policy_provider_);
812 pref.AddRuntimeBlockedHost("*", "*://example.com/*");
813 }
814 extensions::PermissionsRequestFunction::SetIgnoreUserGestureForTests(true);
815 extensions::PermissionsRequestFunction::SetAutoConfirmForTests(true);
816 ASSERT_TRUE(StartEmbeddedTestServer());
817
818 ExtensionTestMessageListener listener("ready", false);
819 const Extension* extension =
820 LoadExtension(test_data_dir_.AppendASCII("webrequest_activetab"));
821 ASSERT_TRUE(extension) << message_;
822 EXPECT_TRUE(listener.WaitUntilSatisfied());
823
824 // Navigate the browser to a page in a new tab.
825 const std::string kHost = "example.com";
826 GURL url = embedded_test_server()->GetURL(kHost, "/empty.html");
827 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK);
828 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
829 ui_test_utils::NavigateToURL(&params);
830
831 content::WebContents* web_contents =
832 browser()->tab_strip_model()->GetActiveWebContents();
833 ASSERT_TRUE(web_contents);
834 ExtensionActionRunner* runner =
835 ExtensionActionRunner::GetForWebContents(web_contents);
836 ASSERT_TRUE(runner);
837
838 int port = embedded_test_server()->port();
839 const std::string kXhrPath = "simple.html";
840
841 // The extension shouldn't have currently received any webRequest events,
842 // since it doesn't have permission (and shouldn't receive any from an XHR).
843 EXPECT_EQ(0, GetWebRequestCountFromBackgroundPage(extension, profile()));
844 PerformXhrInFrame(web_contents->GetMainFrame(), kHost, port, kXhrPath);
845 EXPECT_EQ(0, GetWebRequestCountFromBackgroundPage(extension, profile()));
846
847 // Grant activeTab permission, and perform another XHR. The extension should
848 // still be blocked due to ExtensionSettings policy on example.com.
849 EXPECT_EQ(BLOCKED_ACTION_WEB_REQUEST, runner->GetBlockedActions(extension));
850 runner->set_default_bubble_close_action_for_testing(
851 base::WrapUnique(new ToolbarActionsBarBubbleDelegate::CloseAction(
852 ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE)));
853 runner->RunAction(extension, true);
854 base::RunLoop().RunUntilIdle();
855 EXPECT_TRUE(content::WaitForLoadStop(web_contents));
856 // The runner will have refreshed the page...
857 EXPECT_EQ(BLOCKED_ACTION_NONE, runner->GetBlockedActions(extension));
858 int xhr_count = GetWebRequestCountFromBackgroundPage(extension, profile());
859 // ... which means that we should have a non-zero xhr count.
860 EXPECT_EQ(xhr_count, 0);
861 // And the extension should also block future events.
862 PerformXhrInFrame(web_contents->GetMainFrame(), kHost, port, kXhrPath);
863 EXPECT_EQ(xhr_count,
864 GetWebRequestCountFromBackgroundPage(extension, profile()));
865 }
866
670 } // namespace extensions 867 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698