Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve subresources for IPFS and IPNS too #6582

Merged
merged 1 commit into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions browser/autocomplete/brave_autocomplete_scheme_classifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include "brave/components/brave_webtorrent/browser/webtorrent_util.h"
#endif

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

BraveAutocompleteSchemeClassifier::BraveAutocompleteSchemeClassifier(
Profile* profile)
: ChromeAutocompleteSchemeClassifier(profile) {
Expand Down Expand Up @@ -49,8 +53,8 @@ BraveAutocompleteSchemeClassifier::GetInputTypeForScheme(

#if BUILDFLAG(IPFS_ENABLED)
if (base::IsStringASCII(scheme) &&
(base::LowerCaseEqualsASCII(scheme, kIPFSScheme) ||
base::LowerCaseEqualsASCII(scheme, kIPNSScheme))) {
(base::LowerCaseEqualsASCII(scheme, ipfs::kIPFSScheme) ||
base::LowerCaseEqualsASCII(scheme, ipfs::kIPNSScheme))) {
return metrics::OmniboxInputType::URL;
}
#endif
Expand Down
3 changes: 0 additions & 3 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,6 @@ void BraveContentBrowserClient::BrowserURLHandlerCreated(
#endif
#if BUILDFLAG(IPFS_ENABLED)
if (base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature)) {
handler->AddHandlerPair(
&ipfs::ContentBrowserClientHelper::HandleIPFSURLRewrite,
content::BrowserURLHandler::null_handler());
handler->AddHandlerPair(
&ipfs::ContentBrowserClientHelper::HandleIPFSURLRewrite,
&ipfs::ContentBrowserClientHelper::HandleIPFSURLReverseRewrite);
Expand Down
48 changes: 3 additions & 45 deletions browser/ipfs/content_browser_client_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/task/post_task.h"
#include "brave/common/url_constants.h"
#include "brave/common/pref_names.h"
#include "brave/components/ipfs/browser/translate_ipfs_uri.h"
#include "brave/components/ipfs/common/ipfs_constants.h"
#include "chrome/browser/external_protocol/external_protocol_handler.h"
#include "components/prefs/pref_service.h"
Expand Down Expand Up @@ -42,49 +43,6 @@ bool IsIPFSLocalGateway(content::BrowserContext* browser_context) {

namespace ipfs {

// static
bool ContentBrowserClientHelper::TranslateIPFSURL(
const GURL& url, GURL* new_url, bool local) {
if (!url.SchemeIs(kIPFSScheme) && !url.SchemeIs(kIPNSScheme)) {
return false;
}

std::string path = url.path();
// In the case of a URL like ipfs://[cid]/wiki/Vincent_van_Gogh.html
// host is empty and path is //wiki/Vincent_van_Gogh.html
if (url.host().empty() && path.length() > 2 &&
path.substr(0, 2) == "//") {
std::string cid(path.substr(2));
// If we have a path after the CID, get at the real resource path
size_t pos = cid.find("/");
std::string path;
if (pos != std::string::npos && pos != 0) {
// path would be /wiki/Vincent_van_Gogh.html
path = cid.substr(pos, cid.length() - pos);
// cid would be [cid]
cid = cid.substr(0, pos);
}
bool ipfs_scheme = url.scheme() == "ipfs";
bool ipns_scheme = url.scheme() == "ipns";
if ((ipfs_scheme || ipns_scheme) && std::all_of(cid.begin(), cid.end(),
[loc = std::locale{}](char c) {
return std::isalnum(c, loc);
})) {
// new_url would be:
// https://dweb.link/ipfs/[cid]//wiki/Vincent_van_Gogh.html
if (new_url) {
*new_url = GURL(std::string(
local ? kDefaultIPFSLocalGateway : kDefaultIPFSGateway) +
(ipfs_scheme ? "/ipfs/" : "/ipns/") + cid + path);
VLOG(1) << "[IPFS] " << __func__ << " new URL: " << *new_url;
}

return true;
}
}
return false;
}

// static
bool ContentBrowserClientHelper::HandleIPFSURLReverseRewrite(GURL* url,
content::BrowserContext* browser_context) {
Expand Down Expand Up @@ -119,7 +77,7 @@ bool ContentBrowserClientHelper::HandleIPFSURLRewrite(GURL* url,
content::BrowserContext* browser_context) {
if (!IsIPFSDisabled(browser_context) &&
(url->SchemeIs(kIPFSScheme) || url->SchemeIs(kIPNSScheme))) {
return TranslateIPFSURL(*url, url, IsIPFSLocalGateway(browser_context));
return TranslateIPFSURI(*url, url, IsIPFSLocalGateway(browser_context));
}

return false;
Expand All @@ -141,7 +99,7 @@ void ContentBrowserClientHelper::HandleIPFSProtocol(

// static
bool ContentBrowserClientHelper::IsIPFSProtocol(const GURL& url) {
return TranslateIPFSURL(url, nullptr, false);
return TranslateIPFSURI(url, nullptr, false);
}

} // namespace ipfs
2 changes: 0 additions & 2 deletions browser/ipfs/content_browser_client_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ namespace ipfs {

class ContentBrowserClientHelper {
public:
static bool TranslateIPFSURL(const GURL& url, GURL* new_url, bool local);

static bool HandleIPFSURLReverseRewrite(GURL* url,
content::BrowserContext* browser_context);

Expand Down
14 changes: 14 additions & 0 deletions browser/net/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import("//brave/browser/translate/buildflags/buildflags.gni")
import("//brave/components/brave_referrals/buildflags/buildflags.gni")
import("//brave/components/brave_webtorrent/browser/buildflags/buildflags.gni")
import("//brave/components/ipfs/browser/buildflags/buildflags.gni")
import("//build/config/features.gni")

# Refer to the keyed API spec for more details about the Brave Services Key
Expand Down Expand Up @@ -49,8 +50,10 @@ source_set("net") {
"//brave/components/brave_referrals/buildflags",
"//brave/components/brave_shields/browser",
"//brave/components/brave_webtorrent/browser/buildflags",
"//brave/components/ipfs/browser/buildflags",
"//brave/extensions:common",
"//components/prefs",
"//components/user_prefs",
"//content/public/browser",
"//content/public/common",
"//components/content_settings/core/browser",
Expand All @@ -65,6 +68,17 @@ source_set("net") {
"//url",
]

if (ipfs_enabled) {
sources += [
"ipfs_redirect_network_delegate_helper.cc",
"ipfs_redirect_network_delegate_helper.h",
]
deps += [
"//brave/components/ipfs/browser",
"//brave/components/ipfs/common",
]
}

if (enable_brave_referrals) {
sources += [
"brave_referrals_network_delegate_helper.cc",
Expand Down
11 changes: 11 additions & 0 deletions browser/net/brave_request_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "brave/components/brave_referrals/buildflags/buildflags.h"
#include "brave/components/brave_rewards/browser/buildflags/buildflags.h"
#include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h"
#include "brave/components/ipfs/browser/buildflags/buildflags.h"
#include "chrome/browser/browser_process.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
Expand All @@ -44,6 +45,10 @@
#include "brave/browser/net/brave_translate_redirect_network_delegate_helper.h"
#endif

#if BUILDFLAG(IPFS_ENABLED)
#include "brave/browser/net/ipfs_redirect_network_delegate_helper.h"
#endif

static bool IsInternalScheme(std::shared_ptr<brave::BraveRequestInfo> ctx) {
DCHECK(ctx);
return ctx->request_url.SchemeIs(extensions::kExtensionScheme) ||
Expand Down Expand Up @@ -84,6 +89,12 @@ void BraveRequestHandler::SetupCallbacks() {
before_url_request_callbacks_.push_back(callback);
#endif

#if BUILDFLAG(IPFS_ENABLED)
callback =
base::BindRepeating(ipfs::OnBeforeURLRequest_IPFSRedirectWork);
before_url_request_callbacks_.push_back(callback);
#endif

brave::OnBeforeStartTransactionCallback start_transaction_callback =
base::Bind(brave::OnBeforeStartTransaction_SiteHacksWork);
before_start_transaction_callbacks_.push_back(start_transaction_callback);
Expand Down
22 changes: 22 additions & 0 deletions browser/net/ipfs_redirect_network_delegate_helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* 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 "brave/browser/net/ipfs_redirect_network_delegate_helper.h"

#include "brave/components/ipfs/browser/translate_ipfs_uri.h"

namespace ipfs {

int OnBeforeURLRequest_IPFSRedirectWork(
const brave::ResponseCallback& next_callback,
std::shared_ptr<brave::BraveRequestInfo> ctx) {
GURL new_url;
if (ipfs::TranslateIPFSURI(ctx->request_url, &new_url, ctx->ipfs_local)) {
ctx->new_url_spec = new_url.spec();
}
return net::OK;
}

} // namespace ipfs
20 changes: 20 additions & 0 deletions browser/net/ipfs_redirect_network_delegate_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* 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/. */

#ifndef BRAVE_BROWSER_NET_IPFS_REDIRECT_NETWORK_DELEGATE_HELPER_H_
#define BRAVE_BROWSER_NET_IPFS_REDIRECT_NETWORK_DELEGATE_HELPER_H_

#include <memory>
#include "brave/browser/net/url_context.h"

namespace ipfs {

int OnBeforeURLRequest_IPFSRedirectWork(
const brave::ResponseCallback& next_callback,
std::shared_ptr<brave::BraveRequestInfo> ctx);

} // namespace ipfs

#endif // BRAVE_BROWSER_NET_IPFS_REDIRECT_NETWORK_DELEGATE_HELPER_H_
76 changes: 76 additions & 0 deletions browser/net/ipfs_redirect_network_delegate_helper_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* 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 "brave/browser/net/ipfs_redirect_network_delegate_helper.h"

#include <memory>
#include <string>
#include <vector>

#include "brave/browser/net/url_context.h"
#include "brave/common/translate_network_constants.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_test_util.h"
#include "url/gurl.h"

namespace {

TEST(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIHTTPScheme) {
GURL url("http://a.com/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(),
brave_request_info);
EXPECT_EQ(rc, net::OK);
EXPECT_TRUE(brave_request_info->new_url_spec.empty());
}

TEST(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPFSSchemeLocal) {
GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
brave_request_info->ipfs_local = true;
int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(),
brave_request_info);
EXPECT_EQ(rc, net::OK);
EXPECT_EQ(brave_request_info->new_url_spec,
"http://127.0.0.1:8080/ipfs/"
"QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
}

TEST(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPFSScheme) {
GURL url("ipfs://QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
brave_request_info->ipfs_local = false;
int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(),
brave_request_info);
EXPECT_EQ(rc, net::OK);
EXPECT_EQ(brave_request_info->new_url_spec,
"https://dweb.link/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG");
}

TEST(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPNSSchemeLocal) {
GURL url("ipns://QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
brave_request_info->ipfs_local = true;
int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(),
brave_request_info);
EXPECT_EQ(rc, net::OK);
EXPECT_EQ(brave_request_info->new_url_spec,
"http://127.0.0.1:8080/ipns/"
"QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd");
}

TEST(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPNSScheme) {
GURL url("ipns://QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
brave_request_info->ipfs_local = false;
int rc = ipfs::OnBeforeURLRequest_IPFSRedirectWork(brave::ResponseCallback(),
brave_request_info);
EXPECT_EQ(rc, net::OK);
EXPECT_EQ(brave_request_info->new_url_spec,
"https://dweb.link/ipns/QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd");
}

} // namespace
15 changes: 15 additions & 0 deletions browser/net/url_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h"
#include "brave/components/brave_webtorrent/browser/webtorrent_util.h"
#include "brave/components/ipfs/browser/buildflags/buildflags.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/isolation_info.h"

#if BUILDFLAG(IPFS_ENABLED)
#include "brave/common/pref_names.h"
#include "brave/components/ipfs/common/ipfs_constants.h"
#include "components/prefs/testing_pref_service.h"
#include "components/user_prefs/user_prefs.h"
#endif

namespace brave {

namespace {
Expand Down Expand Up @@ -109,6 +117,13 @@ void BraveRequestInfo::FillCTX(const network::ResourceRequest& request,
!brave_shields::GetHTTPSEverywhereEnabled(map, ctx->tab_origin);
ctx->allow_referrers = brave_shields::AllowReferrers(map, ctx->tab_origin);
ctx->upload_data = GetUploadData(request);

#if BUILDFLAG(IPFS_ENABLED)
auto* prefs = user_prefs::UserPrefs::Get(browser_context);
ctx->ipfs_local = static_cast<ipfs::IPFSResolveMethodTypes>(
prefs->GetInteger(kIPFSResolveMethod)) ==
ipfs::IPFSResolveMethodTypes::IPFS_LOCAL;
#endif
}

} // namespace brave
1 change: 1 addition & 0 deletions browser/net/url_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct BraveRequestInfo {
BlockedBy blocked_by = kNotBlocked;
bool cancel_request_explicitly = false;
std::string mock_data_url;
bool ipfs_local = true;

// Default to invalid type for resource_type, so delegate helpers
// can properly detect that the info couldn't be obtained.
Expand Down
4 changes: 0 additions & 4 deletions common/url_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
const char kChromeExtensionScheme[] = "chrome-extension";
const char kBraveUIScheme[] = "brave";
const char kMagnetScheme[] = "magnet";
const char kIPFSScheme[] = "ipfs";
const char kIPNSScheme[] = "ipns";
const char kDefaultIPFSGateway[] = "https://dweb.link";
const char kDefaultIPFSLocalGateway[] = "http://127.0.0.1:8080";
const char kBinanceScheme[] = "com.brave.binance";
const char kGeminiScheme[] = "com.brave.gemini";
const char kWidevineMoreInfoURL[] = "https://www.eff.org/issues/drm";
Expand Down
4 changes: 0 additions & 4 deletions common/url_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
extern const char kChromeExtensionScheme[];
extern const char kBraveUIScheme[];
extern const char kMagnetScheme[];
extern const char kIPFSScheme[];
extern const char kIPNSScheme[];
extern const char kDefaultIPFSGateway[];
extern const char kDefaultIPFSLocalGateway[];
extern const char kBinanceScheme[];
extern const char kGeminiScheme[];
extern const char kWidevineMoreInfoURL[];
Expand Down
4 changes: 4 additions & 0 deletions components/ipfs/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ source_set("browser") {
"features.h",
"ipfs_json_parser.cc",
"ipfs_json_parser.h",
"translate_ipfs_uri.cc",
"translate_ipfs_uri.h",
]

deps = [
"//base",
"//brave/components/brave_component_updater/browser/",
"//brave/components/ipfs/common",
"//third_party/re2",
"//url",
]
}
Loading