Skip to content

Commit

Permalink
Merge pull request #7307 from brave/ipfs_add_redirect_check
Browse files Browse the repository at this point in the history
Check if IPFS resolution is enabled before redirect URL requests.
  • Loading branch information
yrliou authored Dec 4, 2020
2 parents 3451415 + d9d3733 commit 1099b24
Show file tree
Hide file tree
Showing 21 changed files with 205 additions and 101 deletions.
8 changes: 0 additions & 8 deletions browser/brave_local_state_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "brave/components/brave_referrals/buildflags/buildflags.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/brave_shields_p3a.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "brave/components/ntp_background_images/browser/ntp_background_images_service.h"
#include "brave/components/ntp_background_images/browser/view_counter_service.h"
#include "brave/components/p3a/brave_p3a_service.h"
Expand All @@ -32,10 +31,6 @@
#include "brave/components/tor/tor_profile_service.h"
#endif

#if BUILDFLAG(IPFS_ENABLED)
#include "brave/components/ipfs/ipfs_service.h"
#endif

#include "brave/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.h"

#if !defined(OS_ANDROID)
Expand Down Expand Up @@ -69,9 +64,6 @@ void RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
registry->SetDefaultPrefValue(
metrics::prefs::kMetricsReportingEnabled,
base::Value(GetDefaultPrefValueForMetricsReporting()));
#if BUILDFLAG(IPFS_ENABLED)
ipfs::IpfsService::RegisterLocalStatePrefs(registry);
#endif

#if BUILDFLAG(BRAVE_P3A_ENABLED)
brave::BraveP3AService::RegisterPrefs(registry,
Expand Down
5 changes: 2 additions & 3 deletions browser/extensions/api/ipfs_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
#include <memory>
#include <string>

#include "base/feature_list.h"
#include "base/json/json_writer.h"
#include "base/values.h"
#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/components/ipfs/features.h"
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_service.h"
#include "brave/components/ipfs/ipfs_utils.h"
#include "brave/grit/brave_generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

Expand All @@ -25,7 +24,7 @@ ipfs::IpfsService* GetIpfsService(content::BrowserContext* context) {
}

bool IsIpfsEnabled(content::BrowserContext* context) {
return ipfs::IpfsServiceFactory::IsIpfsEnabled(context);
return ipfs::IsIpfsEnabled(context);
}

base::Value MakeSelectValue(const base::string16& name,
Expand Down
8 changes: 4 additions & 4 deletions browser/extensions/brave_extension_management.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#endif

#if BUILDFLAG(IPFS_ENABLED)
#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/components/ipfs/brave_ipfs_client_updater.h"
#include "brave/components/ipfs/ipfs_utils.h"
#endif

namespace extensions {
Expand All @@ -45,7 +45,7 @@ BraveExtensionManagement::BraveExtensionManagement(Profile* profile)
tor::prefs::kTorDisabled,
base::BindRepeating(&BraveExtensionManagement::OnTorDisabledChanged,
base::Unretained(this)));
Cleanup();
Cleanup(profile);
}

BraveExtensionManagement::~BraveExtensionManagement() {
Expand Down Expand Up @@ -74,14 +74,14 @@ void BraveExtensionManagement::OnTorDisabledChanged() {
#endif
}

void BraveExtensionManagement::Cleanup() {
void BraveExtensionManagement::Cleanup(content::BrowserContext* context) {
// BrowserPolicyConnector enforce policy earlier than this constructor so we
// have to manully cleanup tor executable when tor is disabled by gpo
OnTorDisabledChanged();

#if BUILDFLAG(IPFS_ENABLED)
// Remove ipfs executable if it is disabled by GPO.
if (ipfs::IpfsServiceFactory::IsIpfsDisabledByPolicy())
if (ipfs::IsIpfsDisabledByPolicy(context))
g_brave_browser_process->ipfs_client_updater()->Cleanup();
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion browser/extensions/brave_extension_management.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BraveExtensionManagement : public ExtensionManagement,
UnloadedExtensionReason reason) override;

void OnTorDisabledChanged();
void Cleanup();
void Cleanup(content::BrowserContext* browser_context);

PrefChangeRegistrar local_state_pref_change_registrar_;

Expand Down
3 changes: 2 additions & 1 deletion browser/ipfs/content_browser_client_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "brave/common/url_constants.h"
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_gateway.h"
#include "brave/components/ipfs/ipfs_utils.h"
#include "brave/components/ipfs/pref_names.h"
#include "brave/components/ipfs/translate_ipfs_uri.h"
#include "chrome/browser/external_protocol/external_protocol_handler.h"
Expand Down Expand Up @@ -54,7 +55,7 @@ bool HandleIPFSURLRewrite(
return true;
}

if (!IpfsServiceFactory::IsIpfsResolveMethodDisabled(browser_context) &&
if (!IsIpfsResolveMethodDisabled(browser_context) &&
// When it's not the local gateway we don't want to show a ipfs:// URL.
// We instead will translate the URL later.
IsIPFSLocalGateway(browser_context) &&
Expand Down
13 changes: 10 additions & 3 deletions browser/ipfs/ipfs_policy_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "brave/components/ipfs/features.h"
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_navigation_throttle.h"
#include "brave/components/ipfs/ipfs_utils.h"
#include "brave/components/ipfs/pref_names.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/ui/browser.h"
Expand Down Expand Up @@ -61,7 +62,7 @@ class IpfsPolicyTest : public InProcessBrowserTest {
BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
PolicyMap policies;
policies.Set(key::kIPFSEnabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_MACHINE, POLICY_SOURCE_PLATFORM,
POLICY_SCOPE_USER, POLICY_SOURCE_PLATFORM,
base::Value(enable), nullptr);
provider_.UpdateChromePolicy(policies);
}
Expand Down Expand Up @@ -92,11 +93,17 @@ using IpfsEnabledPolicyTest = IpfsPolicyTest<true>;
using IpfsDisabledPolicyTest = IpfsPolicyTest<false>;

IN_PROC_BROWSER_TEST_F(IpfsEnabledPolicyTest, IsIpfsDisabledByPolicy) {
EXPECT_FALSE(ipfs::IpfsServiceFactory::IsIpfsDisabledByPolicy());
EXPECT_FALSE(ipfs::IsIpfsDisabledByPolicy(browser_context()));
auto* prefs = user_prefs::UserPrefs::Get(browser_context());
EXPECT_TRUE(prefs->FindPreference(kIPFSEnabled));
EXPECT_TRUE(prefs->GetBoolean(kIPFSEnabled));
}

IN_PROC_BROWSER_TEST_F(IpfsDisabledPolicyTest, IsIpfsDisabledByPolicy) {
EXPECT_TRUE(ipfs::IpfsServiceFactory::IsIpfsDisabledByPolicy());
EXPECT_TRUE(ipfs::IsIpfsDisabledByPolicy(browser_context()));
auto* prefs = user_prefs::UserPrefs::Get(browser_context());
EXPECT_TRUE(prefs->FindPreference(kIPFSEnabled));
EXPECT_FALSE(prefs->GetBoolean(kIPFSEnabled));
}

IN_PROC_BROWSER_TEST_F(IpfsEnabledPolicyTest, GetService) {
Expand Down
52 changes: 2 additions & 50 deletions browser/ipfs/ipfs_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,21 @@

#include "brave/browser/ipfs/ipfs_service_factory.h"

#include "base/feature_list.h"
#include "base/path_service.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/components/ipfs/features.h"
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_service.h"
#include "brave/components/ipfs/pref_names.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "brave/components/ipfs/ipfs_utils.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/chrome_paths.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "extensions/browser/extension_registry_factory.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extension_system_provider.h"
#include "extensions/browser/extensions_browser_client.h"

namespace ipfs {

// static
bool IpfsServiceFactory::IsIpfsDisabledByPolicy() {
if (!g_brave_browser_process)
return false;

PrefService* local_state = g_brave_browser_process->local_state();

// Because we currently do not provide a settings switch for IPFSEnabled
// preference to be overwritten by users, the policy is configured that only
// a mandatory value can be set by admins.
return local_state->IsManagedPreference(kIPFSEnabled) &&
!local_state->GetBoolean(kIPFSEnabled);
}

// static
bool IpfsServiceFactory::IsIpfsEnabled(content::BrowserContext* context) {
if (!brave::IsRegularProfile(context) || IsIpfsDisabledByPolicy() ||
!base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature))
return false;

return true;
}

// static
bool IpfsServiceFactory::IsIpfsResolveMethodDisabled(
content::BrowserContext* context) {
// Ignore the actual pref value if IPFS feature is disabled.
if (!IsIpfsEnabled(context)) {
return true;
}

PrefService* user_prefs = user_prefs::UserPrefs::Get(context);
return user_prefs->FindPreference(kIPFSResolveMethod) &&
user_prefs->GetInteger(kIPFSResolveMethod) ==
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_DISABLED);
}

// static
IpfsServiceFactory* IpfsServiceFactory::GetInstance() {
return base::Singleton<IpfsServiceFactory>::get();
Expand All @@ -71,7 +28,7 @@ IpfsServiceFactory* IpfsServiceFactory::GetInstance() {
// static
IpfsService* IpfsServiceFactory::GetForContext(
content::BrowserContext* context) {
if (!IsIpfsEnabled(context))
if (!brave::IsRegularProfile(context) || !IsIpfsEnabled(context))
return nullptr;

return static_cast<IpfsService*>(
Expand Down Expand Up @@ -100,9 +57,4 @@ KeyedService* IpfsServiceFactory::BuildServiceInstanceFor(
user_data_dir, chrome::GetChannel());
}

content::BrowserContext* IpfsServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}

} // namespace ipfs
8 changes: 0 additions & 8 deletions browser/ipfs/ipfs_service_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ class IpfsServiceFactory : public BrowserContextKeyedServiceFactory {
static IpfsService* GetForContext(content::BrowserContext* context);
static IpfsServiceFactory* GetInstance();

// IsIpfsEnabled returns false if IPFS feature is unsupported for the given
// context, disabled by IPFSEnabled policy, or the feature flag.
static bool IsIpfsEnabled(content::BrowserContext* context);
static bool IsIpfsResolveMethodDisabled(content::BrowserContext* context);
static bool IsIpfsDisabledByPolicy();

private:
friend struct base::DefaultSingletonTraits<IpfsServiceFactory>;

Expand All @@ -33,8 +27,6 @@ class IpfsServiceFactory : public BrowserContextKeyedServiceFactory {
// BrowserContextKeyedServiceFactory overrides:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;

DISALLOW_COPY_AND_ASSIGN(IpfsServiceFactory);
};
Expand Down
5 changes: 5 additions & 0 deletions browser/net/ipfs_redirect_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ namespace ipfs {
int OnBeforeURLRequest_IPFSRedirectWork(
const brave::ResponseCallback& next_callback,
std::shared_ptr<brave::BraveRequestInfo> ctx) {
if (!ctx->browser_context ||
IsIpfsResolveMethodDisabled(ctx->browser_context)) {
return net::OK;
}

GURL new_url;
if (ipfs::TranslateIPFSURI(ctx->request_url, &new_url,
ctx->ipfs_gateway_url)) {
Expand Down
71 changes: 71 additions & 0 deletions browser/net/ipfs_redirect_network_delegate_helper_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* 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 "base/test/scoped_feature_list.h"
#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/components/ipfs/features.h"
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"

namespace ipfs {

class IpfsRedirectNetworkDelegateHelperBrowserTest
: public InProcessBrowserTest {
public:
IpfsRedirectNetworkDelegateHelperBrowserTest() {
feature_list_.InitAndEnableFeature(ipfs::features::kIpfsFeature);
}

~IpfsRedirectNetworkDelegateHelperBrowserTest() override = default;

void SetUpOnMainThread() override {
ipfs_url_ = GURL("ipfs://QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR");
gateway_url_ = GURL(
"https://dweb.link/ipfs/"
"QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR");
}

content::WebContents* web_contents() const {
return browser()->tab_strip_model()->GetActiveWebContents();
}

PrefService* GetPrefs() const { return browser()->profile()->GetPrefs(); }
const GURL& ipfs_url() { return ipfs_url_; }
const GURL& gateway_url() { return gateway_url_; }

private:
base::test::ScopedFeatureList feature_list_;
GURL ipfs_url_;
GURL gateway_url_;
};

IN_PROC_BROWSER_TEST_F(IpfsRedirectNetworkDelegateHelperBrowserTest,
IPFSResolveMethodDisabledNoRedirect) {
GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(IPFSResolveMethodTypes::IPFS_DISABLED));

EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), ipfs_url()));
EXPECT_EQ(web_contents()->GetURL(), ipfs_url());
}

IN_PROC_BROWSER_TEST_F(IpfsRedirectNetworkDelegateHelperBrowserTest,
IPFSResolveMethodGatewayRedirect) {
GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(IPFSResolveMethodTypes::IPFS_GATEWAY));

EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), ipfs_url()));
EXPECT_EQ(web_contents()->GetURL(), gateway_url());
}

} // namespace ipfs
Loading

0 comments on commit 1099b24

Please sign in to comment.