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

Allow fetching of IPFS resources #7068

Merged
merged 1 commit into from
Nov 6, 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: 7 additions & 1 deletion chromium_src/content/renderer/render_thread_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
WebString gemini_scheme(WebString::FromASCII(kGeminiScheme)); \
WebSecurityPolicy::RegisterURLSchemeAsDisplayIsolated(gemini_scheme); \
WebSecurityPolicy::RegisterURLSchemeAsNotAllowingJavascriptURLs( \
gemini_scheme);
gemini_scheme); \
\
WebString ipfs_scheme(WebString::FromASCII("ipfs")); \
WebSecurityPolicy::RegisterURLSchemeAsSupportingFetchAPI(ipfs_scheme); \
\
WebString ipns_scheme(WebString::FromASCII("ipns")); \
WebSecurityPolicy::RegisterURLSchemeAsSupportingFetchAPI(ipns_scheme);

#include "../../../../content/renderer/render_thread_impl.cc"
2 changes: 2 additions & 0 deletions components/ipfs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ source_set("ipfs") {
"features.h",
"ipfs_constants.cc",
"ipfs_constants.h",
"ipfs_gateway.cc",
"ipfs_gateway.h",
"ipfs_interstitial_controller_client.cc",
"ipfs_interstitial_controller_client.h",
"ipfs_json_parser.cc",
Expand Down
30 changes: 30 additions & 0 deletions components/ipfs/ipfs_gateway.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* 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/components/ipfs/ipfs_gateway.h"

#include "base/logging.h"
#include "brave/components/ipfs/ipfs_constants.h"

namespace ipfs {

GURL ipfs_default_gateway_for_test;

void SetIPFSDefaultGatewayForTest(const GURL& url) {
ipfs_default_gateway_for_test = url;
}

GURL GetDefaultIPFSLocalGateway() {
return GURL(kDefaultIPFSLocalGateway);
}

GURL GetDefaultIPFSGateway() {
if (!ipfs_default_gateway_for_test.is_empty()) {
return GURL(ipfs_default_gateway_for_test);
}
return GURL(kDefaultIPFSGateway);
}

} // namespace ipfs
19 changes: 19 additions & 0 deletions components/ipfs/ipfs_gateway.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* 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_COMPONENTS_IPFS_IPFS_GATEWAY_H_
#define BRAVE_COMPONENTS_IPFS_IPFS_GATEWAY_H_

#include "url/gurl.h"

namespace ipfs {

void SetIPFSDefaultGatewayForTest(const GURL& url);
GURL GetDefaultIPFSLocalGateway();
GURL GetDefaultIPFSGateway();

} // namespace ipfs

#endif // BRAVE_COMPONENTS_IPFS_IPFS_GATEWAY_H_
59 changes: 58 additions & 1 deletion components/ipfs/ipfs_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
* 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 "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/test/scoped_feature_list.h"
#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/common/brave_paths.h"
#include "brave/components/ipfs/features.h"
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_gateway.h"
#include "brave/components/ipfs/ipfs_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/http_request.h"
Expand All @@ -33,7 +38,7 @@ class IpfsServiceBrowserTest : public InProcessBrowserTest {
IpfsServiceFactory::GetInstance()->GetForContext(browser()->profile());
ASSERT_TRUE(ipfs_service_);
ipfs_service_->SetIpfsLaunchedForTest(true);

host_resolver()->AddRule("*", "127.0.0.1");
InProcessBrowserTest::SetUpOnMainThread();
}

Expand All @@ -47,6 +52,16 @@ class IpfsServiceBrowserTest : public InProcessBrowserTest {
ipfs_service_->SetServerEndpointForTest(test_server_->base_url());
}

GURL GetURL(const std::string& path) {
return test_server_->GetURL("a.com", path);
}

void SetUpCommandLine(base::CommandLine* command_line) override {
// HTTPS server only serves a valid cert for localhost, so this is needed
// to load pages from other hosts without an error.
command_line->AppendSwitch(switches::kIgnoreCertificateErrors);
}

std::unique_ptr<net::test_server::HttpResponse> HandleGetConnectedPeers(
const net::test_server::HttpRequest& request) {
if (request.GetURL().path_piece() != kSwarmPeersPath) {
Expand Down Expand Up @@ -117,6 +132,25 @@ class IpfsServiceBrowserTest : public InProcessBrowserTest {
return http_response;
}

std::unique_ptr<net::test_server::HttpResponse> HandleEmbeddedSrvrRequest(
const net::test_server::HttpRequest& request) {
auto http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_content_type("text/html");

std::string request_path = request.GetURL().path();
if (request_path == "/simple.html") {
http_response->set_content("fetch test");
} else if (request_path ==
"/ipfs/"
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq") {
http_response->set_content("fetch test");
}
http_response->set_code(net::HTTP_OK);

return http_response;
}

const std::vector<std::string>& GetExpectedPeers() {
static std::vector<std::string> peers{
"/ip4/101.101.101.101/tcp/4001/p2p/"
Expand Down Expand Up @@ -232,4 +266,27 @@ IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, GetAddressesConfigServerError) {
WaitForRequest();
}

IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, CanFetchIPFSResources) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandleEmbeddedSrvrRequest,
base::Unretained(this)));
SetIPFSDefaultGatewayForTest(GetURL("/"));
ui_test_utils::NavigateToURL(browser(), GetURL("/simple.html"));
bool as_expected = false;
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(ExecuteScriptAndExtractBool(
contents,
"fetch('ipfs://"
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq')"
" .then(response => { response.text()"
" .then((response_text) => {"
" const result = response_text == 'fetch test';"
" window.domAutomationController.send(result);"
" })})"
".catch((x) => console.log('error: ' + x));",
&as_expected));
ASSERT_TRUE(as_expected);
}

} // namespace ipfs
7 changes: 4 additions & 3 deletions components/ipfs/ipfs_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <vector>

#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_gateway.h"
#include "brave/components/ipfs/translate_ipfs_uri.h"
#include "extensions/common/url_pattern.h"
#include "net/base/url_util.h"
Expand All @@ -25,12 +26,12 @@ bool HasIPFSPath(const GURL& gurl) {
}

bool IsDefaultGatewayURL(const GURL& url) {
return url.GetOrigin() == GURL(kDefaultIPFSGateway) && HasIPFSPath(url);
return url.GetOrigin() == GetDefaultIPFSGateway() && HasIPFSPath(url);
}

bool IsLocalGatewayURL(const GURL& url) {
return url.SchemeIsHTTPOrHTTPS() && net::IsLocalhost(url) &&
url.port_piece() == GURL(kDefaultIPFSLocalGateway).port_piece() &&
url.port_piece() == GetDefaultIPFSLocalGateway().port_piece() &&
HasIPFSPath(url);
}

Expand All @@ -51,7 +52,7 @@ GURL ToPublicGatewayURL(const GURL& url) {
// public gateway URL.
if (IsLocalGatewayURL(url)) {
GURL::Replacements replacements;
GURL gateway_url = GURL(kDefaultIPFSGateway);
GURL gateway_url = GetDefaultIPFSGateway();
replacements.ClearPort();
replacements.SetSchemeStr(gateway_url.scheme_piece());
replacements.SetHostStr(gateway_url.host_piece());
Expand Down
14 changes: 11 additions & 3 deletions components/ipfs/translate_ipfs_uri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/logging.h"
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_gateway.h"

namespace ipfs {

Expand Down Expand Up @@ -41,10 +42,17 @@ bool TranslateIPFSURI(const GURL& url, GURL* new_url, bool local) {
// 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) +
std::string gateway_empty_path =
local ? GetDefaultIPFSLocalGateway().spec()
: GetDefaultIPFSGateway().spec();
if (gateway_empty_path.size() > 0 &&
gateway_empty_path[gateway_empty_path.size() - 1] == '/') {
gateway_empty_path.pop_back();
}
*new_url = GURL(gateway_empty_path +
(ipfs_scheme ? "/ipfs/" : "/ipns/") + cid + path);
VLOG(1) << "[IPFS] " << __func__ << " new URL: " << *new_url;
VLOG(1) << "[IPFS] " << __func__ << " new URL: " << *new_url
<< ", and: " << GetDefaultIPFSGateway();
}

return true;
Expand Down