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