Skip to content

Commit

Permalink
Make sure loaded PDFs are not 3P to themselves for blocking considera…
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Nov 27, 2018
1 parent 828f316 commit 21bac2a
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 4 deletions.
14 changes: 11 additions & 3 deletions browser/net/brave_ad_block_tp_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -97,15 +98,22 @@ void OnBeforeURLRequestAdBlockTPOnTaskRunner(std::shared_ptr<BraveRequestInfo> 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;
}
Expand Down
1 change: 1 addition & 0 deletions browser/net/url_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void BraveRequestInfo::FillCTXFromRequest(const net::URLRequest* request,
std::shared_ptr<brave::BraveRequestInfo> 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) {
Expand Down
1 change: 1 addition & 0 deletions browser/net/url_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ source_set("common") {
"shield_exceptions.h",
"url_constants.cc",
"url_constants.h",
"url_util.cc",
"url_util.h",
]

public_deps = [
Expand Down
1 change: 1 addition & 0 deletions common/extensions/extension_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
1 change: 1 addition & 0 deletions common/extensions/extension_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
28 changes: 28 additions & 0 deletions common/url_util.cc
Original file line number Diff line number Diff line change
@@ -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 <string>

#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
18 changes: 18 additions & 0 deletions common/url_util.h
Original file line number Diff line number Diff line change
@@ -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_
34 changes: 34 additions & 0 deletions common/url_util_unittest.cc
Original file line number Diff line number Diff line change
@@ -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<GURL> 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
3 changes: 2 additions & 1 deletion test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 21bac2a

Please sign in to comment.