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

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

Issue 2495353003: chrome.webRequest support for ExtensionSettings (Closed)
Patch Set: Moved policy check to WebRequestPermissions, code cleanup, rebase Created 3 years, 6 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/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"
11 #include "build/build_config.h" 11 #include "build/build_config.h"
12 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_notification_types.h" 13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/extensions/active_tab_permission_granter.h" 14 #include "chrome/browser/extensions/active_tab_permission_granter.h"
15 #include "chrome/browser/extensions/extension_action_runner.h" 15 #include "chrome/browser/extensions/extension_action_runner.h"
16 #include "chrome/browser/extensions/extension_apitest.h" 16 #include "chrome/browser/extensions/extension_apitest.h"
17 #include "chrome/browser/extensions/extension_service.h" 17 #include "chrome/browser/extensions/extension_service.h"
18 #include "chrome/browser/extensions/extension_with_management_policy_apitest.h"
18 #include "chrome/browser/extensions/tab_helper.h" 19 #include "chrome/browser/extensions/tab_helper.h"
19 #include "chrome/browser/extensions/test_extension_dir.h" 20 #include "chrome/browser/extensions/test_extension_dir.h"
20 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/search_engines/template_url_service_factory.h" 22 #include "chrome/browser/search_engines/template_url_service_factory.h"
22 #include "chrome/browser/ui/browser.h" 23 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_navigator_params.h" 24 #include "chrome/browser/ui/browser_navigator_params.h"
24 #include "chrome/browser/ui/login/login_handler.h" 25 #include "chrome/browser/ui/login/login_handler.h"
25 #include "chrome/browser/ui/tabs/tab_strip_model.h" 26 #include "chrome/browser/ui/tabs/tab_strip_model.h"
26 #include "chrome/common/extensions/extension_process_policy.h" 27 #include "chrome/common/extensions/extension_process_policy.h"
27 #include "chrome/test/base/search_test_utils.h" 28 #include "chrome/test/base/search_test_utils.h"
(...skipping 11 matching lines...) Expand all
39 #include "content/public/test/browser_test_utils.h" 40 #include "content/public/test/browser_test_utils.h"
40 #include "extensions/browser/api/web_request/web_request_api.h" 41 #include "extensions/browser/api/web_request/web_request_api.h"
41 #include "extensions/browser/blocked_action_type.h" 42 #include "extensions/browser/blocked_action_type.h"
42 #include "extensions/browser/extension_system.h" 43 #include "extensions/browser/extension_system.h"
43 #include "extensions/common/extension_builder.h" 44 #include "extensions/common/extension_builder.h"
44 #include "extensions/common/features/feature.h" 45 #include "extensions/common/features/feature.h"
45 #include "extensions/test/extension_test_message_listener.h" 46 #include "extensions/test/extension_test_message_listener.h"
46 #include "extensions/test/result_catcher.h" 47 #include "extensions/test/result_catcher.h"
47 #include "net/dns/mock_host_resolver.h" 48 #include "net/dns/mock_host_resolver.h"
48 #include "net/test/embedded_test_server/embedded_test_server.h" 49 #include "net/test/embedded_test_server/embedded_test_server.h"
50 #include "net/test/embedded_test_server/http_request.h"
49 #include "net/test/test_data_directory.h" 51 #include "net/test/test_data_directory.h"
50 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" 52 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
51 #include "net/url_request/test_url_fetcher_factory.h" 53 #include "net/url_request/test_url_fetcher_factory.h"
52 #include "net/url_request/url_fetcher.h" 54 #include "net/url_request/url_fetcher.h"
53 #include "net/url_request/url_fetcher_delegate.h" 55 #include "net/url_request/url_fetcher_delegate.h"
54 #include "net/url_request/url_request_context_getter.h" 56 #include "net/url_request/url_request_context_getter.h"
55 #include "third_party/WebKit/public/platform/WebInputEvent.h" 57 #include "third_party/WebKit/public/platform/WebInputEvent.h"
56 58
57 #if defined(OS_CHROMEOS) 59 #if defined(OS_CHROMEOS)
58 #include "chromeos/login/login_state.h" 60 #include "chromeos/login/login_state.h"
(...skipping 19 matching lines...) Expand all
78 const content::NotificationSource& source, 80 const content::NotificationSource& source,
79 const content::NotificationDetails& details) override { 81 const content::NotificationDetails& details) override {
80 LoginHandler* handler = 82 LoginHandler* handler =
81 content::Details<LoginNotificationDetails>(details).ptr()->handler(); 83 content::Details<LoginNotificationDetails>(details).ptr()->handler();
82 handler->CancelAuth(); 84 handler->CancelAuth();
83 } 85 }
84 86
85 private: 87 private:
86 content::NotificationRegistrar registrar_; 88 content::NotificationRegistrar registrar_;
87 89
88 DISALLOW_COPY_AND_ASSIGN(CancelLoginDialog); 90 DISALLOW_COPY_AND_ASSIGN(CancelLoginDialog);
89 }; 91 };
90 92
91 // Sends an XHR request to the provided host, port, and path, and responds when 93 // Sends an XHR request to the provided host, port, and path, and responds when
92 // the request was sent. 94 // the request was sent.
93 const char kPerformXhrJs[] = 95 const char kPerformXhrJs[] =
94 "var url = 'http://%s:%d/%s';\n" 96 "var url = 'http://%s:%d/%s';\n"
95 "var xhr = new XMLHttpRequest();\n" 97 "var xhr = new XMLHttpRequest();\n"
96 "xhr.open('GET', url);\n" 98 "xhr.open('GET', url);\n"
97 "xhr.onload = function() {\n" 99 "xhr.onload = function() {\n"
98 " window.domAutomationController.send(true);\n" 100 " window.domAutomationController.send(true);\n"
(...skipping 848 matching lines...) Expand 10 before | Expand all | Expand 10 after
947 // example.com should also succeed, since it's not through the profile's 949 // example.com should also succeed, since it's not through the profile's
948 // request context. 950 // request context.
949 SCOPED_TRACE("example.com with System's request context"); 951 SCOPED_TRACE("example.com with System's request context");
950 TestURLFetcherDelegate url_fetcher(system_context, example_url, 952 TestURLFetcherDelegate url_fetcher(system_context, example_url,
951 net::URLRequestStatus()); 953 net::URLRequestStatus());
952 url_fetcher.SetExpectedResponse(kExampleFullContent); 954 url_fetcher.SetExpectedResponse(kExampleFullContent);
953 url_fetcher.WaitForCompletion(); 955 url_fetcher.WaitForCompletion();
954 } 956 }
955 } 957 }
956 958
959 // Tests that the webRequest events aren't dispatched when the request initiator
960 // is protected by policy.
961 IN_PROC_BROWSER_TEST_F(ExtensionApiTestWithManagementPolicy,
962 InitiatorProtectedByPolicy) {
963 // We expect that no webRequest will be hidden or modification blocked. This
Devlin 2017/06/08 13:58:46 As mentioned at https://codereview.chromium.org/24
nrpeter 2017/06/08 15:47:42 Thanks must have missed this one.
964 // means that the request to example.com will be seen by the extension.
965 {
966 ExtensionManagementPolicyUpdater pref(&policy_provider_);
967 pref.AddRuntimeBlockedHost("*", "*://notexample.com");
968 }
969
970 ASSERT_TRUE(StartEmbeddedTestServer());
971
972 // Host navigated to.
973 const std::string example_com = "example.com";
974
975 // URL within the text extension that initiates cross domain requests when
Devlin 2017/06/08 13:58:46 two nits: - I assume s/text/test? - this actually
nrpeter 2017/06/08 15:47:42 Done.
976 // navigated to.
977 const GURL extension_test_url = embedded_test_server()->GetURL(
978 example_com,
979 "/extensions/api_test/webrequest/policy_blocked/ref_remote_js.html");
980
981 LoadExtension(test_data_dir_.AppendASCII("webrequest/policy_blocked"));
982
983 // Extension communicates back using this listener name.
984 const std::string listener_message = "protected_origin";
985
986 // Listen to verify extension sees the web request.
987 ExtensionTestMessageListener before_request_listener(listener_message, false);
988
989 // Wait until all remote Javascript files have been blocked / pulled down.
990 ui_test_utils::NavigateToURLWithDisposition(
991 browser(), extension_test_url, WindowOpenDisposition::CURRENT_TAB,
992 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
993
994 // Domain that hosts javascript file referenced by example_com.
995 const std::string example2_com = "example2.com";
996
997 // The server saw a request for the remote Javascript file.
998 EXPECT_TRUE(BrowsedTo(example2_com));
999
1000 // The webRequest was seen by the extension.
1001 EXPECT_TRUE(before_request_listener.was_satisfied());
1002
1003 // Clear the list of domains the server has seen.
1004 ClearRequestLog();
1005
1006 // Make sure we've cleared the embedded server history.
1007 EXPECT_FALSE(BrowsedTo(example2_com));
1008
1009 // Set the policy to hide requests to example.com or any resource
1010 // it includes. We expect that in this test, the request to example2.com
1011 // will not be seen by the extension.
1012 {
1013 ExtensionManagementPolicyUpdater pref(&policy_provider_);
1014 pref.AddRuntimeBlockedHost("*", "*://" + example_com);
1015 }
1016
1017 // Listen in case extension sees the requst.
1018 ExtensionTestMessageListener before_request_listener2(listener_message,
1019 false);
1020
1021 // Wait until all remote Javascript files have been pulled down.
1022 ui_test_utils::NavigateToURLWithDisposition(
1023 browser(), extension_test_url, WindowOpenDisposition::CURRENT_TAB,
1024 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1025
1026 // The server saw a request for the remote Javascript file.
1027 EXPECT_TRUE(BrowsedTo(example2_com));
1028
1029 // The request was hidden from the extension.
1030 EXPECT_FALSE(before_request_listener2.was_satisfied());
1031 }
1032
1033 // Tests that the webRequest events aren't dispatched when the URL of the
1034 // request is protected by policy.
1035 IN_PROC_BROWSER_TEST_F(ExtensionApiTestWithManagementPolicy,
1036 UrlProtectedByPolicy) {
1037 // Host protected by policy.
1038 const std::string protected_domain = "example.com";
1039
1040 {
1041 ExtensionManagementPolicyUpdater pref(&policy_provider_);
1042 pref.AddRuntimeBlockedHost("*", "*://" + protected_domain);
1043 }
1044
1045 ASSERT_TRUE(StartEmbeddedTestServer());
1046
1047 LoadExtension(test_data_dir_.AppendASCII("webrequest/policy_blocked"));
1048
1049 // Listen in case extension sees the requst.
1050 ExtensionTestMessageListener before_request_listener("protected_url", false);
1051
1052 // Path to resolve during test navigations.
1053 const std::string test_path = "/defaultresponse?protected_url";
1054
1055 // Navigate to the protected domain and wait until page fully loads.
1056 ui_test_utils::NavigateToURLWithDisposition(
1057 browser(), embedded_test_server()->GetURL(protected_domain, test_path),
1058 WindowOpenDisposition::CURRENT_TAB,
1059 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1060
1061 // The server saw a request for the protected site.
1062 EXPECT_TRUE(BrowsedTo(protected_domain));
1063
1064 // The request was hidden from the extension.
1065 EXPECT_FALSE(before_request_listener.was_satisfied());
1066
1067 // Host not protected by policy.
1068 const std::string unprotected_domain = "notblockedexample.com";
1069
1070 // Now we'll test browsing to a non-protected website where we expect the
1071 // extension to see the request.
1072 ui_test_utils::NavigateToURLWithDisposition(
1073 browser(), embedded_test_server()->GetURL(unprotected_domain, test_path),
1074 WindowOpenDisposition::CURRENT_TAB,
1075 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1076
1077 // The server saw a request for the non-protected site.
1078 EXPECT_TRUE(BrowsedTo(unprotected_domain));
1079
1080 // The request was visible from the extension.
1081 EXPECT_TRUE(before_request_listener.was_satisfied());
1082 }
1083
1084 // Test that no webRequest events are seen for a protected host during normal
1085 // navigation. This replicates most of the tests from
1086 // WebRequestWithWithheldPermissions with a protected host. Granting a tab
1087 // specific permission shouldn't bypass our policy.
1088 IN_PROC_BROWSER_TEST_F(ExtensionApiTestWithManagementPolicy,
1089 WebRequestProtectedByPolicy) {
1090 FeatureSwitch::ScopedOverride enable_scripts_require_action(
1091 FeatureSwitch::scripts_require_action(), true);
1092
1093 // Host protected by policy.
1094 const std::string protected_domain = "example.com";
1095
1096 {
1097 ExtensionManagementPolicyUpdater pref(&policy_provider_);
1098 pref.AddRuntimeBlockedHost("*", "*://" + protected_domain);
1099 }
1100
1101 ASSERT_TRUE(StartEmbeddedTestServer());
1102
1103 ExtensionTestMessageListener listener("ready", false);
1104 const Extension* extension =
1105 LoadExtension(test_data_dir_.AppendASCII("webrequest_activetab"));
1106 ASSERT_TRUE(extension) << message_;
1107 EXPECT_TRUE(listener.WaitUntilSatisfied());
1108
1109 // Navigate the browser to a page in a new tab.
1110 GURL url = embedded_test_server()->GetURL(protected_domain, "/empty.html");
1111 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK);
1112 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
1113 ui_test_utils::NavigateToURL(&params);
1114
1115 content::WebContents* web_contents =
1116 browser()->tab_strip_model()->GetActiveWebContents();
1117 ASSERT_TRUE(web_contents);
1118 ExtensionActionRunner* runner =
1119 ExtensionActionRunner::GetForWebContents(web_contents);
1120 ASSERT_TRUE(runner);
1121
1122 int port = embedded_test_server()->port();
1123 const std::string kXhrPath = "simple.html";
1124
1125 // The extension shouldn't have currently received any webRequest events,
1126 // since it doesn't have permission (and shouldn't receive any from an XHR).
1127 EXPECT_EQ(0, GetWebRequestCountFromBackgroundPage(extension, profile()));
1128 PerformXhrInFrame(web_contents->GetMainFrame(), protected_domain, port,
1129 kXhrPath);
1130 EXPECT_EQ(0, GetWebRequestCountFromBackgroundPage(extension, profile()));
1131
1132 // Grant activeTab permission, and perform another XHR. The extension should
1133 // still be blocked due to ExtensionSettings policy on example.com.
1134 // Only records ACCESS_WITHHELD, not ACCESS_DENIED, this is why it matches
1135 // BLOCKED_ACTION_NONE.
1136 EXPECT_EQ(BLOCKED_ACTION_NONE, runner->GetBlockedActions(extension));
1137 runner->set_default_bubble_close_action_for_testing(
1138 base::WrapUnique(new ToolbarActionsBarBubbleDelegate::CloseAction(
1139 ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE)));
1140 runner->RunAction(extension, true);
1141 base::RunLoop().RunUntilIdle();
1142 EXPECT_TRUE(content::WaitForLoadStop(web_contents));
1143 EXPECT_EQ(BLOCKED_ACTION_NONE, runner->GetBlockedActions(extension));
1144 int xhr_count = GetWebRequestCountFromBackgroundPage(extension, profile());
1145 // ... which means that we should have a non-zero xhr count if the policy
1146 // didn't block the events.
1147 EXPECT_EQ(0, xhr_count);
1148 // And the extension should also block future events.
1149 PerformXhrInFrame(web_contents->GetMainFrame(), protected_domain, port,
1150 kXhrPath);
1151 EXPECT_EQ(0, GetWebRequestCountFromBackgroundPage(extension, profile()));
1152 }
1153
957 } // namespace extensions 1154 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698