Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block .onion requests in non-Tor window (uplift to 1.20.x) #8051

Merged
merged 2 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions browser/tor/onion_location_navigation_throttle_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "components/prefs/pref_service.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"

Expand Down Expand Up @@ -113,6 +114,28 @@ IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest,
EXPECT_TRUE(helper->onion_location().is_empty());
}

IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest,
OnionDomain) {
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
content::TestNavigationObserver nav_observer(web_contents);
ui_test_utils::NavigateToURL(browser(), GURL(kTestOnionURL));
nav_observer.Wait();
// Original request was blocked
EXPECT_EQ(nav_observer.last_net_error_code(), net::ERR_BLOCKED_BY_CLIENT);
tor::OnionLocationTabHelper* helper =
tor::OnionLocationTabHelper::FromWebContents(web_contents);
EXPECT_TRUE(helper->should_show_icon());
EXPECT_EQ(helper->onion_location(), GURL(kTestOnionURL));
CheckOnionLocationLabel(browser());

ui_test_utils::NavigateToURL(browser(), GURL(kTestNotOnionURL));
web_contents = browser()->tab_strip_model()->GetActiveWebContents();
helper = tor::OnionLocationTabHelper::FromWebContents(web_contents);
EXPECT_FALSE(helper->should_show_icon());
EXPECT_TRUE(helper->onion_location().is_empty());
}

IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest,
OnionDomain_AutoOnionRedirect) {
browser()->profile()->GetPrefs()->SetBoolean(tor::prefs::kAutoOnionRedirect,
Expand All @@ -121,17 +144,29 @@ IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest,
ui_test_utils::NavigateToURL(browser(), GURL("https://brave.com"));
EXPECT_EQ(1U, browser_list->size());
ASSERT_FALSE(browser_list->get(0)->profile()->IsTor());
ASSERT_EQ(browser(), browser_list->get(0));

content::WindowedNotificationObserver tor_browser_creation_observer(
chrome::NOTIFICATION_BROWSER_OPENED,
content::NotificationService::AllSources());
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
content::TestNavigationObserver nav_observer(web_contents);
ui_test_utils::NavigateToURL(browser(), GURL(kTestOnionURL));
tor_browser_creation_observer.Wait();
nav_observer.Wait();
// Original request was blocked
EXPECT_EQ(nav_observer.last_net_error_code(), net::ERR_BLOCKED_BY_CLIENT);
EXPECT_EQ(2U, browser_list->size());
ASSERT_TRUE(browser_list->get(1)->profile()->IsTor());
content::WebContents* web_contents =
browser_list->get(1)->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(web_contents->GetURL(), GURL(kTestOnionURL));
Browser* tor_browser = browser_list->get(1);
ASSERT_TRUE(tor_browser->profile()->IsTor());
content::WebContents* tor_web_contents =
tor_browser->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(tor_web_contents->GetURL(), GURL(kTestOnionURL));
// We don't close the original tab
EXPECT_EQ(browser()->tab_strip_model()->count(), 1);
// No new tab in Tor window
EXPECT_EQ(tor_browser->tab_strip_model()->count(), 1);
}

IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest,
Expand Down Expand Up @@ -159,7 +194,7 @@ IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest,
GURL url = test_server()->GetURL("/onion");
ui_test_utils::NavigateToURL(browser(), url);
tor_browser_creation_observer.Wait();
// Last tab will not be closed
// We don't close the original tab
EXPECT_EQ(browser()->tab_strip_model()->count(), 1);
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
Expand All @@ -175,17 +210,19 @@ IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest,
web_contents = tor_browser->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(web_contents->GetURL(), GURL(kTestOnionURL));

// Open a new tab
// Open a new tab and navigate to the url again
NavigateParams params(
NavigateParams(browser(), url, ui::PAGE_TRANSITION_TYPED));
params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
ui_test_utils::NavigateToURL(&params);

EXPECT_EQ(browser()->tab_strip_model()->count(), 1);
// We stll don't close the original tab
EXPECT_EQ(browser()->tab_strip_model()->count(), 2);

EXPECT_EQ(2U, browser_list->size());
web_contents = tor_browser->tab_strip_model()->GetWebContentsAt(2);
EXPECT_EQ(tor_browser->tab_strip_model()->count(), 3);
// No new tab in Tor window and unique one tab per site
EXPECT_EQ(tor_browser->tab_strip_model()->count(), 1);
web_contents = tor_browser->tab_strip_model()->GetWebContentsAt(0);
EXPECT_EQ(web_contents->GetURL(), GURL(kTestOnionURL));
}

Expand Down
9 changes: 2 additions & 7 deletions browser/tor/onion_location_navigation_throttle_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ void OnTorProfileCreated(GURL onion_location,
Profile::CreateStatus status) {
if (status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED)
return;
Browser* browser = chrome::FindTabbedBrowser(profile, true);
Browser* browser = chrome::FindTabbedBrowser(profile, false);
if (!browser)
return;
content::OpenURLParams open_tor(onion_location, content::Referrer(),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
WindowOpenDisposition::SWITCH_TO_TAB,
ui::PAGE_TRANSITION_TYPED, false);
browser->OpenURL(open_tor);
}
Expand All @@ -46,11 +46,6 @@ void OnionLocationNavigationThrottleDelegate::OpenInTorWindow(
Profile::FromBrowserContext(web_contents->GetBrowserContext());
TorProfileManager::SwitchToTorProfile(profile,
base::BindRepeating(&OnTorProfileCreated, std::move(onion_location)));

// We do not close last tab of the window
Browser* browser = chrome::FindBrowserWithProfile(profile);
if (browser && browser->tab_strip_model()->count() > 1)
web_contents->ClosePage();
}

} // namespace tor
2 changes: 1 addition & 1 deletion browser/ui/views/location_bar/onion_location_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void OnTorProfileCreated(GURL onion_location,
if (!browser)
return;
content::OpenURLParams open_tor(onion_location, content::Referrer(),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
WindowOpenDisposition::SWITCH_TO_TAB,
ui::PAGE_TRANSITION_TYPED, false);
browser->OpenURL(open_tor);
}
Expand Down
20 changes: 14 additions & 6 deletions components/tor/onion_location_navigation_throttle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,22 @@ OnionLocationNavigationThrottle::WillProcessResponse() {

content::NavigationThrottle::ThrottleCheckResult
OnionLocationNavigationThrottle::WillStartRequest() {
// Open .onion site in Tor window
// If a user enters .onion address in non-Tor window, we block the request and
// offer "Open in Tor" button or automatically opening it in Tor window.
if (!is_tor_profile_) {
GURL url = navigation_handle()->GetURL();
if (url.SchemeIsHTTPOrHTTPS() && url.DomainIs("onion") &&
pref_service_->GetBoolean(prefs::kAutoOnionRedirect)) {
delegate_->OpenInTorWindow(navigation_handle()->GetWebContents(),
std::move(url));
return content::NavigationThrottle::CANCEL_AND_IGNORE;
if (url.SchemeIsHTTPOrHTTPS() && url.DomainIs("onion")) {
if (pref_service_->GetBoolean(prefs::kAutoOnionRedirect)) {
delegate_->OpenInTorWindow(navigation_handle()->GetWebContents(),
std::move(url));
} else {
OnionLocationTabHelper::SetOnionLocation(
navigation_handle()->GetWebContents(), url);
}
return content::NavigationThrottle::BLOCK_REQUEST;
} else {
OnionLocationTabHelper::SetOnionLocation(
navigation_handle()->GetWebContents(), GURL());
}
}
return content::NavigationThrottle::PROCEED;
Expand Down