safe_browsing_triggered_popup_blocker_unittest.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. #include "components/blocked_content/safe_browsing_triggered_popup_blocker.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/run_loop.h"
  11. #include "base/test/metrics/histogram_tester.h"
  12. #include "base/test/scoped_feature_list.h"
  13. #include "components/blocked_content/popup_blocker.h"
  14. #include "components/blocked_content/popup_blocker_tab_helper.h"
  15. #include "components/blocked_content/popup_navigation_delegate.h"
  16. #include "components/blocked_content/test/test_popup_navigation_delegate.h"
  17. #include "components/content_settings/browser/page_specific_content_settings.h"
  18. #include "components/content_settings/browser/test_page_specific_content_settings_delegate.h"
  19. #include "components/content_settings/core/browser/host_content_settings_map.h"
  20. #include "components/subresource_filter/content/browser/content_subresource_filter_web_contents_helper.h"
  21. #include "components/subresource_filter/content/browser/fake_safe_browsing_database_manager.h"
  22. #include "components/subresource_filter/content/browser/subresource_filter_observer_manager.h"
  23. #include "components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle.h"
  24. #include "components/subresource_filter/content/browser/subresource_filter_safe_browsing_client.h"
  25. #include "components/subresource_filter/core/browser/subresource_filter_constants.h"
  26. #include "components/sync_preferences/testing_pref_service_syncable.h"
  27. #include "components/user_prefs/user_prefs.h"
  28. #include "components/variations/scoped_variations_ids_provider.h"
  29. #include "content/public/browser/browser_context.h"
  30. #include "content/public/browser/browser_task_traits.h"
  31. #include "content/public/browser/navigation_handle.h"
  32. #include "content/public/browser/navigation_throttle.h"
  33. #include "content/public/browser/render_process_host.h"
  34. #include "content/public/test/mock_navigation_handle.h"
  35. #include "content/public/test/navigation_simulator.h"
  36. #include "content/public/test/test_navigation_throttle_inserter.h"
  37. #include "content/public/test/test_renderer_host.h"
  38. #include "testing/gtest/include/gtest/gtest.h"
  39. #include "third_party/blink/public/common/features.h"
  40. #include "third_party/blink/public/mojom/frame/frame.mojom.h"
  41. #include "third_party/blink/public/mojom/window_features/window_features.mojom.h"
  42. #include "ui/base/page_transition_types.h"
  43. #include "ui/base/window_open_disposition.h"
  44. #include "url/gurl.h"
  45. namespace blocked_content {
  46. const char kNumBlockedHistogram[] =
  47. "ContentSettings.Popups.StrongBlocker.NumBlocked";
  48. class SafeBrowsingTriggeredPopupBlockerTestBase
  49. : public content::RenderViewHostTestHarness {
  50. public:
  51. SafeBrowsingTriggeredPopupBlockerTestBase() = default;
  52. SafeBrowsingTriggeredPopupBlockerTestBase(
  53. const SafeBrowsingTriggeredPopupBlockerTestBase&) = delete;
  54. SafeBrowsingTriggeredPopupBlockerTestBase& operator=(
  55. const SafeBrowsingTriggeredPopupBlockerTestBase&) = delete;
  56. ~SafeBrowsingTriggeredPopupBlockerTestBase() override {
  57. settings_map_->ShutdownOnUIThread();
  58. }
  59. // content::RenderViewHostTestHarness:
  60. void SetUp() override {
  61. content::RenderViewHostTestHarness::SetUp();
  62. fake_safe_browsing_database_ =
  63. base::MakeRefCounted<FakeSafeBrowsingDatabaseManager>();
  64. user_prefs::UserPrefs::Set(browser_context(), &pref_service_);
  65. SafeBrowsingTriggeredPopupBlocker::RegisterProfilePrefs(
  66. pref_service_.registry());
  67. HostContentSettingsMap::RegisterProfilePrefs(pref_service_.registry());
  68. settings_map_ = base::MakeRefCounted<HostContentSettingsMap>(
  69. &pref_service_, false /* is_off_the_record */,
  70. false /* store_last_modified */, false /* restore_session*/);
  71. subresource_filter::SubresourceFilterObserverManager::CreateForWebContents(
  72. web_contents());
  73. PopupBlockerTabHelper::CreateForWebContents(web_contents());
  74. content_settings::PageSpecificContentSettings::CreateForWebContents(
  75. web_contents(),
  76. std::make_unique<
  77. content_settings::TestPageSpecificContentSettingsDelegate>(
  78. /*prefs=*/nullptr, settings_map_.get()));
  79. popup_blocker_ =
  80. SafeBrowsingTriggeredPopupBlocker::FromWebContents(web_contents());
  81. throttle_inserter_ =
  82. std::make_unique<content::TestNavigationThrottleInserter>(
  83. web_contents(),
  84. base::BindRepeating(
  85. &SafeBrowsingTriggeredPopupBlockerTestBase::CreateThrottle,
  86. base::Unretained(this)));
  87. }
  88. FakeSafeBrowsingDatabaseManager* fake_safe_browsing_database() {
  89. return fake_safe_browsing_database_.get();
  90. }
  91. SafeBrowsingTriggeredPopupBlocker* popup_blocker() { return popup_blocker_; }
  92. void SimulateDeleteContents() {
  93. DeleteContents();
  94. popup_blocker_ = nullptr;
  95. }
  96. void MarkUrlAsAbusiveWithLevel(const GURL& url,
  97. safe_browsing::SubresourceFilterLevel level) {
  98. safe_browsing::ThreatMetadata metadata;
  99. metadata.subresource_filter_match
  100. [safe_browsing::SubresourceFilterType::ABUSIVE] = level;
  101. fake_safe_browsing_database()->AddBlocklistedUrl(
  102. url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER, metadata);
  103. }
  104. void MarkUrlAsAbusiveEnforce(const GURL& url) {
  105. MarkUrlAsAbusiveWithLevel(url,
  106. safe_browsing::SubresourceFilterLevel::ENFORCE);
  107. }
  108. void MarkUrlAsAbusiveWarning(const GURL& url) {
  109. MarkUrlAsAbusiveWithLevel(url, safe_browsing::SubresourceFilterLevel::WARN);
  110. }
  111. const std::vector<std::string>& GetMainFrameConsoleMessages() {
  112. content::RenderFrameHostTester* rfh_tester =
  113. content::RenderFrameHostTester::For(main_rfh());
  114. return rfh_tester->GetConsoleMessages();
  115. }
  116. HostContentSettingsMap* settings_map() { return settings_map_.get(); }
  117. protected:
  118. std::unique_ptr<content::NavigationThrottle> CreateThrottle(
  119. content::NavigationHandle* handle) {
  120. // Activation is only computed when navigating a subresource filter root
  121. // (see content_subresource_filter_throttle_manager.h for the definition of
  122. // a root).
  123. if (subresource_filter::IsInSubresourceFilterRoot(handle)) {
  124. return std::make_unique<
  125. subresource_filter::SubresourceFilterSafeBrowsingActivationThrottle>(
  126. handle, /*delegate=*/nullptr, content::GetIOThreadTaskRunner({}),
  127. fake_safe_browsing_database_);
  128. }
  129. return nullptr;
  130. }
  131. base::test::ScopedFeatureList scoped_feature_list_;
  132. variations::ScopedVariationsIdsProvider scoped_variations_ids_provider_{
  133. variations::VariationsIdsProvider::Mode::kUseSignedInState};
  134. scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_;
  135. raw_ptr<SafeBrowsingTriggeredPopupBlocker> popup_blocker_ = nullptr;
  136. std::unique_ptr<content::TestNavigationThrottleInserter> throttle_inserter_;
  137. sync_preferences::TestingPrefServiceSyncable pref_service_;
  138. scoped_refptr<HostContentSettingsMap> settings_map_;
  139. };
  140. class SafeBrowsingTriggeredPopupBlockerTest
  141. : public SafeBrowsingTriggeredPopupBlockerTestBase {
  142. public:
  143. SafeBrowsingTriggeredPopupBlockerTest() {
  144. scoped_feature_list_.InitAndEnableFeature(kAbusiveExperienceEnforce);
  145. }
  146. };
  147. struct RedirectSamplesAndResults {
  148. GURL initial_url;
  149. GURL redirect_url;
  150. bool expect_strong_blocker;
  151. };
  152. // We always make our decision to trigger on the last entry in the chain.
  153. TEST_F(SafeBrowsingTriggeredPopupBlockerTest,
  154. MatchOnSafeBrowsingWithRedirectChain) {
  155. GURL enforce_url("https://example.enforce");
  156. GURL warning_url("https://example.warning");
  157. GURL regular_url("https://example.regular");
  158. MarkUrlAsAbusiveEnforce(enforce_url);
  159. MarkUrlAsAbusiveWarning(warning_url);
  160. const RedirectSamplesAndResults kTestCases[] = {
  161. {enforce_url, regular_url, false},
  162. {regular_url, enforce_url, true},
  163. {warning_url, enforce_url, true},
  164. {enforce_url, warning_url, false}};
  165. for (const auto& test_case : kTestCases) {
  166. std::unique_ptr<content::NavigationSimulator> simulator =
  167. content::NavigationSimulator::CreateRendererInitiated(
  168. test_case.initial_url, web_contents()->GetPrimaryMainFrame());
  169. simulator->Start();
  170. simulator->Redirect(test_case.redirect_url);
  171. simulator->Commit();
  172. EXPECT_EQ(test_case.expect_strong_blocker,
  173. popup_blocker()->ShouldApplyAbusivePopupBlocker(
  174. web_contents()->GetPrimaryPage()));
  175. }
  176. }
  177. TEST_F(SafeBrowsingTriggeredPopupBlockerTest, MatchingURL_BlocksPopupAndLogs) {
  178. const GURL url("https://example.test/");
  179. MarkUrlAsAbusiveEnforce(url);
  180. NavigateAndCommit(url);
  181. EXPECT_TRUE(GetMainFrameConsoleMessages().empty());
  182. EXPECT_TRUE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  183. web_contents()->GetPrimaryPage()));
  184. EXPECT_EQ(1u, GetMainFrameConsoleMessages().size());
  185. EXPECT_EQ(GetMainFrameConsoleMessages().front(), kAbusiveEnforceMessage);
  186. }
  187. TEST_F(SafeBrowsingTriggeredPopupBlockerTest,
  188. MatchingURL_BlocksPopupFromOpenURL) {
  189. const GURL url("https://example.test/");
  190. MarkUrlAsAbusiveEnforce(url);
  191. NavigateAndCommit(url);
  192. // If the popup is coming from OpenURL params, the strong popup blocker is
  193. // only going to look at the triggering event info. It will only block the
  194. // popup if we know the triggering event is untrusted.
  195. GURL popup_url("https://example.popup/");
  196. content::OpenURLParams params(
  197. popup_url, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB,
  198. ui::PAGE_TRANSITION_LINK, true /* is_renderer_initiated */);
  199. params.user_gesture = true;
  200. params.triggering_event_info =
  201. blink::mojom::TriggeringEventInfo::kFromUntrustedEvent;
  202. params.source_render_frame_id = main_rfh()->GetRoutingID();
  203. params.source_render_process_id = main_rfh()->GetProcess()->GetID();
  204. MaybeBlockPopup(web_contents(), nullptr,
  205. std::make_unique<TestPopupNavigationDelegate>(
  206. popup_url, nullptr /* result_holder */),
  207. &params, blink::mojom::WindowFeatures(), settings_map());
  208. EXPECT_EQ(1u, PopupBlockerTabHelper::FromWebContents(web_contents())
  209. ->GetBlockedPopupsCount());
  210. }
  211. TEST_F(SafeBrowsingTriggeredPopupBlockerTest,
  212. MatchingURLTrusted_DoesNotBlockPopup) {
  213. const GURL url("https://example.test/");
  214. MarkUrlAsAbusiveEnforce(url);
  215. NavigateAndCommit(url);
  216. // If the popup is coming from OpenURL params, the strong popup blocker is
  217. // only going to look at the triggering event info. It will only block the
  218. // popup if we know the triggering event is untrusted.
  219. GURL popup_url("https://example.popup/");
  220. content::OpenURLParams params(
  221. popup_url, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB,
  222. ui::PAGE_TRANSITION_LINK, true /* is_renderer_initiated */);
  223. params.user_gesture = true;
  224. params.triggering_event_info =
  225. blink::mojom::TriggeringEventInfo::kFromTrustedEvent;
  226. MaybeBlockPopup(web_contents(), nullptr,
  227. std::make_unique<TestPopupNavigationDelegate>(
  228. popup_url, nullptr /* result_holder */),
  229. &params, blink::mojom::WindowFeatures(), settings_map());
  230. EXPECT_EQ(0u, PopupBlockerTabHelper::FromWebContents(web_contents())
  231. ->GetBlockedPopupsCount());
  232. }
  233. TEST_F(SafeBrowsingTriggeredPopupBlockerTest, NoMatch_NoBlocking) {
  234. const GURL url("https://example.test/");
  235. NavigateAndCommit(url);
  236. EXPECT_FALSE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  237. web_contents()->GetPrimaryPage()));
  238. EXPECT_TRUE(GetMainFrameConsoleMessages().empty());
  239. }
  240. class SafeBrowsingTriggeredPopupBlockerDefaultTest
  241. : public SafeBrowsingTriggeredPopupBlockerTestBase {};
  242. TEST_F(SafeBrowsingTriggeredPopupBlockerDefaultTest, FeatureEnabledByDefault) {
  243. SafeBrowsingTriggeredPopupBlocker::MaybeCreate(web_contents());
  244. EXPECT_NE(nullptr,
  245. SafeBrowsingTriggeredPopupBlocker::FromWebContents(web_contents()));
  246. }
  247. TEST_F(SafeBrowsingTriggeredPopupBlockerTest, OnlyBlockOnMatchingUrls) {
  248. const GURL url1("https://example.first/");
  249. const GURL url2("https://example.second/");
  250. const GURL url3("https://example.third/");
  251. // Only mark url2 as abusive.
  252. MarkUrlAsAbusiveEnforce(url2);
  253. NavigateAndCommit(url1);
  254. EXPECT_FALSE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  255. web_contents()->GetPrimaryPage()));
  256. NavigateAndCommit(url2);
  257. EXPECT_TRUE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  258. web_contents()->GetPrimaryPage()));
  259. NavigateAndCommit(url3);
  260. EXPECT_FALSE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  261. web_contents()->GetPrimaryPage()));
  262. NavigateAndCommit(url1);
  263. EXPECT_FALSE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  264. web_contents()->GetPrimaryPage()));
  265. }
  266. TEST_F(SafeBrowsingTriggeredPopupBlockerTest,
  267. SameDocumentNavigation_MaintainsBlocking) {
  268. const GURL url("https://example.first/");
  269. const GURL hash_url("https://example.first/#hash");
  270. MarkUrlAsAbusiveEnforce(url);
  271. NavigateAndCommit(url);
  272. EXPECT_TRUE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  273. web_contents()->GetPrimaryPage()));
  274. // This is merely a same document navigation, keep the popup blocker.
  275. NavigateAndCommit(hash_url);
  276. EXPECT_TRUE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  277. web_contents()->GetPrimaryPage()));
  278. }
  279. TEST_F(SafeBrowsingTriggeredPopupBlockerTest,
  280. FailNavigation_MaintainsBlocking) {
  281. const GURL url("https://example.first/");
  282. const GURL fail_url("https://example.fail/");
  283. MarkUrlAsAbusiveEnforce(url);
  284. NavigateAndCommit(url);
  285. EXPECT_TRUE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  286. web_contents()->GetPrimaryPage()));
  287. // Abort the navigation before it commits.
  288. content::NavigationSimulator::NavigateAndFailFromDocument(
  289. fail_url, net::ERR_ABORTED, main_rfh());
  290. EXPECT_TRUE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  291. web_contents()->GetPrimaryPage()));
  292. // Committing an error page should probably reset the blocker though, despite
  293. // the fact that it is probably a bug for an error page to spawn popups.
  294. content::NavigationSimulator::NavigateAndFailFromDocument(
  295. fail_url, net::ERR_CONNECTION_RESET, main_rfh());
  296. EXPECT_FALSE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  297. web_contents()->GetPrimaryPage()));
  298. }
  299. TEST_F(SafeBrowsingTriggeredPopupBlockerTest, LogActions) {
  300. base::HistogramTester histogram_tester;
  301. const char kActionHistogram[] = "ContentSettings.Popups.StrongBlockerActions";
  302. int total_count = 0;
  303. // Call this when a new histogram entry is logged. Call it multiple times if
  304. // multiple entries are logged.
  305. auto check_histogram = [&](SafeBrowsingTriggeredPopupBlocker::Action action,
  306. int expected_count) {
  307. histogram_tester.ExpectBucketCount(
  308. kActionHistogram, static_cast<int>(action), expected_count);
  309. total_count++;
  310. };
  311. const GURL url_enforce("https://example.enforce/");
  312. const GURL url_warn("https://example.warn/");
  313. const GURL url_nothing("https://example.nothing/");
  314. MarkUrlAsAbusiveEnforce(url_enforce);
  315. MarkUrlAsAbusiveWarning(url_warn);
  316. // Navigate to an enforce site.
  317. NavigateAndCommit(url_enforce);
  318. check_histogram(SafeBrowsingTriggeredPopupBlocker::Action::kNavigation, 1);
  319. check_histogram(SafeBrowsingTriggeredPopupBlocker::Action::kEnforcedSite, 1);
  320. histogram_tester.ExpectTotalCount(kActionHistogram, total_count);
  321. // Block two popups.
  322. EXPECT_TRUE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  323. web_contents()->GetPrimaryPage()));
  324. check_histogram(SafeBrowsingTriggeredPopupBlocker::Action::kConsidered, 1);
  325. check_histogram(SafeBrowsingTriggeredPopupBlocker::Action::kBlocked, 1);
  326. histogram_tester.ExpectTotalCount(kActionHistogram, total_count);
  327. EXPECT_TRUE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  328. web_contents()->GetPrimaryPage()));
  329. check_histogram(SafeBrowsingTriggeredPopupBlocker::Action::kConsidered, 2);
  330. check_histogram(SafeBrowsingTriggeredPopupBlocker::Action::kBlocked, 2);
  331. histogram_tester.ExpectTotalCount(kActionHistogram, total_count);
  332. // Only log the num blocked histogram after navigation.
  333. histogram_tester.ExpectTotalCount(kNumBlockedHistogram, 0);
  334. // Navigate to a warn site.
  335. NavigateAndCommit(url_warn);
  336. histogram_tester.ExpectBucketCount(kNumBlockedHistogram, 2, 1);
  337. check_histogram(SafeBrowsingTriggeredPopupBlocker::Action::kNavigation, 2);
  338. check_histogram(SafeBrowsingTriggeredPopupBlocker::Action::kWarningSite, 1);
  339. histogram_tester.ExpectTotalCount(kActionHistogram, total_count);
  340. // Let one popup through.
  341. EXPECT_FALSE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  342. web_contents()->GetPrimaryPage()));
  343. check_histogram(SafeBrowsingTriggeredPopupBlocker::Action::kConsidered, 3);
  344. histogram_tester.ExpectTotalCount(kActionHistogram, total_count);
  345. // Navigate to a site not matched in Safe Browsing.
  346. NavigateAndCommit(url_nothing);
  347. check_histogram(SafeBrowsingTriggeredPopupBlocker::Action::kNavigation, 3);
  348. histogram_tester.ExpectTotalCount(kActionHistogram, total_count);
  349. // Let one popup through.
  350. EXPECT_FALSE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  351. web_contents()->GetPrimaryPage()));
  352. check_histogram(SafeBrowsingTriggeredPopupBlocker::Action::kConsidered, 4);
  353. histogram_tester.ExpectTotalCount(kActionHistogram, total_count);
  354. histogram_tester.ExpectTotalCount(kNumBlockedHistogram, 1);
  355. }
  356. TEST_F(SafeBrowsingTriggeredPopupBlockerTest, LogBlockMetricsOnClose) {
  357. base::HistogramTester histogram_tester;
  358. const GURL url_enforce("https://example.enforce/");
  359. MarkUrlAsAbusiveEnforce(url_enforce);
  360. NavigateAndCommit(url_enforce);
  361. EXPECT_TRUE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  362. web_contents()->GetPrimaryPage()));
  363. histogram_tester.ExpectTotalCount(kNumBlockedHistogram, 0);
  364. // Simulate deleting the web contents.
  365. SimulateDeleteContents();
  366. histogram_tester.ExpectUniqueSample(kNumBlockedHistogram, 1, 1);
  367. }
  368. class SafeBrowsingTriggeredPopupBlockerFilterAdsDisabledTest
  369. : public SafeBrowsingTriggeredPopupBlockerTestBase {
  370. public:
  371. SafeBrowsingTriggeredPopupBlockerFilterAdsDisabledTest() {
  372. scoped_feature_list_.InitAndDisableFeature(
  373. subresource_filter::kFilterAdsOnAbusiveSites);
  374. }
  375. private:
  376. base::test::ScopedFeatureList scoped_feature_list_;
  377. };
  378. TEST_F(SafeBrowsingTriggeredPopupBlockerFilterAdsDisabledTest,
  379. WarningMatchWithoutAdBlockOnAbusiveSites_OnlyLogs) {
  380. const GURL url("https://example.test/");
  381. MarkUrlAsAbusiveWarning(url);
  382. NavigateAndCommit(url);
  383. // Warning should come at navigation commit time, not at popup time.
  384. EXPECT_EQ(1u, GetMainFrameConsoleMessages().size());
  385. EXPECT_EQ(GetMainFrameConsoleMessages().front(), kAbusiveWarnMessage);
  386. EXPECT_FALSE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  387. web_contents()->GetPrimaryPage()));
  388. }
  389. class SafeBrowsingTriggeredPopupBlockerFilterAdsEnabledTest
  390. : public SafeBrowsingTriggeredPopupBlockerTestBase {
  391. public:
  392. SafeBrowsingTriggeredPopupBlockerFilterAdsEnabledTest() {
  393. scoped_feature_list_.InitAndEnableFeature(
  394. subresource_filter::kFilterAdsOnAbusiveSites);
  395. }
  396. private:
  397. base::test::ScopedFeatureList scoped_feature_list_;
  398. };
  399. TEST_F(SafeBrowsingTriggeredPopupBlockerFilterAdsEnabledTest,
  400. WarningMatchWithAdBlockOnAbusiveSites_OnlyLogs) {
  401. const GURL url("https://example.test/");
  402. MarkUrlAsAbusiveWarning(url);
  403. NavigateAndCommit(url);
  404. // Warning should come at navigation commit time, not at popup time.
  405. EXPECT_EQ(2u, GetMainFrameConsoleMessages().size());
  406. EXPECT_EQ(GetMainFrameConsoleMessages().front(), kAbusiveWarnMessage);
  407. EXPECT_EQ(GetMainFrameConsoleMessages().back(),
  408. subresource_filter::kActivationWarningConsoleMessage);
  409. EXPECT_FALSE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  410. web_contents()->GetPrimaryPage()));
  411. }
  412. TEST_F(SafeBrowsingTriggeredPopupBlockerTest, NonPrimaryFrameTree) {
  413. const GURL url1("https://example.first/");
  414. const GURL url2("https://example.second/");
  415. // Only mark url2 as abusive.
  416. MarkUrlAsAbusiveEnforce(url2);
  417. {
  418. // Simulate a navigation in the primary main frame to an url not marked as
  419. // abusive.
  420. content::MockNavigationHandle handle(url1, main_rfh());
  421. handle.set_has_committed(true);
  422. auto throttle = CreateThrottle(&handle);
  423. auto result = throttle->WillProcessResponse();
  424. if (result.action() == content::NavigationThrottle::ThrottleAction::DEFER) {
  425. base::RunLoop loop;
  426. throttle->set_resume_callback_for_testing(loop.QuitClosure());
  427. loop.Run();
  428. }
  429. popup_blocker()->DidFinishNavigation(&handle);
  430. EXPECT_FALSE(
  431. popup_blocker()->ShouldApplyAbusivePopupBlocker(main_rfh()->GetPage()));
  432. }
  433. {
  434. // Reset the state.
  435. NavigateAndCommit(url1);
  436. EXPECT_FALSE(
  437. popup_blocker()->ShouldApplyAbusivePopupBlocker(main_rfh()->GetPage()));
  438. // Simulate a navigation in the primary main frame to an url marked as
  439. // abusive.
  440. content::MockNavigationHandle handle(url2, main_rfh());
  441. handle.set_has_committed(true);
  442. auto throttle = CreateThrottle(&handle);
  443. auto result = throttle->WillProcessResponse();
  444. if (result.action() == content::NavigationThrottle::ThrottleAction::DEFER) {
  445. base::RunLoop loop;
  446. throttle->set_resume_callback_for_testing(loop.QuitClosure());
  447. loop.Run();
  448. }
  449. popup_blocker()->DidFinishNavigation(&handle);
  450. EXPECT_TRUE(
  451. popup_blocker()->ShouldApplyAbusivePopupBlocker(main_rfh()->GetPage()));
  452. }
  453. {
  454. // Reset the state.
  455. NavigateAndCommit(url1);
  456. EXPECT_FALSE(
  457. popup_blocker()->ShouldApplyAbusivePopupBlocker(main_rfh()->GetPage()));
  458. // Simulate a navigation in a non-primary main frame to an url marked as
  459. // abusive.
  460. content::MockNavigationHandle handle(url2, main_rfh());
  461. handle.set_has_committed(true);
  462. handle.set_is_in_primary_main_frame(false);
  463. auto throttle = CreateThrottle(&handle);
  464. auto result = throttle->WillProcessResponse();
  465. if (result.action() == content::NavigationThrottle::ThrottleAction::DEFER) {
  466. base::RunLoop loop;
  467. throttle->set_resume_callback_for_testing(loop.QuitClosure());
  468. loop.Run();
  469. }
  470. popup_blocker()->DidFinishNavigation(&handle);
  471. EXPECT_TRUE(
  472. popup_blocker()->ShouldApplyAbusivePopupBlocker(main_rfh()->GetPage()));
  473. }
  474. }
  475. class SafeBrowsingTriggeredPopupBlockerFencedFrameTest
  476. : public SafeBrowsingTriggeredPopupBlockerTest {
  477. public:
  478. SafeBrowsingTriggeredPopupBlockerFencedFrameTest() {
  479. scoped_feature_list_.InitAndEnableFeatureWithParameters(
  480. blink::features::kFencedFrames, {{"implementation_type", "mparch"}});
  481. }
  482. ~SafeBrowsingTriggeredPopupBlockerFencedFrameTest() override = default;
  483. private:
  484. base::test::ScopedFeatureList scoped_feature_list_;
  485. };
  486. // Ensures that the popup blocker is not triggered by a fenced frame.
  487. TEST_F(SafeBrowsingTriggeredPopupBlockerFencedFrameTest,
  488. ShouldNotTriggerPopupBlocker) {
  489. const GURL url("https://example.test/");
  490. MarkUrlAsAbusiveEnforce(url);
  491. NavigateAndCommit(url);
  492. // The popup blocker is triggered for a primary page.
  493. EXPECT_TRUE(
  494. popup_blocker()->ShouldApplyAbusivePopupBlocker(main_rfh()->GetPage()));
  495. content::RenderFrameHost* fenced_frame_root =
  496. content::RenderFrameHostTester::For(main_rfh())->AppendFencedFrame();
  497. // Navigate a fenced frame.
  498. const GURL fenced_frame_url("https://fencedframe.test");
  499. MarkUrlAsAbusiveEnforce(fenced_frame_url);
  500. std::unique_ptr<content::NavigationSimulator> navigation_simulator =
  501. content::NavigationSimulator::CreateRendererInitiated(fenced_frame_url,
  502. fenced_frame_root);
  503. navigation_simulator->Commit();
  504. fenced_frame_root = navigation_simulator->GetFinalRenderFrameHost();
  505. // The popup blocker is not triggered for a fenced frame.
  506. EXPECT_FALSE(popup_blocker()->ShouldApplyAbusivePopupBlocker(
  507. fenced_frame_root->GetPage()));
  508. }
  509. } // namespace blocked_content