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" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 10 | #include "base/task/post_task.h" |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 11 | #include "chrome/browser/net/system_network_context_manager.h" |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 12 | #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
Luke Zielinski | 12ef8855 | 2017-06-23 15:36:27 | [diff] [blame] | 13 | #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
Findit | 86f6ae0fa | 2017-09-28 02:19:01 | [diff] [blame] | 14 | #include "chrome/browser/safe_browsing/ui_manager.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 15 | #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 16 | #include "chrome/test/base/scoped_testing_local_state.h" |
Luke Zielinski | 12ef8855 | 2017-06-23 15:36:27 | [diff] [blame] | 17 | #include "chrome/test/base/testing_browser_process.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 18 | #include "chrome/test/base/testing_profile.h" |
Ali Juma | fb3dc1f | 2020-01-07 17:33:47 | [diff] [blame] | 19 | #include "components/safe_browsing/core/common/safe_browsing_prefs.h" |
| 20 | #include "components/safe_browsing/core/db/util.h" |
edwardjung | d7395fb0 | 2017-05-12 23:13:29 | [diff] [blame] | 21 | #include "components/security_interstitials/core/base_safe_browsing_error_ui.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 22 | #include "content/public/browser/browser_task_traits.h" |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 23 | #include "content/public/browser/navigation_entry.h" |
Lukasz Anforowicz | b55fc49 | 2017-10-02 18:38:34 | [diff] [blame] | 24 | #include "content/public/browser/render_frame_host.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 25 | #include "content/public/browser/render_process_host.h" |
| 26 | #include "content/public/browser/render_view_host.h" |
| 27 | #include "content/public/browser/web_contents.h" |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 28 | #include "content/public/browser/web_contents_delegate.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 29 | #include "content/public/test/browser_task_environment.h" |
clamy | 511cf02 | 2017-08-23 14:11:06 | [diff] [blame] | 30 | #include "content/public/test/navigation_simulator.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 31 | #include "content/public/test/web_contents_tester.h" |
Helen Li | fa36ad6 | 2018-06-01 19:52:59 | [diff] [blame] | 32 | #include "net/url_request/url_request_test_util.h" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 33 | #include "testing/gtest/include/gtest/gtest.h" |
| 34 | #include "url/gurl.h" |
| 35 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 36 | using content::BrowserThread; |
| 37 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 38 | static const char* kGoodURL = "https://www.good.com"; |
| 39 | static const char* kBadURL = "https://www.malware.com"; |
| 40 | static const char* kBadURLWithPath = "https://www.malware.com/index.html"; |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 41 | static const char* kAnotherBadURL = "https://www.badware.com"; |
| 42 | static const char* kLandingURL = "https://www.landing.com"; |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 43 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 44 | namespace safe_browsing { |
| 45 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 46 | class SafeBrowsingCallbackWaiter { |
| 47 | public: |
| 48 | SafeBrowsingCallbackWaiter() {} |
| 49 | |
| 50 | bool callback_called() const { return callback_called_; } |
| 51 | bool proceed() const { return proceed_; } |
| 52 | |
| 53 | void OnBlockingPageDone(bool proceed) { |
| 54 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 55 | callback_called_ = true; |
| 56 | proceed_ = proceed; |
| 57 | loop_.Quit(); |
| 58 | } |
| 59 | |
| 60 | void OnBlockingPageDoneOnIO(bool proceed) { |
| 61 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
Sami Kyostila | ad439ec | 2019-08-06 14:49:52 | [diff] [blame] | 62 | base::PostTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 63 | FROM_HERE, {BrowserThread::UI}, |
tzik | b5f84b8 | 2017-04-20 00:55:28 | [diff] [blame] | 64 | base::BindOnce(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 65 | base::Unretained(this), proceed)); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void WaitForCallback() { |
| 69 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 70 | loop_.Run(); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | bool callback_called_ = false; |
| 75 | bool proceed_ = false; |
| 76 | base::RunLoop loop_; |
| 77 | }; |
| 78 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 79 | class SafeBrowsingUIManagerTest : public ChromeRenderViewHostTestHarness { |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 80 | public: |
Gabriel Charette | cc8362b | 2017-09-20 21:59:40 | [diff] [blame] | 81 | SafeBrowsingUIManagerTest() |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 82 | : scoped_testing_local_state_(TestingBrowserProcess::GetGlobal()) { |
kylechar | b59c3d7 | 2019-10-29 05:26:26 | [diff] [blame] | 83 | ui_manager_ = new SafeBrowsingUIManager(nullptr); |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 84 | } |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 85 | |
juncai | 1ee189bd | 2017-06-09 04:25:43 | [diff] [blame] | 86 | ~SafeBrowsingUIManagerTest() override {} |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 87 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 88 | void SetUp() override { |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 89 | ChromeRenderViewHostTestHarness::SetUp(); |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 90 | SafeBrowsingUIManager::CreateWhitelistForTesting(web_contents()); |
Luke Zielinski | 12ef8855 | 2017-06-23 15:36:27 | [diff] [blame] | 91 | |
| 92 | safe_browsing::TestSafeBrowsingServiceFactory sb_service_factory; |
| 93 | auto* safe_browsing_service = |
| 94 | sb_service_factory.CreateSafeBrowsingService(); |
| 95 | TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService( |
| 96 | safe_browsing_service); |
| 97 | g_browser_process->safe_browsing_service()->Initialize(); |
| 98 | // A profile was created already but SafeBrowsingService wasn't around to |
| 99 | // get notified of it, so include that notification now. |
Evan Stade | 44fb6e38 | 2019-10-05 00:43:43 | [diff] [blame] | 100 | safe_browsing_service->OnProfileAdded( |
| 101 | Profile::FromBrowserContext(web_contents()->GetBrowserContext())); |
Ian Vollick | 381389a6 | 2019-06-26 16:49:40 | [diff] [blame] | 102 | content::BrowserThread::RunAllPendingTasksOnThreadForTesting( |
| 103 | content::BrowserThread::IO); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 104 | } |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 105 | |
Luke Zielinski | 12ef8855 | 2017-06-23 15:36:27 | [diff] [blame] | 106 | void TearDown() override { |
| 107 | TestingBrowserProcess::GetGlobal()->safe_browsing_service()->ShutDown(); |
| 108 | TestingBrowserProcess::GetGlobal()->SetSafeBrowsingService(nullptr); |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 109 | |
| 110 | // Depends on LocalState from ChromeRenderViewHostTestHarness. |
| 111 | if (SystemNetworkContextManager::GetInstance()) |
| 112 | SystemNetworkContextManager::DeleteInstance(); |
| 113 | |
Luke Zielinski | 12ef8855 | 2017-06-23 15:36:27 | [diff] [blame] | 114 | ChromeRenderViewHostTestHarness::TearDown(); |
| 115 | } |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 116 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 117 | bool IsWhitelisted(security_interstitials::UnsafeResource resource) { |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 118 | return ui_manager_->IsWhitelisted(resource); |
| 119 | } |
| 120 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 121 | void AddToWhitelist(security_interstitials::UnsafeResource resource) { |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 122 | ui_manager_->AddToWhitelistUrlSet( |
| 123 | SafeBrowsingUIManager::GetMainFrameWhitelistUrlForResourceForTesting( |
| 124 | resource), |
estark | 7ffa8c6 | 2016-11-11 23:21:55 | [diff] [blame] | 125 | web_contents(), false, resource.threat_type); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 126 | } |
| 127 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 128 | security_interstitials::UnsafeResource MakeUnsafeResource( |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 129 | const char* url, |
| 130 | bool is_subresource) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 131 | security_interstitials::UnsafeResource resource; |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 132 | resource.url = GURL(url); |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 133 | resource.is_subresource = is_subresource; |
scottmg | 22e4f25a | 2016-08-15 21:09:03 | [diff] [blame] | 134 | resource.web_contents_getter = |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 135 | security_interstitials::UnsafeResource::GetWebContentsGetter( |
Lukasz Anforowicz | b55fc49 | 2017-10-02 18:38:34 | [diff] [blame] | 136 | web_contents()->GetMainFrame()->GetProcess()->GetID(), |
scottmg | 22e4f25a | 2016-08-15 21:09:03 | [diff] [blame] | 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(), |
| 165 | main_frame_url); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 166 | } |
| 167 | |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 168 | protected: |
| 169 | SafeBrowsingUIManager* ui_manager() { return ui_manager_.get(); } |
| 170 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 171 | private: |
| 172 | scoped_refptr<SafeBrowsingUIManager> ui_manager_; |
John Abd-El-Malek | 1b3f5ca | 2019-06-06 16:42:32 | [diff] [blame] | 173 | ScopedTestingLocalState scoped_testing_local_state_; |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 174 | }; |
| 175 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 176 | // Leaks memory. https://crbug.com/755118 |
| 177 | #if defined(LEAK_SANITIZER) |
| 178 | #define MAYBE_Whitelist DISABLED_Whitelist |
| 179 | #else |
| 180 | #define MAYBE_Whitelist Whitelist |
| 181 | #endif |
| 182 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_Whitelist) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 183 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 184 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 185 | AddToWhitelist(resource); |
| 186 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 187 | } |
| 188 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 189 | // Leaks memory. https://crbug.com/755118 |
| 190 | #if defined(LEAK_SANITIZER) |
| 191 | #define MAYBE_WhitelistIgnoresSitesNotAdded \ |
| 192 | DISABLED_WhitelistIgnoresSitesNotAdded |
| 193 | #else |
| 194 | #define MAYBE_WhitelistIgnoresSitesNotAdded WhitelistIgnoresSitesNotAdded |
| 195 | #endif |
| 196 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresSitesNotAdded) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 197 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 198 | MakeUnsafeResourceAndStartNavigation(kGoodURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 199 | EXPECT_FALSE(IsWhitelisted(resource)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 200 | } |
| 201 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 202 | // Leaks memory. https://crbug.com/755118 |
| 203 | #if defined(LEAK_SANITIZER) |
| 204 | #define MAYBE_WhitelistRemembersThreatType DISABLED_WhitelistRemembersThreatType |
| 205 | #else |
| 206 | #define MAYBE_WhitelistRemembersThreatType WhitelistRemembersThreatType |
| 207 | #endif |
| 208 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistRemembersThreatType) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 209 | security_interstitials::UnsafeResource resource = |
estark | 7ffa8c6 | 2016-11-11 23:21:55 | [diff] [blame] | 210 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
| 211 | AddToWhitelist(resource); |
| 212 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 213 | SBThreatType threat_type; |
| 214 | content::NavigationEntry* entry = |
| 215 | web_contents()->GetController().GetVisibleEntry(); |
| 216 | ASSERT_TRUE(entry); |
| 217 | EXPECT_TRUE(ui_manager()->IsUrlWhitelistedOrPendingForWebContents( |
| 218 | resource.url, resource.is_subresource, entry, |
| 219 | resource.web_contents_getter.Run(), true, &threat_type)); |
| 220 | EXPECT_EQ(resource.threat_type, threat_type); |
| 221 | } |
| 222 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 223 | // Leaks memory. https://crbug.com/755118 |
| 224 | #if defined(LEAK_SANITIZER) |
| 225 | #define MAYBE_WhitelistIgnoresPath DISABLED_WhitelistIgnoresPath |
| 226 | #else |
| 227 | #define MAYBE_WhitelistIgnoresPath WhitelistIgnoresPath |
| 228 | #endif |
| 229 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresPath) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 230 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 231 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 232 | AddToWhitelist(resource); |
| 233 | EXPECT_TRUE(IsWhitelisted(resource)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 234 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 235 | content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 236 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 237 | security_interstitials::UnsafeResource resource_path = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 238 | MakeUnsafeResourceAndStartNavigation(kBadURLWithPath); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 239 | EXPECT_TRUE(IsWhitelisted(resource_path)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 240 | } |
| 241 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 242 | // Leaks memory. https://crbug.com/755118 |
| 243 | #if defined(LEAK_SANITIZER) |
| 244 | #define MAYBE_WhitelistIgnoresThreatType DISABLED_WhitelistIgnoresThreatType |
| 245 | #else |
| 246 | #define MAYBE_WhitelistIgnoresThreatType WhitelistIgnoresThreatType |
| 247 | #endif |
| 248 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistIgnoresThreatType) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 249 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 250 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 251 | AddToWhitelist(resource); |
| 252 | EXPECT_TRUE(IsWhitelisted(resource)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 253 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 254 | security_interstitials::UnsafeResource resource_phishing = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 255 | MakeUnsafeResource(kBadURL, false /* is_subresource */); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 256 | resource_phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING; |
| 257 | EXPECT_TRUE(IsWhitelisted(resource_phishing)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 258 | } |
| 259 | |
Marc Treib | 3d26e92 | 2017-08-14 16:58:26 | [diff] [blame] | 260 | // Leaks memory. https://crbug.com/755118 |
| 261 | #if defined(LEAK_SANITIZER) |
| 262 | #define MAYBE_WhitelistWithUnrelatedPendingLoad \ |
| 263 | DISABLED_WhitelistWithUnrelatedPendingLoad |
| 264 | #else |
| 265 | #define MAYBE_WhitelistWithUnrelatedPendingLoad \ |
| 266 | WhitelistWithUnrelatedPendingLoad |
| 267 | #endif |
| 268 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_WhitelistWithUnrelatedPendingLoad) { |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 269 | // Commit load of landing page. |
| 270 | NavigateAndCommit(GURL(kLandingURL)); |
clamy | 511cf02 | 2017-08-23 14:11:06 | [diff] [blame] | 271 | auto unrelated_navigation = |
| 272 | content::NavigationSimulator::CreateBrowserInitiated(GURL(kGoodURL), |
| 273 | web_contents()); |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 274 | { |
| 275 | // Simulate subresource malware hit on the landing page. |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 276 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 277 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 278 | |
| 279 | // Start pending load to unrelated site. |
clamy | 511cf02 | 2017-08-23 14:11:06 | [diff] [blame] | 280 | unrelated_navigation->Start(); |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 281 | |
| 282 | // Whitelist the resource on the landing page. |
| 283 | AddToWhitelist(resource); |
| 284 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 285 | } |
| 286 | |
| 287 | // Commit the pending load of unrelated site. |
clamy | 511cf02 | 2017-08-23 14:11:06 | [diff] [blame] | 288 | unrelated_navigation->Commit(); |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 289 | { |
| 290 | // The unrelated site is not on the whitelist, even if the same subresource |
| 291 | // was on it. |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 292 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 293 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 294 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 295 | } |
| 296 | |
| 297 | // Navigate back to the original landing url. |
| 298 | NavigateAndCommit(GURL(kLandingURL)); |
| 299 | { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 300 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 301 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 302 | // Original resource url is whitelisted. |
| 303 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 304 | } |
| 305 | { |
| 306 | // A different malware subresource on the same page is also whitelisted. |
| 307 | // (The whitelist is by the page url, not the resource url.) |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 308 | security_interstitials::UnsafeResource resource2 = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 309 | MakeUnsafeResource(kAnotherBadURL, true /* is_subresource */); |
| 310 | EXPECT_TRUE(IsWhitelisted(resource2)); |
| 311 | } |
| 312 | } |
| 313 | |
Evgenii Stepanov | 199c013 | 2017-08-18 21:19:41 | [diff] [blame] | 314 | // Leaks memory. https://crbug.com/755118 |
| 315 | #if defined(LEAK_SANITIZER) |
| 316 | #define MAYBE_UICallbackProceed DISABLED_UICallbackProceed |
| 317 | #else |
| 318 | #define MAYBE_UICallbackProceed UICallbackProceed |
| 319 | #endif |
| 320 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_UICallbackProceed) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 321 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 322 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 323 | SafeBrowsingCallbackWaiter waiter; |
| 324 | resource.callback = |
| 325 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 326 | base::Unretained(&waiter)); |
| 327 | resource.callback_thread = |
Sami Kyostila | ad439ec | 2019-08-06 14:49:52 | [diff] [blame] | 328 | base::CreateSingleThreadTaskRunner({BrowserThread::UI}); |
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)); |
| 351 | resource.callback_thread = |
Sami Kyostila | ad439ec | 2019-08-06 14:49:52 | [diff] [blame] | 352 | base::CreateSingleThreadTaskRunner({BrowserThread::UI}); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 353 | std::vector<security_interstitials::UnsafeResource> resources; |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 354 | resources.push_back(resource); |
| 355 | SimulateBlockingPageDone(resources, false); |
| 356 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 357 | waiter.WaitForCallback(); |
| 358 | EXPECT_TRUE(waiter.callback_called()); |
| 359 | EXPECT_FALSE(waiter.proceed()); |
| 360 | } |
| 361 | |
Evgenii Stepanov | 199c013 | 2017-08-18 21:19:41 | [diff] [blame] | 362 | // Leaks memory. https://crbug.com/755118 |
| 363 | #if defined(LEAK_SANITIZER) |
| 364 | #define MAYBE_IOCallbackProceed DISABLED_IOCallbackProceed |
| 365 | #else |
| 366 | #define MAYBE_IOCallbackProceed IOCallbackProceed |
| 367 | #endif |
| 368 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_IOCallbackProceed) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 369 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 370 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 371 | SafeBrowsingCallbackWaiter waiter; |
| 372 | resource.callback = |
| 373 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 374 | base::Unretained(&waiter)); |
| 375 | resource.callback_thread = |
Sami Kyostila | ad439ec | 2019-08-06 14:49:52 | [diff] [blame] | 376 | base::CreateSingleThreadTaskRunner({BrowserThread::IO}); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 377 | std::vector<security_interstitials::UnsafeResource> resources; |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 378 | resources.push_back(resource); |
| 379 | SimulateBlockingPageDone(resources, true); |
| 380 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 381 | waiter.WaitForCallback(); |
| 382 | EXPECT_TRUE(waiter.callback_called()); |
| 383 | EXPECT_TRUE(waiter.proceed()); |
| 384 | } |
| 385 | |
Evgenii Stepanov | 199c013 | 2017-08-18 21:19:41 | [diff] [blame] | 386 | // Leaks memory. https://crbug.com/755118 |
| 387 | #if defined(LEAK_SANITIZER) |
| 388 | #define MAYBE_IOCallbackDontProceed DISABLED_IOCallbackDontProceed |
| 389 | #else |
| 390 | #define MAYBE_IOCallbackDontProceed IOCallbackDontProceed |
| 391 | #endif |
| 392 | TEST_F(SafeBrowsingUIManagerTest, MAYBE_IOCallbackDontProceed) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 393 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 394 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 395 | SafeBrowsingCallbackWaiter waiter; |
| 396 | resource.callback = |
| 397 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 398 | base::Unretained(&waiter)); |
| 399 | resource.callback_thread = |
Sami Kyostila | ad439ec | 2019-08-06 14:49:52 | [diff] [blame] | 400 | base::CreateSingleThreadTaskRunner({BrowserThread::IO}); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 401 | std::vector<security_interstitials::UnsafeResource> resources; |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 402 | resources.push_back(resource); |
| 403 | SimulateBlockingPageDone(resources, false); |
| 404 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 405 | waiter.WaitForCallback(); |
| 406 | EXPECT_TRUE(waiter.callback_called()); |
| 407 | EXPECT_FALSE(waiter.proceed()); |
| 408 | } |
| 409 | |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 410 | namespace { |
| 411 | |
| 412 | // A WebContentsDelegate that records whether |
| 413 | // VisibleSecurityStateChanged() was called. |
| 414 | class SecurityStateWebContentsDelegate : public content::WebContentsDelegate { |
| 415 | public: |
| 416 | SecurityStateWebContentsDelegate() {} |
| 417 | ~SecurityStateWebContentsDelegate() override {} |
| 418 | |
| 419 | bool visible_security_state_changed() const { |
| 420 | return visible_security_state_changed_; |
| 421 | } |
| 422 | |
| 423 | void ClearVisibleSecurityStateChanged() { |
| 424 | visible_security_state_changed_ = false; |
| 425 | } |
| 426 | |
| 427 | // WebContentsDelegate: |
| 428 | void VisibleSecurityStateChanged(content::WebContents* source) override { |
| 429 | visible_security_state_changed_ = true; |
| 430 | } |
| 431 | |
| 432 | private: |
| 433 | bool visible_security_state_changed_ = false; |
| 434 | DISALLOW_COPY_AND_ASSIGN(SecurityStateWebContentsDelegate); |
| 435 | }; |
| 436 | |
| 437 | // A test blocking page that does not create windows. |
| 438 | class TestSafeBrowsingBlockingPage : public SafeBrowsingBlockingPage { |
| 439 | public: |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 440 | TestSafeBrowsingBlockingPage(BaseUIManager* manager, |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 441 | content::WebContents* web_contents, |
| 442 | const GURL& main_frame_url, |
| 443 | const UnsafeResourceList& unsafe_resources) |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 444 | : SafeBrowsingBlockingPage( |
| 445 | manager, |
| 446 | web_contents, |
| 447 | main_frame_url, |
| 448 | unsafe_resources, |
edwardjung | d7395fb0 | 2017-05-12 23:13:29 | [diff] [blame] | 449 | BaseSafeBrowsingErrorUI::SBErrorDisplayOptions( |
ntfschr | a5448fa | 2017-02-02 01:01:31 | [diff] [blame] | 450 | BaseBlockingPage::IsMainPageLoadBlocked(unsafe_resources), |
Carlos IL | 43dfc26 | 2019-11-27 19:40:31 | [diff] [blame] | 451 | false, // is_extended_reporting_opt_in_allowed |
| 452 | false, // is_off_the_record |
| 453 | false, // is_extended_reporting_enabled |
| 454 | false, // is_extended_reporting_policy_managed |
| 455 | false, // is_proceed_anyway_disabled |
| 456 | true, // should_open_links_in_new_tab |
| 457 | true, // always_show_back_to_safety |
| 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 | |
| 509 | // The callback needs to be set for committed interstitials, just set it to do |
| 510 | // nothing. |
| 511 | resource.callback = base::DoNothing(); |
| 512 | resource.callback_thread = |
| 513 | base::CreateSingleThreadTaskRunner({BrowserThread::IO}); |
| 514 | |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 515 | NavigateAndCommit(GURL("http://example.test")); |
| 516 | |
| 517 | delegate.ClearVisibleSecurityStateChanged(); |
| 518 | EXPECT_FALSE(delegate.visible_security_state_changed()); |
| 519 | ui_manager()->DisplayBlockingPage(resource); |
| 520 | EXPECT_TRUE(delegate.visible_security_state_changed()); |
| 521 | |
| 522 | // Simulate proceeding through the blocking page. |
| 523 | SafeBrowsingCallbackWaiter waiter; |
| 524 | resource.callback = |
| 525 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 526 | base::Unretained(&waiter)); |
| 527 | resource.callback_thread = |
Sami Kyostila | ad439ec | 2019-08-06 14:49:52 | [diff] [blame] | 528 | base::CreateSingleThreadTaskRunner({BrowserThread::IO}); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 529 | std::vector<security_interstitials::UnsafeResource> resources; |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 530 | resources.push_back(resource); |
| 531 | |
| 532 | delegate.ClearVisibleSecurityStateChanged(); |
| 533 | EXPECT_FALSE(delegate.visible_security_state_changed()); |
| 534 | SimulateBlockingPageDone(resources, true); |
| 535 | EXPECT_TRUE(delegate.visible_security_state_changed()); |
| 536 | |
| 537 | waiter.WaitForCallback(); |
| 538 | EXPECT_TRUE(waiter.callback_called()); |
| 539 | EXPECT_TRUE(waiter.proceed()); |
| 540 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 541 | } |
| 542 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 543 | } // namespace safe_browsing |