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

Move hardcoded exceptions rules to more specific rules and unify exception handling #1831

Merged
merged 1 commit into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use_relative_paths = True

deps = {
"vendor/ad-block": "https://github.com/brave/ad-block.git@9c8961f2afa623e35c2cd8fd61827e24c2b44ec0",
"vendor/ad-block": "https://github.com/brave/ad-block.git@84f5b7e7ae4de4e18b1e1673198a91ec15507889",
"vendor/tracking-protection": "https://github.com/brave/tracking-protection.git@e67738e656244f7ab6e0ed9815071ca744f5468f",
"vendor/hashset-cpp": "https://github.com/brave/hashset-cpp.git@4b55fe39bb25bb0d8b11a43d547d75f00c6c46fb",
"vendor/bloom-filter-cpp": "https://github.com/brave/bloom-filter-cpp.git@9be5c63b14e094156e00c8b28f205e7794f0b92c",
Expand Down
62 changes: 41 additions & 21 deletions browser/net/brave_ad_block_tp_network_delegate_helper.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* 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/. */

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

#include <memory>
#include <string>

#include "base/base64url.h"
Expand Down Expand Up @@ -45,9 +47,10 @@ std::string GetGoogleTagManagerPolyfillJS() {
return base64_output;
}
std::string str = ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_BRAVE_TAG_MANAGER_POLYFILL).as_string();
IDR_BRAVE_TAG_MANAGER_POLYFILL).as_string();
base64_output.reserve(180);
Base64UrlEncode(str, base::Base64UrlEncodePolicy::OMIT_PADDING, &base64_output);
Base64UrlEncode(str, base::Base64UrlEncodePolicy::OMIT_PADDING,
&base64_output);
base64_output = std::string(kJSDataURLPrefix) + base64_output;
return base64_output;
}
Expand All @@ -60,20 +63,24 @@ std::string GetGoogleTagServicesPolyfillJS() {
std::string str = ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_BRAVE_TAG_SERVICES_POLYFILL).as_string();
base64_output.reserve(4668);
Base64UrlEncode(str, base::Base64UrlEncodePolicy::OMIT_PADDING, &base64_output);
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
// Polyfills which are related to adblock should only apply when shields
// are up.
if (!allow_brave_shields || allow_ads) {
return false;
}

static URLPattern tag_manager(URLPattern::SCHEME_ALL, kGoogleTagManagerPattern);
static URLPattern tag_services(URLPattern::SCHEME_ALL, kGoogleTagServicesPattern);
static URLPattern tag_manager(URLPattern::SCHEME_ALL,
kGoogleTagManagerPattern);
static URLPattern tag_services(URLPattern::SCHEME_ALL,
kGoogleTagServicesPattern);
if (tag_manager.MatchesURL(gurl)) {
std::string&& data_url = GetGoogleTagManagerPolyfillJS();
*new_url_spec = data_url;
Expand All @@ -89,27 +96,38 @@ bool GetPolyfillForAdBlock(bool allow_brave_shields, bool allow_ads,
return false;
}

void OnBeforeURLRequestAdBlockTPOnTaskRunner(std::shared_ptr<BraveRequestInfo> ctx) {
void OnBeforeURLRequestAdBlockTPOnTaskRunner(
std::shared_ptr<BraveRequestInfo> ctx) {
// If the following info isn't available, then proper content settings can't
// be looked up, so do nothing.
if (ctx->tab_origin.is_empty() || !ctx->tab_origin.has_host() ||
ctx->request_url.is_empty()) {
return;
}
DCHECK(ctx->request_identifier != 0);
DCHECK_NE(ctx->request_identifier, 0UL);

bool did_match_exception = false;
std::string tab_host = ctx->tab_origin.host();
if (!g_brave_browser_process->tracking_protection_service()->
ShouldStartRequest(ctx->request_url, ctx->resource_type, tab_host)) {
ctx->new_url_spec = GetBlankDataURLForResourceType(ctx->resource_type).spec();
ctx->blocked_by = kTrackerBlocked;
} else if (!g_brave_browser_process->ad_block_service()->ShouldStartRequest(
ctx->request_url, ctx->resource_type, tab_host) ||
!g_brave_browser_process->ad_block_regional_service()
->ShouldStartRequest(ctx->request_url, ctx->resource_type,
tab_host)) {
ctx->new_url_spec = GetBlankDataURLForResourceType(ctx->resource_type).spec();
if (!g_brave_browser_process->ad_block_service()->ShouldStartRequest(
ctx->request_url, ctx->resource_type, tab_host,
&did_match_exception)) {
ctx->new_url_spec = GetBlankDataURLForResourceType(
ctx->resource_type).spec();
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)) {
ctx->new_url_spec =
GetBlankDataURLForResourceType(ctx->resource_type).spec();
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)) {
ctx->new_url_spec =
GetBlankDataURLForResourceType(ctx->resource_type).spec();
ctx->blocked_by = kTrackerBlocked;
}
}

Expand Down Expand Up @@ -147,14 +165,16 @@ int OnBeforeURLRequest_AdBlockTPPreWork(
}

// These should probably move to our ad block lists
if (IsEmptyDataURLRedirect(ctx->request_url) || IsBlockedResource(ctx->request_url)) {
if (IsEmptyDataURLRedirect(ctx->request_url) ||
IsBlockedResource(ctx->request_url)) {
ctx->new_url_spec = kEmptyDataURI;
return net::OK;
}

// If the following info isn't available, then proper content settings can't
// be looked up, so do nothing.
if (ctx->tab_origin.is_empty() || !ctx->allow_brave_shields || ctx->allow_ads ||
if (ctx->tab_origin.is_empty() || !ctx->allow_brave_shields ||
ctx->allow_ads ||
ctx->resource_type == content::RESOURCE_TYPE_LAST_TYPE) {
return net::OK;
}
Expand Down
30 changes: 22 additions & 8 deletions components/brave_shields/browser/ad_block_base_service.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* 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/. */

Expand Down Expand Up @@ -28,7 +29,7 @@ namespace {

FilterOption ResourceTypeToFilterOption(content::ResourceType resource_type) {
FilterOption filter_option = FONoFilterOption;
switch(resource_type) {
switch (resource_type) {
// top level page
case content::RESOURCE_TYPE_MAIN_FRAME:
filter_option = FODocument;
Expand Down Expand Up @@ -113,8 +114,8 @@ void AdBlockBaseService::Cleanup() {
}

bool AdBlockBaseService::ShouldStartRequest(const GURL& url,
content::ResourceType resource_type,
const std::string& tab_host) {
content::ResourceType resource_type, const std::string& tab_host,
bool* did_match_exception) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
FilterOption current_option = ResourceTypeToFilterOption(resource_type);

Expand All @@ -129,15 +130,23 @@ bool AdBlockBaseService::ShouldStartRequest(const GURL& url,
current_option = static_cast<FilterOption>(current_option | FOThirdParty);
}

Filter* matching_exception_filter = nullptr;
if (ad_block_client_->matches(url.spec().c_str(),
current_option,
tab_host.c_str())) {
// LOG(ERROR) << "AdBlockBaseService::ShouldStartRequest(), host: " << tab_host
current_option, tab_host.c_str(), nullptr,
&matching_exception_filter)) {
// We'd only possibly match an exception filter if we're returning true.
*did_match_exception = false;
// LOG(ERROR) << "AdBlockBaseService::ShouldStartRequest(), host: "
// << tab_host
// << ", resource type: " << resource_type
// << ", url.spec(): " << url.spec();
return false;
}

if (did_match_exception) {
*did_match_exception = !!matching_exception_filter;
}

return true;
}

Expand All @@ -155,7 +164,8 @@ void AdBlockBaseService::OnDATFileDataReady() {
return;
}
ad_block_client_.reset(new AdBlockClient());
if (!ad_block_client_->deserialize((char*)&buffer_.front())) {
if (!ad_block_client_->deserialize(
reinterpret_cast<char*>(&buffer_.front()))) {
ad_block_client_.reset();
LOG(ERROR) << "Failed to deserialize ad block data";
return;
Expand All @@ -166,6 +176,10 @@ bool AdBlockBaseService::Init() {
return true;
}

AdBlockClient* AdBlockBaseService::GetAdBlockClientForTest() {
return ad_block_client_.get();
}

///////////////////////////////////////////////////////////////////////////////

} // namespace brave_shields
11 changes: 7 additions & 4 deletions components/brave_shields/browser/ad_block_base_service.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* 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/. */

Expand All @@ -19,6 +20,7 @@
#include "content/public/common/resource_type.h"

class AdBlockClient;
class AdBlockServiceTest;

namespace brave_shields {

Expand All @@ -29,15 +31,16 @@ class AdBlockBaseService : public BaseBraveShieldsService {
AdBlockBaseService();
~AdBlockBaseService() override;

bool ShouldStartRequest(const GURL &url,
content::ResourceType resource_type,
const std::string& tab_host) override;
bool ShouldStartRequest(const GURL &url, content::ResourceType resource_type,
const std::string& tab_host, bool* matching_exception_filter) override;

protected:
friend class ::AdBlockServiceTest;
bool Init() override;
void Cleanup() override;

void GetDATFileData(const base::FilePath& dat_file_path);
AdBlockClient* GetAdBlockClientForTest();

std::unique_ptr<AdBlockClient> ad_block_client_;
DATFileDataBuffer buffer_;
Expand Down
15 changes: 7 additions & 8 deletions components/brave_shields/browser/ad_block_service.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* 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/. */

Expand All @@ -14,15 +15,13 @@
#include "brave/components/brave_shields/browser/ad_block_base_service.h"
#include "content/public/common/resource_type.h"

class AdBlockClient;
class AdBlockServiceTest;

namespace brave_shields {

const std::string kAdBlockComponentName("Brave Ad Block Updater");
const std::string kAdBlockComponentId("cffkpbalmllkdoenhmdmpbkajipdjfam");

const std::string kAdBlockComponentBase64PublicKey =
const char kAdBlockComponentName[] = "Brave Ad Block Updater";
const char kAdBlockComponentId[] = "cffkpbalmllkdoenhmdmpbkajipdjfam";
const char kAdBlockComponentBase64PublicKey[] =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs0qzJmHSgIiw7IGFCxij"
"1NnB5hJ5ZQ1LKW9htL4EBOaMJvmqaDs/wfq0nw/goBHWsqqkMBynRTu2Hxxirvdb"
"cugn1Goys5QKPgAvKwDHJp9jlnADWm5xQvPQ4GE1mK1/I3ka9cEOCzPW6GI+wGLi"
Expand All @@ -34,8 +33,8 @@ const std::string kAdBlockComponentBase64PublicKey =
// The brave shields service in charge of ad-block checking and init.
class AdBlockService : public AdBlockBaseService {
public:
AdBlockService();
~AdBlockService() override;
AdBlockService();
~AdBlockService() override;

protected:
bool Init() override;
Expand Down
Loading