| OLD | NEW | 
|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "chrome/browser/permissions/permission_decision_auto_blocker.h" | 5 #include "chrome/browser/permissions/permission_decision_auto_blocker.h" | 
| 6 | 6 | 
| 7 #include <map> | 7 #include <map> | 
| 8 | 8 | 
| 9 #include "base/bind.h" | 9 #include "base/bind.h" | 
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" | 
|  | 11 #include "base/test/histogram_tester.h" | 
| 11 #include "base/test/scoped_feature_list.h" | 12 #include "base/test/scoped_feature_list.h" | 
| 12 #include "base/test/simple_test_clock.h" | 13 #include "base/test/simple_test_clock.h" | 
| 13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 
|  | 15 #include "chrome/browser/permissions/permission_uma_util.h" | 
| 14 #include "chrome/browser/permissions/permission_util.h" | 16 #include "chrome/browser/permissions/permission_util.h" | 
| 15 #include "chrome/common/chrome_features.h" | 17 #include "chrome/common/chrome_features.h" | 
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 
| 17 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" | 
| 18 #include "components/safe_browsing_db/test_database_manager.h" | 20 #include "components/safe_browsing_db/test_database_manager.h" | 
| 19 #include "content/public/browser/permission_type.h" | 21 #include "content/public/browser/permission_type.h" | 
| 20 | 22 | 
| 21 namespace { | 23 namespace { | 
| 22 | 24 | 
| 23 bool FilterGoogle(const GURL& url) { | 25 bool FilterGoogle(const GURL& url) { | 
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 238                    url2, content::PermissionType::GEOLOCATION)); | 240                    url2, content::PermissionType::GEOLOCATION)); | 
| 239   EXPECT_EQ(0, autoblocker()->GetIgnoreCount( | 241   EXPECT_EQ(0, autoblocker()->GetIgnoreCount( | 
| 240                    url2, content::PermissionType::DURABLE_STORAGE)); | 242                    url2, content::PermissionType::DURABLE_STORAGE)); | 
| 241   EXPECT_EQ(0, autoblocker()->GetIgnoreCount( | 243   EXPECT_EQ(0, autoblocker()->GetIgnoreCount( | 
| 242                    url2, content::PermissionType::MIDI_SYSEX)); | 244                    url2, content::PermissionType::MIDI_SYSEX)); | 
| 243 } | 245 } | 
| 244 | 246 | 
| 245 // Test that an origin that has been blacklisted for a permission is embargoed. | 247 // Test that an origin that has been blacklisted for a permission is embargoed. | 
| 246 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) { | 248 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) { | 
| 247   GURL url("https://www.google.com"); | 249   GURL url("https://www.google.com"); | 
|  | 250   base::HistogramTester histograms; | 
| 248 | 251 | 
| 249   scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 252   scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 
| 250       new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | 253       new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | 
| 251   std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 254   std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 
| 252   db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 255   db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 
| 253   SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 256   SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 
| 254                                                      2000 /* timeout in ms */); | 257                                                      2000 /* timeout in ms */); | 
| 255 |  | 
| 256   UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | 258   UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | 
| 257   EXPECT_TRUE(last_embargoed_status()); | 259   EXPECT_TRUE(last_embargoed_status()); | 
|  | 260   histograms.ExpectUniqueSample( | 
|  | 261       "Permissions.AutoBlocker.EmbargoReason", | 
|  | 262       PermissionEmbargoReason::PERMISSIONS_BLACKLISTING, 1); | 
|  | 263   histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", | 
|  | 264                                 SafeBrowsingResponse::BLACKLISTED, 1); | 
| 258 } | 265 } | 
| 259 | 266 | 
| 260 // Check that IsUnderEmbargo returns the correct value when the embargo is set | 267 // Check that IsUnderEmbargo returns the correct value when the embargo is set | 
| 261 // and expires. | 268 // and expires. | 
| 262 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) { | 269 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) { | 
| 263   GURL url("https://www.google.com"); | 270   GURL url("https://www.google.com"); | 
| 264   clock()->SetNow(base::Time::Now()); | 271   clock()->SetNow(base::Time::Now()); | 
| 265 | 272 | 
| 266   PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); | 273   PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); | 
| 267   EXPECT_TRUE( | 274   EXPECT_TRUE( | 
| (...skipping 28 matching lines...) Expand all  Loading... | 
| 296 } | 303 } | 
| 297 | 304 | 
| 298 // Tests the alternating pattern of the block on multiple dismiss behaviour. On | 305 // Tests the alternating pattern of the block on multiple dismiss behaviour. On | 
| 299 // N dismissals, the origin to be embargoed for the requested permission and | 306 // N dismissals, the origin to be embargoed for the requested permission and | 
| 300 // automatically blocked. Each time the embargo is lifted, the site gets another | 307 // automatically blocked. Each time the embargo is lifted, the site gets another | 
| 301 // chance to request the permission, but if it is again dismissed it is placed | 308 // chance to request the permission, but if it is again dismissed it is placed | 
| 302 // under embargo again and its permission requests blocked. | 309 // under embargo again and its permission requests blocked. | 
| 303 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) { | 310 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) { | 
| 304   GURL url("https://www.google.com"); | 311   GURL url("https://www.google.com"); | 
| 305   clock()->SetNow(base::Time::Now()); | 312   clock()->SetNow(base::Time::Now()); | 
|  | 313   base::HistogramTester histograms; | 
| 306 | 314 | 
| 307   // Record some dismisses. | 315   // Record some dismisses. | 
| 308   EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( | 316   EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( | 
| 309       url, content::PermissionType::GEOLOCATION)); | 317       url, content::PermissionType::GEOLOCATION)); | 
| 310   EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( | 318   EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( | 
| 311       url, content::PermissionType::GEOLOCATION)); | 319       url, content::PermissionType::GEOLOCATION)); | 
| 312 | 320 | 
| 313   // A request with < 3 prior dismisses should not be automatically blocked. | 321   // A request with < 3 prior dismisses should not be automatically blocked. | 
| 314   EXPECT_FALSE( | 322   EXPECT_FALSE( | 
| 315       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 323       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 
| 316 | 324 | 
| 317   // After the 3rd dismiss subsequent permission requests should be autoblocked. | 325   // After the 3rd dismiss subsequent permission requests should be autoblocked. | 
| 318   EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | 326   EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | 
| 319       url, content::PermissionType::GEOLOCATION)); | 327       url, content::PermissionType::GEOLOCATION)); | 
| 320   EXPECT_TRUE( | 328   EXPECT_TRUE( | 
| 321       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 329       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 
|  | 330   histograms.ExpectBucketCount("Permissions.AutoBlocker.EmbargoReason", | 
|  | 331                                PermissionEmbargoReason::REPEATED_DISMISSALS, 1); | 
| 322 | 332 | 
| 323   // Accelerate time forward, check that the embargo status is lifted and the | 333   // Accelerate time forward, check that the embargo status is lifted and the | 
| 324   // request won't be automatically blocked. | 334   // request won't be automatically blocked. | 
| 325   clock()->Advance(base::TimeDelta::FromDays(8)); | 335   clock()->Advance(base::TimeDelta::FromDays(8)); | 
| 326   EXPECT_FALSE( | 336   EXPECT_FALSE( | 
| 327       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 337       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 
| 328 | 338 | 
| 329   // Record another dismiss, subsequent requests should be autoblocked again. | 339   // Record another dismiss, subsequent requests should be autoblocked again. | 
| 330   EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | 340   EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | 
| 331       url, content::PermissionType::GEOLOCATION)); | 341       url, content::PermissionType::GEOLOCATION)); | 
| 332   EXPECT_TRUE( | 342   EXPECT_TRUE( | 
| 333       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 343       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 
|  | 344   histograms.ExpectBucketCount("Permissions.AutoBlocker.EmbargoReason", | 
|  | 345                                PermissionEmbargoReason::REPEATED_DISMISSALS, 2); | 
|  | 346   histograms.ExpectBucketCount("Permissions.AutoBlocker.RepeatedEmbargo", | 
|  | 347                                PermissionEmbargoReason::REPEATED_DISMISSALS, 1); | 
| 334 | 348 | 
| 335   // Accelerate time again, check embargo is lifted and another permission | 349   // Accelerate time again, check embargo is lifted and another permission | 
| 336   // request is let through. | 350   // request is let through. | 
| 337   clock()->Advance(base::TimeDelta::FromDays(8)); | 351   clock()->Advance(base::TimeDelta::FromDays(8)); | 
| 338   EXPECT_FALSE( | 352   EXPECT_FALSE( | 
| 339       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 353       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 
|  | 354 | 
|  | 355   // Record another dismiss, subsequent requests should be autoblocked again. | 
|  | 356   EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | 
|  | 357       url, content::PermissionType::GEOLOCATION)); | 
|  | 358   EXPECT_TRUE( | 
|  | 359       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 
|  | 360   histograms.ExpectBucketCount("Permissions.AutoBlocker.EmbargoReason", | 
|  | 361                                PermissionEmbargoReason::REPEATED_DISMISSALS, 3); | 
|  | 362   histograms.ExpectBucketCount("Permissions.AutoBlocker.RepeatedEmbargo", | 
|  | 363                                PermissionEmbargoReason::REPEATED_DISMISSALS, 2); | 
| 340 } | 364 } | 
| 341 | 365 | 
| 342 // Test the logic for a combination of blacklisting and dismissal embargo. | 366 // Test the logic for a combination of blacklisting and dismissal embargo. | 
| 343 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { | 367 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { | 
| 344   GURL url("https://www.google.com"); | 368   GURL url("https://www.google.com"); | 
| 345   clock()->SetNow(base::Time::Now()); | 369   clock()->SetNow(base::Time::Now()); | 
| 346 | 370 | 
| 347   // Place under blacklist embargo and check the status. | 371   // Place under blacklist embargo and check the status. | 
| 348   PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); | 372   PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); | 
| 349   clock()->Advance(base::TimeDelta::FromDays(5)); | 373   clock()->Advance(base::TimeDelta::FromDays(5)); | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 361   // Accelerate time to a point where the blacklist embargo should be expired | 385   // Accelerate time to a point where the blacklist embargo should be expired | 
| 362   // and check that dismissal embargo is still set. | 386   // and check that dismissal embargo is still set. | 
| 363   clock()->Advance(base::TimeDelta::FromDays(3)); | 387   clock()->Advance(base::TimeDelta::FromDays(3)); | 
| 364   EXPECT_TRUE( | 388   EXPECT_TRUE( | 
| 365       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 389       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 
| 366 } | 390 } | 
| 367 | 391 | 
| 368 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { | 392 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { | 
| 369   GURL url("https://www.google.com"); | 393   GURL url("https://www.google.com"); | 
| 370   clock()->SetNow(base::Time::Now()); | 394   clock()->SetNow(base::Time::Now()); | 
|  | 395   base::HistogramTester histograms; | 
| 371 | 396 | 
| 372   scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 397   scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 
| 373       new MockSafeBrowsingDatabaseManager(false /* perform_callback */); | 398       new MockSafeBrowsingDatabaseManager(false /* perform_callback */); | 
| 374   std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 399   std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 
| 375   db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 400   db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 
| 376   SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 401   SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 
| 377                                                      0 /* timeout in ms */); | 402                                                      0 /* timeout in ms */); | 
| 378 | 403 | 
| 379   UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | 404   UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | 
| 380   EXPECT_FALSE(last_embargoed_status()); | 405   EXPECT_FALSE(last_embargoed_status()); | 
| 381   EXPECT_FALSE( | 406   EXPECT_FALSE( | 
| 382       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 407       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 
|  | 408   histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", | 
|  | 409                                 SafeBrowsingResponse::TIMEOUT, 1); | 
| 383   db_manager->SetPerformCallback(true); | 410   db_manager->SetPerformCallback(true); | 
| 384   SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 411   SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 
| 385                                                      2000 /* timeout in ms */); | 412                                                      2000 /* timeout in ms */); | 
| 386 | 413 | 
| 387   clock()->Advance(base::TimeDelta::FromDays(1)); | 414   clock()->Advance(base::TimeDelta::FromDays(1)); | 
| 388   UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | 415   UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); | 
| 389   EXPECT_TRUE(last_embargoed_status()); | 416   EXPECT_TRUE(last_embargoed_status()); | 
|  | 417   histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse", | 
|  | 418                               2); | 
|  | 419   histograms.ExpectBucketCount( | 
|  | 420       "Permissions.AutoBlocker.EmbargoReason", | 
|  | 421       PermissionEmbargoReason::PERMISSIONS_BLACKLISTING, 1); | 
| 390 | 422 | 
| 391   clock()->Advance(base::TimeDelta::FromDays(1)); | 423   clock()->Advance(base::TimeDelta::FromDays(1)); | 
| 392   EXPECT_TRUE( | 424   EXPECT_TRUE( | 
| 393       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 425       autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); | 
| 394 } | 426 } | 
|  | 427 | 
|  | 428 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingResponse) { | 
|  | 429   GURL url("https://www.google.com"); | 
|  | 430   clock()->SetNow(base::Time::Now()); | 
|  | 431   base::HistogramTester histograms; | 
|  | 432 | 
|  | 433   scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 
|  | 434       new MockSafeBrowsingDatabaseManager(true /* perform_callback */); | 
|  | 435   std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 
|  | 436   db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 
|  | 437   SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 
|  | 438                                                      0 /* timeout in ms */); | 
|  | 439 | 
|  | 440   UpdateEmbargoedStatus(content::PermissionType::NOTIFICATIONS, url); | 
|  | 441   EXPECT_FALSE(last_embargoed_status()); | 
|  | 442   histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", | 
|  | 443                                 SafeBrowsingResponse::NOT_BLACKLISTED, 1); | 
|  | 444 } | 
| OLD | NEW | 
|---|