Skip to content
Permalink
Browse files Browse the repository at this point in the history
Merge pull request #13989 from brave/brave_23646
Do not proceed loading ipfs resources if ipfs is disabled
  • Loading branch information
cypt4 committed Jul 2, 2022
2 parents fc16eb6 + 0fb80b9 commit 7ef8cb2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
19 changes: 17 additions & 2 deletions browser/net/ipfs_redirect_network_delegate_helper.cc
Expand Up @@ -19,10 +19,25 @@ namespace ipfs {
int OnBeforeURLRequest_IPFSRedirectWork(
const brave::ResponseCallback& next_callback,
std::shared_ptr<brave::BraveRequestInfo> ctx) {
if (!ctx->browser_context || !brave::IsRegularProfile(ctx->browser_context))
const bool has_ipfs_scheme = IsIPFSScheme(ctx->request_url);
if (!ctx->browser_context) {
// IPFS url translation depends on selected gateway.
// So we block IPFS requests if we don't have access to prefs.
if (has_ipfs_scheme) {
ctx->blocked_by = brave::kOtherBlocked;
}
return net::OK;
}

auto* prefs = user_prefs::UserPrefs::Get(ctx->browser_context);
if (IsIpfsResolveMethodDisabled(prefs)) {
const bool ipfs_disabled = IsIpfsResolveMethodDisabled(prefs);

if (ipfs_disabled || !brave::IsRegularProfile(ctx->browser_context)) {
// Don't allow IPFS requests without translation of IPFS urls.
if (has_ipfs_scheme &&
ctx->resource_type != blink::mojom::ResourceType::kMainFrame) {
ctx->blocked_by = brave::kOtherBlocked;
}
return net::OK;
}

Expand Down
47 changes: 47 additions & 0 deletions browser/net/ipfs_redirect_network_delegate_helper_unittest.cc
Expand Up @@ -85,6 +85,53 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPFSSchemeLocal) {
"QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
}

TEST_F(IPFSRedirectNetworkDelegateHelperTest,
SubFrameRequestDisabledWhenIPFSDisabled) {
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::kSubFrame;
brave_request_info->browser_context = profile();
int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(),
brave_request_info);
EXPECT_EQ(rc, net::OK);
EXPECT_EQ(brave_request_info->blocked_by, brave::kOtherBlocked);
}

TEST_F(IPFSRedirectNetworkDelegateHelperTest,
SubFrameRequestDisabledWhenIPFSDisabled_Incognito) {
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->resource_type = blink::mojom::ResourceType::kSubFrame;
brave_request_info->browser_context = profile()->GetOffTheRecordProfile(
Profile::OTRProfileID::CreateUnique("incognito"), true);
int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(),
brave_request_info);
EXPECT_EQ(rc, net::OK);
EXPECT_EQ(brave_request_info->blocked_by, brave::kOtherBlocked);
}

TEST_F(IPFSRedirectNetworkDelegateHelperTest,
SubFrameRequestDisabledWhen_NoContext) {
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->resource_type = blink::mojom::ResourceType::kSubFrame;
brave_request_info->browser_context = nullptr;
int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(),
brave_request_info);
EXPECT_EQ(rc, net::OK);
EXPECT_EQ(brave_request_info->blocked_by, brave::kOtherBlocked);
}

TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPFSScheme) {
GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
Expand Down

0 comments on commit 7ef8cb2

Please sign in to comment.