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