-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6762 from brave/onion-location
Handle onion-location HTTP header & .onion domain
- Loading branch information
Showing
30 changed files
with
798 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
Oops, something went wrong.