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" |
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 Loading... | |
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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 const net::URLRequestStatus expected_request_status_; | 182 const net::URLRequestStatus expected_request_status_; |
181 base::Optional<std::string> expected_response_; | 183 base::Optional<std::string> expected_response_; |
182 std::unique_ptr<net::URLFetcher> fetcher_; | 184 std::unique_ptr<net::URLFetcher> fetcher_; |
183 base::RunLoop run_loop_; | 185 base::RunLoop run_loop_; |
184 | 186 |
185 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherDelegate); | 187 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherDelegate); |
186 }; | 188 }; |
187 | 189 |
188 } // namespace | 190 } // namespace |
189 | 191 |
190 class ExtensionWebRequestApiTest : public ExtensionApiTest { | 192 class ExtensionWebRequestApiTest : public ExtensionApiTestWithManagementPolicy { |
Devlin
2017/05/25 20:38:32
Let's not have all web request API tests inherit f
nrpeter
2017/05/26 02:46:52
Done.
| |
191 public: | 193 public: |
192 void SetUpInProcessBrowserTestFixture() override { | 194 void SetUpInProcessBrowserTestFixture() override { |
193 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); | 195 ExtensionApiTestWithManagementPolicy::SetUpInProcessBrowserTestFixture(); |
194 host_resolver()->AddRule("*", "127.0.0.1"); | 196 host_resolver()->AddRule("*", "127.0.0.1"); |
195 } | 197 } |
196 | 198 |
197 void RunPermissionTest( | 199 void RunPermissionTest( |
198 const char* extension_directory, | 200 const char* extension_directory, |
199 bool load_extension_with_incognito_permission, | 201 bool load_extension_with_incognito_permission, |
200 bool wait_for_extension_loaded_in_incognito, | 202 bool wait_for_extension_loaded_in_incognito, |
201 const char* expected_content_regular_window, | 203 const char* expected_content_regular_window, |
202 const char* exptected_content_incognito_window); | 204 const char* exptected_content_incognito_window); |
203 }; | 205 }; |
(...skipping 743 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 // Test that web requests made by a webpage protected by the | |
Devlin
2017/05/25 20:38:32
test comments should be descriptive, not imperativ
nrpeter
2017/05/26 02:46:52
Done.
| |
960 // runtime_blocked_hosts of the ExtensionsSettings policy are not modifyable. | |
Devlin
2017/05/25 20:38:32
typo: modifiable
nrpeter
2017/05/26 02:46:52
Done.
| |
961 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, policyBlockByOrigin) { | |
Devlin
2017/05/25 20:38:32
Tests should be TitleCase
nrpeter
2017/05/26 02:46:52
Done.
| |
962 // Browse to protected example.com page which uses javascript to | |
963 // include a script from another domain example2.com. We use the | |
964 // chrome.webRequest API on the extension's background page to listen for | |
965 // requests to example2.com. In this first part of the test, we want to | |
966 // make sure our extension successfully detects requests to example2.com with | |
967 // the webRequest API. Additionally this tests that this protection only | |
968 // applies for domains listed in the policy. | |
969 | |
970 // Host that we navigate to in Chrome. | |
971 const std::string example_com = "example.com"; | |
972 | |
973 // Domain that hosts javascript file referenced by example_com. | |
974 const std::string example2_com = "example2.com"; | |
975 | |
976 // URL within the test extension we navigate to to initiate the requests. | |
977 const std::string extension_test_url = | |
978 "/extensions/api_test/webrequest/policy_blocked/ref_remote_js.html"; | |
979 | |
980 // Extension communicates back using this listener name. | |
981 const std::string listener_name = "protected_origin"; | |
982 | |
983 // We expect that no webRequest will be hidden or modification blocked. This | |
984 // means that the request to example.com will be seen by the extension. | |
985 { | |
986 ExtensionManagementPolicyUpdater pref(&policy_provider_); | |
987 pref.AddRuntimeBlockedHost("*", "*://not_example.com"); | |
Devlin
2017/05/25 20:38:32
Underscores aren't allowed in domain names by the
nrpeter
2017/05/26 02:46:51
Done.
| |
988 } | |
989 // Set auto confirm UI flag. | |
990 PermissionsRequestFunction::SetAutoConfirmForTests(true); | |
Devlin
2017/05/25 20:38:32
Why do we need this?
nrpeter
2017/05/26 02:46:51
Done.
| |
991 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true); | |
992 | |
993 ASSERT_TRUE(StartEmbeddedTestServer()); | |
994 | |
995 LoadExtension(test_data_dir_.AppendASCII("webrequest/policy_blocked")); | |
996 | |
997 // Listen to verify extension sees the web request. | |
998 ExtensionTestMessageListener before_request_listener(listener_name, false); | |
999 | |
1000 // Wait until all remote Javascript files have been blocked / pulled down. | |
1001 ui_test_utils::NavigateToURLWithDisposition( | |
1002 browser(), | |
1003 embedded_test_server()->GetURL(example_com, extension_test_url), | |
Devlin
2017/05/25 20:38:32
instead of const'ing example.com and the file, we
nrpeter
2017/05/26 02:46:52
Made a const GURL as suggested. However I kept exa
| |
1004 WindowOpenDisposition::CURRENT_TAB, | |
1005 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
1006 | |
1007 // The server saw a request for the remote Javascript file. | |
1008 EXPECT_TRUE(BrowsedTo(example2_com)); | |
1009 | |
1010 // The webRequest was seen by the extension. | |
1011 EXPECT_TRUE(before_request_listener.was_satisfied()); | |
1012 | |
1013 // Clear the list of domains the server has seen. | |
1014 ClearRequestLog(); | |
1015 | |
1016 // Make sure we've cleared the embedded server history. | |
1017 EXPECT_FALSE(BrowsedTo(example2_com)); | |
1018 | |
1019 // Now lets set the policy to hide webRequests to example.com or any resource | |
Devlin
2017/05/25 20:38:32
In comments like this, omit words like "let's", "w
nrpeter
2017/05/26 02:46:51
Think I got all of these. If you notice any I miss
| |
1020 // it includes. We expect that in this test, the webRequest to example2.com | |
1021 // will not be seen by the extension. | |
1022 { | |
1023 ExtensionManagementPolicyUpdater pref(&policy_provider_); | |
1024 pref.AddRuntimeBlockedHost("*", "*://" + example_com); | |
1025 } | |
1026 | |
1027 // Listen in case extension sees the web requst. | |
1028 ExtensionTestMessageListener before_request_listener2(listener_name, false); | |
1029 | |
1030 // Wait until all remote Javascript files have been pulled down. | |
1031 ui_test_utils::NavigateToURLWithDisposition( | |
1032 browser(), | |
1033 embedded_test_server()->GetURL(example_com, extension_test_url), | |
1034 WindowOpenDisposition::CURRENT_TAB, | |
1035 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
1036 | |
1037 // The server saw a request for the remote Javascript file. | |
1038 EXPECT_TRUE(BrowsedTo(example2_com)); | |
1039 | |
1040 // The webRequest was hidden from the extension. | |
1041 EXPECT_FALSE(before_request_listener2.was_satisfied()); | |
1042 } | |
1043 | |
1044 // Test that web requests made to a resource protected by the | |
1045 // runtime_blocked_hosts of the ExtensionsSettings policy are not visible. | |
1046 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, policyBlockByUrl) { | |
1047 // We'll set policy to block webRequest events for example.com domains. Then | |
1048 // navigate to example.com and make sure our extension doesn't see the event. | |
1049 // We then navigate to a website not protected by policy and make sure that | |
1050 // our extension can see the event. | |
1051 | |
1052 // Set example.com as protected using runtime_blocked_hosts from the | |
1053 // ExtensionSettings policy. | |
1054 { | |
1055 ExtensionManagementPolicyUpdater pref(&policy_provider_); | |
1056 pref.AddRuntimeBlockedHost("*", "*://example.com"); | |
1057 } | |
1058 | |
1059 // Set auto confirm UI flag. | |
1060 PermissionsRequestFunction::SetAutoConfirmForTests(true); | |
1061 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true); | |
1062 | |
1063 ASSERT_TRUE(StartEmbeddedTestServer()); | |
1064 | |
1065 LoadExtension(test_data_dir_.AppendASCII("webrequest/policy_blocked")); | |
1066 | |
1067 // Listen in case extension sees the web requst. | |
1068 ExtensionTestMessageListener before_request_listener("protected_url", false); | |
1069 | |
1070 // Navigate to the protected domain and wait until page fully loads. | |
1071 ui_test_utils::NavigateToURLWithDisposition( | |
1072 browser(), | |
1073 embedded_test_server()->GetURL( | |
1074 "example.com", | |
1075 "/extensions/api_test/webrequest/policy_blocked/protected_url.html"), | |
1076 WindowOpenDisposition::CURRENT_TAB, | |
1077 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
1078 | |
1079 // The server saw a request for the protected site. | |
1080 EXPECT_TRUE(BrowsedTo("example.com")); | |
1081 | |
1082 // The webRequest was hidden from the extension. | |
1083 EXPECT_FALSE(before_request_listener.was_satisfied()); | |
1084 | |
1085 // Now we'll test browsing to a non-protected website where we expect the | |
1086 // extension to see the webrequest event. | |
1087 ui_test_utils::NavigateToURLWithDisposition( | |
1088 browser(), | |
1089 embedded_test_server()->GetURL( | |
1090 "not_blocked_example.com", | |
1091 "/extensions/api_test/webrequest/policy_blocked/protected_url.html"), | |
1092 WindowOpenDisposition::CURRENT_TAB, | |
1093 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
1094 | |
1095 // The server saw a request for the non-protected site. | |
1096 EXPECT_TRUE(BrowsedTo("not_blocked_example.com")); | |
1097 | |
1098 // The webRequest was visible from the extension. | |
1099 EXPECT_TRUE(before_request_listener.was_satisfied()); | |
1100 } | |
1101 | |
1102 // Test that no webRequest events are seen for a protected host during normal | |
1103 // navigation. This replicates most of the tests from | |
1104 // WebRequestWithWithheldPermissions with a protected host. Granting a tab | |
1105 // specific permission shouldn't bypass our policy. | |
1106 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, | |
1107 WebRequestProtectedByPolicy) { | |
1108 FeatureSwitch::ScopedOverride enable_scripts_require_action( | |
1109 FeatureSwitch::scripts_require_action(), true); | |
1110 { | |
1111 ExtensionManagementPolicyUpdater pref(&policy_provider_); | |
1112 pref.AddRuntimeBlockedHost("*", "*://example.com"); | |
1113 } | |
1114 extensions::PermissionsRequestFunction::SetIgnoreUserGestureForTests(true); | |
1115 extensions::PermissionsRequestFunction::SetAutoConfirmForTests(true); | |
1116 ASSERT_TRUE(StartEmbeddedTestServer()); | |
1117 | |
1118 ExtensionTestMessageListener listener("ready", false); | |
1119 const Extension* extension = | |
1120 LoadExtension(test_data_dir_.AppendASCII("webrequest_activetab")); | |
1121 ASSERT_TRUE(extension) << message_; | |
1122 EXPECT_TRUE(listener.WaitUntilSatisfied()); | |
1123 | |
1124 // Navigate the browser to a page in a new tab. | |
1125 const std::string kHost = "example.com"; | |
1126 GURL url = embedded_test_server()->GetURL(kHost, "/empty.html"); | |
1127 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK); | |
1128 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; | |
1129 ui_test_utils::NavigateToURL(¶ms); | |
1130 | |
1131 content::WebContents* web_contents = | |
1132 browser()->tab_strip_model()->GetActiveWebContents(); | |
1133 ASSERT_TRUE(web_contents); | |
1134 ExtensionActionRunner* runner = | |
1135 ExtensionActionRunner::GetForWebContents(web_contents); | |
1136 ASSERT_TRUE(runner); | |
1137 | |
1138 int port = embedded_test_server()->port(); | |
1139 const std::string kXhrPath = "simple.html"; | |
1140 | |
1141 // The extension shouldn't have currently received any webRequest events, | |
1142 // since it doesn't have permission (and shouldn't receive any from an XHR). | |
1143 EXPECT_EQ(0, GetWebRequestCountFromBackgroundPage(extension, profile())); | |
1144 PerformXhrInFrame(web_contents->GetMainFrame(), kHost, port, kXhrPath); | |
1145 EXPECT_EQ(0, GetWebRequestCountFromBackgroundPage(extension, profile())); | |
1146 | |
1147 // Grant activeTab permission, and perform another XHR. The extension should | |
1148 // still be blocked due to ExtensionSettings policy on example.com. | |
1149 // Only records ACCESS_WITHHELD, not ACCESS_DENIED, this is why we match | |
1150 // BLOCKED_ACTION_NONE. | |
1151 EXPECT_EQ(BLOCKED_ACTION_NONE, runner->GetBlockedActions(extension)); | |
1152 runner->set_default_bubble_close_action_for_testing( | |
1153 base::WrapUnique(new ToolbarActionsBarBubbleDelegate::CloseAction( | |
1154 ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE))); | |
1155 runner->RunAction(extension, true); | |
1156 base::RunLoop().RunUntilIdle(); | |
1157 EXPECT_TRUE(content::WaitForLoadStop(web_contents)); | |
1158 // The runner will have refreshed the page... | |
1159 EXPECT_EQ(BLOCKED_ACTION_NONE, runner->GetBlockedActions(extension)); | |
1160 int xhr_count = GetWebRequestCountFromBackgroundPage(extension, profile()); | |
1161 // ... which means that we should have a non-zero xhr count if the policy | |
1162 // didn't block the events. | |
1163 EXPECT_EQ(xhr_count, 0); | |
1164 // And the extension should also block future events. | |
1165 PerformXhrInFrame(web_contents->GetMainFrame(), kHost, port, kXhrPath); | |
1166 EXPECT_EQ(xhr_count, | |
1167 GetWebRequestCountFromBackgroundPage(extension, profile())); | |
1168 } | |
1169 | |
957 } // namespace extensions | 1170 } // namespace extensions |
OLD | NEW |