OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 |
| 5 #include "components/subresource_filter/content/browser/subresource_filter_safe_
browsing_activation_throttle.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/metrics/field_trial.h" |
| 11 #include "base/test/histogram_tester.h" |
| 12 #include "components/safe_browsing_db/test_database_manager.h" |
| 13 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" |
| 14 #include "components/subresource_filter/content/browser/subresource_filter_clien
t.h" |
| 15 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" |
| 16 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" |
| 17 #include "components/subresource_filter/core/common/test_ruleset_creator.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/navigation_handle.h" |
| 20 #include "content/public/browser/web_contents_observer.h" |
| 21 #include "content/public/test/navigation_simulator.h" |
| 22 #include "content/public/test/test_renderer_host.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 namespace subresource_filter { |
| 27 |
| 28 namespace { |
| 29 |
| 30 char kURL[] = "http://example.test/"; |
| 31 char kRedirectURL[] = "http://foo.test/"; |
| 32 |
| 33 // Names of navigation chain patterns histogram. |
| 34 const char kMatchesPatternHistogramNameSubresourceFilterSuffix[] = |
| 35 "SubresourceFilter.PageLoad.RedirectChainMatchPattern." |
| 36 "SubresourceFilterOnly"; |
| 37 const char kNavigationChainSizeSubresourceFilterSuffix[] = |
| 38 "SubresourceFilter.PageLoad.RedirectChainLength.SubresourceFilterOnly"; |
| 39 |
| 40 // Human readable representation of expected redirect chain match patterns. |
| 41 // The explanations for the buckets given for the following redirect chain: |
| 42 // A->B->C->D, where A is initial URL and D is a final URL. |
| 43 enum RedirectChainMatchPattern { |
| 44 EMPTY, // No histograms were recorded. |
| 45 F0M0L1, // D is a Safe Browsing match. |
| 46 F0M1L0, // B or C, or both are Safe Browsing matches. |
| 47 F0M1L1, // B or C, or both and D are Safe Browsing matches. |
| 48 F1M0L0, // A is Safe Browsing match |
| 49 F1M0L1, // A and D are Safe Browsing matches. |
| 50 F1M1L0, // B and/or C and A are Safe Browsing matches. |
| 51 F1M1L1, // B and/or C and A and D are Safe Browsing matches. |
| 52 NO_REDIRECTS_HIT, // Redirect chain consists of single URL, aka no redirects |
| 53 // has happened, and this URL was a Safe Browsing hit. |
| 54 NUM_HIT_PATTERNS, |
| 55 }; |
| 56 |
| 57 // Database manager that allows any URL to be configured as blacklisted for |
| 58 // testing. |
| 59 class FakeSafeBrowsingDatabaseManager |
| 60 : public safe_browsing::TestSafeBrowsingDatabaseManager { |
| 61 public: |
| 62 FakeSafeBrowsingDatabaseManager() : simulate_timeout_(false) {} |
| 63 |
| 64 void AddBlacklistedUrl(const GURL& url, |
| 65 safe_browsing::SBThreatType threat_type) { |
| 66 url_to_threat_type_[url] = threat_type; |
| 67 } |
| 68 |
| 69 void SimulateTimeout() { simulate_timeout_ = true; } |
| 70 |
| 71 protected: |
| 72 ~FakeSafeBrowsingDatabaseManager() override {} |
| 73 |
| 74 bool CheckUrlForSubresourceFilter(const GURL& url, Client* client) override { |
| 75 if (simulate_timeout_) |
| 76 return false; |
| 77 if (!url_to_threat_type_.count(url)) |
| 78 return true; |
| 79 |
| 80 content::BrowserThread::PostTask( |
| 81 content::BrowserThread::IO, FROM_HERE, |
| 82 base::Bind(&Client::OnCheckBrowseUrlResult, base::Unretained(client), |
| 83 url, url_to_threat_type_[url], |
| 84 safe_browsing::ThreatMetadata())); |
| 85 return false; |
| 86 } |
| 87 |
| 88 bool CheckResourceUrl(const GURL& url, Client* client) override { |
| 89 return true; |
| 90 } |
| 91 |
| 92 bool IsSupported() const override { return true; } |
| 93 bool ChecksAreAlwaysAsync() const override { return false; } |
| 94 bool CanCheckResourceType( |
| 95 content::ResourceType /* resource_type */) const override { |
| 96 return true; |
| 97 } |
| 98 |
| 99 safe_browsing::ThreatSource GetThreatSource() const override { |
| 100 return safe_browsing::ThreatSource::LOCAL_PVER4; |
| 101 } |
| 102 |
| 103 bool CheckExtensionIDs(const std::set<std::string>& extension_ids, |
| 104 Client* client) override { |
| 105 return true; |
| 106 } |
| 107 |
| 108 private: |
| 109 std::map<GURL, safe_browsing::SBThreatType> url_to_threat_type_; |
| 110 bool simulate_timeout_; |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); |
| 113 }; |
| 114 |
| 115 class MockSubresourceFilterClient |
| 116 : public subresource_filter::SubresourceFilterClient { |
| 117 public: |
| 118 MockSubresourceFilterClient() {} |
| 119 |
| 120 ~MockSubresourceFilterClient() override = default; |
| 121 |
| 122 MOCK_METHOD1(ToggleNotificationVisibility, void(bool)); |
| 123 MOCK_METHOD1(IsWhitelistedByContentSettings, bool(const GURL&)); |
| 124 MOCK_METHOD1(WhitelistByContentSettings, void(const GURL&)); |
| 125 MOCK_METHOD0(GetRulesetDealer, VerifiedRulesetDealer::Handle*()); |
| 126 |
| 127 private: |
| 128 DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterClient); |
| 129 }; |
| 130 |
| 131 // Throttle to call WillProcessResponse on the factory, which is otherwise |
| 132 // called by the ThrottleManager. |
| 133 class TestForwardingNavigationThrottle : public content::NavigationThrottle { |
| 134 public: |
| 135 TestForwardingNavigationThrottle(content::NavigationHandle* handle) |
| 136 : content::NavigationThrottle(handle) {} |
| 137 ~TestForwardingNavigationThrottle() override {} |
| 138 |
| 139 // content::NavigationThrottle: |
| 140 content::NavigationThrottle::ThrottleCheckResult WillProcessResponse() |
| 141 override { |
| 142 content::WebContents* web_contents = navigation_handle()->GetWebContents(); |
| 143 ContentSubresourceFilterDriverFactory* factory = |
| 144 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents); |
| 145 factory->WillProcessResponse(navigation_handle()); |
| 146 return content::NavigationThrottle::PROCEED; |
| 147 } |
| 148 |
| 149 private: |
| 150 DISALLOW_COPY_AND_ASSIGN(TestForwardingNavigationThrottle); |
| 151 }; |
| 152 |
| 153 } // namespace |
| 154 |
| 155 class SubresourceFilterSafeBrowsingActivationThrottleTest |
| 156 : public content::RenderViewHostTestHarness, |
| 157 public content::WebContentsObserver { |
| 158 public: |
| 159 SubresourceFilterSafeBrowsingActivationThrottleTest() |
| 160 : field_trial_list_(nullptr) {} |
| 161 ~SubresourceFilterSafeBrowsingActivationThrottleTest() override {} |
| 162 |
| 163 void SetUp() override { |
| 164 content::RenderViewHostTestHarness::SetUp(); |
| 165 scoped_feature_toggle_.reset( |
| 166 new testing::ScopedSubresourceFilterFeatureToggle( |
| 167 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, |
| 168 kActivationScopeActivationList, kActivationListSubresourceFilter)); |
| 169 auto client = base::MakeUnique<MockSubresourceFilterClient>(); |
| 170 ContentSubresourceFilterDriverFactory::CreateForWebContents( |
| 171 RenderViewHostTestHarness::web_contents(), std::move(client)); |
| 172 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); |
| 173 NavigateAndCommit(GURL("https://test.com")); |
| 174 Observe(RenderViewHostTestHarness::web_contents()); |
| 175 } |
| 176 |
| 177 ContentSubresourceFilterDriverFactory* factory() { |
| 178 return ContentSubresourceFilterDriverFactory::FromWebContents( |
| 179 RenderViewHostTestHarness::web_contents()); |
| 180 } |
| 181 |
| 182 // content::WebContentsObserver: |
| 183 void DidStartNavigation( |
| 184 content::NavigationHandle* navigation_handle) override { |
| 185 ASSERT_TRUE(navigation_handle->IsInMainFrame()); |
| 186 navigation_handle_ = navigation_handle; |
| 187 navigation_handle->RegisterThrottleForTesting( |
| 188 base::MakeUnique<SubresourceFilterSafeBrowsingActivationThrottle>( |
| 189 navigation_handle, fake_safe_browsing_database_)); |
| 190 navigation_handle->RegisterThrottleForTesting( |
| 191 base::MakeUnique<TestForwardingNavigationThrottle>(navigation_handle)); |
| 192 } |
| 193 |
| 194 void SimulateStartAndExpectProceed() { |
| 195 navigation_simulator_->Start(); |
| 196 EXPECT_EQ(content::NavigationThrottle::PROCEED, |
| 197 navigation_simulator_->GetLastThrottleCheckResult()); |
| 198 } |
| 199 |
| 200 void SimulateRedirectAndExpectProceed(const GURL& new_url) { |
| 201 navigation_simulator_->Redirect(new_url); |
| 202 EXPECT_EQ(content::NavigationThrottle::PROCEED, |
| 203 navigation_simulator_->GetLastThrottleCheckResult()); |
| 204 } |
| 205 |
| 206 void SimulateCommitAndExpectProceed() { |
| 207 navigation_simulator_->Commit(); |
| 208 EXPECT_EQ(content::NavigationThrottle::PROCEED, |
| 209 navigation_simulator_->GetLastThrottleCheckResult()); |
| 210 } |
| 211 |
| 212 void CreateTestNavigationForMainFrame(const GURL& first_url) { |
| 213 navigation_simulator_ = |
| 214 content::NavigationSimulator::CreateRendererInitiated(first_url, |
| 215 main_rfh()); |
| 216 } |
| 217 |
| 218 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { |
| 219 fake_safe_browsing_database_->AddBlacklistedUrl( |
| 220 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER); |
| 221 } |
| 222 |
| 223 void SimulateTimeout() { fake_safe_browsing_database_->SimulateTimeout(); } |
| 224 |
| 225 const base::HistogramTester& tester() const { return tester_; } |
| 226 |
| 227 private: |
| 228 base::FieldTrialList field_trial_list_; |
| 229 std::unique_ptr<testing::ScopedSubresourceFilterFeatureToggle> |
| 230 scoped_feature_toggle_; |
| 231 std::unique_ptr<content::NavigationSimulator> navigation_simulator_; |
| 232 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; |
| 233 base::HistogramTester tester_; |
| 234 content::NavigationHandle* navigation_handle_; |
| 235 |
| 236 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingActivationThrottleTest); |
| 237 }; |
| 238 |
| 239 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
| 240 ListNotMatched_NoActivation) { |
| 241 const GURL url(kURL); |
| 242 CreateTestNavigationForMainFrame(url); |
| 243 SimulateStartAndExpectProceed(); |
| 244 SimulateCommitAndExpectProceed(); |
| 245 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: |
| 246 ACTIVATION_LIST_NOT_MATCHED, |
| 247 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
| 248 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, |
| 249 0); |
| 250 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); |
| 251 } |
| 252 |
| 253 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
| 254 ListMatched_Activation) { |
| 255 const GURL url(kURL); |
| 256 ConfigureAsSubresourceFilterOnlyURL(url); |
| 257 CreateTestNavigationForMainFrame(url); |
| 258 SimulateStartAndExpectProceed(); |
| 259 SimulateCommitAndExpectProceed(); |
| 260 EXPECT_EQ( |
| 261 ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED, |
| 262 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
| 263 tester().ExpectUniqueSample( |
| 264 kMatchesPatternHistogramNameSubresourceFilterSuffix, NO_REDIRECTS_HIT, 1); |
| 265 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 1, |
| 266 1); |
| 267 } |
| 268 |
| 269 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
| 270 ListNotMatchedAfterRedirect_NoActivation) { |
| 271 const GURL url(kURL); |
| 272 CreateTestNavigationForMainFrame(url); |
| 273 SimulateStartAndExpectProceed(); |
| 274 SimulateRedirectAndExpectProceed(GURL(kRedirectURL)); |
| 275 SimulateCommitAndExpectProceed(); |
| 276 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: |
| 277 ACTIVATION_LIST_NOT_MATCHED, |
| 278 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
| 279 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, |
| 280 0); |
| 281 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); |
| 282 } |
| 283 |
| 284 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
| 285 ListMatchedAfterRedirect_Activation) { |
| 286 const GURL url(kURL); |
| 287 ConfigureAsSubresourceFilterOnlyURL(GURL(kRedirectURL)); |
| 288 CreateTestNavigationForMainFrame(url); |
| 289 SimulateStartAndExpectProceed(); |
| 290 SimulateRedirectAndExpectProceed(GURL(kRedirectURL)); |
| 291 SimulateCommitAndExpectProceed(); |
| 292 EXPECT_EQ( |
| 293 ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED, |
| 294 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
| 295 tester().ExpectUniqueSample( |
| 296 kMatchesPatternHistogramNameSubresourceFilterSuffix, F0M0L1, 1); |
| 297 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2, |
| 298 1); |
| 299 } |
| 300 |
| 301 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
| 302 ListNotMatchedAndTimeout_NoActivation) { |
| 303 const GURL url(kURL); |
| 304 SimulateTimeout(); |
| 305 CreateTestNavigationForMainFrame(url); |
| 306 SimulateStartAndExpectProceed(); |
| 307 SimulateCommitAndExpectProceed(); |
| 308 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: |
| 309 ACTIVATION_LIST_NOT_MATCHED, |
| 310 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
| 311 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, |
| 312 0); |
| 313 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); |
| 314 } |
| 315 |
| 316 // TODO(melandory): Once non-defering check in WillStart is implemented add one |
| 317 // more test that destroys the Navigation along with corresponding throttles |
| 318 // while the SB check is pending? (To be run by ASAN bots to ensure |
| 319 // no use-after-free.) |
| 320 |
| 321 } // namespace subresource_filter |
OLD | NEW |