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" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 9 | #include "chrome/browser/safe_browsing/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" |
| 12 | #include "chrome/test/base/testing_profile.h" |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 13 | #include "components/safe_browsing_db/safe_browsing_prefs.h" |
vakh | fa183fa | 2016-03-29 17:33:37 | [diff] [blame] | 14 | #include "components/safe_browsing_db/util.h" |
ntfschr | a5448fa | 2017-02-02 01:01:31 | [diff] [blame] | 15 | #include "components/security_interstitials/core/safe_browsing_error_ui.h" |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 16 | #include "content/public/browser/navigation_entry.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 17 | #include "content/public/browser/render_process_host.h" |
| 18 | #include "content/public/browser/render_view_host.h" |
| 19 | #include "content/public/browser/web_contents.h" |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 20 | #include "content/public/browser/web_contents_delegate.h" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 21 | #include "content/public/test/test_browser_thread_bundle.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 22 | #include "content/public/test/web_contents_tester.h" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 23 | #include "testing/gtest/include/gtest/gtest.h" |
| 24 | #include "url/gurl.h" |
| 25 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 26 | using content::BrowserThread; |
| 27 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 28 | static const char* kGoodURL = "https://www.good.com"; |
| 29 | static const char* kBadURL = "https://www.malware.com"; |
| 30 | static const char* kBadURLWithPath = "https://www.malware.com/index.html"; |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 31 | static const char* kAnotherBadURL = "https://www.badware.com"; |
| 32 | static const char* kLandingURL = "https://www.landing.com"; |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 33 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 34 | namespace safe_browsing { |
| 35 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 36 | class SafeBrowsingCallbackWaiter { |
| 37 | public: |
| 38 | SafeBrowsingCallbackWaiter() {} |
| 39 | |
| 40 | bool callback_called() const { return callback_called_; } |
| 41 | bool proceed() const { return proceed_; } |
| 42 | |
| 43 | void OnBlockingPageDone(bool proceed) { |
| 44 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 45 | callback_called_ = true; |
| 46 | proceed_ = proceed; |
| 47 | loop_.Quit(); |
| 48 | } |
| 49 | |
| 50 | void OnBlockingPageDoneOnIO(bool proceed) { |
| 51 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 52 | BrowserThread::PostTask( |
| 53 | BrowserThread::UI, FROM_HERE, |
| 54 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 55 | base::Unretained(this), proceed)); |
| 56 | } |
| 57 | |
| 58 | void WaitForCallback() { |
| 59 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 60 | loop_.Run(); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | bool callback_called_ = false; |
| 65 | bool proceed_ = false; |
| 66 | base::RunLoop loop_; |
| 67 | }; |
| 68 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 69 | class SafeBrowsingUIManagerTest : public ChromeRenderViewHostTestHarness { |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 70 | public: |
| 71 | SafeBrowsingUIManagerTest() : ui_manager_(new SafeBrowsingUIManager(NULL)) {} |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 72 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 73 | ~SafeBrowsingUIManagerTest() override{}; |
| 74 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 75 | void SetUp() override { |
| 76 | SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); |
| 77 | ChromeRenderViewHostTestHarness::SetUp(); |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 78 | SafeBrowsingUIManager::CreateWhitelistForTesting(web_contents()); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 79 | } |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 80 | |
| 81 | void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); } |
| 82 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 83 | bool IsWhitelisted(security_interstitials::UnsafeResource resource) { |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 84 | return ui_manager_->IsWhitelisted(resource); |
| 85 | } |
| 86 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 87 | void AddToWhitelist(security_interstitials::UnsafeResource resource) { |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 88 | ui_manager_->AddToWhitelistUrlSet( |
| 89 | SafeBrowsingUIManager::GetMainFrameWhitelistUrlForResourceForTesting( |
| 90 | resource), |
estark | 7ffa8c6 | 2016-11-11 23:21:55 | [diff] [blame] | 91 | web_contents(), false, resource.threat_type); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 92 | } |
| 93 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 94 | security_interstitials::UnsafeResource MakeUnsafeResource( |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 95 | const char* url, |
| 96 | bool is_subresource) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 97 | security_interstitials::UnsafeResource resource; |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 98 | resource.url = GURL(url); |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 99 | resource.is_subresource = is_subresource; |
scottmg | 22e4f25a | 2016-08-15 21:09:03 | [diff] [blame] | 100 | resource.web_contents_getter = |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 101 | security_interstitials::UnsafeResource::GetWebContentsGetter( |
scottmg | 22e4f25a | 2016-08-15 21:09:03 | [diff] [blame] | 102 | web_contents()->GetRenderProcessHost()->GetID(), |
| 103 | web_contents()->GetMainFrame()->GetRoutingID()); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 104 | resource.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 105 | return resource; |
| 106 | } |
| 107 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 108 | security_interstitials::UnsafeResource MakeUnsafeResourceAndStartNavigation( |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 109 | const char* url) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 110 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 111 | MakeUnsafeResource(url, false /* is_subresource */); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 112 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 113 | // The WC doesn't have a URL without a navigation. A main-frame malware |
| 114 | // unsafe resource must be a pending navigation. |
| 115 | content::WebContentsTester::For(web_contents())->StartNavigation(GURL(url)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 116 | return resource; |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 117 | } |
| 118 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 119 | void SimulateBlockingPageDone( |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 120 | const std::vector<security_interstitials::UnsafeResource>& resources, |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 121 | bool proceed) { |
estark | 1ca09ca | 2016-11-01 04:04:12 | [diff] [blame] | 122 | GURL main_frame_url; |
| 123 | content::NavigationEntry* entry = |
| 124 | web_contents()->GetController().GetVisibleEntry(); |
| 125 | if (entry) |
| 126 | main_frame_url = entry->GetURL(); |
| 127 | |
| 128 | ui_manager_->OnBlockingPageDone(resources, proceed, web_contents(), |
| 129 | main_frame_url); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 130 | } |
| 131 | |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 132 | protected: |
| 133 | SafeBrowsingUIManager* ui_manager() { return ui_manager_.get(); } |
| 134 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 135 | private: |
| 136 | scoped_refptr<SafeBrowsingUIManager> ui_manager_; |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | TEST_F(SafeBrowsingUIManagerTest, Whitelist) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 140 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 141 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 142 | AddToWhitelist(resource); |
| 143 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 144 | } |
| 145 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 146 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresSitesNotAdded) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 147 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 148 | MakeUnsafeResourceAndStartNavigation(kGoodURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 149 | EXPECT_FALSE(IsWhitelisted(resource)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 150 | } |
| 151 | |
estark | 7ffa8c6 | 2016-11-11 23:21:55 | [diff] [blame] | 152 | TEST_F(SafeBrowsingUIManagerTest, WhitelistRemembersThreatType) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 153 | security_interstitials::UnsafeResource resource = |
estark | 7ffa8c6 | 2016-11-11 23:21:55 | [diff] [blame] | 154 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
| 155 | AddToWhitelist(resource); |
| 156 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 157 | SBThreatType threat_type; |
| 158 | content::NavigationEntry* entry = |
| 159 | web_contents()->GetController().GetVisibleEntry(); |
| 160 | ASSERT_TRUE(entry); |
| 161 | EXPECT_TRUE(ui_manager()->IsUrlWhitelistedOrPendingForWebContents( |
| 162 | resource.url, resource.is_subresource, entry, |
| 163 | resource.web_contents_getter.Run(), true, &threat_type)); |
| 164 | EXPECT_EQ(resource.threat_type, threat_type); |
| 165 | } |
| 166 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 167 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresPath) { |
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)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 172 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 173 | content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 174 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 175 | security_interstitials::UnsafeResource resource_path = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 176 | MakeUnsafeResourceAndStartNavigation(kBadURLWithPath); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 177 | EXPECT_TRUE(IsWhitelisted(resource_path)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 178 | } |
| 179 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 180 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresThreatType) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 181 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 182 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 183 | AddToWhitelist(resource); |
| 184 | EXPECT_TRUE(IsWhitelisted(resource)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 185 | |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 186 | security_interstitials::UnsafeResource resource_phishing = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 187 | MakeUnsafeResource(kBadURL, false /* is_subresource */); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 188 | resource_phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING; |
| 189 | EXPECT_TRUE(IsWhitelisted(resource_phishing)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 190 | } |
| 191 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 192 | TEST_F(SafeBrowsingUIManagerTest, WhitelistWithUnrelatedPendingLoad) { |
| 193 | // Commit load of landing page. |
| 194 | NavigateAndCommit(GURL(kLandingURL)); |
| 195 | { |
| 196 | // Simulate subresource malware hit on the landing page. |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 197 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 198 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 199 | |
| 200 | // Start pending load to unrelated site. |
| 201 | content::WebContentsTester::For(web_contents()) |
| 202 | ->StartNavigation(GURL(kGoodURL)); |
| 203 | |
| 204 | // Whitelist the resource on the landing page. |
| 205 | AddToWhitelist(resource); |
| 206 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 207 | } |
| 208 | |
| 209 | // Commit the pending load of unrelated site. |
| 210 | content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 211 | { |
| 212 | // The unrelated site is not on the whitelist, even if the same subresource |
| 213 | // was on it. |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 214 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 215 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 216 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 217 | } |
| 218 | |
| 219 | // Navigate back to the original landing url. |
| 220 | NavigateAndCommit(GURL(kLandingURL)); |
| 221 | { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 222 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 223 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 224 | // Original resource url is whitelisted. |
| 225 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 226 | } |
| 227 | { |
| 228 | // A different malware subresource on the same page is also whitelisted. |
| 229 | // (The whitelist is by the page url, not the resource url.) |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 230 | security_interstitials::UnsafeResource resource2 = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 231 | MakeUnsafeResource(kAnotherBadURL, true /* is_subresource */); |
| 232 | EXPECT_TRUE(IsWhitelisted(resource2)); |
| 233 | } |
| 234 | } |
| 235 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 236 | TEST_F(SafeBrowsingUIManagerTest, UICallbackProceed) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 237 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 238 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 239 | SafeBrowsingCallbackWaiter waiter; |
| 240 | resource.callback = |
| 241 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 242 | base::Unretained(&waiter)); |
| 243 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 244 | BrowserThread::GetTaskRunnerForThread(BrowserThread::UI); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 245 | std::vector<security_interstitials::UnsafeResource> resources; |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 246 | resources.push_back(resource); |
| 247 | SimulateBlockingPageDone(resources, true); |
| 248 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 249 | waiter.WaitForCallback(); |
| 250 | EXPECT_TRUE(waiter.callback_called()); |
| 251 | EXPECT_TRUE(waiter.proceed()); |
| 252 | } |
| 253 | |
| 254 | TEST_F(SafeBrowsingUIManagerTest, UICallbackDontProceed) { |
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); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 257 | SafeBrowsingCallbackWaiter waiter; |
| 258 | resource.callback = |
| 259 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 260 | base::Unretained(&waiter)); |
| 261 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 262 | BrowserThread::GetTaskRunnerForThread(BrowserThread::UI); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 263 | std::vector<security_interstitials::UnsafeResource> resources; |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 264 | resources.push_back(resource); |
| 265 | SimulateBlockingPageDone(resources, false); |
| 266 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 267 | waiter.WaitForCallback(); |
| 268 | EXPECT_TRUE(waiter.callback_called()); |
| 269 | EXPECT_FALSE(waiter.proceed()); |
| 270 | } |
| 271 | |
| 272 | TEST_F(SafeBrowsingUIManagerTest, IOCallbackProceed) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 273 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 274 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 275 | SafeBrowsingCallbackWaiter waiter; |
| 276 | resource.callback = |
| 277 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 278 | base::Unretained(&waiter)); |
| 279 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 280 | BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 281 | std::vector<security_interstitials::UnsafeResource> resources; |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 282 | resources.push_back(resource); |
| 283 | SimulateBlockingPageDone(resources, true); |
| 284 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 285 | waiter.WaitForCallback(); |
| 286 | EXPECT_TRUE(waiter.callback_called()); |
| 287 | EXPECT_TRUE(waiter.proceed()); |
| 288 | } |
| 289 | |
| 290 | TEST_F(SafeBrowsingUIManagerTest, IOCallbackDontProceed) { |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 291 | security_interstitials::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 292 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 293 | SafeBrowsingCallbackWaiter waiter; |
| 294 | resource.callback = |
| 295 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 296 | base::Unretained(&waiter)); |
| 297 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 298 | BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 299 | std::vector<security_interstitials::UnsafeResource> resources; |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 300 | resources.push_back(resource); |
| 301 | SimulateBlockingPageDone(resources, false); |
| 302 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 303 | waiter.WaitForCallback(); |
| 304 | EXPECT_TRUE(waiter.callback_called()); |
| 305 | EXPECT_FALSE(waiter.proceed()); |
| 306 | } |
| 307 | |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 308 | namespace { |
| 309 | |
| 310 | // A WebContentsDelegate that records whether |
| 311 | // VisibleSecurityStateChanged() was called. |
| 312 | class SecurityStateWebContentsDelegate : public content::WebContentsDelegate { |
| 313 | public: |
| 314 | SecurityStateWebContentsDelegate() {} |
| 315 | ~SecurityStateWebContentsDelegate() override {} |
| 316 | |
| 317 | bool visible_security_state_changed() const { |
| 318 | return visible_security_state_changed_; |
| 319 | } |
| 320 | |
| 321 | void ClearVisibleSecurityStateChanged() { |
| 322 | visible_security_state_changed_ = false; |
| 323 | } |
| 324 | |
| 325 | // WebContentsDelegate: |
| 326 | void VisibleSecurityStateChanged(content::WebContents* source) override { |
| 327 | visible_security_state_changed_ = true; |
| 328 | } |
| 329 | |
| 330 | private: |
| 331 | bool visible_security_state_changed_ = false; |
| 332 | DISALLOW_COPY_AND_ASSIGN(SecurityStateWebContentsDelegate); |
| 333 | }; |
| 334 | |
| 335 | // A test blocking page that does not create windows. |
| 336 | class TestSafeBrowsingBlockingPage : public SafeBrowsingBlockingPage { |
| 337 | public: |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 338 | TestSafeBrowsingBlockingPage(BaseUIManager* manager, |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 339 | content::WebContents* web_contents, |
| 340 | const GURL& main_frame_url, |
| 341 | const UnsafeResourceList& unsafe_resources) |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 342 | : SafeBrowsingBlockingPage( |
| 343 | manager, |
| 344 | web_contents, |
| 345 | main_frame_url, |
| 346 | unsafe_resources, |
ntfschr | a5448fa | 2017-02-02 01:01:31 | [diff] [blame] | 347 | SafeBrowsingErrorUI::SBErrorDisplayOptions( |
| 348 | BaseBlockingPage::IsMainPageLoadBlocked(unsafe_resources), |
| 349 | false, |
| 350 | false, |
| 351 | false, |
| 352 | false, |
| 353 | false)) { |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 354 | // Don't delay details at all for the unittest. |
ntfschr | fef42f9 | 2017-02-24 02:15:47 | [diff] [blame^] | 355 | SetThreatDetailsProceedDelayForTesting(0); |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 356 | DontCreateViewForTesting(); |
| 357 | } |
| 358 | }; |
| 359 | |
| 360 | // A factory that creates TestSafeBrowsingBlockingPages. |
| 361 | class TestSafeBrowsingBlockingPageFactory |
| 362 | : public SafeBrowsingBlockingPageFactory { |
| 363 | public: |
| 364 | TestSafeBrowsingBlockingPageFactory() {} |
| 365 | ~TestSafeBrowsingBlockingPageFactory() override {} |
| 366 | |
| 367 | SafeBrowsingBlockingPage* CreateSafeBrowsingPage( |
jialiul | 3d6032e | 2017-01-12 00:41:31 | [diff] [blame] | 368 | BaseUIManager* delegate, |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 369 | content::WebContents* web_contents, |
| 370 | const GURL& main_frame_url, |
| 371 | const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources) |
| 372 | override { |
| 373 | return new TestSafeBrowsingBlockingPage(delegate, web_contents, |
| 374 | main_frame_url, unsafe_resources); |
| 375 | } |
| 376 | }; |
| 377 | |
| 378 | } // namespace |
| 379 | |
| 380 | // Tests that the WebContentsDelegate is notified of a visible security |
| 381 | // state change when a blocking page is shown for a subresource. |
| 382 | TEST_F(SafeBrowsingUIManagerTest, |
| 383 | VisibleSecurityStateChangedForUnsafeSubresource) { |
| 384 | TestSafeBrowsingBlockingPageFactory factory; |
| 385 | SafeBrowsingBlockingPage::RegisterFactory(&factory); |
| 386 | SecurityStateWebContentsDelegate delegate; |
| 387 | web_contents()->SetDelegate(&delegate); |
| 388 | |
| 389 | // Simulate a blocking page showing for an unsafe subresource. |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 390 | security_interstitials::UnsafeResource resource = |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 391 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 392 | // Needed for showing the blocking page. |
| 393 | resource.threat_source = safe_browsing::ThreatSource::REMOTE; |
| 394 | NavigateAndCommit(GURL("http://example.test")); |
| 395 | |
| 396 | delegate.ClearVisibleSecurityStateChanged(); |
| 397 | EXPECT_FALSE(delegate.visible_security_state_changed()); |
| 398 | ui_manager()->DisplayBlockingPage(resource); |
| 399 | EXPECT_TRUE(delegate.visible_security_state_changed()); |
| 400 | |
| 401 | // Simulate proceeding through the blocking page. |
| 402 | SafeBrowsingCallbackWaiter waiter; |
| 403 | resource.callback = |
| 404 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 405 | base::Unretained(&waiter)); |
| 406 | resource.callback_thread = |
| 407 | BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
jialiul | 792a666 | 2016-12-03 01:44:10 | [diff] [blame] | 408 | std::vector<security_interstitials::UnsafeResource> resources; |
dalecurtis | 6c58ed0 | 2016-10-28 23:02:37 | [diff] [blame] | 409 | resources.push_back(resource); |
| 410 | |
| 411 | delegate.ClearVisibleSecurityStateChanged(); |
| 412 | EXPECT_FALSE(delegate.visible_security_state_changed()); |
| 413 | SimulateBlockingPageDone(resources, true); |
| 414 | EXPECT_TRUE(delegate.visible_security_state_changed()); |
| 415 | |
| 416 | waiter.WaitForCallback(); |
| 417 | EXPECT_TRUE(waiter.callback_called()); |
| 418 | EXPECT_TRUE(waiter.proceed()); |
| 419 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 420 | } |
| 421 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 422 | } // namespace safe_browsing |