Skip to content

Commit

Permalink
Merge pull request #6762 from brave/onion-location
Browse files Browse the repository at this point in the history
Handle onion-location HTTP header & .onion domain
  • Loading branch information
darkdh authored Oct 14, 2020
2 parents 54fdb85 + 008196f commit 3728080
Show file tree
Hide file tree
Showing 30 changed files with 798 additions and 37 deletions.
12 changes: 12 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@
<message name="IDS_SETTINGS_ENABLE_TOR_DESC" desc="Text fragment for enabling tor component">
Tor hides your IP address from the sites you visit.
</message>
<message name="IDS_SETTINGS_AUTO_ONION_LOCATION_TITLE" desc="Text fragment for onion site component">
Automatically redirect .onion sites
</message>
<message name="IDS_SETTINGS_AUTO_ONION_LOCATION_DESC" desc="Text fragment for onion site component">
Brave will open onion service of the website in Tor window when available
</message>
<if expr="is_linux">
<message name="IDS_WIDEVINE_PERMISSION_REQUEST_TEXT_FRAGMENT_INSTALL" desc="Text fragment for Widevine permission request. 'Widevine' is the name of a plugin and should not be translated.">
Install Widevine
Expand Down Expand Up @@ -325,6 +331,12 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_PROFILES_OPEN_TOR_PROFILE_BUTTON" desc="Button in the avatar menu bubble view to open a Tor window.">
Open Tor Window
</message>
<message name="IDS_LOCATION_BAR_OPEN_IN_TOR" desc="Button in location bar to open onion available site in tor window.">
Open in Tor
</message>
<message name="IDS_LOCATION_BAR_ONION_AVAILABLE" desc="Button in location bar to indicate onion available site to open a new tab in tor window.">
Onion Available
</message>
<if expr="use_titlecase">
<message name="IDS_NEW_OFFTHERECORD_WINDOW_TOR" desc="Title case: The text label of a menu item to open a new off-the-record window with Tor.">
New Private Window with Tor
Expand Down
1 change: 1 addition & 0 deletions app/vector_icons/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ aggregate_vector_icons("brave_vector_icons") {

icons = [
"download_unlock.icon",
"open_in_tor.icon",
"speedreader.icon",
"speedreader_on_active.icon",
"speedreader_on_inactive.icon",
Expand Down
38 changes: 38 additions & 0 deletions app/vector_icons/open_in_tor.icon
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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/.

CANVAS_DIMENSIONS, 12,
MOVE_TO, 11.5f, 10,
R_CUBIC_TO, -0.28f, 0, -0.5f, -0.22f, -0.5f, -0.5f,
V_LINE_TO, 1,
H_LINE_TO, 2.5f,
CUBIC_TO, 2.22f, 1, 2, 0.78f, 2, 0.5f,
R_CUBIC_TO, 0, -0.28f, 0.22f, -0.5f, 0.5f, -0.5f,
H_LINE_TO, 11,
R_CUBIC_TO, 0.55f, 0, 1, 0.45f, 1, 1,
R_V_LINE_TO, 8.5f,
R_CUBIC_TO, 0, 0.28f, -0.22f, 0.5f, -0.5f, 0.5f,
CLOSE,
MOVE_TO, 10, 3,
R_V_LINE_TO, 8,
R_CUBIC_TO, 0, 0.55f, -0.45f, 1, -1, 1,
H_LINE_TO, 1,
R_CUBIC_TO, -0.55f, 0, -1, -0.45f, -1, -1,
V_LINE_TO, 3,
R_CUBIC_TO, 0, -0.55f, 0.45f, -1, 1, -1,
R_H_LINE_TO, 8,
R_CUBIC_TO, 0.55f, 0, 1, 0.45f, 1, 1,
CLOSE,
R_MOVE_TO, -9, 8,
R_H_LINE_TO, 8,
R_LINE_TO, 0, -6,
H_LINE_TO, 1,
R_V_LINE_TO, 6,
CLOSE,
R_MOVE_TO, 0, -7,
R_H_LINE_TO, 8,
V_LINE_TO, 3,
H_LINE_TO, 1,
R_V_LINE_TO, 1,
CLOSE
6 changes: 6 additions & 0 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ using extensions::ChromeContentBrowserClientExtensionsPart;
#endif

#if BUILDFLAG(ENABLE_TOR)
#include "brave/browser/tor/onion_location_navigation_throttle.h"
#include "brave/browser/tor/tor_navigation_throttle.h"
#endif

Expand Down Expand Up @@ -460,6 +461,11 @@ BraveContentBrowserClient::CreateThrottlesForNavigation(
tor::TorNavigationThrottle::MaybeCreateThrottleFor(handle);
if (tor_navigation_throttle)
throttles.push_back(std::move(tor_navigation_throttle));
std::unique_ptr<content::NavigationThrottle>
onion_location_navigation_throttle =
tor::OnionLocationNavigationThrottle::MaybeCreateThrottleFor(handle);
if (onion_location_navigation_throttle)
throttles.push_back(std::move(onion_location_navigation_throttle));
#endif

#if BUILDFLAG(IPFS_ENABLED)
Expand Down
9 changes: 9 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "brave/browser/search/ntp_utils.h"
#include "brave/browser/themes/brave_dark_mode_utils.h"
#include "brave/browser/tor/buildflags.h"
#include "brave/browser/ui/omnibox/brave_omnibox_client_impl.h"
#include "brave/common/pref_names.h"
#include "brave/components/binance/browser/buildflags/buildflags.h"
Expand Down Expand Up @@ -87,6 +88,10 @@
#include "brave/components/crypto_dot_com/common/pref_names.h"
#endif

#if BUILDFLAG(ENABLE_TOR)
#include "brave/browser/tor/tor_profile_service.h"
#endif

#if defined(OS_ANDROID)
#include "components/feed/core/shared_prefs/pref_names.h"
#include "components/ntp_tiles/pref_names.h"
Expand Down Expand Up @@ -297,6 +302,10 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
crypto_dot_com::RegisterPrefs(registry);
#endif

#if BUILDFLAG(ENABLE_TOR)
tor::TorProfileService::RegisterPrefs(registry);
#endif

#if !defined(OS_ANDROID)
BraveOmniboxClientImpl::RegisterPrefs(registry);
#endif
Expand Down
2 changes: 2 additions & 0 deletions browser/brave_tab_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

#if BUILDFLAG(ENABLE_TOR)
#include "brave/browser/tor/tor_tab_helper.h"
#include "brave/browser/tor/onion_location_tab_helper.h"
#endif

#if BUILDFLAG(IPFS_ENABLED)
Expand Down Expand Up @@ -118,6 +119,7 @@ void AttachTabHelpers(content::WebContents* web_contents) {

#if BUILDFLAG(ENABLE_TOR)
tor::TorTabHelper::MaybeCreateForWebContents(web_contents);
tor::OnionLocationTabHelper::CreateForWebContents(web_contents);
#endif

#if BUILDFLAG(IPFS_ENABLED)
Expand Down
9 changes: 9 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "brave/browser/extensions/api/settings_private/brave_prefs_util.h"

#include "brave/browser/tor/buildflags.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_rewards/common/pref_names.h"
#include "brave/components/brave_wallet/browser/buildflags/buildflags.h"
Expand Down Expand Up @@ -39,6 +40,10 @@
#include "brave/components/crypto_dot_com/common/pref_names.h"
#endif

#if BUILDFLAG(ENABLE_TOR)
#include "brave/common/tor/pref_names.h"
#endif

namespace extensions {

using ntp_background_images::prefs::kNewTabPageShowBackgroundImage;
Expand Down Expand Up @@ -185,6 +190,10 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetWhitelistedKeys() {
// Omnibox pref
(*s_brave_whitelist)[omnibox::kPreventUrlElisionsInOmnibox] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
#if BUILDFLAG(ENABLE_TOR)
(*s_brave_whitelist)[tor::prefs::kAutoOnionLocation] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
#endif

return *s_brave_whitelist;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@
disabled="[[disableTorOption_]]"
on-settings-boolean-control-change="onTorEnabledChange_">
</settings-toggle-button>
<settings-toggle-button
pref="{{prefs.tor.auto_onion_location}}"
class="cr-row"
label="$i18n{autoOnionLocationLabel}"
sub-label="$i18n{autoOnionLocationDesc}"
disabled="[[!torEnabled_]]">
</settings-toggle-button>
</if>
<settings-toggle-button id="webTorrentEnabled"
class="cr-row"
Expand Down
8 changes: 5 additions & 3 deletions browser/tor/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ source_set("tor") {
"//mojo/public/cpp/bindings",
]

public_deps = [
":buildflags",
]
public_deps = [ ":buildflags" ]

if (enable_tor) {
sources += [
"onion_location_navigation_throttle.cc",
"onion_location_navigation_throttle.h",
"onion_location_tab_helper.cc",
"onion_location_tab_helper.h",
"tor_control.cc",
"tor_control.h",
"tor_launcher_factory.cc",
Expand Down
119 changes: 119 additions & 0 deletions browser/tor/onion_location_navigation_throttle.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* Copyright (c) 2020 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 http://mozilla.org/MPL/2.0/. */

#include "brave/browser/tor/onion_location_navigation_throttle.h"

#include <string>
#include <utility>

#include "base/bind.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/browser/tor/onion_location_tab_helper.h"
#include "brave/browser/tor/tor_profile_service.h"
#include "brave/common/tor/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"

namespace tor {

namespace {

bool GetOnionLocation(const net::HttpResponseHeaders* headers,
std::string* onion_location) {
DCHECK(onion_location);

onion_location->clear();
std::string name = "onion-location";

if (!headers || !headers->EnumerateHeader(nullptr, name, onion_location))
return false;
return true;
}

void OnTorProfileCreated(GURL onion_location,
Profile* profile,
Profile::CreateStatus status) {
if (status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED)
return;
Browser* browser = chrome::FindTabbedBrowser(profile, true);
if (!browser)
return;
content::OpenURLParams open_tor(onion_location, content::Referrer(),
WindowOpenDisposition::OFF_THE_RECORD,
ui::PAGE_TRANSITION_TYPED, false);
browser->OpenURL(open_tor);
}

} // namespace

// static
std::unique_ptr<OnionLocationNavigationThrottle>
OnionLocationNavigationThrottle::MaybeCreateThrottleFor(
content::NavigationHandle* navigation_handle) {
if (tor::TorProfileService::IsTorDisabled() ||
!navigation_handle->IsInMainFrame())
return nullptr;
return std::make_unique<OnionLocationNavigationThrottle>(navigation_handle);
}

OnionLocationNavigationThrottle::OnionLocationNavigationThrottle(
content::NavigationHandle* navigation_handle)
: content::NavigationThrottle(navigation_handle) {
profile_ = Profile::FromBrowserContext(
navigation_handle->GetWebContents()->GetBrowserContext());
}

OnionLocationNavigationThrottle::~OnionLocationNavigationThrottle() {}

content::NavigationThrottle::ThrottleCheckResult
OnionLocationNavigationThrottle::WillProcessResponse() {
auto* headers = navigation_handle()->GetResponseHeaders();
std::string onion_location;
// The webpage defining the Onion-Location header must not be an onionsite.
// https://gitweb.torproject.org/tor-browser-spec.git/plain/proposals/100-onion-location-header.txt
if (headers && GetOnionLocation(headers, &onion_location) &&
!navigation_handle()->GetURL().DomainIs("onion")) {
// If user prefers opening it automatically
if (profile_->GetPrefs()->GetBoolean(prefs::kAutoOnionLocation)) {
profiles::SwitchToTorProfile(
base::BindRepeating(&OnTorProfileCreated, GURL(onion_location)));
// We do not close last tab of the window
Browser* browser = chrome::FindBrowserWithProfile(profile_);
if (browser && browser->tab_strip_model()->count() > 1)
navigation_handle()->GetWebContents()->ClosePage();
} else {
OnionLocationTabHelper::SetOnionLocation(
navigation_handle()->GetWebContents(), GURL(onion_location));
}
} else {
OnionLocationTabHelper::SetOnionLocation(
navigation_handle()->GetWebContents(), GURL());
}
return content::NavigationThrottle::PROCEED;
}

content::NavigationThrottle::ThrottleCheckResult
OnionLocationNavigationThrottle::WillStartRequest() {
// Open .onion site in Tor window
if (!brave::IsTorProfile(profile_)) {
GURL url = navigation_handle()->GetURL();
if (url.SchemeIsHTTPOrHTTPS() && url.DomainIs("onion")) {
profiles::SwitchToTorProfile(
base::BindRepeating(&OnTorProfileCreated, std::move(url)));
return content::NavigationThrottle::CANCEL_AND_IGNORE;
}
}
return content::NavigationThrottle::PROCEED;
}

const char* OnionLocationNavigationThrottle::GetNameForLogging() {
return "OnionLocationNavigationThrottle";
}

} // namespace tor
46 changes: 46 additions & 0 deletions browser/tor/onion_location_navigation_throttle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Copyright (c) 2020 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 http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_
#define BRAVE_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_

#include <memory>

#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/navigation_throttle.h"

class Profile;

namespace content {
class NavigationHandle;
} // namespace content

namespace tor {

class OnionLocationNavigationThrottle : public content::NavigationThrottle {
public:
static std::unique_ptr<OnionLocationNavigationThrottle>
MaybeCreateThrottleFor(content::NavigationHandle* navigation_handle);
explicit OnionLocationNavigationThrottle(
content::NavigationHandle* navigation_handle);
~OnionLocationNavigationThrottle() override;

// content::NavigationThrottle implementation:
ThrottleCheckResult WillProcessResponse() override;
ThrottleCheckResult WillStartRequest() override;
const char* GetNameForLogging() override;

private:
Profile* profile_;

OnionLocationNavigationThrottle(const OnionLocationNavigationThrottle&) =
delete;
OnionLocationNavigationThrottle& operator=(
const OnionLocationNavigationThrottle&) = delete;
};

} // namespace tor

#endif // BRAVE_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_
Loading

0 comments on commit 3728080

Please sign in to comment.