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" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 8 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
scottmg | 22e4f25a | 2016-08-15 21:09:03 | [diff] [blame] | 9 | #include "chrome/browser/safe_browsing/ui_manager.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 10 | #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 11 | #include "chrome/test/base/testing_profile.h" |
vakh | fa183fa | 2016-03-29 17:33:37 | [diff] [blame] | 12 | #include "components/safe_browsing_db/util.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 13 | #include "content/public/browser/render_process_host.h" |
| 14 | #include "content/public/browser/render_view_host.h" |
| 15 | #include "content/public/browser/web_contents.h" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 16 | #include "content/public/test/test_browser_thread_bundle.h" |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 17 | #include "content/public/test/web_contents_tester.h" |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | #include "url/gurl.h" |
| 20 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 21 | using content::BrowserThread; |
| 22 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 23 | static const char* kGoodURL = "https://www.good.com"; |
| 24 | static const char* kBadURL = "https://www.malware.com"; |
| 25 | static const char* kBadURLWithPath = "https://www.malware.com/index.html"; |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 26 | static const char* kAnotherBadURL = "https://www.badware.com"; |
| 27 | static const char* kLandingURL = "https://www.landing.com"; |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 28 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 29 | namespace safe_browsing { |
| 30 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 31 | class SafeBrowsingCallbackWaiter { |
| 32 | public: |
| 33 | SafeBrowsingCallbackWaiter() {} |
| 34 | |
| 35 | bool callback_called() const { return callback_called_; } |
| 36 | bool proceed() const { return proceed_; } |
| 37 | |
| 38 | void OnBlockingPageDone(bool proceed) { |
| 39 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 40 | callback_called_ = true; |
| 41 | proceed_ = proceed; |
| 42 | loop_.Quit(); |
| 43 | } |
| 44 | |
| 45 | void OnBlockingPageDoneOnIO(bool proceed) { |
| 46 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 47 | BrowserThread::PostTask( |
| 48 | BrowserThread::UI, FROM_HERE, |
| 49 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 50 | base::Unretained(this), proceed)); |
| 51 | } |
| 52 | |
| 53 | void WaitForCallback() { |
| 54 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 55 | loop_.Run(); |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | bool callback_called_ = false; |
| 60 | bool proceed_ = false; |
| 61 | base::RunLoop loop_; |
| 62 | }; |
| 63 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 64 | class SafeBrowsingUIManagerTest : public ChromeRenderViewHostTestHarness { |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 65 | public: |
| 66 | SafeBrowsingUIManagerTest() : ui_manager_(new SafeBrowsingUIManager(NULL)) {} |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 67 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 68 | ~SafeBrowsingUIManagerTest() override{}; |
| 69 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 70 | void SetUp() override { |
| 71 | SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); |
| 72 | ChromeRenderViewHostTestHarness::SetUp(); |
| 73 | } |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 74 | |
| 75 | void TearDown() override { ChromeRenderViewHostTestHarness::TearDown(); } |
| 76 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 77 | bool IsWhitelisted(SafeBrowsingUIManager::UnsafeResource resource) { |
| 78 | return ui_manager_->IsWhitelisted(resource); |
| 79 | } |
| 80 | |
| 81 | void AddToWhitelist(SafeBrowsingUIManager::UnsafeResource resource) { |
felt | 49e363a | 2016-08-26 13:46:08 | [diff] [blame] | 82 | ui_manager_->AddToWhitelistUrlSet(resource, false); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 83 | } |
| 84 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 85 | SafeBrowsingUIManager::UnsafeResource MakeUnsafeResource( |
| 86 | const char* url, |
| 87 | bool is_subresource) { |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 88 | SafeBrowsingUIManager::UnsafeResource resource; |
| 89 | resource.url = GURL(url); |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 90 | resource.is_subresource = is_subresource; |
scottmg | 22e4f25a | 2016-08-15 21:09:03 | [diff] [blame] | 91 | resource.web_contents_getter = |
| 92 | SafeBrowsingUIManager::UnsafeResource::GetWebContentsGetter( |
| 93 | web_contents()->GetRenderProcessHost()->GetID(), |
| 94 | web_contents()->GetMainFrame()->GetRoutingID()); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 95 | resource.threat_type = SB_THREAT_TYPE_URL_MALWARE; |
| 96 | return resource; |
| 97 | } |
| 98 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 99 | SafeBrowsingUIManager::UnsafeResource MakeUnsafeResourceAndStartNavigation( |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 100 | const char* url) { |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 101 | SafeBrowsingUIManager::UnsafeResource resource = |
| 102 | MakeUnsafeResource(url, false /* is_subresource */); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 103 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 104 | // The WC doesn't have a URL without a navigation. A main-frame malware |
| 105 | // unsafe resource must be a pending navigation. |
| 106 | content::WebContentsTester::For(web_contents())->StartNavigation(GURL(url)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 107 | return resource; |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 108 | } |
| 109 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 110 | void SimulateBlockingPageDone( |
| 111 | const std::vector<SafeBrowsingUIManager::UnsafeResource>& resources, |
| 112 | bool proceed) { |
| 113 | ui_manager_->OnBlockingPageDone(resources, proceed); |
| 114 | } |
| 115 | |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 116 | private: |
| 117 | scoped_refptr<SafeBrowsingUIManager> ui_manager_; |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | TEST_F(SafeBrowsingUIManagerTest, Whitelist) { |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 121 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 122 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 123 | AddToWhitelist(resource); |
| 124 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 125 | } |
| 126 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 127 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresSitesNotAdded) { |
| 128 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 129 | MakeUnsafeResourceAndStartNavigation(kGoodURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 130 | EXPECT_FALSE(IsWhitelisted(resource)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresPath) { |
| 134 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 135 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 136 | AddToWhitelist(resource); |
| 137 | EXPECT_TRUE(IsWhitelisted(resource)); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 138 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 139 | content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 140 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 141 | SafeBrowsingUIManager::UnsafeResource resource_path = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 142 | MakeUnsafeResourceAndStartNavigation(kBadURLWithPath); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 143 | EXPECT_TRUE(IsWhitelisted(resource_path)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 144 | } |
| 145 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 146 | TEST_F(SafeBrowsingUIManagerTest, WhitelistIgnoresThreatType) { |
| 147 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 148 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 149 | AddToWhitelist(resource); |
| 150 | EXPECT_TRUE(IsWhitelisted(resource)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 151 | |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 152 | SafeBrowsingUIManager::UnsafeResource resource_phishing = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 153 | MakeUnsafeResource(kBadURL, false /* is_subresource */); |
felt | fb11857 | 2015-08-18 05:22:01 | [diff] [blame] | 154 | resource_phishing.threat_type = SB_THREAT_TYPE_URL_PHISHING; |
| 155 | EXPECT_TRUE(IsWhitelisted(resource_phishing)); |
felt | bc2eda2d | 2015-06-23 02:06:03 | [diff] [blame] | 156 | } |
| 157 | |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 158 | TEST_F(SafeBrowsingUIManagerTest, WhitelistWithUnrelatedPendingLoad) { |
| 159 | // Commit load of landing page. |
| 160 | NavigateAndCommit(GURL(kLandingURL)); |
| 161 | { |
| 162 | // Simulate subresource malware hit on the landing page. |
| 163 | SafeBrowsingUIManager::UnsafeResource resource = |
| 164 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 165 | |
| 166 | // Start pending load to unrelated site. |
| 167 | content::WebContentsTester::For(web_contents()) |
| 168 | ->StartNavigation(GURL(kGoodURL)); |
| 169 | |
| 170 | // Whitelist the resource on the landing page. |
| 171 | AddToWhitelist(resource); |
| 172 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 173 | } |
| 174 | |
| 175 | // Commit the pending load of unrelated site. |
| 176 | content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 177 | { |
| 178 | // The unrelated site is not on the whitelist, even if the same subresource |
| 179 | // was on it. |
| 180 | SafeBrowsingUIManager::UnsafeResource resource = |
| 181 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 182 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 183 | } |
| 184 | |
| 185 | // Navigate back to the original landing url. |
| 186 | NavigateAndCommit(GURL(kLandingURL)); |
| 187 | { |
| 188 | SafeBrowsingUIManager::UnsafeResource resource = |
| 189 | MakeUnsafeResource(kBadURL, true /* is_subresource */); |
| 190 | // Original resource url is whitelisted. |
| 191 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 192 | } |
| 193 | { |
| 194 | // A different malware subresource on the same page is also whitelisted. |
| 195 | // (The whitelist is by the page url, not the resource url.) |
| 196 | SafeBrowsingUIManager::UnsafeResource resource2 = |
| 197 | MakeUnsafeResource(kAnotherBadURL, true /* is_subresource */); |
| 198 | EXPECT_TRUE(IsWhitelisted(resource2)); |
| 199 | } |
| 200 | } |
| 201 | |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 202 | TEST_F(SafeBrowsingUIManagerTest, UICallbackProceed) { |
| 203 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 204 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 205 | SafeBrowsingCallbackWaiter waiter; |
| 206 | resource.callback = |
| 207 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 208 | base::Unretained(&waiter)); |
| 209 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 210 | BrowserThread::GetTaskRunnerForThread(BrowserThread::UI); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 211 | std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
| 212 | resources.push_back(resource); |
| 213 | SimulateBlockingPageDone(resources, true); |
| 214 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 215 | waiter.WaitForCallback(); |
| 216 | EXPECT_TRUE(waiter.callback_called()); |
| 217 | EXPECT_TRUE(waiter.proceed()); |
| 218 | } |
| 219 | |
| 220 | TEST_F(SafeBrowsingUIManagerTest, UICallbackDontProceed) { |
| 221 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 222 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 223 | SafeBrowsingCallbackWaiter waiter; |
| 224 | resource.callback = |
| 225 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDone, |
| 226 | base::Unretained(&waiter)); |
| 227 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 228 | BrowserThread::GetTaskRunnerForThread(BrowserThread::UI); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 229 | std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
| 230 | resources.push_back(resource); |
| 231 | SimulateBlockingPageDone(resources, false); |
| 232 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 233 | waiter.WaitForCallback(); |
| 234 | EXPECT_TRUE(waiter.callback_called()); |
| 235 | EXPECT_FALSE(waiter.proceed()); |
| 236 | } |
| 237 | |
| 238 | TEST_F(SafeBrowsingUIManagerTest, IOCallbackProceed) { |
| 239 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 240 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 241 | SafeBrowsingCallbackWaiter waiter; |
| 242 | resource.callback = |
| 243 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 244 | base::Unretained(&waiter)); |
| 245 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 246 | BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 247 | std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
| 248 | resources.push_back(resource); |
| 249 | SimulateBlockingPageDone(resources, true); |
| 250 | EXPECT_TRUE(IsWhitelisted(resource)); |
| 251 | waiter.WaitForCallback(); |
| 252 | EXPECT_TRUE(waiter.callback_called()); |
| 253 | EXPECT_TRUE(waiter.proceed()); |
| 254 | } |
| 255 | |
| 256 | TEST_F(SafeBrowsingUIManagerTest, IOCallbackDontProceed) { |
| 257 | SafeBrowsingUIManager::UnsafeResource resource = |
mattm | bfc4060d | 2015-12-18 23:11:38 | [diff] [blame] | 258 | MakeUnsafeResourceAndStartNavigation(kBadURL); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 259 | SafeBrowsingCallbackWaiter waiter; |
| 260 | resource.callback = |
| 261 | base::Bind(&SafeBrowsingCallbackWaiter::OnBlockingPageDoneOnIO, |
| 262 | base::Unretained(&waiter)); |
| 263 | resource.callback_thread = |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 264 | BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
clamy | 4edbf0e | 2015-12-02 13:35:41 | [diff] [blame] | 265 | std::vector<SafeBrowsingUIManager::UnsafeResource> resources; |
| 266 | resources.push_back(resource); |
| 267 | SimulateBlockingPageDone(resources, false); |
| 268 | EXPECT_FALSE(IsWhitelisted(resource)); |
| 269 | waiter.WaitForCallback(); |
| 270 | EXPECT_TRUE(waiter.callback_called()); |
| 271 | EXPECT_FALSE(waiter.proceed()); |
| 272 | } |
| 273 | |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 274 | } // namespace safe_browsing |