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 448c08296960..ce509b7ad8eb 100644 --- a/browser/net/brave_ad_block_tp_network_delegate_helper.cc +++ b/browser/net/brave_ad_block_tp_network_delegate_helper.cc @@ -11,6 +11,7 @@ #include "brave/browser/brave_browser_process_impl.h" #include "brave/common/network_constants.h" #include "brave/common/shield_exceptions.h" +#include "brave/common/url_util.h" #include "brave/components/brave_shields/browser/ad_block_regional_service.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/browser/brave_shields_util.h" @@ -97,15 +98,22 @@ void OnBeforeURLRequestAdBlockTPOnTaskRunner(std::shared_ptr c return; } DCHECK(ctx->request_identifier != 0); + + // For PDFJS we don't want to treat 2 different URLs as 3p for the main pdf + // load which is meant to be top level + // chrome-extension://oemmndcbldboiebfnladdacbdfmadadm/https://developer.att.com/.../file.pdf + // https://developer.att.com/.../file.pdf + // So if the tab origin is chrome-extension, set it to that of the PDF only for PDFJS + std::string tab_host = brave::GetURLOrPDFURL(ctx->tab_url).host(); if (!g_brave_browser_process->tracking_protection_service()-> - ShouldStartRequest(ctx->request_url, ctx->resource_type, ctx->tab_origin.host())) { + 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, ctx->tab_origin.host()) || + ctx->request_url, ctx->resource_type, tab_host) || !g_brave_browser_process->ad_block_regional_service() ->ShouldStartRequest(ctx->request_url, ctx->resource_type, - ctx->tab_origin.host())) { + tab_host)) { ctx->new_url_spec = GetBlankDataURLForResourceType(ctx->resource_type).spec(); ctx->blocked_by = kAdBlocked; } diff --git a/browser/net/url_context.cc b/browser/net/url_context.cc index bdae0568349c..79afcf8c0847 100644 --- a/browser/net/url_context.cc +++ b/browser/net/url_context.cc @@ -23,6 +23,7 @@ void BraveRequestInfo::FillCTXFromRequest(const net::URLRequest* request, std::shared_ptr ctx) { ctx->request_identifier = request->identifier(); ctx->request_url = request->url(); + ctx->tab_url = request->site_for_cookies(); ctx->tab_origin = request->site_for_cookies().GetOrigin(); auto* request_info = content::ResourceRequestInfo::ForRequest(request); if (request_info) { diff --git a/browser/net/url_context.h b/browser/net/url_context.h index ec71bfd80d3a..ebee381a407f 100644 --- a/browser/net/url_context.h +++ b/browser/net/url_context.h @@ -51,6 +51,7 @@ struct BraveRequestInfo { ~BraveRequestInfo(); GURL request_url; GURL tab_origin; + GURL tab_url; std::string new_url_spec; bool allow_brave_shields = true; bool allow_ads = false; diff --git a/common/BUILD.gn b/common/BUILD.gn index c194b5a5a8f0..e6087d5df90b 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -49,6 +49,8 @@ source_set("common") { "shield_exceptions.h", "url_constants.cc", "url_constants.h", + "url_util.cc", + "url_util.h", ] public_deps = [ diff --git a/common/extensions/extension_constants.cc b/common/extensions/extension_constants.cc index bebb46369019..8208f67e2ea5 100644 --- a/common/extensions/extension_constants.cc +++ b/common/extensions/extension_constants.cc @@ -14,3 +14,4 @@ const char crl_set_extension_id[] = "hfnkpimlhhgieaddgfemjhofmfblmnib"; const char pdfjs_extension_id[] = "oemmndcbldboiebfnladdacbdfmadadm"; const char pdfjs_extension_name[] = "PDF Viewer (PDF.js)"; const char pdfjs_extension_public_key[] = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDb5PIb8ayK6vHvEIY1nJKRSCDE8iJ1T43qFN+5dvCVQrmyEkgqB9ZuZNT24Lwot96HV51VoITHKRNIVKI2Nrbfn0M49t7qtaP34g/GXJ7mAIbSzsY4+i+Wsz8EL2SNEIw6uH8RmXG7nZ29NJ7sk7jn17QmMsO2UJ01UT8hfOOOEQIDAQAB"; +const char pdfjs_extension_origin[] = "chrome-extension://oemmndcbldboiebfnladdacbdfmadadm/"; diff --git a/common/extensions/extension_constants.h b/common/extensions/extension_constants.h index 4ab3451b655c..185ab03026cf 100644 --- a/common/extensions/extension_constants.h +++ b/common/extensions/extension_constants.h @@ -12,3 +12,4 @@ extern const char crl_set_extension_id[]; extern const char pdfjs_extension_id[]; extern const char pdfjs_extension_name[]; extern const char pdfjs_extension_public_key[]; +extern const char pdfjs_extension_origin[]; diff --git a/common/url_util.cc b/common/url_util.cc new file mode 100644 index 000000000000..9609f687998f --- /dev/null +++ b/common/url_util.cc @@ -0,0 +1,28 @@ +/* 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/common/url_util.h" + +#include + +#include "brave/common/extensions/extension_constants.h" +#include "url/gurl.h" + +namespace brave { + +GURL GetURLOrPDFURL(const GURL& url) { + if (url.SchemeIs("chrome-extension") && + url.host() == pdfjs_extension_id) { + static size_t pdfjs_substring_len = std::string(pdfjs_extension_origin).length(); + size_t http_pos = url.spec().find(std::string(pdfjs_extension_origin) + "http://"); + size_t https_pos = url.spec().find(std::string(pdfjs_extension_origin) + "https://"); + if (http_pos != std::string::npos || https_pos != std::string::npos) { + return GURL(url.spec().substr(pdfjs_substring_len, + url.spec().length() - pdfjs_substring_len)); + } + } + return url; +} + +} // namespace brave diff --git a/common/url_util.h b/common/url_util.h new file mode 100644 index 000000000000..2f8a15ab5eda --- /dev/null +++ b/common/url_util.h @@ -0,0 +1,18 @@ +/* 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_COMMON_URL_UTIL_H_ +#define BRAVE_COMMON_URL_UTIL_H_ + +class GURL; + +namespace brave { + +// Returns the location of the PDF if this URL is a PDFJS extension URL. +// Otherwise simply just returns the same URL as passed in. +GURL GetURLOrPDFURL(const GURL& url); + +} // namespace brave + +#endif // BRAVE_COMMON_URL_UTIL_H_ diff --git a/common/url_util_unittest.cc b/common/url_util_unittest.cc new file mode 100644 index 000000000000..673ab9c24e3b --- /dev/null +++ b/common/url_util_unittest.cc @@ -0,0 +1,34 @@ +/* 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/common/url_util.h" +#include "chrome/test/base/chrome_render_view_host_test_harness.h" +#include "url/gurl.h" + +typedef testing::Test BraveUrlUtilTest; + +namespace brave { + +TEST_F(BraveUrlUtilTest, GetURLOrPDFURL) { + std::vector unchanged_urls({ + // PDFJS URL but not to a PDF + GURL("chrome-extension://oemmndcbldboiebfnladdacbdfmadadm/test.html"), + // PDFJS ID but not chrome-extension scheme + GURL("chrome://oemmndcbldboiebfnladdacbdfmadadm/https://test.html"), + // Not PDFJS ID but format of a PDFJS PDF URL + GURL("chrome-extension://aaamndcbldboiebfnladdacbdfmadaaa/https://example.com/test.html"), + // Random other URL + GURL("https://example.com") + }); + std::for_each(unchanged_urls.begin(), unchanged_urls.end(), + [this](GURL url){ + EXPECT_EQ(brave::GetURLOrPDFURL(url), url); + }); + EXPECT_EQ(brave::GetURLOrPDFURL(GURL("chrome-extension://oemmndcbldboiebfnladdacbdfmadadm/http://example.com?test")), + GURL("http://example.com?test")); + EXPECT_EQ(brave::GetURLOrPDFURL(GURL("chrome-extension://oemmndcbldboiebfnladdacbdfmadadm/https://example.com?test")), + GURL("https://example.com?test")); +} + +} // namespace diff --git a/test/BUILD.gn b/test/BUILD.gn index 1cbb1d4eb6be..60fd688f9a26 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -50,7 +50,7 @@ test("brave_unit_tests") { "//brave/browser/resources/settings/reset_report_uploader_unittest.cc", "//brave/chromium_src/chrome/browser/external_protocol/external_protocol_handler_unittest.cc", "//brave/chromium_src/chrome/browser/signin/account_consistency_disabled_unittest.cc", - "//brave\chromium_src/chrome/browser/ui/bookmarks/brave_bookmark_context_menu_controller_unittest.cc", + "//brave/chromium_src/chrome/browser/ui/bookmarks/brave_bookmark_context_menu_controller_unittest.cc", "//brave/chromium_src/components/search_engines/brave_template_url_prepopulate_data_unittest.cc", "//brave/chromium_src/components/search_engines/brave_template_url_service_util_unittest.cc", "//brave/chromium_src/components/version_info/brave_version_info_unittest.cc", @@ -59,6 +59,7 @@ test("brave_unit_tests") { "//brave/common/shield_exceptions_unittest.cc", "//brave/common/tor/tor_test_constants.cc", "//brave/common/tor/tor_test_constants.h", + "//brave/common/url_util_unittest.cc", "//brave/components/assist_ranker/ranker_model_loader_impl_unittest.cc", "//brave/components/brave_shields/browser/ad_block_regional_service_unittest.cc", "//brave/components/brave_sync/bookmark_order_util_unittest.cc",