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