Merge pull request #14424 from brave/brave_24166

Brave 24166
This commit is contained in:
cypt4
2022-08-05 00:58:02 -07:00
committed by GitHub
12 changed files with 165 additions and 10 deletions
+1
View File
@@ -29,6 +29,7 @@ source_set("unittests") {
"//brave/components/tor/buildflags",
"//chrome/common:channel_info",
"//chrome/test:test_support",
"//components/security_interstitials/content:security_interstitial_page",
"//content/test:test_support",
"//net",
"//net:test_support",
@@ -24,6 +24,7 @@
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/security_interstitials/content/security_interstitial_tab_helper.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/mock_navigation_handle.h"
@@ -42,6 +43,12 @@ namespace {
constexpr char kTestProfileName[] = "TestProfile";
const GURL& GetIPFSSchemeURL() {
static const GURL ipfs_url(
"ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/");
return ipfs_url;
}
const GURL& GetIPFSURL() {
static const GURL ipfs_url(
"http://localhost:48080/ipfs/"
@@ -356,6 +363,45 @@ TEST_F(IpfsNavigationThrottleUnitTest, ProceedForAskNodeMode) {
<< GetIPFSURL();
}
TEST_F(IpfsNavigationThrottleUnitTest, ShowInterstitial) {
profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod, static_cast<int>(IPFSResolveMethodTypes::IPFS_ASK));
content::MockNavigationHandle test_handle(web_contents());
test_handle.set_url(GetIPFSSchemeURL());
auto throttle = IpfsNavigationThrottle::MaybeCreateThrottleFor(
&test_handle, ipfs_service(profile()), profile()->GetPrefs(), locale());
ASSERT_TRUE(throttle != nullptr);
EXPECT_EQ(NavigationThrottle::CANCEL, throttle->WillStartRequest().action());
ASSERT_TRUE(
security_interstitials::SecurityInterstitialTabHelper::FromWebContents(
web_contents())
->IsInterstitialPendingForNavigation(test_handle.GetNavigationId()));
}
TEST_F(IpfsNavigationThrottleUnitTest, ShowInterstitialAfterNavigationFails) {
profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod, static_cast<int>(IPFSResolveMethodTypes::IPFS_ASK));
content::MockNavigationHandle test_handle(web_contents());
test_handle.set_url(GetNonIPFSURL());
auto throttle = IpfsNavigationThrottle::MaybeCreateThrottleFor(
&test_handle, ipfs_service(profile()), profile()->GetPrefs(), locale());
ASSERT_TRUE(throttle != nullptr);
EXPECT_EQ(NavigationThrottle::PROCEED, throttle->WillStartRequest().action())
<< GetIPFSURL();
test_handle.set_net_error_code(net::ERR_IPFS_RESOLVE_METHOD_NOT_SELECTED);
EXPECT_EQ(NavigationThrottle::CANCEL, throttle->WillFailRequest().action());
ASSERT_TRUE(
security_interstitials::SecurityInterstitialTabHelper::FromWebContents(
web_contents())
->IsInterstitialPendingForNavigation(test_handle.GetNavigationId()));
}
TEST_F(IpfsNavigationThrottleUnitTest, ProceedForNonLocalGatewayURL) {
profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod, static_cast<int>(IPFSResolveMethodTypes::IPFS_LOCAL));
@@ -32,20 +32,34 @@ int OnBeforeURLRequest_IPFSRedirectWork(
auto* prefs = user_prefs::UserPrefs::Get(ctx->browser_context);
const bool ipfs_disabled = IsIpfsResolveMethodDisabled(prefs);
if (has_ipfs_scheme && ipfs_disabled) {
ctx->blocked_by = brave::kOtherBlocked;
return net::OK;
}
if (has_ipfs_scheme && !brave::IsRegularProfile(ctx->browser_context)) {
// Don't allow IPFS requests without translation of IPFS urls.
ctx->blocked_by = brave::kOtherBlocked;
// Only net::OK navigation will be actually blocked without commit.
// Show proper error for mainframe navigation.
return ctx->resource_type == blink::mojom::ResourceType::kMainFrame
? net::ERR_INCOGNITO_IPFS_NOT_ALLOWED
: net::OK;
}
if (has_ipfs_scheme && ipfs_disabled) {
ctx->blocked_by = brave::kOtherBlocked;
// Only net::OK navigation will be actually blocked without commit.
// Show proper error for mainframe navigation.
return ctx->resource_type == blink::mojom::ResourceType::kMainFrame
? net::ERR_IPFS_DISABLED
: net::OK;
}
if (has_ipfs_scheme && IsIpfsResolveMethodAsk(prefs)) {
ctx->blocked_by = brave::kOtherBlocked;
// Only net::OK navigation will be actually blocked without commit.
// Show proper error for mainframe navigation.
return ctx->resource_type == blink::mojom::ResourceType::kMainFrame
? net::ERR_IPFS_RESOLVE_METHOD_NOT_SELECTED
: net::OK;
}
GURL new_url;
if (ipfs::TranslateIPFSURI(ctx->request_url, &new_url, ctx->ipfs_gateway_url,
false)) {
@@ -72,7 +72,7 @@ IN_PROC_BROWSER_TEST_F(IpfsRedirectNetworkDelegateHelperBrowserTest,
}
IN_PROC_BROWSER_TEST_F(IpfsRedirectNetworkDelegateHelperBrowserTest,
IPFSResolveRedirectsToErrorPage) {
IPFSResolveRedirectsToErrorPage_Incognito) {
GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(IPFSResolveMethodTypes::IPFS_GATEWAY));
@@ -92,4 +92,24 @@ IN_PROC_BROWSER_TEST_F(IpfsRedirectNetworkDelegateHelperBrowserTest,
EXPECT_EQ(net::ERR_INCOGNITO_IPFS_NOT_ALLOWED, observer.net_error_code());
}
IN_PROC_BROWSER_TEST_F(IpfsRedirectNetworkDelegateHelperBrowserTest,
IPFSResolveRedirectsToErrorPage_IpfsDisabled) {
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());
auto* wc = browser()->tab_strip_model()->GetActiveWebContents();
content::NavigationHandleObserver observer(wc, ipfs_url());
// Try to navigate to the url. The navigation should be canceled and the
// NavigationHandle should have the right error code.
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), ipfs_url()));
EXPECT_TRUE(wc->GetMainFrame()->IsErrorDocument());
EXPECT_EQ(net::ERR_IPFS_DISABLED, observer.net_error_code());
}
} // namespace ipfs
@@ -60,6 +60,10 @@ class IPFSRedirectNetworkDelegateHelperTest : public testing::Test {
};
TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIHTTPScheme) {
profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(IPFSResolveMethodTypes::IPFS_GATEWAY));
GURL url("http://a.com/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
brave_request_info->browser_context = profile();
@@ -70,6 +74,9 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIHTTPScheme) {
}
TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPFSSchemeLocal) {
profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod, static_cast<int>(IPFSResolveMethodTypes::IPFS_LOCAL));
GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
brave_request_info->browser_context = profile();
@@ -85,6 +92,22 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPFSSchemeLocal) {
"QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
}
TEST_F(IPFSRedirectNetworkDelegateHelperTest,
ProperMainFrameErrorCodeWhenIPFSDisabled) {
profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(IPFSResolveMethodTypes::IPFS_DISABLED));
GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
brave_request_info->resource_type = blink::mojom::ResourceType::kMainFrame;
brave_request_info->browser_context = profile();
int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(),
brave_request_info);
EXPECT_EQ(rc, net::ERR_IPFS_DISABLED);
EXPECT_EQ(brave_request_info->blocked_by, brave::kOtherBlocked);
}
TEST_F(IPFSRedirectNetworkDelegateHelperTest,
SubFrameRequestDisabledWhenIPFSDisabled) {
profile()->GetPrefs()->SetInteger(
@@ -204,6 +227,10 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest,
}
TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPFSScheme) {
profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(IPFSResolveMethodTypes::IPFS_GATEWAY));
GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
brave_request_info->browser_context = profile();
@@ -219,6 +246,9 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPFSScheme) {
}
TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPNSSchemeLocal) {
profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod, static_cast<int>(IPFSResolveMethodTypes::IPFS_LOCAL));
GURL url("ipns://QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
brave_request_info->browser_context = profile();
@@ -235,6 +265,10 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPNSSchemeLocal) {
}
TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPNSScheme) {
profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(IPFSResolveMethodTypes::IPFS_GATEWAY));
GURL url("ipns://QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
brave_request_info->browser_context = profile();
@@ -17,6 +17,15 @@
SHOW_NO_BUTTONS, \
}); \
return &error; \
} else if (error_code == net::ERR_IPFS_DISABLED) { \
static LocalizedErrorMap error({ \
net::ERR_IPFS_DISABLED, \
IDS_ERRORPAGES_IPFS_DISABLED_HEADING, \
IDS_ERRORPAGES_IPFS_DISABLED_SUMMARY, \
SUGGEST_NONE, \
SHOW_BUTTON_RELOAD, \
}); \
return &error; \
}
namespace error_page {
+2
View File
@@ -11,3 +11,5 @@
// Error occurs when user tries to access ipfs sites in
// incognito context
NET_ERROR(INCOGNITO_IPFS_NOT_ALLOWED, -10001)
NET_ERROR(IPFS_DISABLED, -10002)
NET_ERROR(IPFS_RESOLVE_METHOD_NOT_SELECTED, -10003)
+11 -4
View File
@@ -99,10 +99,7 @@ content::NavigationThrottle::ThrottleCheckResult
IpfsNavigationThrottle::WillStartRequest() {
GURL url = navigation_handle()->GetURL();
bool should_ask =
pref_service_->FindPreference(kIPFSResolveMethod) &&
pref_service_->GetInteger(kIPFSResolveMethod) ==
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK);
bool should_ask = IsIpfsResolveMethodAsk(pref_service_);
if (IsIPFSScheme(url) && should_ask) {
return ShowIPFSOnboardingInterstitial();
@@ -135,6 +132,16 @@ IpfsNavigationThrottle::WillStartRequest() {
return content::NavigationThrottle::PROCEED;
}
content::NavigationThrottle::ThrottleCheckResult
IpfsNavigationThrottle::WillFailRequest() {
auto* handle = navigation_handle();
if (handle &&
handle->GetNetErrorCode() == net::ERR_IPFS_RESOLVE_METHOD_NOT_SELECTED) {
return ShowIPFSOnboardingInterstitial();
}
return content::NavigationThrottle::PROCEED;
}
void IpfsNavigationThrottle::GetConnectedPeers() {
ipfs_service_->GetConnectedPeers(
base::BindOnce(&IpfsNavigationThrottle::OnGetConnectedPeers,
@@ -45,6 +45,7 @@ class IpfsNavigationThrottle : public content::NavigationThrottle {
// content::NavigationThrottle implementation:
ThrottleCheckResult WillStartRequest() override;
ThrottleCheckResult WillFailRequest() override;
const char* GetNameForLogging() override;
private:
@@ -64,6 +65,7 @@ class IpfsNavigationThrottle : public content::NavigationThrottle {
void GetConnectedPeers();
void OnGetConnectedPeers(bool success, const std::vector<std::string>& peers);
void OnIpfsLaunched(bool result);
bool ShouldAsk();
bool resume_pending_ = false;
IpfsService* ipfs_service_ = nullptr;
+13
View File
@@ -102,6 +102,19 @@ bool IsIpfsResolveMethodDisabled(PrefService* prefs) {
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_DISABLED);
}
bool IsIpfsResolveMethodAsk(PrefService* prefs) {
DCHECK(prefs);
// Ignore the actual pref value if IPFS feature is disabled.
if (IsIpfsResolveMethodDisabled(prefs)) {
return false;
}
return prefs->FindPreference(kIPFSResolveMethod) &&
prefs->GetInteger(kIPFSResolveMethod) ==
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK);
}
bool IsIpfsMenuEnabled(PrefService* prefs) {
return !ipfs::IsIpfsDisabledByFeatureOrPolicy(prefs) &&
ipfs::IsLocalGatewayConfigured(prefs);
+1
View File
@@ -61,6 +61,7 @@ bool ParsePeerConnectionString(const std::string& value,
GURL ContentHashToCIDv1URL(const std::string& contenthash);
bool IsAPIGateway(const GURL& url, version_info::Channel channel);
bool IsIpfsResolveMethodDisabled(PrefService* prefs);
bool IsIpfsResolveMethodAsk(PrefService* prefs);
std::string GetRegistryDomainFromIPNS(const GURL& url);
bool IsValidCIDOrDomain(const std::string& value);
@@ -169,6 +169,12 @@
<message name="IDS_ERRORPAGES_IPFS_INCOGNITO_SUMMARY" desc="Summary in the error page when IPFS failed in incognito mode.">
IPFS pages are not allowed in private windows.
</message>
<message name="IDS_ERRORPAGES_IPFS_DISABLED_HEADING" desc="Heading of the IPFS disabled error page.">
IPFS page cannot be loaded.
</message>
<message name="IDS_ERRORPAGES_IPFS_DISABLED_SUMMARY" desc="Summary in the error page when IPFS failed because it is disabled.">
IPFS is disabled. Select IPFS resolve method in settings to open this site.
</message>
<!-- WebUI newtab resources -->
<message name="IDS_BRAVE_NEW_TAB_TOTAL_ADS_TRACKERS_BLOCKED" desc="total number of ads and trackers blocked">Trackers &amp; ads blocked</message>
<message name="IDS_BRAVE_NEW_TAB_TOTAL_HTTPS_UPGRADES" desc="total number of HTTPS upgrades">HTTPS Upgrades</message>