Skip to content

Commit

Permalink
Merge pull request #14340 from brave/match-default-1p-blocking-griffin
Browse files Browse the repository at this point in the history
Match the default 1p blocking policy shipped by Griffin
  • Loading branch information
antonok-edm committed Jul 28, 2022
2 parents 094c0a2 + 4effc7d commit 15ece28
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
20 changes: 15 additions & 5 deletions browser/brave_shields/ad_block_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ AdBlockServiceTest::~AdBlockServiceTest() {}
void AdBlockServiceTest::SetUpOnMainThread() {
ExtensionBrowserTest::SetUpOnMainThread();
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
// behavior.
brave_shields::SetCosmeticFilteringControlType(
content_settings(), brave_shields::ControlType::BLOCK, GURL());
}

void AdBlockServiceTest::SetUp() {
Expand Down Expand Up @@ -318,6 +323,12 @@ void AdBlockServiceTest::ShieldsDown(const GURL& url) {
brave_shields::SetBraveShieldsEnabled(content_settings(), false, url);
}

void AdBlockServiceTest::DisableAggressiveMode() {
brave_shields::SetCosmeticFilteringControlType(
content_settings(), brave_shields::ControlType::BLOCK_THIRD_PARTY,
GURL());
}

// Load a page with an ad image, and make sure it is blocked.
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByDefaultBlocker) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
Expand Down Expand Up @@ -1497,6 +1508,7 @@ class Default1pBlockingFlagDisabledTest : public AdBlockServiceTest {
// blocked while the first-party one is allowed.
IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, Default1pBlocking) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
DisableAggressiveMode();
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);
UpdateAdBlockInstanceWithRules("^ad_banner.png");

Expand All @@ -1523,8 +1535,6 @@ IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest,
Aggressive1pBlocking) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);
brave_shields::SetCosmeticFilteringControlType(
content_settings(), brave_shields::ControlType::BLOCK, GURL());
UpdateAdBlockInstanceWithRules("^ad_banner.png");

GURL url = embedded_test_server()->GetURL(kAdBlockTestPage);
Expand All @@ -1548,6 +1558,7 @@ IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest,
// blocked.
IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, Custom1pBlocking) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
DisableAggressiveMode();
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);
UpdateCustomAdBlockInstanceWithRules("^ad_banner.png");
WaitForAdBlockServiceThreads();
Expand Down Expand Up @@ -1908,6 +1919,7 @@ IN_PROC_BROWSER_TEST_F(CosmeticFilteringChildFramesFlagEnabledTest,
// Test cosmetic filtering ignores content determined to be 1st party
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringProtect1p) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
DisableAggressiveMode();
UpdateAdBlockInstanceWithRules(
"appspot.com##.fpsponsored\n"
"appspot.com##.fpsponsored1\n"
Expand Down Expand Up @@ -1939,11 +1951,9 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringProtect1p) {
"checkSelector('#another-etld', 'display', 'none')"));
}

// Test cosmetic filtering bypasses 1st party checks when toggled
// Test cosmetic filtering bypasses 1st party checks in Aggressive mode
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringHide1pContent) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
brave_shields::SetCosmeticFilteringControlType(
content_settings(), brave_shields::ControlType::BLOCK, GURL());
UpdateAdBlockInstanceWithRules("b.com##.fpsponsored\n");

GURL tab_url =
Expand Down
1 change: 1 addition & 0 deletions browser/brave_shields/ad_block_service_browsertest.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class AdBlockServiceTest : public extensions::ExtensionBrowserTest {
void SetSubscriptionIntervals();
void WaitForAdBlockServiceThreads();
void ShieldsDown(const GURL& url);
void DisableAggressiveMode();
void LoadDAT(base::FilePath path);
void EnableRedirectUrlParsing();
content::WebContents* web_contents();
Expand Down
33 changes: 33 additions & 0 deletions browser/net/brave_ad_block_tp_network_delegate_helper_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,44 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, RequestDataURL) {
TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, SimpleBlocking) {
ResetAdblockInstance("||brave.com/test.txt", "");

const GURL url("https://brave.com/test.txt");
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);
request_info->request_identifier = 1;
request_info->resource_type = blink::mojom::ResourceType::kScript;
request_info->initiator_url = GURL("https://bravesoftware.com");

EXPECT_TRUE(CheckRequest(request_info));
EXPECT_EQ(request_info->blocked_by, brave::kAdBlocked);
EXPECT_TRUE(request_info->new_url_spec.empty());
// It's unclear whether or not this is a Tor request, so no DNS queries are
// made (`browser_context` is `nullptr`).
EXPECT_EQ(0ULL, host_resolver_->num_resolve());
}

TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, Default1pException) {
ResetAdblockInstance("||brave.com/test.txt", "");

const GURL url("https://brave.com/test.txt");
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);
request_info->request_identifier = 1;
request_info->resource_type = blink::mojom::ResourceType::kScript;
request_info->initiator_url = GURL("https://brave.com");

EXPECT_TRUE(CheckRequest(request_info));
EXPECT_EQ(request_info->blocked_by, brave::kNotBlocked);
EXPECT_TRUE(request_info->new_url_spec.empty());
EXPECT_EQ(0ULL, host_resolver_->num_resolve());
}

TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, AggressiveNo1pException) {
ResetAdblockInstance("||brave.com/test.txt", "");

const GURL url("https://brave.com/test.txt");
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);
request_info->request_identifier = 1;
request_info->resource_type = blink::mojom::ResourceType::kScript;
request_info->initiator_url = GURL("https://brave.com");
request_info->aggressive_blocking = true;

EXPECT_TRUE(CheckRequest(request_info));
EXPECT_EQ(request_info->blocked_by, brave::kAdBlocked);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include "brave/components/brave_component_updater/browser/local_data_files_service.h"
#include "brave/components/brave_perf_predictor/common/pref_names.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/browser/test_filters_provider.h"
#include "brave/components/constants/brave_paths.h"
#include "brave/components/constants/pref_names.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
Expand Down Expand Up @@ -47,6 +49,15 @@ class PerfPredictorTabHelperTest : public InProcessBrowserTest {
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");

auto* content_settings =
HostContentSettingsMapFactory::GetForProfile(browser()->profile());

// Most tests are written for aggressive mode. Individual tests should reset
// this using `DisableAggressiveMode` if they are testing standard mode
// behavior.
brave_shields::SetCosmeticFilteringControlType(
content_settings, brave_shields::ControlType::BLOCK, GURL());
}

void SetUp() override {
Expand Down
2 changes: 1 addition & 1 deletion components/brave_shields/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace features {
// allow first-party requests in "standard" blocking mode regardless of whether
// or not they appear in a filter list.
const base::Feature kBraveAdblockDefault1pBlocking{
"BraveAdblockDefault1pBlocking", base::FEATURE_ENABLED_BY_DEFAULT};
"BraveAdblockDefault1pBlocking", base::FEATURE_DISABLED_BY_DEFAULT};
// When enabled, Brave will issue DNS queries for requests that the adblock
// engine has not blocked, then check them again with the original hostname
// substituted for any canonical name found.
Expand Down

0 comments on commit 15ece28

Please sign in to comment.