Skip to content

Commit

Permalink
Add error code for ipfs navigations when ipfs is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
cypt4 committed Aug 3, 2022
1 parent 50ca5d2 commit 5b34daa
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
15 changes: 10 additions & 5 deletions browser/net/ipfs_redirect_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,25 @@ 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;
}

GURL new_url;
if (ipfs::TranslateIPFSURI(ctx->request_url, &new_url, ctx->ipfs_gateway_url,
false)) {
Expand Down
22 changes: 21 additions & 1 deletion browser/net/ipfs_redirect_network_delegate_helper_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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(), gateway_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
34 changes: 34 additions & 0 deletions browser/net/ipfs_redirect_network_delegate_helper_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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(
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions chromium_src/components/error_page/common/localized_error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions chromium_src/net/base/net_error_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
// Error occurs when user tries to access ipfs sites in
// incognito context
NET_ERROR(INCOGNITO_IPFS_NOT_ALLOWED, -10001)
NET_ERROR(IPFS_DISABLED, -10002)
6 changes: 6 additions & 0 deletions components/resources/brave_components_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 5b34daa

Please sign in to comment.