Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ABP filter redirects as mock responses #3419

Merged
merged 4 commits into from Nov 19, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

Use ABP filter redirects as mock responses

  • Loading branch information
bbondy committed Sep 12, 2019
commit 898f5d7fd99202a8634f1906d51e2afeca96acef
2 DEPS
@@ -1,7 +1,7 @@
use_relative_paths = True

deps = {
"vendor/adblock_rust_ffi": "https://github.com/brave/adblock-rust-ffi.git@2360813922ceed63e3798737a268858c11e24d80",
"vendor/adblock_rust_ffi": "https://github.com/brave/adblock-rust-ffi.git@89127a30655eaf54cf73794309846084ea8b91b9",
"vendor/autoplay-whitelist": "https://github.com/brave/autoplay-whitelist.git@ea527a4d36051daedb34421e129c98eda06cb5d3",
"vendor/extension-whitelist": "https://github.com/brave/extension-whitelist.git@7843f62e26a23c51336330e220e9d7992680aae9",
"vendor/hashset-cpp": "https://github.com/brave/hashset-cpp.git@6eab0271d014ff09bd9f38abe1e0c117e13e9aa9",
@@ -649,10 +649,5 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
You can read <ph name="BEGIN_LINK_BUILD_INSTRUCTIONS">&lt;a target="_blank" href="$3"&gt;</ph>instructions on how to download and build for yourself<ph name="END_LINK_BUILD_INSTRUCTIONS">&lt;/a&gt;</ph> the specific <ph name="BEGIN_LINK_SOURCE_CODE">&lt;a target="_blank" href="$4"&gt;</ph>source code used to create this copy<ph name="END_LINK_SOURCE_CODE">&lt;/a&gt;</ph>.
</message>
</messages>
<includes>
<include name="IDR_BRAVE_GOOGLE_ANALYTICS_POLYFILL" file="resources/js/google_analytics_polyfill.js" type="BINDATA" />
<include name="IDR_BRAVE_TAG_SERVICES_POLYFILL" file="resources/js/tag_services_polyfill.js" type="BINDATA" />
<include name="IDR_BRAVE_TAG_MANAGER_POLYFILL" file="resources/js/tag_manager_polyfill.js" type="BINDATA" />
</includes>
</release>
</grit>

This file was deleted.

This file was deleted.

This file was deleted.

@@ -29,98 +29,27 @@ using content::ResourceType;

namespace brave {

std::string GetGoogleAnalyticsPolyfillJS() {
static std::string base64_output;
if (base64_output.length() != 0) {
return base64_output;
}
std::string str = ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_BRAVE_GOOGLE_ANALYTICS_POLYFILL).as_string();
Base64UrlEncode(str, base::Base64UrlEncodePolicy::OMIT_PADDING,
&base64_output);
base64_output = std::string(kJSDataURLPrefix) + base64_output;
return base64_output;
}

std::string GetGoogleTagManagerPolyfillJS() {
static std::string base64_output;
if (base64_output.length() != 0) {
return base64_output;
}
std::string str = ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_BRAVE_TAG_MANAGER_POLYFILL).as_string();
Base64UrlEncode(str, base::Base64UrlEncodePolicy::OMIT_PADDING,
&base64_output);
base64_output = std::string(kJSDataURLPrefix) + base64_output;
return base64_output;
}

std::string GetGoogleTagServicesPolyfillJS() {
static std::string base64_output;
if (base64_output.length() != 0) {
return base64_output;
}
std::string str = ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_BRAVE_TAG_SERVICES_POLYFILL).as_string();
Base64UrlEncode(str, base::Base64UrlEncodePolicy::OMIT_PADDING,
&base64_output);
base64_output = std::string(kJSDataURLPrefix) + base64_output;
return base64_output;
}

bool GetPolyfillForAdBlock(bool allow_brave_shields, bool allow_ads,
const GURL& tab_origin, const GURL& gurl, std::string* new_url_spec) {
// Polyfills which are related to adblock should only apply when shields
// are up.
if (!allow_brave_shields || allow_ads) {
return false;
}

static URLPattern analytics(URLPattern::SCHEME_ALL,
kGoogleAnalyticsPattern);
static URLPattern tag_manager(URLPattern::SCHEME_ALL,
kGoogleTagManagerPattern);
static URLPattern tag_services(URLPattern::SCHEME_ALL,
kGoogleTagServicesPattern);
if (analytics.MatchesURL(gurl)) {
std::string&& data_url = GetGoogleAnalyticsPolyfillJS();
*new_url_spec = data_url;
return true;
}

if (tag_manager.MatchesURL(gurl)) {
std::string&& data_url = GetGoogleTagManagerPolyfillJS();
*new_url_spec = data_url;
return true;
}

if (tag_services.MatchesURL(gurl)) {
std::string&& data_url = GetGoogleTagServicesPolyfillJS();
*new_url_spec = data_url;
return true;
}

return false;
}

void ShouldBlockAdOnTaskRunner(std::shared_ptr<BraveRequestInfo> ctx) {
bool did_match_exception = false;
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, &ctx->cancel_request_explicitly)) {
&did_match_exception, &ctx->cancel_request_explicitly,
&ctx->redirect)) {
ctx->blocked_by = kAdBlocked;
} else if (!did_match_exception &&
!g_brave_browser_process->ad_block_regional_service_manager()
->ShouldStartRequest(ctx->request_url, ctx->resource_type,
tab_host, &did_match_exception,
&ctx->cancel_request_explicitly)) {
&ctx->cancel_request_explicitly,
&ctx->redirect)) {
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,
&ctx->cancel_request_explicitly)) {
&ctx->cancel_request_explicitly,
&ctx->redirect)) {
ctx->blocked_by = kAdBlocked;
}
}
@@ -164,11 +93,6 @@ int OnBeforeURLRequest_AdBlockTPPreWork(
return net::OK;
}

if (GetPolyfillForAdBlock(ctx->allow_brave_shields, ctx->allow_ads,
ctx->tab_origin, ctx->request_url, &ctx->new_url_spec)) {
return net::OK;
}

// Most blocked resources have been moved to our ad block lists.
// This is only for special cases like the PDFjs ping which can
// occur before the ad block lists are fully loaded.
@@ -1,9 +1,12 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_NET_BRAVE_AD_BLOCK_TP_NETWORK_DELEGATE_H_
#define BRAVE_BROWSER_NET_BRAVE_AD_BLOCK_TP_NETWORK_DELEGATE_H_
#ifndef BRAVE_BROWSER_NET_BRAVE_AD_BLOCK_TP_NETWORK_DELEGATE_HELPER_H_
#define BRAVE_BROWSER_NET_BRAVE_AD_BLOCK_TP_NETWORK_DELEGATE_HELPER_H_

#include <memory>

#include "brave/browser/net/url_context.h"

@@ -13,9 +16,6 @@ int OnBeforeURLRequest_AdBlockTPPreWork(
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx);

bool GetPolyfillForAdBlock(bool allow_brave_shields, bool allow_ads,
const GURL& tab_origin, const GURL& gurl, std::string* new_url_spec);

} // namespace brave

#endif // BRAVE_BROWSER_NET_BRAVE_AD_BLOCK_TP_NETWORK_DELEGATE_H_
#endif // BRAVE_BROWSER_NET_BRAVE_AD_BLOCK_TP_NETWORK_DELEGATE_HELPER_H_
@@ -32,18 +32,6 @@ TEST(BraveAdBlockTPNetworkDelegateHelperTest, EmptyRequestURL) {
EXPECT_EQ(rc, net::OK);
}

TEST(BraveAdBlockTPNetworkDelegateHelperTest, RedirectsToStubs) {
const std::vector<const GURL> urls(
{GURL(kGoogleTagManagerPattern), GURL(kGoogleTagServicesPattern)});
for (const auto& url : urls) {
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);
int rc =
OnBeforeURLRequest_AdBlockTPPreWork(ResponseCallback(), request_info);
EXPECT_EQ(rc, net::OK);
EXPECT_TRUE(GURL(request_info->new_url_spec).SchemeIs("data"));
}
}

TEST(BraveAdBlockTPNetworkDelegateHelperTest, Blocking) {
const std::vector<const GURL> urls({
GURL("https://pdfjs.robwu.nl/ping"),
@@ -56,66 +44,3 @@ TEST(BraveAdBlockTPNetworkDelegateHelperTest, Blocking) {
EXPECT_EQ(rc, net::OK);
}
}

TEST(BraveAdBlockTPNetworkDelegateHelperTest, GetPolyfill) {
using brave::GetPolyfillForAdBlock;

const GURL tab_origin("https://test.com");
const GURL google_analytics_url(kGoogleAnalyticsPattern);
const GURL tag_manager_url(kGoogleTagManagerPattern);
const GURL tag_services_url(kGoogleTagServicesPattern);
const GURL normal_url("https://a.com");

std::string out_url_spec;
// Shields up, block ads, google analytics should get polyfill
ASSERT_TRUE(GetPolyfillForAdBlock(true, false, tab_origin,
google_analytics_url, &out_url_spec));
// Shields up, block ads, tag manager should get polyfill
ASSERT_TRUE(GetPolyfillForAdBlock(true, false, tab_origin, tag_manager_url,
&out_url_spec));
// Shields up, block ads, tag services should get polyfill
ASSERT_TRUE(GetPolyfillForAdBlock(true, false, tab_origin, tag_services_url,
&out_url_spec));
// Shields up, block ads, normal URL should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, false, tab_origin, normal_url,
&out_url_spec));

// Shields up, allow ads, google analytics should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, true, tab_origin,
google_analytics_url, &out_url_spec));
// Shields up, allow ads, tag manager should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, true, tab_origin, tag_manager_url,
&out_url_spec));
// Shields up, allow ads, tag services should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, true, tab_origin, tag_services_url,
&out_url_spec));
// Shields up, allow ads, normal URL should NOT get polyfill
ASSERT_FALSE(
GetPolyfillForAdBlock(true, true, tab_origin, normal_url, &out_url_spec));

// Shields down, allow ads, google analytics should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin,
google_analytics_url, &out_url_spec));
// Shields down, allow ads, tag manager should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin, tag_manager_url,
&out_url_spec));
// Shields down, allow ads, tag services should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin, tag_services_url,
&out_url_spec));
// Shields down, allow ads, normal URL should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin, normal_url,
&out_url_spec));

// Shields down, block ads, google analytics should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin,
google_analytics_url, &out_url_spec));
// Shields down, block ads, tag manager should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin, tag_manager_url,
&out_url_spec));
// Shields down, block ads, tag services should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin, tag_services_url,
&out_url_spec));
// Shields down, block ads, normal URL should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin, normal_url,
&out_url_spec));
}
@@ -337,7 +337,8 @@ void BraveProxyingURLLoaderFactory::InProgressRequest::
}
network::ResourceResponseHead response;
std::string response_data;
brave_shields::MakeStubResponse(request_, &response, &response_data);
brave_shields::MakeStubResponse(ctx_->redirect, request_, &response,
&response_data);
This conversation was marked as resolved by bbondy

This comment has been minimized.

Copy link
@iefremov

iefremov Nov 13, 2019

Contributor

non-CC spacing, clang-format it


target_client_->OnReceiveResponse(response);

@@ -90,6 +90,8 @@ struct BraveRequestInfo {
const base::ListValue* referral_headers_list = nullptr;
BlockedBy blocked_by = kNotBlocked;
bool cancel_request_explicitly = false;
std::string redirect;
This conversation was marked as resolved by bbondy

This comment has been minimized.

Copy link
@iefremov

iefremov Nov 14, 2019

Contributor

IMO this name is quite confusing: (1) we don't do any redirects with it (2) we already have new_url below.
So maybe rename it to data_url_response (if this is always a data URL)? Or stub_response?

I'd also prefer using base::Optional but if it looks wordy/inconvenient I'm ok to leave it as is.

This comment has been minimized.

Copy link
@iefremov

iefremov Nov 14, 2019

Contributor

also some human-readable description would be useful

This comment has been minimized.

Copy link
@bbondy

bbondy Nov 18, 2019

Author Member

Yep, the name comes from ABP filter syntax though that we use, and uBlock implements it as a redirect. I'll update it in most places to data_url.


// Default to invalid type for resource_type, so delegate helpers
// can properly detect that the info couldn't be obtained.
// TODO(iefremov): Replace with something like |WebRequestResourceType| to
@@ -44,14 +44,8 @@ const char kCRLSetPrefix3[] =
const char kCRLSetPrefix4[] =
"*://storage.googleapis.com/update-delta/hfnkpimlhhgieaddgfemjhofmfblmnib"
"/*crxd";
const char kGoogleAnalyticsPattern[] =
"https://www.google-analytics.com/analytics.js";
const char kChromeCastPrefix[] =
"*://*.gvt1.com/edgedl/chromewebstore/*pkedcjkdefgpdelpbcmbmeomcjbeemfm*";
const char kGoogleTagManagerPattern[] =
"https://www.googletagmanager.com/gtm.js";
const char kGoogleTagServicesPattern[] =
"https://www.googletagservices.com/tag/js/gpt.js";
const char kForbesPattern[] = "https://www.forbes.com/*";
const char kForbesExtraCookies[] =
"forbes_ab=true; welcomeAd=true; adblock_session=Off; "
@@ -25,9 +25,6 @@ extern const char kEmptyDataURI[];
extern const char kEmptyImageDataURI[];
extern const char kJSDataURLPrefix[];
extern const char kGeoLocationsPattern[];
extern const char kGoogleAnalyticsPattern[];
extern const char kGoogleTagManagerPattern[];
extern const char kGoogleTagServicesPattern[];
extern const char kForbesPattern[];
extern const char kForbesExtraCookies[];
extern const char kSafeBrowsingPrefix[];
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.