Skip to content

Commit

Permalink
Merge pull request #10367 from brave/web3_allowed_context
Browse files Browse the repository at this point in the history
Allow web3 on regular profiles only
  • Loading branch information
SergeyZhukovsky authored Oct 5, 2021
2 parents b09f96e + eb067f5 commit 991b83e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ source_set("brave_wallet") {
"swap_controller_factory.h",
]
deps = [
"//brave/browser/profiles:util",
"//brave/components/brave_wallet/browser",
"//brave/components/brave_wallet/common",
"//brave/components/brave_wallet/common:mojom",
Expand Down
3 changes: 2 additions & 1 deletion browser/brave_wallet/brave_wallet_context_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
#include "brave/browser/profiles/profile_util.h"
#include "content/public/browser/browser_context.h"

namespace brave_wallet {

bool IsAllowedForContext(content::BrowserContext* context) {
if (context && context->IsTor())
if (context && !brave::IsRegularProfile(context))
return false;

return true;
Expand Down
24 changes: 24 additions & 0 deletions browser/brave_wallet/brave_wallet_ethereum_chain_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,27 @@ IN_PROC_BROWSER_TEST_F(BraveWalletEthereumChainTest, AddBrokenChain) {
ASSERT_FALSE(tab_helper->IsShowingBubble());
EXPECT_EQ(base::Value(true), result_first.value);
}

IN_PROC_BROWSER_TEST_F(BraveWalletEthereumChainTest, CheckIncognitoTab) {
GURL url =
https_server()->GetURL("a.com", "/brave_wallet_ethereum_chain.html");
Browser* private_browser = CreateIncognitoBrowser(nullptr);
ui_test_utils::NavigateToURL(private_browser, url);
content::WebContents* contents =
private_browser->tab_strip_model()->GetActiveWebContents();
WaitForLoadStop(contents);
EXPECT_EQ(url, contents->GetURL());
base::RunLoop().RunUntilIdle();
std::string title;
ASSERT_TRUE(
ExecuteScriptAndExtractString(contents,
"window.domAutomationController.send("
"document.title)",
&title));
EXPECT_EQ(title, "PAGE_SCRIPT_STARTED");
auto result_first = EvalJs(contents,
"window.domAutomationController.send("
"window.ethereum != null)",
content::EXECUTE_SCRIPT_USE_MANUAL_REPLY);
EXPECT_EQ(base::Value(false), result_first.value);
}
7 changes: 5 additions & 2 deletions browser/profiles/brave_renderer_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#endif

BraveRendererUpdater::BraveRendererUpdater(Profile* profile)
: profile_(profile) {
: profile_(profile), is_wallet_allowed_for_context_(false) {
PrefService* pref_service = profile->GetPrefs();
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
brave_wallet_web3_provider_.Init(kBraveWalletWeb3Provider, pref_service);
Expand All @@ -43,6 +43,9 @@ BraveRendererUpdater::~BraveRendererUpdater() {}
void BraveRendererUpdater::InitializeRenderer(
content::RenderProcessHost* render_process_host) {
auto renderer_configuration = GetRendererConfiguration(render_process_host);
Profile* profile =
Profile::FromBrowserContext(render_process_host->GetBrowserContext());
is_wallet_allowed_for_context_ = brave_wallet::IsAllowedForContext(profile);
UpdateRenderer(&renderer_configuration);
}

Expand Down Expand Up @@ -94,7 +97,7 @@ void BraveRendererUpdater::UpdateRenderer(
(static_cast<brave_wallet::mojom::DefaultWallet>(
brave_wallet_web3_provider_.GetValue()) ==
brave_wallet::mojom::DefaultWallet::BraveWallet) &&
brave_wallet::IsAllowedForContext(profile_);
is_wallet_allowed_for_context_;

(*renderer_configuration)
->SetConfiguration(
Expand Down
1 change: 1 addition & 0 deletions browser/profiles/brave_renderer_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class BraveRendererUpdater : public KeyedService {

// Prefs that we sync to the renderers.
IntegerPrefMember brave_wallet_web3_provider_;
bool is_wallet_allowed_for_context_;
};

#endif // BRAVE_BROWSER_PROFILES_BRAVE_RENDERER_UPDATER_H_
1 change: 1 addition & 0 deletions test/data/brave-wallet/brave_wallet_ethereum_chain.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<html>
<head><title>OK</title>
<script>
document.title = "PAGE_SCRIPT_STARTED";
var request_finished = false;
var chain_added_result = false;
const url = new URL(window.location.href)
Expand Down

0 comments on commit 991b83e

Please sign in to comment.