From c560b95bc115ab8b1d90e8231bdb39e91b468297 Mon Sep 17 00:00:00 2001 From: brave-builds Date: Wed, 19 Jun 2019 06:48:04 +0000 Subject: [PATCH] Uplift of #2488 (squashed) to beta --- app/brave_generated_resources.grd | 1 + app/resources/js/google_analytics_polyfill.js | 50 +++++++++ ...ave_ad_block_tp_network_delegate_helper.cc | 23 +++- ...ock_tp_network_delegate_helper_unittest.cc | 104 ++++++++++++------ common/network_constants.cc | 2 + common/network_constants.h | 1 + 6 files changed, 144 insertions(+), 37 deletions(-) create mode 100644 app/resources/js/google_analytics_polyfill.js diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index ca77033ce201..5f21f21ac448 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -435,6 +435,7 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U + diff --git a/app/resources/js/google_analytics_polyfill.js b/app/resources/js/google_analytics_polyfill.js new file mode 100644 index 000000000000..6418e964a55a --- /dev/null +++ b/app/resources/js/google_analytics_polyfill.js @@ -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(); + } +})(); 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 d126cbece4f5..26c608825bb5 100644 --- a/browser/net/brave_ad_block_tp_network_delegate_helper.cc +++ b/browser/net/brave_ad_block_tp_network_delegate_helper.cc @@ -30,6 +30,19 @@ 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) { @@ -37,7 +50,6 @@ std::string GetGoogleTagManagerPolyfillJS() { } 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; @@ -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; @@ -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; diff --git a/browser/net/brave_ad_block_tp_network_delegate_helper_unittest.cc b/browser/net/brave_ad_block_tp_network_delegate_helper_unittest.cc index 45e30fb2da4a..f0d36b20ca3f 100644 --- a/browser/net/brave_ad_block_tp_network_delegate_helper_unittest.cc +++ b/browser/net/brave_ad_block_tp_network_delegate_helper_unittest.cc @@ -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 +#include +#include + #include "brave/browser/net/url_context.h" #include "brave/common/network_constants.h" #include "chrome/test/base/chrome_render_view_host_test_harness.h" @@ -40,12 +45,13 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, NoChangeURL) { TRAFFIC_ANNOTATION_FOR_TESTS); std::shared_ptr 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); } @@ -57,12 +63,13 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, EmptyRequestURL) { TRAFFIC_ANNOTATION_FOR_TESTS); std::shared_ptr 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); } @@ -81,12 +88,13 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, RedirectsToStubs) { TRAFFIC_ANNOTATION_FOR_TESTS); std::shared_ptr 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")); }); @@ -103,12 +111,13 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, Blocking) { TRAFFIC_ANNOTATION_FOR_TESTS); std::shared_ptr 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); }); @@ -116,37 +125,62 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, Blocking) { 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 diff --git a/common/network_constants.cc b/common/network_constants.cc index 179b0c19f28d..8d1dd767d79c 100644 --- a/common/network_constants.cc +++ b/common/network_constants.cc @@ -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[] = diff --git a/common/network_constants.h b/common/network_constants.h index 16ea19ce52fa..d2d8774b66ef 100644 --- a/common/network_constants.h +++ b/common/network_constants.h @@ -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[];