| 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 for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) { | 311 for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) { |
| 297 if (frame->GetFrameName() == name) | 312 if (frame->GetFrameName() == name) |
| 298 return frame; | 313 return frame; |
| 299 } | 314 } |
| 300 return nullptr; | 315 return nullptr; |
| 301 } | 316 } |
| 302 | 317 |
| 303 bool WasParsedScriptElementLoaded(content::RenderFrameHost* rfh) { | 318 bool WasParsedScriptElementLoaded(content::RenderFrameHost* rfh) { |
| 304 DCHECK(rfh); | 319 DCHECK(rfh); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 TestRulesetCreator ruleset_creator_; | 394 TestRulesetCreator ruleset_creator_; |
| 380 ScopedSubresourceFilterConfigurator scoped_configuration_; | 395 ScopedSubresourceFilterConfigurator scoped_configuration_; |
| 381 TestRulesetPublisher test_ruleset_publisher_; | 396 TestRulesetPublisher test_ruleset_publisher_; |
| 382 | 397 |
| 383 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> sb_factory_; | 398 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> sb_factory_; |
| 384 // Owned by the V4Database. | 399 // Owned by the V4Database. |
| 385 safe_browsing::TestV4DatabaseFactory* v4_db_factory_; | 400 safe_browsing::TestV4DatabaseFactory* v4_db_factory_; |
| 386 // Owned by the V4GetHashProtocolManager. | 401 // Owned by the V4GetHashProtocolManager. |
| 387 safe_browsing::TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; | 402 safe_browsing::TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; |
| 388 | 403 |
| 404 // Owned by the profile. |
| 405 SubresourceFilterContentSettingsManager* settings_manager_; |
| 406 |
| 389 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterBrowserTest); | 407 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterBrowserTest); |
| 390 }; | 408 }; |
| 391 | 409 |
| 392 enum WebSocketCreationPolicy { | 410 enum WebSocketCreationPolicy { |
| 393 IN_MAIN_FRAME, | 411 IN_MAIN_FRAME, |
| 394 IN_WORKER, | 412 IN_WORKER, |
| 395 }; | 413 }; |
| 396 class SubresourceFilterWebSocketBrowserTest | 414 class SubresourceFilterWebSocketBrowserTest |
| 397 : public SubresourceFilterBrowserTest, | 415 : public SubresourceFilterBrowserTest, |
| 398 public ::testing::WithParamInterface<WebSocketCreationPolicy> { | 416 public ::testing::WithParamInterface<WebSocketCreationPolicy> { |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 // page load right after start-up. | 839 // page load right after start-up. |
| 822 ui_test_utils::NavigateToURL(browser(), url); | 840 ui_test_utils::NavigateToURL(browser(), url); |
| 823 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); | 841 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 824 } | 842 } |
| 825 | 843 |
| 826 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, | 844 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| 827 PromptShownAgainOnNextNavigation) { | 845 PromptShownAgainOnNextNavigation) { |
| 828 ASSERT_NO_FATAL_FAILURE( | 846 ASSERT_NO_FATAL_FAILURE( |
| 829 SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); | 847 SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); |
| 830 GURL url(GetTestUrl(kTestFrameSetPath)); | 848 GURL url(GetTestUrl(kTestFrameSetPath)); |
| 849 GURL a_url(embedded_test_server()->GetURL( |
| 850 "a.com", "/subresource_filter/frame_with_included_script.html")); |
| 831 ConfigureAsPhishingURL(url); | 851 ConfigureAsPhishingURL(url); |
| 832 base::HistogramTester tester; | 852 base::HistogramTester tester; |
| 833 ui_test_utils::NavigateToURL(browser(), url); | 853 ui_test_utils::NavigateToURL(browser(), url); |
| 834 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, | 854 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, |
| 835 1); | 855 1); |
| 836 // Check that the bubble is not shown again for this navigation. | 856 // Check that the bubble is not shown again for this navigation. |
| 837 EXPECT_FALSE(IsDynamicScriptElementLoaded(FindFrameByName("five"))); | 857 EXPECT_FALSE(IsDynamicScriptElementLoaded(FindFrameByName("five"))); |
| 838 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, | 858 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, |
| 839 1); | 859 1); |
| 840 // Check that bubble is shown for new navigation. | 860 // Check that bubble is shown for new navigation. Must be cross site to avoid |
| 841 ui_test_utils::NavigateToURL(browser(), url); | 861 // triggering smart UI on Android. |
| 862 ConfigureAsPhishingURL(a_url); |
| 863 ui_test_utils::NavigateToURL(browser(), a_url); |
| 842 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, | 864 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, |
| 843 2); | 865 2); |
| 844 } | 866 } |
| 845 | 867 |
| 846 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, | 868 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| 847 CrossSiteSubFrameActivationWithoutWhitelist) { | 869 CrossSiteSubFrameActivationWithoutWhitelist) { |
| 848 GURL a_url(embedded_test_server()->GetURL( | 870 GURL a_url(embedded_test_server()->GetURL( |
| 849 "a.com", "/subresource_filter/frame_cross_site_set.html")); | 871 "a.com", "/subresource_filter/frame_cross_site_set.html")); |
| 850 ConfigureAsPhishingURL(a_url); | 872 ConfigureAsPhishingURL(a_url); |
| 851 ASSERT_NO_FATAL_FAILURE( | 873 ASSERT_NO_FATAL_FAILURE( |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1000 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); | 1022 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 1001 | 1023 |
| 1002 // A cross site blacklisted navigation should stay activated, however. | 1024 // A cross site blacklisted navigation should stay activated, however. |
| 1003 GURL a_url(embedded_test_server()->GetURL( | 1025 GURL a_url(embedded_test_server()->GetURL( |
| 1004 "a.com", "/subresource_filter/frame_with_included_script.html")); | 1026 "a.com", "/subresource_filter/frame_with_included_script.html")); |
| 1005 ConfigureAsPhishingURL(a_url); | 1027 ConfigureAsPhishingURL(a_url); |
| 1006 ui_test_utils::NavigateToURL(browser(), a_url); | 1028 ui_test_utils::NavigateToURL(browser(), a_url); |
| 1007 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); | 1029 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 1008 } | 1030 } |
| 1009 | 1031 |
| 1032 // Test the "smart" UI, aka the logic to hide the UI on subsequent same-domain |
| 1033 // navigations, until a certain time threshold has been reached. This is an |
| 1034 // android-only feature. |
| 1035 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| 1036 DoNotShowUIUntilThresholdReached) { |
| 1037 ASSERT_NO_FATAL_FAILURE( |
| 1038 SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); |
| 1039 GURL a_url(embedded_test_server()->GetURL( |
| 1040 "a.com", "/subresource_filter/frame_with_included_script.html")); |
| 1041 GURL b_url(embedded_test_server()->GetURL( |
| 1042 "b.com", "/subresource_filter/frame_with_included_script.html")); |
| 1043 // Test utils only support one blacklisted site at a time. |
| 1044 // TODO(csharrison): Add support for more than one URL. |
| 1045 ConfigureAsPhishingURL(a_url); |
| 1046 |
| 1047 // Cast is safe because this is the only type of client in non-unittest code. |
| 1048 ChromeSubresourceFilterClient* client = |
| 1049 static_cast<ChromeSubresourceFilterClient*>( |
| 1050 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents()) |
| 1051 ->client()); |
| 1052 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 1053 base::SimpleTestClock* raw_clock = test_clock.get(); |
| 1054 settings_manager()->set_clock_for_testing(std::move(test_clock)); |
| 1055 |
| 1056 base::HistogramTester histogram_tester; |
| 1057 |
| 1058 // First load should trigger the UI. |
| 1059 ui_test_utils::NavigateToURL(browser(), a_url); |
| 1060 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 1061 EXPECT_TRUE(client->did_show_ui_for_navigation()); |
| 1062 |
| 1063 histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, |
| 1064 kActionUISuppressed, 0); |
| 1065 |
| 1066 // Second load should not trigger the UI, but should still filter content. |
| 1067 ui_test_utils::NavigateToURL(browser(), a_url); |
| 1068 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 1069 |
| 1070 bool use_smart_ui = settings_manager()->should_use_smart_ui(); |
| 1071 EXPECT_EQ(client->did_show_ui_for_navigation(), !use_smart_ui); |
| 1072 |
| 1073 histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, |
| 1074 kActionUISuppressed, use_smart_ui ? 1 : 0); |
| 1075 |
| 1076 ConfigureAsPhishingURL(b_url); |
| 1077 |
| 1078 // Load to another domain should trigger the UI. |
| 1079 ui_test_utils::NavigateToURL(browser(), b_url); |
| 1080 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 1081 EXPECT_TRUE(client->did_show_ui_for_navigation()); |
| 1082 |
| 1083 ConfigureAsPhishingURL(a_url); |
| 1084 |
| 1085 // Fast forward the clock, and a_url should trigger the UI again. |
| 1086 raw_clock->Advance( |
| 1087 SubresourceFilterContentSettingsManager::kDelayBeforeShowingInfobarAgain); |
| 1088 ui_test_utils::NavigateToURL(browser(), a_url); |
| 1089 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| 1090 EXPECT_TRUE(client->did_show_ui_for_navigation()); |
| 1091 |
| 1092 histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, |
| 1093 kActionUISuppressed, use_smart_ui ? 1 : 0); |
| 1094 } |
| 1095 |
| 1010 IN_PROC_BROWSER_TEST_P(SubresourceFilterWebSocketBrowserTest, BlockWebSocket) { | 1096 IN_PROC_BROWSER_TEST_P(SubresourceFilterWebSocketBrowserTest, BlockWebSocket) { |
| 1011 GURL url(GetTestUrl( | 1097 GURL url(GetTestUrl( |
| 1012 base::StringPrintf("subresource_filter/page_with_websocket.html?%s", | 1098 base::StringPrintf("subresource_filter/page_with_websocket.html?%s", |
| 1013 GetParam() == IN_WORKER ? "inWorker" : ""))); | 1099 GetParam() == IN_WORKER ? "inWorker" : ""))); |
| 1014 GURL websocket_url(GetWebSocketUrl("echo-with-no-extension")); | 1100 GURL websocket_url(GetWebSocketUrl("echo-with-no-extension")); |
| 1015 ConfigureAsPhishingURL(url); | 1101 ConfigureAsPhishingURL(url); |
| 1016 ASSERT_NO_FATAL_FAILURE( | 1102 ASSERT_NO_FATAL_FAILURE( |
| 1017 SetRulesetToDisallowURLsWithPathSuffix("echo-with-no-extension")); | 1103 SetRulesetToDisallowURLsWithPathSuffix("echo-with-no-extension")); |
| 1018 ui_test_utils::NavigateToURL(browser(), url); | 1104 ui_test_utils::NavigateToURL(browser(), url); |
| 1019 CreateWebSocketAndExpectResult(websocket_url, | 1105 CreateWebSocketAndExpectResult(websocket_url, |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1376 std::string(kSubresourceFilterOnlySuffix)), | 1462 std::string(kSubresourceFilterOnlySuffix)), |
| 1377 ::testing::IsEmpty()); | 1463 ::testing::IsEmpty()); |
| 1378 | 1464 |
| 1379 EXPECT_THAT(tester.GetAllSamples(std::string(kNavigationChainSize) + | 1465 EXPECT_THAT(tester.GetAllSamples(std::string(kNavigationChainSize) + |
| 1380 std::string(kSubresourceFilterOnlySuffix)), | 1466 std::string(kSubresourceFilterOnlySuffix)), |
| 1381 ::testing::IsEmpty()); | 1467 ::testing::IsEmpty()); |
| 1382 } | 1468 } |
| 1383 #endif | 1469 #endif |
| 1384 | 1470 |
| 1385 } // namespace subresource_filter | 1471 } // namespace subresource_filter |
| OLD | NEW |