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 google analytics polyfill, fixes brave/brave-browser#4402 (uplift to 0.66.x) #2736

Merged
merged 1 commit into from
Jun 24, 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
1 change: 1 addition & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
</if>
</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>
Expand Down
50 changes: 50 additions & 0 deletions app/resources/js/google_analytics_polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(function() {
// https://developers.google.com/analytics/devguides/collection/analyticsjs/
var noopfn = function() {
;
};
var noopnullfn = function() {
return null;
};
//
var Tracker = function() {
;
};
var p = Tracker.prototype;
p.get = noopfn;
p.set = noopfn;
p.send = noopfn;
//
var w = window,
gaName = w.GoogleAnalyticsObject || 'ga';
var ga = function() {
var len = arguments.length;
if ( len === 0 ) {
return;
}
var f = arguments[len-1];
if ( typeof f !== 'object' || f === null || typeof f.hitCallback !== 'function' ) {
return;
}
try {
f.hitCallback();
} catch (ex) {
}
};
ga.create = function() {
return new Tracker();
};
ga.getByName = noopnullfn;
ga.getAll = function() {
return [];
};
ga.remove = noopfn;
// https://github.com/uBlockOrigin/uAssets/issues/2107
ga.loaded = true;
w[gaName] = ga;
// https://github.com/gorhill/uBlock/issues/3075
var dl = w.dataLayer;
if ( dl instanceof Object && dl.hide instanceof Object && typeof dl.hide.end === 'function' ) {
dl.hide.end();
}
})();
23 changes: 21 additions & 2 deletions browser/net/brave_ad_block_tp_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,26 @@ 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();
base64_output.reserve(180);
Base64UrlEncode(str, base::Base64UrlEncodePolicy::OMIT_PADDING,
&base64_output);
base64_output = std::string(kJSDataURLPrefix) + base64_output;
Expand All @@ -51,7 +63,6 @@ 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);
base64_output = std::string(kJSDataURLPrefix) + base64_output;
Expand All @@ -66,10 +77,18 @@ bool GetPolyfillForAdBlock(bool allow_brave_shields, bool 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;
Expand Down
104 changes: 69 additions & 35 deletions browser/net/brave_ad_block_tp_network_delegate_helper_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/* 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/. */
/* Copyright (c) 2019 The Brave Software Team. Distributed under the MPL2
* license. 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 <vector>

#include "brave/browser/net/url_context.h"
#include "brave/common/network_constants.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
Expand Down Expand Up @@ -40,12 +45,13 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, NoChangeURL) {
TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::BraveRequestInfo>
brave_request_info(new brave::BraveRequestInfo());
brave::BraveRequestInfo::FillCTXFromRequest(request.get(), brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret =
OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(), brave_request_info);
int ret = OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
EXPECT_TRUE(brave_request_info->new_url_spec.empty());
EXPECT_EQ(ret, net::OK);
}
Expand All @@ -57,12 +63,13 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, EmptyRequestURL) {
TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::BraveRequestInfo>
brave_request_info(new brave::BraveRequestInfo());
brave::BraveRequestInfo::FillCTXFromRequest(request.get(), brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret =
OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(), brave_request_info);
int ret = OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
EXPECT_TRUE(brave_request_info->new_url_spec.empty());
EXPECT_EQ(ret, net::OK);
}
Expand All @@ -81,12 +88,13 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, RedirectsToStubs) {
TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::BraveRequestInfo>
brave_request_info(new brave::BraveRequestInfo());
brave::BraveRequestInfo::FillCTXFromRequest(request.get(), brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret =
OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(), brave_request_info);
int ret = OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
EXPECT_EQ(ret, net::OK);
EXPECT_TRUE(GURL(brave_request_info->new_url_spec).SchemeIs("data"));
});
Expand All @@ -103,50 +111,76 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, Blocking) {
TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::BraveRequestInfo>
brave_request_info(new brave::BraveRequestInfo());
brave::BraveRequestInfo::FillCTXFromRequest(request.get(), brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret =
OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(), brave_request_info);
int ret = OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
EXPECT_STREQ(brave_request_info->new_url_spec.c_str(), kEmptyDataURI);
EXPECT_EQ(ret, net::OK);
});
}

TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, GetPolyfill) {
GURL tab_origin("https://test.com");
GURL google_analytics_url(kGoogleAnalyticsPattern);
GURL tag_manager_url(kGoogleTagManagerPattern);
GURL tag_services_url(kGoogleTagServicesPattern);
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin, normal_url,
&out_url_spec));
}

} // namespace
2 changes: 2 additions & 0 deletions common/network_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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 kGoogleTagManagerPattern[] =
"https://www.googletagmanager.com/gtm.js";
const char kGoogleTagServicesPattern[] =
Expand Down
1 change: 1 addition & 0 deletions common/network_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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[];
Expand Down