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

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

Powered by Google App Engine
This is Rietveld 408576698