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

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_service_browsertest.cc

Issue 2814733002: Add the SocEng as a type for checking in CheckUrlForSubresourceFilter. (Closed)
Patch Set: . 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 // This test creates a safebrowsing service using test safebrowsing database 5 // This test creates a safebrowsing service using test safebrowsing database
6 // and a test protocol manager. It is used to test logics in safebrowsing 6 // and a test protocol manager. It is used to test logics in safebrowsing
7 // service. 7 // service.
8 8
9 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 9 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
10 10
(...skipping 23 matching lines...) Expand all
34 #include "chrome/browser/prerender/prerender_manager.h" 34 #include "chrome/browser/prerender/prerender_manager.h"
35 #include "chrome/browser/profiles/profile.h" 35 #include "chrome/browser/profiles/profile.h"
36 #include "chrome/browser/profiles/profile_manager.h" 36 #include "chrome/browser/profiles/profile_manager.h"
37 #include "chrome/browser/safe_browsing/client_side_detection_service.h" 37 #include "chrome/browser/safe_browsing/client_side_detection_service.h"
38 #include "chrome/browser/safe_browsing/local_database_manager.h" 38 #include "chrome/browser/safe_browsing/local_database_manager.h"
39 #include "chrome/browser/safe_browsing/protocol_manager.h" 39 #include "chrome/browser/safe_browsing/protocol_manager.h"
40 #include "chrome/browser/safe_browsing/safe_browsing_database.h" 40 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
41 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" 41 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
42 #include "chrome/browser/safe_browsing/ui_manager.h" 42 #include "chrome/browser/safe_browsing/ui_manager.h"
43 #include "chrome/browser/safe_browsing/v4_test_utils.h" 43 #include "chrome/browser/safe_browsing/v4_test_utils.h"
44 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h"
45 #include "chrome/browser/ui/browser.h" 44 #include "chrome/browser/ui/browser.h"
46 #include "chrome/browser/ui/browser_navigator_params.h" 45 #include "chrome/browser/ui/browser_navigator_params.h"
47 #include "chrome/browser/ui/tabs/tab_strip_model.h" 46 #include "chrome/browser/ui/tabs/tab_strip_model.h"
48 #include "chrome/common/chrome_paths.h" 47 #include "chrome/common/chrome_paths.h"
49 #include "chrome/common/pref_names.h" 48 #include "chrome/common/pref_names.h"
50 #include "chrome/test/base/in_process_browser_test.h" 49 #include "chrome/test/base/in_process_browser_test.h"
51 #include "chrome/test/base/ui_test_utils.h" 50 #include "chrome/test/base/ui_test_utils.h"
52 #include "components/bookmarks/browser/startup_task_runner_service.h" 51 #include "components/bookmarks/browser/startup_task_runner_service.h"
53 #include "components/content_settings/core/browser/host_content_settings_map.h" 52 #include "components/content_settings/core/browser/host_content_settings_map.h"
54 #include "components/prefs/pref_service.h" 53 #include "components/prefs/pref_service.h"
55 #include "components/safe_browsing/common/safebrowsing_switches.h" 54 #include "components/safe_browsing/common/safebrowsing_switches.h"
56 #include "components/safe_browsing_db/database_manager.h" 55 #include "components/safe_browsing_db/database_manager.h"
57 #include "components/safe_browsing_db/metadata.pb.h" 56 #include "components/safe_browsing_db/metadata.pb.h"
58 #include "components/safe_browsing_db/test_database_manager.h" 57 #include "components/safe_browsing_db/test_database_manager.h"
59 #include "components/safe_browsing_db/util.h" 58 #include "components/safe_browsing_db/util.h"
60 #include "components/safe_browsing_db/v4_database.h" 59 #include "components/safe_browsing_db/v4_database.h"
61 #include "components/safe_browsing_db/v4_feature_list.h" 60 #include "components/safe_browsing_db/v4_feature_list.h"
62 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" 61 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
63 #include "components/safe_browsing_db/v4_protocol_manager_util.h" 62 #include "components/safe_browsing_db/v4_protocol_manager_util.h"
64 #include "components/subresource_filter/core/browser/subresource_filter_features .h"
65 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h"
66 #include "components/subresource_filter/core/common/activation_level.h"
67 #include "components/subresource_filter/core/common/activation_list.h"
68 #include "components/subresource_filter/core/common/activation_state.h"
69 #include "components/subresource_filter/core/common/test_ruleset_creator.h"
70 #include "content/public/browser/interstitial_page.h" 63 #include "content/public/browser/interstitial_page.h"
71 #include "content/public/browser/navigation_entry.h" 64 #include "content/public/browser/navigation_entry.h"
72 #include "content/public/browser/render_frame_host.h" 65 #include "content/public/browser/render_frame_host.h"
73 #include "content/public/browser/web_contents.h" 66 #include "content/public/browser/web_contents.h"
74 #include "content/public/common/content_switches.h" 67 #include "content/public/common/content_switches.h"
75 #include "content/public/test/browser_test_utils.h" 68 #include "content/public/test/browser_test_utils.h"
76 #include "crypto/sha2.h" 69 #include "crypto/sha2.h"
77 #include "net/cookies/cookie_store.h" 70 #include "net/cookies/cookie_store.h"
78 #include "net/cookies/cookie_util.h" 71 #include "net/cookies/cookie_util.h"
79 #include "net/test/embedded_test_server/embedded_test_server.h" 72 #include "net/test/embedded_test_server/embedded_test_server.h"
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 SafeBrowsingDatabase::RegisterFactory(nullptr); 529 SafeBrowsingDatabase::RegisterFactory(nullptr);
537 SafeBrowsingProtocolManager::RegisterFactory(nullptr); 530 SafeBrowsingProtocolManager::RegisterFactory(nullptr);
538 SafeBrowsingService::RegisterFactory(nullptr); 531 SafeBrowsingService::RegisterFactory(nullptr);
539 } 532 }
540 533
541 void SetUpCommandLine(base::CommandLine* command_line) override { 534 void SetUpCommandLine(base::CommandLine* command_line) override {
542 // Makes sure the auto update is not triggered during the test. 535 // Makes sure the auto update is not triggered during the test.
543 // This test will fill up the database using testing prefixes 536 // This test will fill up the database using testing prefixes
544 // and urls. 537 // and urls.
545 command_line->AppendSwitch(safe_browsing::switches::kSbDisableAutoUpdate); 538 command_line->AppendSwitch(safe_browsing::switches::kSbDisableAutoUpdate);
546 command_line->AppendSwitchASCII(
547 ::switches::kEnableFeatures,
548 subresource_filter::kSafeBrowsingSubresourceFilter.name);
549 #if defined(OS_CHROMEOS) 539 #if defined(OS_CHROMEOS)
550 command_line->AppendSwitch( 540 command_line->AppendSwitch(
551 chromeos::switches::kIgnoreUserProfileMappingForTests); 541 chromeos::switches::kIgnoreUserProfileMappingForTests);
552 #endif 542 #endif
553 } 543 }
554 544
555 void SetUpOnMainThread() override { 545 void SetUpOnMainThread() override {
556 InProcessBrowserTest::SetUpOnMainThread(); 546 InProcessBrowserTest::SetUpOnMainThread();
557 g_browser_process->safe_browsing_service()->ui_manager()->AddObserver( 547 g_browser_process->safe_browsing_service()->ui_manager()->AddObserver(
558 &observer_); 548 &observer_);
(...skipping 30 matching lines...) Expand all
589 pm->AddGetFullHashResponse(full_hash); 579 pm->AddGetFullHashResponse(full_hash);
590 } 580 }
591 581
592 bool ShowingInterstitialPage() { 582 bool ShowingInterstitialPage() {
593 WebContents* contents = 583 WebContents* contents =
594 browser()->tab_strip_model()->GetActiveWebContents(); 584 browser()->tab_strip_model()->GetActiveWebContents();
595 InterstitialPage* interstitial_page = contents->GetInterstitialPage(); 585 InterstitialPage* interstitial_page = contents->GetInterstitialPage();
596 return interstitial_page != nullptr; 586 return interstitial_page != nullptr;
597 } 587 }
598 588
599 bool WasSubresourceFilterProbeScriptLoaded() {
600 bool script_resource_was_loaded = false;
601 WebContents* web_contents =
602 browser()->tab_strip_model()->GetActiveWebContents();
603 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
604 web_contents->GetMainFrame(),
605 "domAutomationController.send(!!document.scriptExecuted)",
606 &script_resource_was_loaded));
607 return script_resource_was_loaded;
608 }
609
610 void IntroduceGetHashDelay(const base::TimeDelta& delay) { 589 void IntroduceGetHashDelay(const base::TimeDelta& delay) {
611 pm_factory_.GetProtocolManager()->IntroduceDelay(delay); 590 pm_factory_.GetProtocolManager()->IntroduceDelay(delay);
612 } 591 }
613 592
614 // TODO(nparker): Remove the need for this by wiring in our own 593 // TODO(nparker): Remove the need for this by wiring in our own
615 // SafeBrowsingDatabaseManager factory and keep a ptr to the subclass. 594 // SafeBrowsingDatabaseManager factory and keep a ptr to the subclass.
616 // Or add a Get/SetTimeout to sbdbmgr. 595 // Or add a Get/SetTimeout to sbdbmgr.
617 static LocalSafeBrowsingDatabaseManager* LocalDatabaseManagerForService( 596 static LocalSafeBrowsingDatabaseManager* LocalDatabaseManagerForService(
618 SafeBrowsingService* sb_service) { 597 SafeBrowsingService* sb_service) {
619 return static_cast<LocalSafeBrowsingDatabaseManager*>( 598 return static_cast<LocalSafeBrowsingDatabaseManager*>(
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
900 ui_test_utils::NavigateToURL(&params); 879 ui_test_utils::NavigateToURL(&params);
901 880
902 EXPECT_TRUE(ShowingInterstitialPage()); 881 EXPECT_TRUE(ShowingInterstitialPage());
903 EXPECT_TRUE(got_hit_report()); 882 EXPECT_TRUE(got_hit_report());
904 EXPECT_EQ(bad_url, hit_report().malicious_url); 883 EXPECT_EQ(bad_url, hit_report().malicious_url);
905 EXPECT_EQ(bad_url, hit_report().page_url); 884 EXPECT_EQ(bad_url, hit_report().page_url);
906 EXPECT_EQ(first_url, hit_report().referrer_url); 885 EXPECT_EQ(first_url, hit_report().referrer_url);
907 EXPECT_FALSE(hit_report().is_subresource); 886 EXPECT_FALSE(hit_report().is_subresource);
908 } 887 }
909 888
910 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, SubresourceFilterEndToEndTest) {
911 subresource_filter::testing::ScopedSubresourceFilterConfigurator
912 scoped_configuration(subresource_filter::Configuration::
913 MakePresetForLiveRunOnPhishingSites());
914
915 subresource_filter::testing::TestRulesetCreator ruleset_creator;
916 subresource_filter::testing::TestRulesetPair test_ruleset_pair;
917 ruleset_creator.CreateRulesetToDisallowURLsWithPathSuffix(
918 "included_script.js", &test_ruleset_pair);
919 subresource_filter::testing::TestRulesetPublisher test_ruleset_publisher;
920 ASSERT_NO_FATAL_FAILURE(
921 test_ruleset_publisher.SetRuleset(test_ruleset_pair.unindexed));
922
923 GURL phishing_url = embedded_test_server()->GetURL(
924 "/subresource_filter/frame_with_included_script.html");
925 SBFullHashResult malware_full_hash;
926 GenUrlFullHashResultWithMetadata(phishing_url, PHISH,
927 ThreatPatternType::SOCIAL_ENGINEERING_ADS,
928 &malware_full_hash);
929 SetupResponseForUrl(phishing_url, malware_full_hash);
930
931 WebContents* web_contents =
932 browser()->tab_strip_model()->GetActiveWebContents();
933
934 // Navigation to a phishing page should trigger an interstitial. If the user
935 // clicks through it, the page load should proceed, but with subresource
936 // filtering activated. This is verified by probing whether `included_script`
937 // that is disallowed above indeed fails to load.
938 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(phishing_url)));
939 ui_test_utils::NavigateToURL(browser(), phishing_url);
940 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&observer_));
941 ASSERT_TRUE(got_hit_report());
942 content::WaitForInterstitialAttach(web_contents);
943 ASSERT_TRUE(ShowingInterstitialPage());
944
945 content::WindowedNotificationObserver load_stop_observer(
946 content::NOTIFICATION_LOAD_STOP,
947 content::Source<content::NavigationController>(
948 &web_contents->GetController()));
949 InterstitialPage* interstitial_page = web_contents->GetInterstitialPage();
950 ASSERT_TRUE(interstitial_page);
951 interstitial_page->Proceed();
952 load_stop_observer.Wait();
953 ASSERT_FALSE(ShowingInterstitialPage());
954 EXPECT_FALSE(WasSubresourceFilterProbeScriptLoaded());
955
956 // Navigate to a page that loads the same script, but is not a phishing page.
957 // The load should be allowed.
958 GURL safe_url = embedded_test_server()->GetURL(
959 "/subresource_filter/frame_with_allowed_script.html");
960 ui_test_utils::NavigateToURL(browser(), safe_url);
961 EXPECT_FALSE(ShowingInterstitialPage());
962 EXPECT_TRUE(WasSubresourceFilterProbeScriptLoaded());
963
964 // Navigate to the phishing page again -- should be no interstitial shown, but
965 // subresource filtering should still be activated.
966 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(phishing_url)))
967 .Times(0);
968 ui_test_utils::NavigateToURL(browser(), phishing_url);
969 EXPECT_FALSE(ShowingInterstitialPage());
970 EXPECT_FALSE(WasSubresourceFilterProbeScriptLoaded());
971 }
972
973 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, 889 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest,
974 SubResourceHitWithMainFrameReferrer) { 890 SubResourceHitWithMainFrameReferrer) {
975 GURL first_url = embedded_test_server()->GetURL(kEmptyPage); 891 GURL first_url = embedded_test_server()->GetURL(kEmptyPage);
976 GURL second_url = embedded_test_server()->GetURL(kMalwarePage); 892 GURL second_url = embedded_test_server()->GetURL(kMalwarePage);
977 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg); 893 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg);
978 894
979 SBFullHashResult malware_full_hash; 895 SBFullHashResult malware_full_hash;
980 GenUrlFullHashResult(bad_url, MALWARE, &malware_full_hash); 896 GenUrlFullHashResult(bad_url, MALWARE, &malware_full_hash);
981 SetupResponseForUrl(bad_url, malware_full_hash); 897 SetupResponseForUrl(bad_url, malware_full_hash);
982 898
(...skipping 991 matching lines...) Expand 10 before | Expand all | Expand 10 after
1974 1890
1975 EXPECT_TRUE(ShowingInterstitialPage()); 1891 EXPECT_TRUE(ShowingInterstitialPage());
1976 EXPECT_TRUE(got_hit_report()); 1892 EXPECT_TRUE(got_hit_report());
1977 EXPECT_EQ(bad_url, hit_report().malicious_url); 1893 EXPECT_EQ(bad_url, hit_report().malicious_url);
1978 EXPECT_EQ(bad_url, hit_report().page_url); 1894 EXPECT_EQ(bad_url, hit_report().page_url);
1979 EXPECT_EQ(first_url, hit_report().referrer_url); 1895 EXPECT_EQ(first_url, hit_report().referrer_url);
1980 EXPECT_FALSE(hit_report().is_subresource); 1896 EXPECT_FALSE(hit_report().is_subresource);
1981 } 1897 }
1982 1898
1983 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, 1899 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
1984 SubresourceFilterEndToEndTest) {
1985 subresource_filter::testing::ScopedSubresourceFilterConfigurator
1986 scoped_configuration(subresource_filter::Configuration::
1987 MakePresetForLiveRunOnPhishingSites());
1988
1989 subresource_filter::testing::TestRulesetCreator ruleset_creator;
1990 subresource_filter::testing::TestRulesetPair test_ruleset_pair;
1991 ruleset_creator.CreateRulesetToDisallowURLsWithPathSuffix(
1992 "included_script.js", &test_ruleset_pair);
1993 subresource_filter::testing::TestRulesetPublisher test_ruleset_publisher;
1994 ASSERT_NO_FATAL_FAILURE(
1995 test_ruleset_publisher.SetRuleset(test_ruleset_pair.unindexed));
1996
1997 GURL phishing_url = embedded_test_server()->GetURL(
1998 "/subresource_filter/frame_with_included_script.html");
1999 MarkUrlForPhishingUnexpired(phishing_url,
2000 ThreatPatternType::SOCIAL_ENGINEERING_ADS);
2001
2002 WebContents* web_contents =
2003 browser()->tab_strip_model()->GetActiveWebContents();
2004
2005 // Navigation to a phishing page should trigger an interstitial. If the user
2006 // clicks through it, the page load should proceed, but with subresource
2007 // filtering activated. This is verified by probing whether `included_script`
2008 // that is disallowed above indeed fails to load.
2009 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(phishing_url)));
2010 ui_test_utils::NavigateToURL(browser(), phishing_url);
2011 ASSERT_TRUE(Mock::VerifyAndClearExpectations(&observer_));
2012 ASSERT_TRUE(got_hit_report());
2013 content::WaitForInterstitialAttach(web_contents);
2014 ASSERT_TRUE(ShowingInterstitialPage());
2015
2016 content::WindowedNotificationObserver load_stop_observer(
2017 content::NOTIFICATION_LOAD_STOP,
2018 content::Source<content::NavigationController>(
2019 &web_contents->GetController()));
2020 InterstitialPage* interstitial_page = web_contents->GetInterstitialPage();
2021 ASSERT_TRUE(interstitial_page);
2022 interstitial_page->Proceed();
2023 load_stop_observer.Wait();
2024 ASSERT_FALSE(ShowingInterstitialPage());
2025 EXPECT_FALSE(WasSubresourceFilterProbeScriptLoaded());
2026
2027 // Navigate to a page that loads the same script, but is not a phishing page.
2028 // The load should be allowed.
2029 GURL safe_url = embedded_test_server()->GetURL(
2030 "/subresource_filter/frame_with_allowed_script.html");
2031 ui_test_utils::NavigateToURL(browser(), safe_url);
2032 EXPECT_FALSE(ShowingInterstitialPage());
2033 EXPECT_TRUE(WasSubresourceFilterProbeScriptLoaded());
2034
2035 // Navigate to the phishing page again -- should be no interstitial shown, but
2036 // subresource filtering should still be activated.
2037 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(phishing_url)))
2038 .Times(0);
2039 ui_test_utils::NavigateToURL(browser(), phishing_url);
2040 EXPECT_FALSE(ShowingInterstitialPage());
2041 EXPECT_FALSE(WasSubresourceFilterProbeScriptLoaded());
2042 }
2043
2044 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest,
2045 SubResourceHitWithMainFrameReferrer) { 1900 SubResourceHitWithMainFrameReferrer) {
2046 GURL first_url = embedded_test_server()->GetURL(kEmptyPage); 1901 GURL first_url = embedded_test_server()->GetURL(kEmptyPage);
2047 GURL second_url = embedded_test_server()->GetURL(kMalwarePage); 1902 GURL second_url = embedded_test_server()->GetURL(kMalwarePage);
2048 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg); 1903 GURL bad_url = embedded_test_server()->GetURL(kMalwareImg);
2049 1904
2050 MarkUrlForMalwareUnexpired(bad_url); 1905 MarkUrlForMalwareUnexpired(bad_url);
2051 1906
2052 // Navigate to first, safe page. 1907 // Navigate to first, safe page.
2053 ui_test_utils::NavigateToURL(browser(), first_url); 1908 ui_test_utils::NavigateToURL(browser(), first_url);
2054 EXPECT_FALSE(ShowingInterstitialPage()); 1909 EXPECT_FALSE(ShowingInterstitialPage());
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after
2491 } 2346 }
2492 2347
2493 INSTANTIATE_TEST_CASE_P( 2348 INSTANTIATE_TEST_CASE_P(
2494 MaybeSetMetadata, 2349 MaybeSetMetadata,
2495 V4SafeBrowsingServiceMetadataTest, 2350 V4SafeBrowsingServiceMetadataTest,
2496 testing::Values(ThreatPatternType::NONE, 2351 testing::Values(ThreatPatternType::NONE,
2497 ThreatPatternType::MALWARE_LANDING, 2352 ThreatPatternType::MALWARE_LANDING,
2498 ThreatPatternType::MALWARE_DISTRIBUTION)); 2353 ThreatPatternType::MALWARE_DISTRIBUTION));
2499 2354
2500 } // namespace safe_browsing 2355 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698