| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <map> | 5 #include <map> |
| 6 #include <memory> | 6 #include <memory> |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/strings/string_piece.h" | 17 #include "base/strings/string_piece.h" |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 20 #include "base/test/histogram_tester.h" | 20 #include "base/test/histogram_tester.h" |
| 21 #include "base/test/simple_test_clock.h" |
| 21 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
| 22 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 23 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 23 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 24 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 24 #include "chrome/browser/metrics/subprocess_metrics_provider.h" | 25 #include "chrome/browser/metrics/subprocess_metrics_provider.h" |
| 25 #include "chrome/browser/page_load_metrics/observers/subresource_filter_metrics_
observer.h" | 26 #include "chrome/browser/page_load_metrics/observers/subresource_filter_metrics_
observer.h" |
| 26 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" | 27 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| 27 #include "chrome/browser/safe_browsing/v4_test_utils.h" | 28 #include "chrome/browser/safe_browsing/v4_test_utils.h" |
| 28 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" | 29 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
| 30 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_
manager.h" |
| 31 #include "chrome/browser/subresource_filter/subresource_filter_profile_context.h
" |
| 32 #include "chrome/browser/subresource_filter/subresource_filter_profile_context_f
actory.h" |
| 29 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h" | 33 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h" |
| 30 #include "chrome/browser/ui/browser.h" | 34 #include "chrome/browser/ui/browser.h" |
| 31 #include "chrome/browser/ui/browser_commands.h" | 35 #include "chrome/browser/ui/browser_commands.h" |
| 32 #include "chrome/browser/ui/browser_navigator.h" | 36 #include "chrome/browser/ui/browser_navigator.h" |
| 33 #include "chrome/browser/ui/browser_navigator_params.h" | 37 #include "chrome/browser/ui/browser_navigator_params.h" |
| 34 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 38 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 35 #include "chrome/common/chrome_paths.h" | 39 #include "chrome/common/chrome_paths.h" |
| 36 #include "chrome/common/url_constants.h" | 40 #include "chrome/common/url_constants.h" |
| 37 #include "chrome/test/base/in_process_browser_test.h" | 41 #include "chrome/test/base/in_process_browser_test.h" |
| 38 #include "chrome/test/base/ui_test_utils.h" | 42 #include "chrome/test/base/ui_test_utils.h" |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 | 259 |
| 256 void SetUpOnMainThread() override { | 260 void SetUpOnMainThread() override { |
| 257 base::FilePath test_data_dir; | 261 base::FilePath test_data_dir; |
| 258 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | 262 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
| 259 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | 263 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
| 260 host_resolver()->AddSimulatedFailure("host-with-dns-lookup-failure"); | 264 host_resolver()->AddSimulatedFailure("host-with-dns-lookup-failure"); |
| 261 host_resolver()->AddRule("*", "127.0.0.1"); | 265 host_resolver()->AddRule("*", "127.0.0.1"); |
| 262 content::SetupCrossSiteRedirector(embedded_test_server()); | 266 content::SetupCrossSiteRedirector(embedded_test_server()); |
| 263 ASSERT_TRUE(embedded_test_server()->Start()); | 267 ASSERT_TRUE(embedded_test_server()->Start()); |
| 264 ResetConfigurationToEnableOnPhishingSites(); | 268 ResetConfigurationToEnableOnPhishingSites(); |
| 269 |
| 270 settings_manager_ = SubresourceFilterProfileContextFactory::GetForProfile( |
| 271 browser()->profile()) |
| 272 ->settings_manager(); |
| 273 #if defined(OS_ANDROID) |
| 274 EXPECT_TRUE(settings_manager->should_use_smart_ui()); |
| 275 #endif |
| 265 } | 276 } |
| 266 | 277 |
| 267 GURL GetTestUrl(const std::string& relative_url) { | 278 GURL GetTestUrl(const std::string& relative_url) { |
| 268 return embedded_test_server()->base_url().Resolve(relative_url); | 279 return embedded_test_server()->base_url().Resolve(relative_url); |
| 269 } | 280 } |
| 270 | 281 |
| 271 void MarkUrlAsMatchingListWithId( | 282 void MarkUrlAsMatchingListWithId( |
| 272 const GURL& bad_url, | 283 const GURL& bad_url, |
| 273 const safe_browsing::ListIdentifier& list_id, | 284 const safe_browsing::ListIdentifier& list_id, |
| 274 safe_browsing::ThreatPatternType threat_pattern_type) { | 285 safe_browsing::ThreatPatternType threat_pattern_type) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 285 | 296 |
| 286 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { | 297 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { |
| 287 MarkUrlAsMatchingListWithId(url, safe_browsing::GetUrlSubresourceFilterId(), | 298 MarkUrlAsMatchingListWithId(url, safe_browsing::GetUrlSubresourceFilterId(), |
| 288 safe_browsing::ThreatPatternType::NONE); | 299 safe_browsing::ThreatPatternType::NONE); |
| 289 } | 300 } |
| 290 | 301 |
| 291 content::WebContents* web_contents() { | 302 content::WebContents* web_contents() { |
| 292 return browser()->tab_strip_model()->GetActiveWebContents(); | 303 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 293 } | 304 } |
| 294 | 305 |
| 306 SubresourceFilterContentSettingsManager* settings_manager() { |
| 307 return settings_manager_; |
| 308 } |
| 309 |
| 295 content::RenderFrameHost* FindFrameByName(const std::string& name) { | 310 content::RenderFrameHost* FindFrameByName(const std::string& name) { |
| 296 return content::FrameMatchingPredicate( | 311 return content::FrameMatchingPredicate( |
| 297 web_contents(), base::Bind(&content::FrameMatchesName, name)); | 312 web_contents(), base::Bind(&content::FrameMatchesName, name)); |
| 298 } | 313 } |
| 299 | 314 |
| 300 bool WasParsedScriptElementLoaded(content::RenderFrameHost* rfh) { | 315 bool WasParsedScriptElementLoaded(content::RenderFrameHost* rfh) { |
| 301 DCHECK(rfh); | 316 DCHECK(rfh); |
| 302 bool script_resource_was_loaded = false; | 317 bool script_resource_was_loaded = false; |
| 303 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | 318 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 304 rfh, "domAutomationController.send(!!document.scriptExecuted)", | 319 rfh, "domAutomationController.send(!!document.scriptExecuted)", |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 TestRulesetCreator ruleset_creator_; | 391 TestRulesetCreator ruleset_creator_; |
| 377 ScopedSubresourceFilterConfigurator scoped_configuration_; | 392 ScopedSubresourceFilterConfigurator scoped_configuration_; |
| 378 TestRulesetPublisher test_ruleset_publisher_; | 393 TestRulesetPublisher test_ruleset_publisher_; |
| 379 | 394 |
| 380 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> sb_factory_; | 395 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> sb_factory_; |
| 381 // Owned by the V4Database. | 396 // Owned by the V4Database. |
| 382 safe_browsing::TestV4DatabaseFactory* v4_db_factory_; | 397 safe_browsing::TestV4DatabaseFactory* v4_db_factory_; |
| 383 // Owned by the V4GetHashProtocolManager. | 398 // Owned by the V4GetHashProtocolManager. |
| 384 safe_browsing::TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; | 399 safe_browsing::TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; |
| 385 | 400 |
| 401 // Owned by the profile. |
| 402 SubresourceFilterContentSettingsManager* settings_manager_; |
| 403 |
| 386 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterBrowserTest); | 404 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterBrowserTest); |
| 387 }; | 405 }; |
| 388 | 406 |
| 389 enum WebSocketCreationPolicy { | 407 enum WebSocketCreationPolicy { |
| 390 IN_MAIN_FRAME, | 408 IN_MAIN_FRAME, |
| 391 IN_WORKER, | 409 IN_WORKER, |
| 392 }; | 410 }; |
| 393 class SubresourceFilterWebSocketBrowserTest | 411 class SubresourceFilterWebSocketBrowserTest |
| 394 : public SubresourceFilterBrowserTest, | 412 : public SubresourceFilterBrowserTest, |
| 395 public ::testing::WithParamInterface<WebSocketCreationPolicy> { | 413 public ::testing::WithParamInterface<WebSocketCreationPolicy> { |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 // page load right after start-up. | 836 // page load right after start-up. |
| 819 ui_test_utils::NavigateToURL(browser(), url); | 837 ui_test_utils::NavigateToURL(browser(), url); |
| 820 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); | 838 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 821 } | 839 } |
| 822 | 840 |
| 823 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, | 841 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| 824 PromptShownAgainOnNextNavigation) { | 842 PromptShownAgainOnNextNavigation) { |
| 825 ASSERT_NO_FATAL_FAILURE( | 843 ASSERT_NO_FATAL_FAILURE( |
| 826 SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); | 844 SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); |
| 827 GURL url(GetTestUrl(kTestFrameSetPath)); | 845 GURL url(GetTestUrl(kTestFrameSetPath)); |
| 846 GURL a_url(embedded_test_server()->GetURL( |
| 847 "a.com", "/subresource_filter/frame_with_included_script.html")); |
| 828 ConfigureAsPhishingURL(url); | 848 ConfigureAsPhishingURL(url); |
| 829 base::HistogramTester tester; | 849 base::HistogramTester tester; |
| 830 ui_test_utils::NavigateToURL(browser(), url); | 850 ui_test_utils::NavigateToURL(browser(), url); |
| 831 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, | 851 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, |
| 832 1); | 852 1); |
| 833 // Check that the bubble is not shown again for this navigation. | 853 // Check that the bubble is not shown again for this navigation. |
| 834 EXPECT_FALSE(IsDynamicScriptElementLoaded(FindFrameByName("five"))); | 854 EXPECT_FALSE(IsDynamicScriptElementLoaded(FindFrameByName("five"))); |
| 835 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, | 855 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, |
| 836 1); | 856 1); |
| 837 // Check that bubble is shown for new navigation. | 857 // Check that bubble is shown for new navigation. Must be cross site to avoid |
| 838 ui_test_utils::NavigateToURL(browser(), url); | 858 // triggering smart UI on Android. |
| 859 ConfigureAsPhishingURL(a_url); |
| 860 ui_test_utils::NavigateToURL(browser(), a_url); |
| 839 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, | 861 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, |
| 840 2); | 862 2); |
| 841 } | 863 } |
| 842 | 864 |
| 843 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, | 865 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| 844 CrossSiteSubFrameActivationWithoutWhitelist) { | 866 CrossSiteSubFrameActivationWithoutWhitelist) { |
| 845 GURL a_url(embedded_test_server()->GetURL( | 867 GURL a_url(embedded_test_server()->GetURL( |
| 846 "a.com", "/subresource_filter/frame_cross_site_set.html")); | 868 "a.com", "/subresource_filter/frame_cross_site_set.html")); |
| 847 ConfigureAsPhishingURL(a_url); | 869 ConfigureAsPhishingURL(a_url); |
| 848 ASSERT_NO_FATAL_FAILURE( | 870 ASSERT_NO_FATAL_FAILURE( |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 997 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); | 1019 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 998 | 1020 |
| 999 // A cross site blacklisted navigation should stay activated, however. | 1021 // A cross site blacklisted navigation should stay activated, however. |
| 1000 GURL a_url(embedded_test_server()->GetURL( | 1022 GURL a_url(embedded_test_server()->GetURL( |
| 1001 "a.com", "/subresource_filter/frame_with_included_script.html")); | 1023 "a.com", "/subresource_filter/frame_with_included_script.html")); |
| 1002 ConfigureAsPhishingURL(a_url); | 1024 ConfigureAsPhishingURL(a_url); |
| 1003 ui_test_utils::NavigateToURL(browser(), a_url); | 1025 ui_test_utils::NavigateToURL(browser(), a_url); |
| 1004 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); | 1026 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 1005 } | 1027 } |
| 1006 | 1028 |
| 1029 // Test the "smart" UI, aka the logic to hide the UI on subsequent same-domain |
| 1030 // navigations, until a certain time threshold has been reached. This is an |
| 1031 // android-only feature. |
| 1032 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| 1033 DoNotShowUIUntilThresholdReached) { |
| 1034 ASSERT_NO_FATAL_FAILURE( |
| 1035 SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); |
| 1036 GURL a_url(embedded_test_server()->GetURL( |
| 1037 "a.com", "/subresource_filter/frame_with_included_script.html")); |
| 1038 GURL b_url(embedded_test_server()->GetURL( |
| 1039 "b.com", "/subresource_filter/frame_with_included_script.html")); |
| 1040 // Test utils only support one blacklisted site at a time. |
| 1041 // TODO(csharrison): Add support for more than one URL. |
| 1042 ConfigureAsPhishingURL(a_url); |
| 1043 |
| 1044 // Cast is safe because this is the only type of client in non-unittest code. |
| 1045 ChromeSubresourceFilterClient* client = |
| 1046 static_cast<ChromeSubresourceFilterClient*>( |
| 1047 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents()) |
| 1048 ->client()); |
| 1049 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 1050 base::SimpleTestClock* raw_clock = test_clock.get(); |
| 1051 settings_manager()->set_clock_for_testing(std::move(test_clock)); |
| 1052 |
| 1053 base::HistogramTester histogram_tester; |
| 1054 |
| 1055 // First load should trigger the UI. |
| 1056 ui_test_utils::NavigateToURL(browser(), a_url); |
| 1057 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 1058 EXPECT_TRUE(client->did_show_ui_for_navigation()); |
| 1059 |
| 1060 histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, |
| 1061 kActionUISuppressed, 0); |
| 1062 |
| 1063 // Second load should not trigger the UI, but should still filter content. |
| 1064 ui_test_utils::NavigateToURL(browser(), a_url); |
| 1065 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 1066 |
| 1067 bool use_smart_ui = settings_manager()->should_use_smart_ui(); |
| 1068 EXPECT_EQ(client->did_show_ui_for_navigation(), !use_smart_ui); |
| 1069 |
| 1070 histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, |
| 1071 kActionUISuppressed, use_smart_ui ? 1 : 0); |
| 1072 |
| 1073 ConfigureAsPhishingURL(b_url); |
| 1074 |
| 1075 // Load to another domain should trigger the UI. |
| 1076 ui_test_utils::NavigateToURL(browser(), b_url); |
| 1077 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 1078 EXPECT_TRUE(client->did_show_ui_for_navigation()); |
| 1079 |
| 1080 ConfigureAsPhishingURL(a_url); |
| 1081 |
| 1082 // Fast forward the clock, and a_url should trigger the UI again. |
| 1083 raw_clock->Advance( |
| 1084 SubresourceFilterContentSettingsManager::kDelayBeforeShowingInfobarAgain); |
| 1085 ui_test_utils::NavigateToURL(browser(), a_url); |
| 1086 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 1087 EXPECT_TRUE(client->did_show_ui_for_navigation()); |
| 1088 |
| 1089 histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, |
| 1090 kActionUISuppressed, use_smart_ui ? 1 : 0); |
| 1091 } |
| 1092 |
| 1007 IN_PROC_BROWSER_TEST_P(SubresourceFilterWebSocketBrowserTest, BlockWebSocket) { | 1093 IN_PROC_BROWSER_TEST_P(SubresourceFilterWebSocketBrowserTest, BlockWebSocket) { |
| 1008 GURL url(GetTestUrl( | 1094 GURL url(GetTestUrl( |
| 1009 base::StringPrintf("subresource_filter/page_with_websocket.html?%s", | 1095 base::StringPrintf("subresource_filter/page_with_websocket.html?%s", |
| 1010 GetParam() == IN_WORKER ? "inWorker" : ""))); | 1096 GetParam() == IN_WORKER ? "inWorker" : ""))); |
| 1011 GURL websocket_url(GetWebSocketUrl("echo-with-no-extension")); | 1097 GURL websocket_url(GetWebSocketUrl("echo-with-no-extension")); |
| 1012 ConfigureAsPhishingURL(url); | 1098 ConfigureAsPhishingURL(url); |
| 1013 ASSERT_NO_FATAL_FAILURE( | 1099 ASSERT_NO_FATAL_FAILURE( |
| 1014 SetRulesetToDisallowURLsWithPathSuffix("echo-with-no-extension")); | 1100 SetRulesetToDisallowURLsWithPathSuffix("echo-with-no-extension")); |
| 1015 ui_test_utils::NavigateToURL(browser(), url); | 1101 ui_test_utils::NavigateToURL(browser(), url); |
| 1016 CreateWebSocketAndExpectResult(websocket_url, | 1102 CreateWebSocketAndExpectResult(websocket_url, |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1373 std::string(kSubresourceFilterOnlySuffix)), | 1459 std::string(kSubresourceFilterOnlySuffix)), |
| 1374 ::testing::IsEmpty()); | 1460 ::testing::IsEmpty()); |
| 1375 | 1461 |
| 1376 EXPECT_THAT(tester.GetAllSamples(std::string(kNavigationChainSize) + | 1462 EXPECT_THAT(tester.GetAllSamples(std::string(kNavigationChainSize) + |
| 1377 std::string(kSubresourceFilterOnlySuffix)), | 1463 std::string(kSubresourceFilterOnlySuffix)), |
| 1378 ::testing::IsEmpty()); | 1464 ::testing::IsEmpty()); |
| 1379 } | 1465 } |
| 1380 #endif | 1466 #endif |
| 1381 | 1467 |
| 1382 } // namespace subresource_filter | 1468 } // namespace subresource_filter |
| OLD | NEW |