Skip to content

Commit

Permalink
Strip referrer header in xorigin requests from .onion (fixes brave/br…
Browse files Browse the repository at this point in the history
  • Loading branch information
fmarier committed Oct 30, 2021
1 parent 548ee9a commit 977de84
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
12 changes: 12 additions & 0 deletions browser/net/brave_site_hacks_network_delegate_helper_unittest.cc
Expand Up @@ -13,6 +13,7 @@
#include "brave/browser/net/url_context.h"
#include "brave/common/network_constants.h"
#include "net/base/net_errors.h"
#include "net/url_request/url_request_job.h"
#include "testing/gtest/include/gtest/gtest.h"

using brave::ResponseCallback;
Expand Down Expand Up @@ -100,6 +101,17 @@ TEST(BraveSiteHacksNetworkDelegateHelperTest,
}
}

TEST(BraveSiteHacksNetworkDelegateHelperTest, OnionReferrerStripped) {
const GURL original_referrer(
"https://"
"brave4u7jddbv7cyviptqjc7jusxh72uik7zt6adtckl5f4nwy2v72qd.onion/");
const GURL destination("https://brave.com");

auto url = net::URLRequestJob::ComputeReferrerForPolicy(
net::ReferrerPolicy::NEVER_CLEAR, original_referrer, destination);
EXPECT_EQ(url, GURL());
}

TEST(BraveSiteHacksNetworkDelegateHelperTest, QueryStringUntouched) {
const std::vector<const std::string> urls({
"https://example.com/",
Expand Down
25 changes: 25 additions & 0 deletions chromium_src/net/url_request/url_request_job.cc
@@ -0,0 +1,25 @@
/* Copyright 2021 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 https://mozilla.org/MPL/2.0/. */

#include "net/url_request/url_request_job.h"

#define ComputeReferrerForPolicy \
ComputeReferrerForPolicy( \
ReferrerPolicy policy, const GURL& original_referrer, \
const GURL& destination, bool* same_origin_out_for_metrics) { \
if (base::EndsWith(original_referrer.host_piece(), ".onion", \
base::CompareCase::INSENSITIVE_ASCII) && \
!url::Origin::Create(original_referrer) \
.IsSameOriginWith(url::Origin::Create(destination))) { \
return GURL(); \
} \
return ComputeReferrerForPolicy_Chromium( \
policy, original_referrer, destination, same_origin_out_for_metrics); \
} \
GURL URLRequestJob::ComputeReferrerForPolicy_Chromium

#include "../../../../net/url_request/url_request_job.cc"

#undef ComputeReferrerForPolicy
19 changes: 19 additions & 0 deletions chromium_src/net/url_request/url_request_job.h
@@ -0,0 +1,19 @@
/* Copyright 2021 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 https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_NET_URL_REQUEST_URL_REQUEST_JOB_H_
#define BRAVE_CHROMIUM_SRC_NET_URL_REQUEST_URL_REQUEST_JOB_H_

#define ComputeReferrerForPolicy \
ComputeReferrerForPolicy( \
ReferrerPolicy policy, const GURL& original_referrer, \
const GURL& destination, bool* same_origin_out_for_metrics); \
static GURL ComputeReferrerForPolicy_Chromium

#include "../../../../net/url_request/url_request_job.h"

#undef ComputeReferrerForPolicy

#endif // BRAVE_CHROMIUM_SRC_NET_URL_REQUEST_URL_REQUEST_JOB_H_

0 comments on commit 977de84

Please sign in to comment.