diff --git a/DEPS b/DEPS index 9fb80baa0e7c..e423f85393f8 100644 --- a/DEPS +++ b/DEPS @@ -1,7 +1,7 @@ use_relative_paths = True deps = { - "vendor/ad-block": "https://github.com/brave/ad-block.git@74b167c8b49c33f3f61c15dafa2ffde953a25653", + "vendor/ad-block": "https://github.com/brave/ad-block.git@e54c59fe288d8f08de683b8f4f320a4c7bead4eb", "vendor/autoplay-whitelist": "https://github.com/brave/autoplay-whitelist.git@458053a3c95b403cbe0872f289a2aafa106ee9d8", "vendor/extension-whitelist": "https://github.com/brave/extension-whitelist.git@463e5e4e06e0ca84927176e8c72f6076ae9b6829", "vendor/tracking-protection": "https://github.com/brave/tracking-protection.git@29b1f86b11a8c7438fd7d57b446a77a84946712a", 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 d59cefa43f38..1bc36bfe54b2 100644 --- a/browser/net/brave_ad_block_tp_network_delegate_helper.cc +++ b/browser/net/brave_ad_block_tp_network_delegate_helper.cc @@ -98,22 +98,25 @@ void OnBeforeURLRequestAdBlockTPOnTaskRunner( std::string tab_host = ctx->tab_origin.host(); if (!g_brave_browser_process->ad_block_service()->ShouldStartRequest( ctx->request_url, ctx->resource_type, tab_host, - &did_match_exception)) { + &did_match_exception, &ctx->cancel_request_explicitly)) { ctx->blocked_by = kAdBlocked; } else if (!did_match_exception && !g_brave_browser_process->ad_block_regional_service() ->ShouldStartRequest(ctx->request_url, ctx->resource_type, - tab_host, &did_match_exception)) { + tab_host, &did_match_exception, + &ctx->cancel_request_explicitly)) { ctx->blocked_by = kAdBlocked; } else if (!did_match_exception && !g_brave_browser_process->ad_block_custom_filters_service() ->ShouldStartRequest(ctx->request_url, ctx->resource_type, - tab_host, &did_match_exception)) { + tab_host, &did_match_exception, + &ctx->cancel_request_explicitly)) { ctx->blocked_by = kAdBlocked; } else if (!did_match_exception && !g_brave_browser_process->tracking_protection_service() ->ShouldStartRequest(ctx->request_url, ctx->resource_type, - tab_host, &did_match_exception)) { + tab_host, &did_match_exception, + &ctx->cancel_request_explicitly)) { ctx->blocked_by = kTrackerBlocked; } } diff --git a/browser/net/brave_network_delegate_base.cc b/browser/net/brave_network_delegate_base.cc index 41177ccc4baa..dd7ef1047c4f 100644 --- a/browser/net/brave_network_delegate_base.cc +++ b/browser/net/brave_network_delegate_base.cc @@ -381,6 +381,11 @@ void BraveNetworkDelegateBase::RunNextCallback( ctx->blocked_by == brave::kTrackerBlocked) { // We are going to intercept this request and block it later in the // network stack. + if (ctx->cancel_request_explicitly) { + RunCallbackForRequestIdentifier(ctx->request_identifier, + net::ERR_ABORTED); + return; + } request->SetExtraRequestHeaderByName("X-Brave-Block", "", true); } rv = ChromeNetworkDelegate::OnBeforeURLRequest( diff --git a/browser/net/url_context.h b/browser/net/url_context.h index 518075710f32..3de40e1ac504 100644 --- a/browser/net/url_context.h +++ b/browser/net/url_context.h @@ -73,6 +73,7 @@ struct BraveRequestInfo { BraveNetworkDelegateEventType event_type = kUnknownEventType; const base::ListValue* referral_headers_list = nullptr; BlockedBy blocked_by = kNotBlocked; + bool cancel_request_explicitly = false; // Default to invalid type for resource_type, so delegate helpers // can properly detect that the info couldn't be obtained. content::ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE; diff --git a/components/brave_shields/browser/ad_block_base_service.cc b/components/brave_shields/browser/ad_block_base_service.cc index 69f102c060bc..01cc3fa78257 100644 --- a/components/brave_shields/browser/ad_block_base_service.cc +++ b/components/brave_shields/browser/ad_block_base_service.cc @@ -119,7 +119,7 @@ void AdBlockBaseService::Cleanup() { bool AdBlockBaseService::ShouldStartRequest(const GURL& url, content::ResourceType resource_type, const std::string& tab_host, - bool* did_match_exception) { + bool* did_match_exception, bool* cancel_request_explicitly) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); FilterOption current_option = ResourceTypeToFilterOption(resource_type); @@ -134,10 +134,15 @@ bool AdBlockBaseService::ShouldStartRequest(const GURL& url, current_option = static_cast(current_option | FOThirdParty); } + Filter* matching_filter = nullptr; Filter* matching_exception_filter = nullptr; if (ad_block_client_->matches(url.spec().c_str(), - current_option, tab_host.c_str(), nullptr, + current_option, tab_host.c_str(), &matching_filter, &matching_exception_filter)) { + if (matching_filter && cancel_request_explicitly && + (matching_filter->filterOption & FOExplicitCancel)) { + *cancel_request_explicitly = true; + } // We'd only possibly match an exception filter if we're returning true. *did_match_exception = false; // LOG(ERROR) << "AdBlockBaseService::ShouldStartRequest(), host: " diff --git a/components/brave_shields/browser/ad_block_base_service.h b/components/brave_shields/browser/ad_block_base_service.h index f2e3c137bc00..0e8980075220 100644 --- a/components/brave_shields/browser/ad_block_base_service.h +++ b/components/brave_shields/browser/ad_block_base_service.h @@ -32,7 +32,8 @@ class AdBlockBaseService : public BaseBraveShieldsService { ~AdBlockBaseService() override; bool ShouldStartRequest(const GURL &url, content::ResourceType resource_type, - const std::string& tab_host, bool* matching_exception_filter) override; + const std::string& tab_host, bool* did_match_exception, + bool* cancel_request_explicitly) override; void EnableTag(const std::string& tag, bool enabled); protected: diff --git a/components/brave_shields/browser/ad_block_service_browsertest.cc b/components/brave_shields/browser/ad_block_service_browsertest.cc index a50948a99dec..aca8de9bcac3 100644 --- a/components/brave_shields/browser/ad_block_service_browsertest.cc +++ b/components/brave_shields/browser/ad_block_service_browsertest.cc @@ -239,7 +239,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByDefaultBlocker) { bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(0, 1, 0, 0);" + "setExpectations(0, 1, 0, 0, 0, 0);" "addImage('ad_banner.png')", &as_expected)); EXPECT_TRUE(as_expected); @@ -262,7 +262,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(1, 0, 0, 0);" + "setExpectations(1, 0, 0, 0, 0, 0);" "addImage('logo.png')", &as_expected)); EXPECT_TRUE(as_expected); @@ -283,7 +283,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByCustomBlocker) { bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(0, 1, 0, 0);" + "setExpectations(0, 1, 0, 0, 0, 0);" "addImage('ad_banner.png')", &as_expected)); EXPECT_TRUE(as_expected); @@ -307,7 +307,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(1, 0, 0, 0);" + "setExpectations(1, 0, 0, 0, 0, 0);" "addImage('logo.png')", &as_expected)); EXPECT_TRUE(as_expected); @@ -335,7 +335,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByRegionalBlocker) { bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(0, 1, 0, 0);" + "setExpectations(0, 1, 0, 0, 0, 0);" "addImage('ad_fr.png')", &as_expected)); EXPECT_TRUE(as_expected); @@ -364,7 +364,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(1, 0, 0, 0);" + "setExpectations(1, 0, 0, 0, 0, 0);" "addImage('logo.png')", &as_expected)); EXPECT_TRUE(as_expected); @@ -398,7 +398,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(0, 1, 0, 0);" + "setExpectations(0, 1, 0, 0, 0, 0);" "addImage('v4_specific_banner.png')", &as_expected)); EXPECT_TRUE(as_expected); @@ -421,7 +421,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, TwoSameAdsGetCountedAsOne) { bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(0, 0, 1, 2);" + "setExpectations(0, 0, 0, 1, 2, 0);" "xhr('adbanner.js');" "xhr('normal.js');" "xhr('adbanner.js')", @@ -445,7 +445,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, TwoDiffAdsGetCountedAsTwo) { bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(0, 0, 1, 2);" + "setExpectations(0, 0, 0, 1, 2, 0);" "xhr('adbanner.js?1');" "xhr('normal.js');" "xhr('adbanner.js?2')", @@ -469,7 +469,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, NewTabContinuesToBlock) { bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(0, 0, 0, 1);" + "setExpectations(0, 0, 0, 0, 1, 0);" "xhr('adbanner.js');", &as_expected)); EXPECT_TRUE(as_expected); @@ -480,7 +480,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, NewTabContinuesToBlock) { as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(0, 0, 0, 1);" + "setExpectations(0, 0, 0, 0, 1, 0);" "xhr('adbanner.js');", &as_expected)); EXPECT_TRUE(as_expected); @@ -504,7 +504,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubFrame) { bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents->GetAllFrames()[1], - "setExpectations(0, 0, 0, 1);" + "setExpectations(0, 0, 0, 0, 1, 0);" "xhr('adbanner.js?1');", &as_expected)); EXPECT_TRUE(as_expected); @@ -544,7 +544,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, browser()->tab_strip_model()->GetActiveWebContents(); bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - "setExpectations(1, 0, 0, 0);" + "setExpectations(1, 0, 0, 0, 0, 0);" "addImage('ad_fr.png')", &as_expected)); EXPECT_TRUE(as_expected); @@ -570,11 +570,11 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, GURL test_url = embedded_test_server()->GetURL("365dm.com", "/logo.png"); bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - base::StringPrintf( - "setExpectations(0, 1, 0, 0);" - "addImage('%s')", - test_url.spec().c_str()), - &as_expected)); + base::StringPrintf( + "setExpectations(0, 1, 0, 0, 0, 0);" + "addImage('%s')", + test_url.spec().c_str()), + &as_expected)); EXPECT_TRUE(as_expected); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kTrackersBlocked), 1ULL); @@ -596,11 +596,11 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, GURL test_url = embedded_test_server()->GetURL("365dm.com", "/logo.png"); bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - base::StringPrintf( - "setExpectations(1, 0, 0, 0);" - "addImage('%s')", - test_url.spec().c_str()), - &as_expected)); + base::StringPrintf( + "setExpectations(1, 0, 0, 0, 0, 0);" + "addImage('%s')", + test_url.spec().c_str()), + &as_expected)); EXPECT_TRUE(as_expected); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kTrackersBlocked), 0ULL); @@ -620,11 +620,11 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdBlockThirdPartyWorksByETLDP1) { browser()->tab_strip_model()->GetActiveWebContents(); bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - base::StringPrintf( - "setExpectations(1, 0, 0, 0);" - "addImage('%s')", - resource_url.spec().c_str()), - &as_expected)); + base::StringPrintf( + "setExpectations(1, 0, 0, 0, 0, 0);" + "addImage('%s')", + resource_url.spec().c_str()), + &as_expected)); EXPECT_TRUE(as_expected); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); } @@ -643,11 +643,11 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, browser()->tab_strip_model()->GetActiveWebContents(); bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, - base::StringPrintf( - "setExpectations(0, 1, 0, 0);" - "addImage('%s')", - resource_url.spec().c_str()), - &as_expected)); + base::StringPrintf( + "setExpectations(0, 1, 0, 0, 0, 0);" + "addImage('%s')", + resource_url.spec().c_str()), + &as_expected)); EXPECT_TRUE(as_expected); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL); } @@ -666,7 +666,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, BlockNYP) { bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, base::StringPrintf( - "setExpectations(0, 1, 0, 0);" + "setExpectations(0, 1, 0, 0, 0, 0);" "addImage('%s')", resource_url.spec().c_str()), &as_expected)); @@ -692,7 +692,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SocialButttonAdBLockTagTest) { bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, base::StringPrintf( - "setExpectations(0, 1, 0, 0);" + "setExpectations(0, 1, 0, 0, 0, 0);" "addImage('%s')", resource_url.spec().c_str()), &as_expected)); @@ -717,7 +717,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SocialButttonAdBlockDiffTagTest) { bool as_expected = false; ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, base::StringPrintf( - "setExpectations(1, 0, 0, 0);" + "setExpectations(1, 0, 0, 0, 0, 0);" "addImage('%s')", resource_url.spec().c_str()), &as_expected)); @@ -772,3 +772,25 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, TagPrefsControlTags) { AssertTagExists(brave_shields::kTwitterEmbeds, true); AssertTagExists(brave_shields::kLinkedInEmbeds, false); } + +// Make sure that cancelrequest actually blocks +IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CancelRequestOptionTest) { + AddRulesToAdBlock("logo.png$explicitcancel"); + EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); + GURL tab_url = embedded_test_server()->GetURL("b.com", + kAdBlockTestPage); + GURL resource_url = + embedded_test_server()->GetURL("example.com", "/logo.png"); + ui_test_utils::NavigateToURL(browser(), tab_url); + content::WebContents* contents = + browser()->tab_strip_model()->GetActiveWebContents(); + bool as_expected = false; + ASSERT_TRUE(ExecuteScriptAndExtractBool(contents, + base::StringPrintf( + "setExpectations(0, 0, 1, 0, 0, 0);" + "addImage('%s')", + resource_url.spec().c_str()), + &as_expected)); + EXPECT_TRUE(as_expected); + EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL); +} diff --git a/components/brave_shields/browser/base_brave_shields_service.cc b/components/brave_shields/browser/base_brave_shields_service.cc index 28dde5533897..11c6a5f673b5 100644 --- a/components/brave_shields/browser/base_brave_shields_service.cc +++ b/components/brave_shields/browser/base_brave_shields_service.cc @@ -61,10 +61,12 @@ void BaseBraveShieldsService::Stop() { bool BaseBraveShieldsService::ShouldStartRequest(const GURL& url, content::ResourceType resource_type, const std::string& tab_host, - bool* did_match_exception) { + bool* did_match_exception, + bool* cancel_request_explicitly) { if (did_match_exception) { *did_match_exception = false; } + // Intentionally don't set cancel_request_explicitly return true; } diff --git a/components/brave_shields/browser/base_brave_shields_service.h b/components/brave_shields/browser/base_brave_shields_service.h index acd7276928cd..9c9ff6063741 100644 --- a/components/brave_shields/browser/base_brave_shields_service.h +++ b/components/brave_shields/browser/base_brave_shields_service.h @@ -34,7 +34,8 @@ class BaseBraveShieldsService : public BraveComponentExtension { virtual bool ShouldStartRequest(const GURL& url, content::ResourceType resource_type, const std::string& tab_host, - bool* did_match_exception); + bool* did_match_exception, + bool* cancel_request_explicitly); virtual scoped_refptr GetTaskRunner(); protected: diff --git a/components/brave_shields/browser/tracking_protection_service.cc b/components/brave_shields/browser/tracking_protection_service.cc index 942ea53109ac..6921d0f1bd68 100644 --- a/components/brave_shields/browser/tracking_protection_service.cc +++ b/components/brave_shields/browser/tracking_protection_service.cc @@ -40,12 +40,14 @@ TrackingProtectionService::~TrackingProtectionService() { bool TrackingProtectionService::ShouldStartRequest(const GURL& url, content::ResourceType resource_type, const std::string &tab_host, - bool* matching_exception_filter) { + bool* matching_exception_filter, + bool* cancel_request_explicitly) { // There are no exceptions in the TP service, but exceptions are // combined with brave/ad-block. if (matching_exception_filter) { *matching_exception_filter = false; } + // Intentionally don't set cancel_request_explicitly DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); std::string host = url.host(); if (!tracking_protection_client_->matchesTracker( diff --git a/components/brave_shields/browser/tracking_protection_service.h b/components/brave_shields/browser/tracking_protection_service.h index 568a0b5917ab..becb0899407a 100644 --- a/components/brave_shields/browser/tracking_protection_service.h +++ b/components/brave_shields/browser/tracking_protection_service.h @@ -38,7 +38,8 @@ class TrackingProtectionService : public BaseLocalDataFilesObserver { bool ShouldStartRequest(const GURL& spec, content::ResourceType resource_type, const std::string& tab_host, - bool* matching_exception_filter); + bool* matching_exception_filter, + bool* cancel_request_explicitly); scoped_refptr GetTaskRunner(); // implementation of BaseLocalDataFilesObserver diff --git a/test/data/blocking.html b/test/data/blocking.html index bc78a3f100ed..0d4a68c41691 100644 --- a/test/data/blocking.html +++ b/test/data/blocking.html @@ -2,21 +2,28 @@