felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | 7bacc0e | 2016-04-11 20:10:54 | [diff] [blame] | 5 | #include "chrome/browser/safe_browsing/ui_manager.h" |
| 6 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 7 | #include "base/bind.h" |
Carlos IL | e7464b9c | 2020-01-18 01:38:30 | [diff] [blame] | 8 | #include "base/bind_helpers.h" |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 9 | #include "base/run_loop.h" |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 10 | #include "chrome/browser/net/system_network_context_manager.h" |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 11 | #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
Luke Zielinski | 12ef8855 | 2017-06-23 15:36:27 | [diff] [blame] | 12 | #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
Findit | 86f6ae0fa | 2017-09-28 02:19:01 | [diff] [blame] | 13 | #include "chrome/browser/safe_browsing/ui_manager.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 14 | #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 15 | #include "chrome/test/base/scoped_testing_local_state.h" |
Luke Zielinski | 12ef8855 | 2017-06-23 15:36:27 | [diff] [blame] | 16 | #include "chrome/test/base/testing_browser_process.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 17 | #include "chrome/test/base/testing_profile.h" |
Ali Juma | fb3dc1f | 2020-01-07 17:33:47 | [diff] [blame] | 18 | #include "components/safe_browsing/core/common/safe_browsing_prefs.h" |
| 19 | #include "components/safe_browsing/core/db/util.h" |
Ali Juma | ee60293 | 2020-01-24 16:39:18 | [diff] [blame] | 20 | #include "components/security_interstitials/content/unsafe_resource_util.h" |
edwardjung | d7395fb0 | 2017-05-12 23:13:29 | [diff] [blame] | 21 | #include "components/security_interstitials/core/base_safe_browsing_error_ui.h" |
Ali Juma | ee60293 | 2020-01-24 16:39:18 | [diff] [blame] | 22 | #include "components/security_interstitials/core/unsafe_resource.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 23 | #include "content/public/browser/browser_task_traits.h" |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 24 | #include "content/public/browser/browser_thread.h" |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 25 | #include "content/public/browser/navigation_entry.h" |
Lukasz Anforowicz | b55fc49 | 2017-10-02 18:38:34 | [diff] [blame] | 26 | #include "content/public/browser/render_frame_host.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 27 | #include "content/public/browser/render_process_host.h" |
| 28 | #include "content/public/browser/render_view_host.h" |
| 29 | #include "content/public/browser/web_contents.h" |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 30 | #include "content/public/browser/web_contents_delegate.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 31 | #include "content/public/test/browser_task_environment.h" |
clamy | 511cf02 | 2017-08-23 14:11:06 | [diff] [blame] | 32 | #include "content/public/test/navigation_simulator.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 33 | #include "content/public/test/web_contents_tester.h" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 34 | #include "testing/gtest/include/gtest/gtest.h" |
| 35 | #include "url/gurl.h" |
| 36 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 37 | using content::BrowserThread; |
| 38 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 39 | static const char* kGoodURL = "https://www.good.com"; |
| 40 | static const char* kBadURL = "https://www.malware.com"; |
| 41 | static const char* kBadURLWithPath = "https://www.malware.com/index.html"; |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 42 | static const char* kAnotherBadURL = "https://www.badware.com"; |
| 43 | static const char* kLandingURL = "https://www.landing.com"; |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 44 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 45 | namespace safe_browsing { |
| 46 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 47 | class SafeBrowsingCallbackWaiter { |
| 48 | public: |
| 49 | SafeBrowsingCallbackWaiter() {} |
| 50 | |
| 51 | bool callback_called() const { return callback_called_; } |
| 52 | bool proceed() const { return proceed_; } |
| 53 | |
Carlos IL | 5edbd0d | 2020-01-28 01:27:46 | [diff] [blame] | 54 | void OnBlockingPageDone(bool proceed, bool showed_interstitial) { |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 55 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 56 | callback_called_ = true; |
| 57 | proceed_ = proceed; |
| 58 | loop_.Quit(); |
| 59 | } |
| 60 | |
Carlos IL | 5edbd0d | 2020-01-28 01:27:46 | [diff] [blame] | 61 | void OnBlockingPageDoneOnIO(bool proceed, bool showed_interstitial) { |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 62 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 63 | content::GetUIThreadTaskRunner({})->PostTask( |
| 64 | FROM_HERE, |
tzik | b5f84b8 | 2017-04-20 00:55:28 | [diff] [blame] | 65 | base::BindOnce(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
Carlos IL | 5edbd0d | 2020-01-28 01:27:46 | [diff] [blame] | 66 | base::Unretained(this), proceed, showed_interstitial)); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void WaitForCallback() { |
| 70 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 71 | loop_.Run(); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | bool callback_called_ = false; |
| 76 | bool proceed_ = false; |
| 77 | base::RunLoop loop_; |
| 78 | }; |
| 79 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 80 | class SafeBrowsingUIManagerTest : public ChromeRenderViewHostTestHarness { |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 81 | public: |
Gabriel Charette | cc8362b | 2017-09-20 21:59:40 | [diff] [blame] | 82 | SafeBrowsingUIManagerTest() |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 83 | : scoped_testing_local_state_(TestingBrowserProcess::GetGlobal()) { |
kylechar | b59c3d7 | 2019-10-29 05:26:26 | [diff] [blame] | 84 | ui_manager_ = new SafeBrowsingUIManager(nullptr); |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 85 | } |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 86 | |
juncai | 1ee189bd | 2017-06-09 04:25:43 | [diff] [blame] | 87 | ~SafeBrowsingUIManagerTest() override {} |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 88 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 89 | void SetUp() override { |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 90 | ChromeRenderViewHostTestHarness::SetUp(); |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 91 | SafeBrowsingUIManager::CreateWhitelistForTesting(web_contents()); |
Luke Zielinski | 12ef8855 | 2017-06-23 15:36:27 | [diff] [blame] | 92 | |
| 93 | safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; |
| 94 | auto* safe_browsing_service = |
| 95 | sb_service_factory.CreateSafeBrowsingService(); |
| 96 | TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService( |
| 97 | safe_browsing_service); |
| 98 | g_browser_process->safe_browsing_service()->Initialize(); |
| 99 | // A profile was created already but SafeBrowsingService wasn't around to |
| 100 | // get notified of it, so include that notification now. |
Evan Stade | 44fb6e38 | 2019-10-05 00:43:43 | [diff] [blame] | 101 | safe_browsing_service->OnProfileAdded( |
| 102 | Profile::FromBrowserContext(web_contents()->GetBrowserContext())); |
Ian Vollick | 381389a6 | 2019-06-26 16:49:40 | [diff] [blame] | 103 | content::BrowserThread::RunAllPendingTasksOnThreadForTesting( |
| 104 | content::BrowserThread::IO); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 105 | } |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 106 | |
Luke Zielinski | 12ef8855 | 2017-06-23 15:36:27 | [diff] [blame] | 107 | void TearDown() override { |
| 108 | TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown(); |
| 109 | TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr); |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 110 | |
| 111 | // Depends on LocalState from ChromeRenderViewHostTestHarness. |
| 112 | if (SystemNetworkContextManager::GetInstance()) |
| 113 | SystemNetworkContextManager::DeleteInstance(); |
| 114 | |
Luke Zielinski | 12ef8855 | 2017-06-23 15:36:27 | [diff] [blame] | 115 | ChromeRenderViewHostTestHarness::TearDown(); |
| 116 | } |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 117 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 118 | bool IsWhitelisted(security_interstitials::UnsafeResource resource) { |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 119 | return ui_manager_->IsWhitelisted(resource); |
| 120 | } |
| 121 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 122 | void AddToWhitelist(security_interstitials::UnsafeResource resource) { |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 123 | ui_manager_->AddToWhitelistUrlSet( |
| 124 | SafeBrowsingUIManager::GetMainFrameWhitelistUrlForResourceForTesting( |
| 125 | resource), |
estark | 7ffa8c6 | 2016-11-11 23:21:55 | [diff] [blame] | 126 | web_contents(), false, resource.threat_type); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 127 | } |
| 128 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 129 | security_interstitials::UnsafeResource MakeUnsafeResource( |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 130 | const char* url, |
| 131 | bool is_subresource) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 132 | security_interstitials::UnsafeResource resource; |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 133 | resource.url = GURL(url); |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 134 | resource.is_subresource = is_subresource; |
Ali Juma | ee60293 | 2020-01-24 16:39:18 | [diff] [blame] | 135 | resource.web_contents_getter = security_interstitials::GetWebContentsGetter( |
| 136 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
| 137 | web_contents()->GetMainFrame()->GetRoutingID()); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 138 | resource.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 139 | return resource; |
| 140 | } |
| 141 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 142 | security_interstitials::UnsafeResource MakeUnsafeResourceAndStartNavigation( |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 143 | const char* url) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 144 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 145 | MakeUnsafeResource(url, false /* is_subresource */); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 146 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 147 | // The WC doesn't have a URL without a navigation. A main-frame malware |
| 148 | // unsafe resource must be a pending navigation. |
clamy | 511cf02 | 2017-08-23 14:11:06 | [diff] [blame] | 149 | auto navigation = content::NavigationSimulator::CreateBrowserInitiated( |
| 150 | GURL(url), web_contents()); |
| 151 | navigation->Start(); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 152 | return resource; |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 153 | } |
| 154 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 155 | void SimulateBlockingPageDone( |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 156 | const std::vector<security_interstitials::UnsafeResource>& resources, |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 157 | bool proceed) { |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 158 | GURL main_frame_url; |
| 159 | content::NavigationEntry* entry = |
| 160 | web_contents()->GetController().GetVisibleEntry(); |
| 161 | if (entry) |
| 162 | main_frame_url = entry->GetURL(); |
| 163 | |
| 164 | ui_manager_->OnBlockingPageDone(resources, proceed, web_contents(), |
Carlos IL | 5edbd0d | 2020-01-28 01:27:46 | [diff] [blame] | 165 | main_frame_url, |
| 166 | true /* showed_interstitial */); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 167 | } |
| 168 | |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 169 | protected: |
| 170 | SafeBrowsingUIManager* ui_manager() { return ui_manager_.get(); } |
| 171 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 172 | private: |
| 173 | scoped_refptr<SafeBrowsingUIManager> ui_manager_; |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 174 | ScopedTestingLocalState scoped_testing_local_state_; |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 175 | }; |
| 176 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 177 | // Leaks memory. https://crbug.com/755118 |
| 178 | #if defined(LEAK_SANITIZER) |
| 179 | #define MAYBE_Whitelist DISABLED_Whitelist |
| 180 | #else |
| 181 | #define MAYBE_Whitelist Whitelist |
| 182 | #endif |
| 183 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_Whitelist) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 184 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 185 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 186 | AddToWhitelist(resource); |
| 187 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 188 | } |
| 189 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 190 | // Leaks memory. https://crbug.com/755118 |
| 191 | #if defined(LEAK_SANITIZER) |
| 192 | #define MAYBE_WhitelistIgnoresSitesNotAdded \ |
| 193 | DISABLED_WhitelistIgnoresSitesNotAdded |
| 194 | #else |
| 195 | #define MAYBE_WhitelistIgnoresSitesNotAdded WhitelistIgnoresSitesNotAdded |
| 196 | #endif |
| 197 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresSitesNotAdded) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 198 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 199 | MakeUnsafeResourceAndStartNavigation(kGoodURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 200 | EXPECT_FALSE(IsWhitelisted(resource)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 201 | } |
| 202 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 203 | // Leaks memory. https://crbug.com/755118 |
| 204 | #if defined(LEAK_SANITIZER) |
| 205 | #define MAYBE_WhitelistRemembersThreatType DISABLED_WhitelistRemembersThreatType |
| 206 | #else |
| 207 | #define MAYBE_WhitelistRemembersThreatType WhitelistRemembersThreatType |
| 208 | #endif |
| 209 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistRemembersThreatType) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 210 | security_interstitials::UnsafeResource resource = |
estark | 7ffa8c6 | 2016-11-11 23:21:55 | [diff] [blame] | 211 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
| 212 | AddToWhitelist(resource); |
| 213 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 214 | SBThreatType threat_type; |
| 215 | content::NavigationEntry* entry = |
| 216 | web_contents()->GetController().GetVisibleEntry(); |
| 217 | ASSERT_TRUE(entry); |
| 218 | EXPECT_TRUE(ui_manager()->IsUrlWhitelistedOrPendingForWebContents( |
| 219 | resource.url, resource.is_subresource, entry, |
| 220 | resource.web_contents_getter.Run(), true, &threat_type)); |
| 221 | EXPECT_EQ(resource.threat_type, threat_type); |
| 222 | } |
| 223 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 224 | // Leaks memory. https://crbug.com/755118 |
| 225 | #if defined(LEAK_SANITIZER) |
| 226 | #define MAYBE_WhitelistIgnoresPath DISABLED_WhitelistIgnoresPath |
| 227 | #else |
| 228 | #define MAYBE_WhitelistIgnoresPath WhitelistIgnoresPath |
| 229 | #endif |
| 230 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresPath) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 231 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 232 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 233 | AddToWhitelist(resource); |
| 234 | EXPECT_TRUE(IsWhitelisted(resource)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 235 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 236 | content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 237 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 238 | security_interstitials::UnsafeResource resource_path = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 239 | MakeUnsafeResourceAndStartNavigation(kBadURLWithPath); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 240 | EXPECT_TRUE(IsWhitelisted(resource_path)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 241 | } |
| 242 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 243 | // Leaks memory. https://crbug.com/755118 |
| 244 | #if defined(LEAK_SANITIZER) |
| 245 | #define MAYBE_WhitelistIgnoresThreatType DISABLED_WhitelistIgnoresThreatType |
| 246 | #else |
| 247 | #define MAYBE_WhitelistIgnoresThreatType WhitelistIgnoresThreatType |
| 248 | #endif |
| 249 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresThreatType) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 250 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 251 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 252 | AddToWhitelist(resource); |
| 253 | EXPECT_TRUE(IsWhitelisted(resource)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 254 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 255 | security_interstitials::UnsafeResource resource_phishing = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 256 | MakeUnsafeResource(kBadURL, false /* is_subresource */); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 257 | resource_phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING; |
| 258 | EXPECT_TRUE(IsWhitelisted(resource_phishing)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 259 | } |
| 260 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 261 | // Leaks memory. https://crbug.com/755118 |
| 262 | #if defined(LEAK_SANITIZER) |
| 263 | #define MAYBE_WhitelistWithUnrelatedPendingLoad \ |
| 264 | DISABLED_WhitelistWithUnrelatedPendingLoad |
| 265 | #else |
| 266 | #define MAYBE_WhitelistWithUnrelatedPendingLoad \ |
| 267 | WhitelistWithUnrelatedPendingLoad |
| 268 | #endif |
| 269 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistWithUnrelatedPendingLoad) { |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 270 | // Commit load of landing page. |
| 271 | NavigateAndCommit(GURL(kLandingURL)); |
clamy | 511cf02 | 2017-08-23 14:11:06 | [diff] [blame] | 272 | auto unrelated_navigation = |
| 273 | content::NavigationSimulator::CreateBrowserInitiated(GURL(kGoodURL), |
| 274 | web_contents()); |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 275 | { |
| 276 | // Simulate subresource malware hit on the landing page. |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 277 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 278 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 279 | |
| 280 | // Start pending load to unrelated site. |
clamy | 511cf02 | 2017-08-23 14:11:06 | [diff] [blame] | 281 | unrelated_navigation->Start(); |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 282 | |
| 283 | // Whitelist the resource on the landing page. |
| 284 | AddToWhitelist(resource); |
| 285 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 286 | } |
| 287 | |
| 288 | // Commit the pending load of unrelated site. |
clamy | 511cf02 | 2017-08-23 14:11:06 | [diff] [blame] | 289 | unrelated_navigation->Commit(); |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 290 | { |
| 291 | // The unrelated site is not on the whitelist, even if the same subresource |
| 292 | // was on it. |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 293 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 294 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 295 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 296 | } |
| 297 | |
| 298 | // Navigate back to the original landing url. |
| 299 | NavigateAndCommit(GURL(kLandingURL)); |
| 300 | { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 301 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 302 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 303 | // Original resource url is whitelisted. |
| 304 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 305 | } |
| 306 | { |
| 307 | // A different malware subresource on the same page is also whitelisted. |
| 308 | // (The whitelist is by the page url, not the resource url.) |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 309 | security_interstitials::UnsafeResource resource2 = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 310 | MakeUnsafeResource(kAnotherBadURL, true /* is_subresource */); |
| 311 | EXPECT_TRUE(IsWhitelisted(resource2)); |
| 312 | } |
| 313 | } |
| 314 | |
Evgenii Stepanov | 199c013 | 2017-08-18 21:19:41 | [diff] [blame] | 315 | // Leaks memory. https://crbug.com/755118 |
| 316 | #if defined(LEAK_SANITIZER) |
| 317 | #define MAYBE_UICallbackProceed DISABLED_UICallbackProceed |
| 318 | #else |
| 319 | #define MAYBE_UICallbackProceed UICallbackProceed |
| 320 | #endif |
| 321 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_UICallbackProceed) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 322 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 323 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 324 | SafeBrowsingCallbackWaiter waiter; |
| 325 | resource.callback = |
| 326 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 327 | base::Unretained(&waiter)); |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 328 | resource.callback_thread = content::GetUIThreadTaskRunner({}); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 329 | std::vector<security_interstitials::UnsafeResource> resources; |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 330 | resources.push_back(resource); |
| 331 | SimulateBlockingPageDone(resources, true); |
| 332 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 333 | waiter.WaitForCallback(); |
| 334 | EXPECT_TRUE(waiter.callback_called()); |
| 335 | EXPECT_TRUE(waiter.proceed()); |
| 336 | } |
| 337 | |
Evgenii Stepanov | 199c013 | 2017-08-18 21:19:41 | [diff] [blame] | 338 | // Leaks memory. https://crbug.com/755118 |
| 339 | #if defined(LEAK_SANITIZER) |
| 340 | #define MAYBE_UICallbackDontProceed DISABLED_UICallbackDontProceed |
| 341 | #else |
| 342 | #define MAYBE_UICallbackDontProceed UICallbackDontProceed |
| 343 | #endif |
| 344 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_UICallbackDontProceed) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 345 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 346 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 347 | SafeBrowsingCallbackWaiter waiter; |
| 348 | resource.callback = |
| 349 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 350 | base::Unretained(&waiter)); |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 351 | resource.callback_thread = content::GetUIThreadTaskRunner({}); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 352 | std::vector<security_interstitials::UnsafeResource> resources; |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 353 | resources.push_back(resource); |
| 354 | SimulateBlockingPageDone(resources, false); |
| 355 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 356 | waiter.WaitForCallback(); |
| 357 | EXPECT_TRUE(waiter.callback_called()); |
| 358 | EXPECT_FALSE(waiter.proceed()); |
| 359 | } |
| 360 | |
Evgenii Stepanov | 199c013 | 2017-08-18 21:19:41 | [diff] [blame] | 361 | // Leaks memory. https://crbug.com/755118 |
| 362 | #if defined(LEAK_SANITIZER) |
| 363 | #define MAYBE_IOCallbackProceed DISABLED_IOCallbackProceed |
| 364 | #else |
| 365 | #define MAYBE_IOCallbackProceed IOCallbackProceed |
| 366 | #endif |
| 367 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_IOCallbackProceed) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 368 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 369 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 370 | SafeBrowsingCallbackWaiter waiter; |
| 371 | resource.callback = |
| 372 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 373 | base::Unretained(&waiter)); |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 374 | resource.callback_thread = content::GetIOThreadTaskRunner({}); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 375 | std::vector<security_interstitials::UnsafeResource> resources; |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 376 | resources.push_back(resource); |
| 377 | SimulateBlockingPageDone(resources, true); |
| 378 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 379 | waiter.WaitForCallback(); |
| 380 | EXPECT_TRUE(waiter.callback_called()); |
| 381 | EXPECT_TRUE(waiter.proceed()); |
| 382 | } |
| 383 | |
Evgenii Stepanov | 199c013 | 2017-08-18 21:19:41 | [diff] [blame] | 384 | // Leaks memory. https://crbug.com/755118 |
| 385 | #if defined(LEAK_SANITIZER) |
| 386 | #define MAYBE_IOCallbackDontProceed DISABLED_IOCallbackDontProceed |
| 387 | #else |
| 388 | #define MAYBE_IOCallbackDontProceed IOCallbackDontProceed |
| 389 | #endif |
| 390 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_IOCallbackDontProceed) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 391 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 392 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 393 | SafeBrowsingCallbackWaiter waiter; |
| 394 | resource.callback = |
| 395 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 396 | base::Unretained(&waiter)); |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 397 | resource.callback_thread = content::GetIOThreadTaskRunner({}); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 398 | std::vector<security_interstitials::UnsafeResource> resources; |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 399 | resources.push_back(resource); |
| 400 | SimulateBlockingPageDone(resources, false); |
| 401 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 402 | waiter.WaitForCallback(); |
| 403 | EXPECT_TRUE(waiter.callback_called()); |
| 404 | EXPECT_FALSE(waiter.proceed()); |
| 405 | } |
| 406 | |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 407 | namespace { |
| 408 | |
| 409 | // A WebContentsDelegate that records whether |
| 410 | // VisibleSecurityStateChanged() was called. |
| 411 | class SecurityStateWebContentsDelegate : public content::WebContentsDelegate { |
| 412 | public: |
| 413 | SecurityStateWebContentsDelegate() {} |
| 414 | ~SecurityStateWebContentsDelegate() override {} |
| 415 | |
| 416 | bool visible_security_state_changed() const { |
| 417 | return visible_security_state_changed_; |
| 418 | } |
| 419 | |
| 420 | void ClearVisibleSecurityStateChanged() { |
| 421 | visible_security_state_changed_ = false; |
| 422 | } |
| 423 | |
| 424 | // WebContentsDelegate: |
| 425 | void VisibleSecurityStateChanged(content::WebContents* source) override { |
| 426 | visible_security_state_changed_ = true; |
| 427 | } |
| 428 | |
| 429 | private: |
| 430 | bool visible_security_state_changed_ = false; |
| 431 | DISALLOW_COPY_AND_ASSIGN(SecurityStateWebContentsDelegate); |
| 432 | }; |
| 433 | |
| 434 | // A test blocking page that does not create windows. |
| 435 | class TestSafeBrowsingBlockingPage : public SafeBrowsingBlockingPage { |
| 436 | public: |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 437 | TestSafeBrowsingBlockingPage(BaseUIManager* manager, |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 438 | content::WebContents* web_contents, |
| 439 | const GURL& main_frame_url, |
| 440 | const UnsafeResourceList& unsafe_resources) |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 441 | : SafeBrowsingBlockingPage( |
| 442 | manager, |
| 443 | web_contents, |
| 444 | main_frame_url, |
| 445 | unsafe_resources, |
edwardjung | d7395fb0 | 2017-05-12 23:13:29 | [diff] [blame] | 446 | BaseSafeBrowsingErrorUI::SBErrorDisplayOptions( |
ntfschr | a5448fa | 2017-02-02 01:01:31 | [diff] [blame] | 447 | BaseBlockingPage::IsMainPageLoadBlocked(unsafe_resources), |
Carlos IL | 43dfc26 | 2019-11-27 19:40:31 | [diff] [blame] | 448 | false, // is_extended_reporting_opt_in_allowed |
| 449 | false, // is_off_the_record |
| 450 | false, // is_extended_reporting_enabled |
| 451 | false, // is_extended_reporting_policy_managed |
Bettina | 460d995 | 2020-03-23 19:44:01 | [diff] [blame] | 452 | false, // is_enhanced_protection_enabled |
Carlos IL | 43dfc26 | 2019-11-27 19:40:31 | [diff] [blame] | 453 | false, // is_proceed_anyway_disabled |
| 454 | true, // should_open_links_in_new_tab |
| 455 | true, // always_show_back_to_safety |
Xinghui Lu | 4dba30c | 2020-10-12 23:13:11 | [diff] [blame^] | 456 | false, // is_enhanced_protection_message_enabled |
| 457 | false, // is_safe_browsing_managed |
Carlos IL | 43dfc26 | 2019-11-27 19:40:31 | [diff] [blame] | 458 | "cpn_safe_browsing"), // help_center_article_link |
| 459 | true) { // should_trigger_reporting |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 460 | // Don't delay details at all for the unittest. |
ntfschr | fef42f9 | 2017-02-24 02:15:47 | [diff] [blame] | 461 | SetThreatDetailsProceedDelayForTesting(0); |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 462 | DontCreateViewForTesting(); |
| 463 | } |
| 464 | }; |
| 465 | |
| 466 | // A factory that creates TestSafeBrowsingBlockingPages. |
| 467 | class TestSafeBrowsingBlockingPageFactory |
| 468 | : public SafeBrowsingBlockingPageFactory { |
| 469 | public: |
| 470 | TestSafeBrowsingBlockingPageFactory() {} |
| 471 | ~TestSafeBrowsingBlockingPageFactory() override {} |
| 472 | |
| 473 | SafeBrowsingBlockingPage* CreateSafeBrowsingPage( |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 474 | BaseUIManager* delegate, |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 475 | content::WebContents* web_contents, |
| 476 | const GURL& main_frame_url, |
Carlos IL | 43dfc26 | 2019-11-27 19:40:31 | [diff] [blame] | 477 | const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources, |
| 478 | bool should_trigger_reporting) override { |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 479 | return new TestSafeBrowsingBlockingPage(delegate, web_contents, |
| 480 | main_frame_url, unsafe_resources); |
| 481 | } |
| 482 | }; |
| 483 | |
| 484 | } // namespace |
| 485 | |
| 486 | // Tests that the WebContentsDelegate is notified of a visible security |
| 487 | // state change when a blocking page is shown for a subresource. |
Evgenii Stepanov | 199c013 | 2017-08-18 21:19:41 | [diff] [blame] | 488 | // Leaks memory. https://crbug.com/755118 |
| 489 | #if defined(LEAK_SANITIZER) |
| 490 | #define MAYBE_VisibleSecurityStateChangedForUnsafeSubresource \ |
| 491 | DISABLED_VisibleSecurityStateChangedForUnsafeSubresource |
| 492 | #else |
| 493 | #define MAYBE_VisibleSecurityStateChangedForUnsafeSubresource \ |
| 494 | VisibleSecurityStateChangedForUnsafeSubresource |
| 495 | #endif |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 496 | TEST_F(SafeBrowsingUIManagerTest, |
Evgenii Stepanov | 199c013 | 2017-08-18 21:19:41 | [diff] [blame] | 497 | MAYBE_VisibleSecurityStateChangedForUnsafeSubresource) { |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 498 | TestSafeBrowsingBlockingPageFactory factory; |
| 499 | SafeBrowsingBlockingPage::RegisterFactory(&factory); |
| 500 | SecurityStateWebContentsDelegate delegate; |
| 501 | web_contents()->SetDelegate(&delegate); |
| 502 | |
| 503 | // Simulate a blocking page showing for an unsafe subresource. |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 504 | security_interstitials::UnsafeResource resource = |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 505 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 506 | // Needed for showing the blocking page. |
| 507 | resource.threat_source = safe_browsing::ThreatSource::REMOTE; |
Carlos IL | e7464b9c | 2020-01-18 01:38:30 | [diff] [blame] | 508 | |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 509 | NavigateAndCommit(GURL("http://example.test")); |
| 510 | |
| 511 | delegate.ClearVisibleSecurityStateChanged(); |
| 512 | EXPECT_FALSE(delegate.visible_security_state_changed()); |
| 513 | ui_manager()->DisplayBlockingPage(resource); |
| 514 | EXPECT_TRUE(delegate.visible_security_state_changed()); |
| 515 | |
| 516 | // Simulate proceeding through the blocking page. |
| 517 | SafeBrowsingCallbackWaiter waiter; |
| 518 | resource.callback = |
| 519 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 520 | base::Unretained(&waiter)); |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 521 | resource.callback_thread = content::GetIOThreadTaskRunner({}); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 522 | std::vector<security_interstitials::UnsafeResource> resources; |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 523 | resources.push_back(resource); |
| 524 | |
| 525 | delegate.ClearVisibleSecurityStateChanged(); |
| 526 | EXPECT_FALSE(delegate.visible_security_state_changed()); |
| 527 | SimulateBlockingPageDone(resources, true); |
| 528 | EXPECT_TRUE(delegate.visible_security_state_changed()); |
| 529 | |
| 530 | waiter.WaitForCallback(); |
| 531 | EXPECT_TRUE(waiter.callback_called()); |
| 532 | EXPECT_TRUE(waiter.proceed()); |
| 533 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 534 | } |
| 535 | |
Daniel Rubery | a5af634d | 2020-03-03 23:24:40 | [diff] [blame] | 536 | TEST_F(SafeBrowsingUIManagerTest, ShowBlockPageNoCallback) { |
| 537 | TestSafeBrowsingBlockingPageFactory factory; |
| 538 | SafeBrowsingBlockingPage::RegisterFactory(&factory); |
| 539 | SecurityStateWebContentsDelegate delegate; |
| 540 | web_contents()->SetDelegate(&delegate); |
| 541 | |
| 542 | // Simulate a blocking page showing for an unsafe subresource. |
| 543 | security_interstitials::UnsafeResource resource = |
| 544 | MakeUnsafeResource(kBadURL, false /* is_subresource */); |
| 545 | // Needed for showing the blocking page. |
| 546 | resource.threat_source = safe_browsing::ThreatSource::REMOTE; |
| 547 | |
| 548 | // This call caused a crash in https://crbug.com/1058094. Just verify that we |
| 549 | // don't crash anymore. |
| 550 | ui_manager()->DisplayBlockingPage(resource); |
| 551 | } |
| 552 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 553 | } // namespace safe_browsing |