From 374790b6ee0f29e7a75241f25b3abec7d4ab9cfe Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Wed, 7 Jun 2023 21:14:23 -0700 Subject: [PATCH 1/3] force aggressive cosmetic filtering on youtube --- browser/net/brave_ad_block_tp_network_delegate_helper.cc | 2 +- .../renderer/cosmetic_filters_js_handler.cc | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/browser/net/brave_ad_block_tp_network_delegate_helper.cc b/browser/net/brave_ad_block_tp_network_delegate_helper.cc index ca6f9da3230d..3514b375c6fb 100644 --- a/browser/net/brave_ad_block_tp_network_delegate_helper.cc +++ b/browser/net/brave_ad_block_tp_network_delegate_helper.cc @@ -186,7 +186,7 @@ EngineFlags ShouldBlockRequestOnTaskRunner( bool force_aggressive = SameDomainOrHost( ctx->initiator_url, - url::Origin::CreateFromNormalizedTuple("https", "youtube.com", 80), + url::Origin::CreateFromNormalizedTuple("https", "youtube.com", 443), net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); std::string rewritten_url; diff --git a/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc b/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc index a1797065bdfd..248613378cfd 100644 --- a/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc +++ b/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc @@ -361,7 +361,11 @@ bool CosmeticFiltersJSHandler::ProcessURL( enabled_1st_party_cf_ = force_cosmetic_filtering || render_frame_->GetWebFrame()->IsCrossOriginToOutermostMainFrame() || - content_settings->IsFirstPartyCosmeticFilteringEnabled(url_); + content_settings->IsFirstPartyCosmeticFilteringEnabled(url_) || + net::registry_controlled_domains::SameDomainOrHost( + url_, + url::Origin::CreateFromNormalizedTuple("https", "youtube.com", 443), + net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); if (callback.has_value()) { SCOPED_UMA_HISTOGRAM_TIMER_MICROS( From 444d8f716ace00a7b2cedaeb6427066b95c7e853 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Fri, 7 Jul 2023 15:33:15 -0700 Subject: [PATCH 2/3] add HTTPS test server to AdBlockServiceTest --- .../ad_block_service_browsertest.cc | 24 ++++++++++++++++++- .../ad_block_service_browsertest.h | 4 ++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/browser/brave_shields/ad_block_service_browsertest.cc b/browser/brave_shields/ad_block_service_browsertest.cc index 459f70b54ff2..bd063c736510 100644 --- a/browser/brave_shields/ad_block_service_browsertest.cc +++ b/browser/brave_shields/ad_block_service_browsertest.cc @@ -92,14 +92,26 @@ using brave_shields::features::kCosmeticFilteringJsPerformance; AdBlockServiceTest::AdBlockServiceTest() : ws_server_(net::SpawnedTestServer::TYPE_WS, - net::GetWebSocketTestDataDirectory()) { + net::GetWebSocketTestDataDirectory()), + https_server_(net::EmbeddedTestServer::Type::TYPE_HTTPS) { brave_shields::SetDefaultAdBlockComponentIdAndBase64PublicKeyForTest( kDefaultAdBlockComponentTestId, kDefaultAdBlockComponentTest64PublicKey); } AdBlockServiceTest::~AdBlockServiceTest() = default; +void AdBlockServiceTest::SetUpCommandLine(base::CommandLine* command_line) { + InProcessBrowserTest::SetUpCommandLine(command_line); + mock_cert_verifier_.SetUpCommandLine(command_line); +} + +void AdBlockServiceTest::SetUpInProcessBrowserTestFixture() { + InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); + mock_cert_verifier_.SetUpInProcessBrowserTestFixture(); +} + void AdBlockServiceTest::SetUpOnMainThread() { ExtensionBrowserTest::SetUpOnMainThread(); + mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK); host_resolver()->AddRule("*", "127.0.0.1"); // Most tests are written for aggressive mode. Individual tests should reset // this using `DisableAggressiveMode` if they are testing standard mode @@ -124,6 +136,11 @@ void AdBlockServiceTest::TearDownOnMainThread() { ExtensionBrowserTest::TearDownOnMainThread(); } +void AdBlockServiceTest::TearDownInProcessBrowserTestFixture() { + mock_cert_verifier_.TearDownInProcessBrowserTestFixture(); + InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); +} + content::WebContents* AdBlockServiceTest::web_contents() { return browser()->tab_strip_model()->GetActiveWebContents(); } @@ -195,6 +212,11 @@ void AdBlockServiceTest::InitEmbeddedTestServer() { brave::RegisterPathProvider(); base::FilePath test_data_dir; base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir); + + https_server_.ServeFilesFromDirectory(test_data_dir); + content::SetupCrossSiteRedirector(&https_server_); + ASSERT_TRUE(https_server_.Start()); + embedded_test_server()->ServeFilesFromDirectory(test_data_dir); content::SetupCrossSiteRedirector(embedded_test_server()); ASSERT_TRUE(embedded_test_server()->Start()); diff --git a/browser/brave_shields/ad_block_service_browsertest.h b/browser/brave_shields/ad_block_service_browsertest.h index 4b15e161e568..3fa8e3f6b21d 100644 --- a/browser/brave_shields/ad_block_service_browsertest.h +++ b/browser/brave_shields/ad_block_service_browsertest.h @@ -27,10 +27,13 @@ class AdBlockServiceTest : public extensions::ExtensionBrowserTest { ~AdBlockServiceTest() override; // ExtensionBrowserTest overrides + void SetUpCommandLine(base::CommandLine* command_line) override; + void SetUpInProcessBrowserTestFixture() override; void SetUpOnMainThread() override; void SetUp() override; void PreRunTestOnMainThread() override; void TearDownOnMainThread() override; + void TearDownInProcessBrowserTestFixture() override; protected: content::ContentMockCertVerifier mock_cert_verifier_; @@ -66,6 +69,7 @@ class AdBlockServiceTest : public extensions::ExtensionBrowserTest { net::SpawnedTestServer ws_server_; net::EmbeddedTestServer dynamic_server_; + net::EmbeddedTestServer https_server_; }; #endif // BRAVE_BROWSER_BRAVE_SHIELDS_AD_BLOCK_SERVICE_BROWSERTEST_H_ From a9d5f0816a64751b05040a1c1ba4300fc844cebe Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Fri, 7 Jul 2023 15:38:27 -0700 Subject: [PATCH 3/3] add test --- .../ad_block_service_browsertest.cc | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/browser/brave_shields/ad_block_service_browsertest.cc b/browser/brave_shields/ad_block_service_browsertest.cc index bd063c736510..c3665da36c57 100644 --- a/browser/brave_shields/ad_block_service_browsertest.cc +++ b/browser/brave_shields/ad_block_service_browsertest.cc @@ -1528,6 +1528,32 @@ IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, Default1pBlocking) { EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL); } +// Load a page with an image from a first party and a third party, which both +// match the same filter in the default engine. They should both be blocked on +// special URLs like this one. +IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, SpecialUrlException) { + ASSERT_TRUE(InstallDefaultAdBlockExtension()); + DisableAggressiveMode(); + EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); + UpdateAdBlockInstanceWithRules("^ad_banner.png"); + + // Must use HTTPS because `youtube.com` is in Chromium's HSTS preload list + GURL url = https_server_.GetURL("youtube.com", kAdBlockTestPage); + ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url)); + content::WebContents* contents = + browser()->tab_strip_model()->GetActiveWebContents(); + + ASSERT_EQ(true, EvalJs(contents, + "setExpectations(0, 1, 0, 0);" + "addImage('ad_banner.png')")); + EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL); + + ASSERT_EQ(true, EvalJs(contents, + "setExpectations(0, 2, 0, 0);" + "addImage('https://thirdparty.com/ad_banner.png')")); + EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 2ULL); +} + // Load a page with an image from a first party and a third party, which both // match the same filter in the default engine. Enable aggressive mode, and // ensure that both are blocked.