From 493addeb06181e5f5cb26b142b4b4597f76ca276 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Tue, 21 Jun 2022 11:45:48 -0700 Subject: [PATCH 1/2] Fix DevToolsWindow using original profile for Tor window --- browser/tor/BUILD.gn | 1 + browser/tor/tor_profile_manager_unittest.cc | 19 ++++++++++++++++--- .../chrome/browser/devtools/devtools_window.h | 17 +++++++++++++++++ ...-browser-devtools-devtools_window.cc.patch | 13 +++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 chromium_src/chrome/browser/devtools/devtools_window.h create mode 100644 patches/chrome-browser-devtools-devtools_window.cc.patch diff --git a/browser/tor/BUILD.gn b/browser/tor/BUILD.gn index d2092c125b6f..be4b90f7897c 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -70,6 +70,7 @@ source_set("unit_tests") { "//brave/components/tor", "//brave/components/tor:test_support", "//brave/components/translate/core/common:buildflags", + "//chrome/browser/devtools", "//chrome/common", "//chrome/test:test_support", "//components/keyed_service/content", diff --git a/browser/tor/tor_profile_manager_unittest.cc b/browser/tor/tor_profile_manager_unittest.cc index 93ecded3e196..544ab7d5c1c0 100644 --- a/browser/tor/tor_profile_manager_unittest.cc +++ b/browser/tor/tor_profile_manager_unittest.cc @@ -11,6 +11,7 @@ #include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h" #include "brave/components/constants/pref_names.h" #include "brave/components/translate/core/common/buildflags.h" +#include "chrome/browser/devtools/devtools_window.h" #include "chrome/common/pref_names.h" #include "chrome/test/base/testing_browser_process.h" #include "chrome/test/base/testing_profile.h" @@ -18,6 +19,7 @@ #include "components/prefs/pref_service.h" #include "components/safe_browsing/core/common/safe_browsing_prefs.h" #include "content/public/test/browser_task_environment.h" +#include "content/public/test/web_contents_tester.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/blink/public/common/peerconnection/webrtc_ip_handling_policy.h" @@ -48,6 +50,10 @@ class TorProfileManagerUnitTest : public testing::Test { Profile* profile() { return profile_; } + Profile* GetProfileForDevToolsWindow(content::WebContents* web_contents) { + return DevToolsWindow::GetProfileForDevToolsWindow(web_contents); + } + private: content::BrowserTaskEnvironment task_environment_; raw_ptr profile_ = nullptr; @@ -65,9 +71,8 @@ TEST_F(TorProfileManagerUnitTest, InitTorProfileUserPrefs) { ASSERT_TRUE(tor_profile->IsTor()); // Check WebRTC IP handling policy. - EXPECT_EQ( - tor_profile->GetPrefs()->GetString(prefs::kWebRTCIPHandlingPolicy), - blink::kWebRTCIPHandlingDisableNonProxiedUdp); + EXPECT_EQ(tor_profile->GetPrefs()->GetString(prefs::kWebRTCIPHandlingPolicy), + blink::kWebRTCIPHandlingDisableNonProxiedUdp); // Check SafeBrowsing status EXPECT_FALSE( @@ -84,3 +89,11 @@ TEST_F(TorProfileManagerUnitTest, InitTorProfileUserPrefs) { translate::prefs::kOfferTranslateEnabled)); #endif } + +TEST_F(TorProfileManagerUnitTest, ProfileForDevToolsWindow) { + Profile* tor_profile = + TorProfileManager::GetInstance().GetTorProfile(profile()); + std::unique_ptr tor_web_contents = + content::WebContentsTester::CreateTestWebContents(tor_profile, nullptr); + EXPECT_EQ(tor_profile, GetProfileForDevToolsWindow(tor_web_contents.get())); +} diff --git a/chromium_src/chrome/browser/devtools/devtools_window.h b/chromium_src/chrome/browser/devtools/devtools_window.h new file mode 100644 index 000000000000..27a0170bf425 --- /dev/null +++ b/chromium_src/chrome/browser/devtools/devtools_window.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2022 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_CHROMIUM_SRC_CHROME_BROWSER_DEVTOOLS_DEVTOOLS_WINDOW_H_ +#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_DEVTOOLS_DEVTOOLS_WINDOW_H_ + +#define HatsNextWebDialogBrowserTest \ + HatsNextWebDialogBrowserTest; \ + friend class TorProfileManagerUnitTest; + +#include "src/chrome/browser/devtools/devtools_window.h" + +#undef HatsNextWebDialogBrowserTest + +#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_DEVTOOLS_DEVTOOLS_WINDOW_H_ diff --git a/patches/chrome-browser-devtools-devtools_window.cc.patch b/patches/chrome-browser-devtools-devtools_window.cc.patch new file mode 100644 index 000000000000..2504a99899d1 --- /dev/null +++ b/patches/chrome-browser-devtools-devtools_window.cc.patch @@ -0,0 +1,13 @@ +diff --git a/chrome/browser/devtools/devtools_window.cc b/chrome/browser/devtools/devtools_window.cc +index 47e5047e23b2e423cb27ee9e4b7bb0c9386c7169..4e7077ee4a7cec5c4088e40134d076ab22e43bce 100644 +--- a/chrome/browser/devtools/devtools_window.cc ++++ b/chrome/browser/devtools/devtools_window.cc +@@ -738,7 +738,7 @@ Profile* DevToolsWindow::GetProfileForDevToolsWindow( + content::WebContents* web_contents) { + Profile* profile = + Profile::FromBrowserContext(web_contents->GetBrowserContext()); +- if (profile->IsPrimaryOTRProfile()) { ++ if (profile->IsPrimaryOTRProfile() || profile->IsTor()) { + return profile; + } + return profile->GetOriginalProfile(); From e3de6db360b35150246be2cf4f52d7569e6e93d8 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Tue, 21 Jun 2022 12:24:29 -0700 Subject: [PATCH 2/2] Override IsPrimaryOTRProfile to disable Ambient authentication in Tor --- browser/tor/BUILD.gn | 2 ++ browser/tor/tor_profile_manager_browsertest.cc | 4 ++++ chromium_src/chrome/browser/profiles/profile.cc | 9 +++++++++ chromium_src/chrome/browser/profiles/profile.h | 4 ++++ ...chrome-browser-devtools-devtools_window.cc.patch | 13 ------------- 5 files changed, 19 insertions(+), 13 deletions(-) delete mode 100644 patches/chrome-browser-devtools-devtools_window.cc.patch diff --git a/browser/tor/BUILD.gn b/browser/tor/BUILD.gn index be4b90f7897c..c37b07db949f 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -89,6 +89,8 @@ source_set("browser_tests") { testonly = true if (enable_tor) { sources = [ + "//chrome//browser/net/profile_network_context_service_test_utils.cc", + "//chrome//browser/net/profile_network_context_service_test_utils.h", "brave_local_state_browsertest.cc", "brave_tor_client_updater_browsertest.cc", "onion_location_navigation_throttle_browsertest.cc", diff --git a/browser/tor/tor_profile_manager_browsertest.cc b/browser/tor/tor_profile_manager_browsertest.cc index d7c7268f34e9..da153256f7db 100644 --- a/browser/tor/tor_profile_manager_browsertest.cc +++ b/browser/tor/tor_profile_manager_browsertest.cc @@ -20,6 +20,7 @@ #include "chrome/browser/bookmarks/bookmark_model_factory.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" +#include "chrome/browser/net/profile_network_context_service_test_utils.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/ui/browser_finder.h" #include "chrome/browser/ui/browser_list.h" @@ -197,6 +198,9 @@ IN_PROC_BROWSER_TEST_F(TorProfileManagerTest, #if BUILDFLAG(ENABLE_IPFS) EXPECT_EQ(ipfs::IpfsServiceFactory::GetForContext(tor_profile), nullptr); #endif + // Ambient Auth should be disabled + EXPECT_FALSE(AmbientAuthenticationTestHelper::IsAmbientAuthAllowedForProfile( + tor_profile)); } IN_PROC_BROWSER_TEST_F(TorProfileManagerTest, SwitchToTorProfileInheritPrefs) { diff --git a/chromium_src/chrome/browser/profiles/profile.cc b/chromium_src/chrome/browser/profiles/profile.cc index 6ed8a9e65b53..251ddfaaf36b 100644 --- a/chromium_src/chrome/browser/profiles/profile.cc +++ b/chromium_src/chrome/browser/profiles/profile.cc @@ -10,8 +10,10 @@ #define BRAVE_ALLOWS_BROWSER_WINDOWS *this == TorID() || #define IsIncognitoProfile IsIncognitoProfile_ChromiumImpl +#define IsPrimaryOTRProfile IsPrimaryOTRProfile_ChromiumImpl #include "src/chrome/browser/profiles/profile.cc" #undef IsIncognitoProfile +#undef IsPrimaryOTRProfile // static const Profile::OTRProfileID Profile::OTRProfileID::TorID() { @@ -27,3 +29,10 @@ bool Profile::IsIncognitoProfile() const { return true; return IsIncognitoProfile_ChromiumImpl(); } + +// Tor profile should behave like primary OTR profile used in private window +bool Profile::IsPrimaryOTRProfile() const { + if (IsTor()) + return true; + return IsPrimaryOTRProfile_ChromiumImpl(); +} diff --git a/chromium_src/chrome/browser/profiles/profile.h b/chromium_src/chrome/browser/profiles/profile.h index fe6b7ac0a2a6..753727015d63 100644 --- a/chromium_src/chrome/browser/profiles/profile.h +++ b/chromium_src/chrome/browser/profiles/profile.h @@ -14,9 +14,13 @@ #define IsIncognitoProfile \ IsIncognitoProfile_ChromiumImpl() const; \ bool IsIncognitoProfile +#define IsPrimaryOTRProfile \ + IsPrimaryOTRProfile_ChromiumImpl() const; \ + bool IsPrimaryOTRProfile #include "src/chrome/browser/profiles/profile.h" +#undef IsPrimaryOTRProfile #undef IsIncognitoProfile #undef HasPrimaryOTRProfile #undef PrimaryID diff --git a/patches/chrome-browser-devtools-devtools_window.cc.patch b/patches/chrome-browser-devtools-devtools_window.cc.patch deleted file mode 100644 index 2504a99899d1..000000000000 --- a/patches/chrome-browser-devtools-devtools_window.cc.patch +++ /dev/null @@ -1,13 +0,0 @@ -diff --git a/chrome/browser/devtools/devtools_window.cc b/chrome/browser/devtools/devtools_window.cc -index 47e5047e23b2e423cb27ee9e4b7bb0c9386c7169..4e7077ee4a7cec5c4088e40134d076ab22e43bce 100644 ---- a/chrome/browser/devtools/devtools_window.cc -+++ b/chrome/browser/devtools/devtools_window.cc -@@ -738,7 +738,7 @@ Profile* DevToolsWindow::GetProfileForDevToolsWindow( - content::WebContents* web_contents) { - Profile* profile = - Profile::FromBrowserContext(web_contents->GetBrowserContext()); -- if (profile->IsPrimaryOTRProfile()) { -+ if (profile->IsPrimaryOTRProfile() || profile->IsTor()) { - return profile; - } - return profile->GetOriginalProfile();