Skip to content

Commit

Permalink
Do not proceed loading ipfs resources if ipfs is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
cypt4 committed Jun 30, 2022
1 parent 9a2aa47 commit 42e546f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
16 changes: 14 additions & 2 deletions browser/net/ipfs_redirect_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,22 @@ 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))
bool hasIpfsScheme = IsIPFSScheme(ctx->request_url);
if (!ctx->browser_context) {
if (hasIpfsScheme) {
ctx->blocked_by = brave::kOtherBlocked;
}
return net::OK;
}

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

if (ipfsDisabled || !brave::IsRegularProfile(ctx->browser_context)) {
if (hasIpfsScheme &&
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
Original file line number Diff line number Diff line change
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 42e546f

Please sign in to comment.