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