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