Revert "Redirect gateway-like urls to ipfs://"
This commit is contained in:
@@ -395,20 +395,6 @@ class IpfsServiceBrowserTest : public InProcessBrowserTest {
|
||||
return http_response;
|
||||
}
|
||||
|
||||
std::unique_ptr<net::test_server::HttpResponse> HandlePublicGatewayRequest(
|
||||
const net::test_server::HttpRequest& request) {
|
||||
auto http_response =
|
||||
std::make_unique<net::test_server::BasicHttpResponse>();
|
||||
http_response->set_content_type("text/html");
|
||||
|
||||
// IPFS gateways set this
|
||||
http_response->AddCustomHeader("access-control-allow-origin", "*");
|
||||
http_response->AddCustomHeader("x-ipfs-path", "/ipfs/Qmm");
|
||||
http_response->set_code(net::HTTP_OK);
|
||||
|
||||
return http_response;
|
||||
}
|
||||
|
||||
std::unique_ptr<net::test_server::HttpResponse> HandleEmbeddedSrvrRequest(
|
||||
const net::test_server::HttpRequest& request) {
|
||||
auto http_response =
|
||||
@@ -1067,291 +1053,57 @@ IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, CannotLoadIPFSImageFromHTTP) {
|
||||
EXPECT_EQ(base::Value(true), loaded.value);
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest,
|
||||
TopLevelAutoRedirectsOff_DoNotTranslateToIPFS) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, false);
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url = GetURL(
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.a.com",
|
||||
"/simple.html?a=b");
|
||||
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(contents->GetURL(), tab_url);
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(
|
||||
IpfsServiceBrowserTest,
|
||||
TopLevelAutoRedirectsOn_DoNotRedirectFromGatewayLikeUrl_IpfsDisabled) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_DISABLED));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url = GetURL(
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.a.com",
|
||||
"/simple.html?a=b");
|
||||
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(contents->GetURL(), tab_url);
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(
|
||||
IpfsServiceBrowserTest,
|
||||
TopLevelAutoRedirectsOn_Gateway_RedirectFromGatewayLikeUrl_IpfsSubDomain) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url = GetURL(
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.a.com",
|
||||
"/simple.html?a=b");
|
||||
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(
|
||||
contents->GetURL(),
|
||||
GetURL(
|
||||
"b.com",
|
||||
"/ipfs/bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
|
||||
"simple.html?a=b"));
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(
|
||||
IpfsServiceBrowserTest,
|
||||
TopLevelAutoRedirectsOn_DoNotRedirectFromGatewayLikeUrl_IfGatewayUrl) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url = GetURL(
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.b.com",
|
||||
"/simple.html?a=b");
|
||||
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(contents->GetURL(), tab_url);
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(
|
||||
IpfsServiceBrowserTest,
|
||||
TopLevelAutoRedirectsOn_DoNotRedirectFromGatewayLikeUrl_IfLocalhostUrl) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url = GetURL(
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs."
|
||||
"localhost",
|
||||
"/simple.html?a=b");
|
||||
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(contents->GetURL(), tab_url);
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(
|
||||
IpfsServiceBrowserTest,
|
||||
TopLevelAutoRedirectsOn_DoNotRedirectFromGatewayLikeUrl_NoXIpfsPathHeader) {
|
||||
IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, TopLevelAutoRedirectsOn) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandleEmbeddedSrvrRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
auto tab_url = GetURL("a.com", "/simple.html");
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(contents->GetURL().host(), tab_url.host());
|
||||
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url =
|
||||
GetURL("a.com",
|
||||
"/ipfs/"
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq");
|
||||
|
||||
tab_url = GURL("ipfs://Qmc2JTQo4iXf24g98otZmGFQq176eQ2Cdbb88qA5ToMEvC/2");
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(contents->GetURL(), tab_url);
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(
|
||||
IpfsServiceBrowserTest,
|
||||
TopLevelAutoRedirectsOn_Gateway_RedirectFromGatewayLikeUrl_IpfsPath) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url = GetURL(
|
||||
"a.com",
|
||||
"/ipfs/bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
|
||||
"simple.html?a=b");
|
||||
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(
|
||||
auto domain = GetDomainAndRegistry(
|
||||
contents->GetURL(),
|
||||
GetURL(
|
||||
"b.com",
|
||||
"/ipfs/bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
|
||||
"simple.html?a=b"));
|
||||
}
|
||||
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(
|
||||
IpfsServiceBrowserTest,
|
||||
TopLevelAutoRedirectsOn_ASK_RedirectFromGatewayLikeUrl_IpfsPath) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url = GetURL(
|
||||
"a.com",
|
||||
"/ipfs/bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
|
||||
"simple.html?a=b");
|
||||
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(
|
||||
contents->GetURL(),
|
||||
GURL("ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
|
||||
"simple.html?a=b"));
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(
|
||||
IpfsServiceBrowserTest,
|
||||
TopLevelAutoRedirectsOn_ASK_RedirectFromGatewayLikeUrl_IpfsSubDomain) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url = GetURL(
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.a.com",
|
||||
"/simple.html?a=b");
|
||||
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(
|
||||
contents->GetURL(),
|
||||
GURL("ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
|
||||
"simple.html?a=b"));
|
||||
EXPECT_EQ(domain, gateway.host());
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest,
|
||||
TopLevelAutoRedirectsOn_DoNotTranslateSimpleUrls) {
|
||||
TopLevelAutoRedirectsOnWithQuery) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandleEmbeddedSrvrRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url = GetURL("a.com", "/simple.html?a=b");
|
||||
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(
|
||||
browser(), GetURL("a.com", "/simple.html?abc=123xyz&other=qwerty")));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(contents->GetURL(), tab_url);
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest,
|
||||
TopLevelAutoRedirectsOn_DoNotTranslateIncompleteUrls) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url = GetURL("ipfs.a.com", "/simple.html?a=b");
|
||||
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(contents->GetURL(), tab_url);
|
||||
EXPECT_EQ(contents->GetURL().query(), "abc=123xyz&other=qwerty");
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, TopLevelAutoRedirectsOff) {
|
||||
ResetTestServer(
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
|
||||
base::BindRepeating(&IpfsServiceBrowserTest::HandleEmbeddedSrvrRequest,
|
||||
base::Unretained(this)));
|
||||
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, false);
|
||||
browser()->profile()->GetPrefs()->SetInteger(
|
||||
kIPFSResolveMethod,
|
||||
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK));
|
||||
GURL gateway = GetURL("b.com", "/");
|
||||
SetIPFSDefaultGatewayForTest(gateway);
|
||||
|
||||
auto tab_url = GetURL(
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.a.com",
|
||||
"/simple.html?a=b");
|
||||
|
||||
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
|
||||
SetIPFSDefaultGatewayForTest(GetURL("b.com", "/"));
|
||||
GURL other_gateway = GetURL("a.com", "/simple.html");
|
||||
ASSERT_TRUE(
|
||||
ui_test_utils::NavigateToURL(browser(), GetURL("a.com", "/simple.html")));
|
||||
content::WebContents* contents =
|
||||
browser()->tab_strip_model()->GetActiveWebContents();
|
||||
EXPECT_EQ(contents->GetURL(), tab_url);
|
||||
EXPECT_EQ(contents->GetURL().host(), other_gateway.host());
|
||||
}
|
||||
|
||||
IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, ImportTextToIpfs) {
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
#include "components/user_prefs/user_prefs.h"
|
||||
#include "content/public/browser/browser_context.h"
|
||||
#include "net/base/net_errors.h"
|
||||
#include "net/base/url_util.h"
|
||||
|
||||
namespace ipfs {
|
||||
|
||||
@@ -95,13 +94,6 @@ int OnHeadersReceived_IPFSRedirectWork(
|
||||
std::shared_ptr<brave::BraveRequestInfo> ctx) {
|
||||
if (!ctx->browser_context)
|
||||
return net::OK;
|
||||
|
||||
// Auto-redirect gateway-like urls is enabled only for top-level frames
|
||||
// to avoid mixed content corner cases.
|
||||
if (ctx->resource_type == blink::mojom::ResourceType::kSubFrame) {
|
||||
return net::OK;
|
||||
}
|
||||
|
||||
auto* prefs = user_prefs::UserPrefs::Get(ctx->browser_context);
|
||||
if (IsIpfsResolveMethodDisabled(prefs)) {
|
||||
return net::OK;
|
||||
@@ -113,21 +105,24 @@ int OnHeadersReceived_IPFSRedirectWork(
|
||||
response_headers->GetNormalizedHeader("x-ipfs-path", &ipfs_path) &&
|
||||
// Make sure we don't infinite redirect
|
||||
!ctx->request_url.DomainIs(ctx->ipfs_gateway_url.host()) &&
|
||||
!net::IsLocalhost(ctx->request_url)) {
|
||||
auto translated_url = ipfs::TranslateToCurrentGatewayUrl(ctx->request_url);
|
||||
// Do not redirect if the frame is not ipfs/ipns
|
||||
IsIPFSScheme(ctx->initiator_url)) {
|
||||
GURL::Replacements replacements;
|
||||
replacements.SetPathStr(ipfs_path);
|
||||
|
||||
if (!translated_url) {
|
||||
return net::OK;
|
||||
if (ctx->request_url.has_query()) {
|
||||
replacements.SetQueryStr(ctx->request_url.query_piece());
|
||||
}
|
||||
|
||||
GURL new_url = ctx->ipfs_gateway_url.ReplaceComponents(replacements);
|
||||
|
||||
*override_response_headers =
|
||||
new net::HttpResponseHeaders(response_headers->raw_headers());
|
||||
(*override_response_headers)
|
||||
->ReplaceStatusLine("HTTP/1.1 307 Temporary Redirect");
|
||||
(*override_response_headers)->RemoveHeader("Location");
|
||||
(*override_response_headers)
|
||||
->AddHeader("Location", translated_url.value().spec());
|
||||
*allowed_unsafe_redirect_url = translated_url.value();
|
||||
(*override_response_headers)->AddHeader("Location", new_url.spec());
|
||||
*allowed_unsafe_redirect_url = new_url;
|
||||
}
|
||||
|
||||
return net::OK;
|
||||
|
||||
@@ -286,7 +286,7 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest, TranslateIPFSURIIPNSScheme) {
|
||||
TEST_F(IPFSRedirectNetworkDelegateHelperTest, HeadersIPFSWorkWithRedirect) {
|
||||
GURL url(
|
||||
"https://cloudflare-ipfs.com/ipfs/"
|
||||
"QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd/path/?a=b");
|
||||
"QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd");
|
||||
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);
|
||||
request_info->browser_context = profile();
|
||||
request_info->ipfs_gateway_url = GetPublicGateway();
|
||||
@@ -312,15 +312,16 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest, HeadersIPFSWorkWithRedirect) {
|
||||
std::string location;
|
||||
EXPECT_TRUE(overwrite_response_headers->EnumerateHeader(nullptr, "Location",
|
||||
&location));
|
||||
GURL converted_url =
|
||||
GURL("ipfs://QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd/path?a=b");
|
||||
EXPECT_EQ(GURL(location), converted_url);
|
||||
GURL converted_url = GURL("https://dweb.link/test");
|
||||
EXPECT_EQ(location, converted_url);
|
||||
EXPECT_EQ(allowed_unsafe_redirect_url, converted_url);
|
||||
}
|
||||
|
||||
TEST_F(IPFSRedirectNetworkDelegateHelperTest,
|
||||
HeadersIPFSWorkWithNoRedirect_NoHeader) {
|
||||
GURL url("https://brave.com");
|
||||
HeadersIPFSWorkWithNoRedirectHttps) {
|
||||
GURL url(
|
||||
"https://cloudflare-ipfs.com/ipfs/"
|
||||
"QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd");
|
||||
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);
|
||||
request_info->browser_context = profile();
|
||||
request_info->ipfs_gateway_url = GetPublicGateway();
|
||||
@@ -332,10 +333,10 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest,
|
||||
|
||||
scoped_refptr<net::HttpResponseHeaders> orig_response_headers =
|
||||
new net::HttpResponseHeaders(std::string());
|
||||
orig_response_headers->AddHeader("x-ipfs-path", "/test");
|
||||
scoped_refptr<net::HttpResponseHeaders> overwrite_response_headers =
|
||||
new net::HttpResponseHeaders(std::string());
|
||||
GURL allowed_unsafe_redirect_url;
|
||||
orig_response_headers->AddHeader("x-ipfs-path", "/test");
|
||||
|
||||
int rc = ipfs::OnHeadersReceived_IPFSRedirectWork(
|
||||
orig_response_headers.get(), &overwrite_response_headers,
|
||||
@@ -350,8 +351,7 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest,
|
||||
EXPECT_TRUE(allowed_unsafe_redirect_url.is_empty());
|
||||
}
|
||||
|
||||
TEST_F(IPFSRedirectNetworkDelegateHelperTest,
|
||||
HeadersIPFSWorkNoRedirect_WrongFormat) {
|
||||
TEST_F(IPFSRedirectNetworkDelegateHelperTest, HeadersIPFSWorkNoRedirect) {
|
||||
GURL url(
|
||||
"https://cloudflare-ipfs.com/ipfs/"
|
||||
"QmSrPmbaUKA3ZodhzPWZnpFgcPMFWF4QsxXbkWfEptTBJd");
|
||||
@@ -365,6 +365,7 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest,
|
||||
|
||||
scoped_refptr<net::HttpResponseHeaders> orig_response_headers =
|
||||
new net::HttpResponseHeaders(std::string());
|
||||
orig_response_headers->AddHeader("x-ipfs-path", "/test");
|
||||
scoped_refptr<net::HttpResponseHeaders> overwrite_response_headers =
|
||||
new net::HttpResponseHeaders(std::string());
|
||||
GURL allowed_unsafe_redirect_url;
|
||||
|
||||
@@ -445,47 +445,4 @@ std::string GetRegistryDomainFromIPNS(const GURL& url) {
|
||||
cid, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
|
||||
}
|
||||
|
||||
// gateway.io/ipfs/bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy ->
|
||||
// ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy
|
||||
// bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy.ipfs.gateway.io ->
|
||||
// ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy
|
||||
absl::optional<GURL> TranslateToCurrentGatewayUrl(const GURL& url) {
|
||||
if (!url.is_valid() || !url.SchemeIsHTTPOrHTTPS()) {
|
||||
return absl::nullopt;
|
||||
}
|
||||
|
||||
std::vector<std::string> host_parts = base::SplitStringUsingSubstr(
|
||||
url.host(), ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
|
||||
|
||||
if (host_parts.size() > 2 && IsValidCID(host_parts.at(0)) &&
|
||||
host_parts.at(1) == "ipfs") {
|
||||
GURL final_url = GURL("ipfs://" + host_parts.at(0) + url.path());
|
||||
GURL::Replacements replacements;
|
||||
replacements.SetQueryStr(url.query_piece());
|
||||
replacements.SetRefStr(url.ref_piece());
|
||||
return final_url.ReplaceComponents(replacements);
|
||||
}
|
||||
|
||||
std::vector<std::string> path_parts = base::SplitStringUsingSubstr(
|
||||
url.path(), "/", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
|
||||
|
||||
if (path_parts.size() >= 2 && path_parts.at(0) == "ipfs" &&
|
||||
IsValidCID(path_parts.at(1))) {
|
||||
std::string final_path;
|
||||
if (path_parts.size() >= 3) {
|
||||
std::vector<std::string> final_path_parts(path_parts.begin() + 2,
|
||||
path_parts.end());
|
||||
final_path = "/" + base::JoinString(final_path_parts, "/");
|
||||
}
|
||||
|
||||
GURL final_url = GURL("ipfs://" + path_parts.at(1) + final_path);
|
||||
GURL::Replacements replacements;
|
||||
replacements.SetQueryStr(url.query_piece());
|
||||
replacements.SetRefStr(url.ref_piece());
|
||||
return final_url.ReplaceComponents(replacements);
|
||||
}
|
||||
|
||||
return absl::nullopt;
|
||||
}
|
||||
|
||||
} // namespace ipfs
|
||||
|
||||
@@ -66,7 +66,6 @@ bool IsIpfsResolveMethodDisabled(PrefService* prefs);
|
||||
bool IsIpfsResolveMethodAsk(PrefService* prefs);
|
||||
std::string GetRegistryDomainFromIPNS(const GURL& url);
|
||||
bool IsValidCIDOrDomain(const std::string& value);
|
||||
absl::optional<GURL> TranslateToCurrentGatewayUrl(const GURL& url);
|
||||
|
||||
} // namespace ipfs
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
#include "components/version_info/channel.h"
|
||||
#include "net/base/url_util.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
#include "third_party/abseil-cpp/absl/types/optional.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
class IpfsUtilsUnitTest : public testing::Test {
|
||||
@@ -714,61 +713,3 @@ TEST_F(IpfsUtilsUnitTest, TranslateXIPFSPath) {
|
||||
ASSERT_FALSE(ipfs::TranslateXIPFSPath("/ipfs/"));
|
||||
ASSERT_FALSE(ipfs::TranslateXIPFSPath("/ipns/"));
|
||||
}
|
||||
|
||||
TEST_F(IpfsUtilsUnitTest, TranslateToCurrentGatewayUrl) {
|
||||
{
|
||||
GURL url =
|
||||
ipfs::TranslateToCurrentGatewayUrl(
|
||||
GURL("https://ipfs.io/ipfs/"
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"))
|
||||
.value();
|
||||
EXPECT_EQ(url, GURL("ipfs://"
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy"
|
||||
"avhwq"));
|
||||
}
|
||||
|
||||
{
|
||||
GURL url = ipfs::TranslateToCurrentGatewayUrl(
|
||||
GURL("https://ipfs.io/ipfs//////"
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy"
|
||||
"avhwq////p1////Index.html#ref"))
|
||||
.value();
|
||||
EXPECT_EQ(url, GURL("ipfs://"
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy"
|
||||
"avhwq/p1/Index.html#ref"));
|
||||
}
|
||||
|
||||
{
|
||||
GURL url = ipfs::TranslateToCurrentGatewayUrl(
|
||||
GURL("https://ipfs.io/ipfs////"
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy"
|
||||
"avhwq////p1/Index.html?a=b#ref"))
|
||||
.value();
|
||||
EXPECT_EQ(url, GURL("ipfs://"
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy"
|
||||
"avhwq/p1/Index.html?a=b#ref"));
|
||||
}
|
||||
|
||||
{
|
||||
GURL url = ipfs::TranslateToCurrentGatewayUrl(
|
||||
GURL("https://"
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy"
|
||||
"avhwq.ipfs.ipfs.io/p1/Index.html?a=b#ref"))
|
||||
.value();
|
||||
EXPECT_EQ(url, GURL("ipfs://"
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy"
|
||||
"avhwq/p1/Index.html?a=b#ref"));
|
||||
}
|
||||
|
||||
{
|
||||
EXPECT_FALSE(ipfs::TranslateToCurrentGatewayUrl(
|
||||
GURL("https://"
|
||||
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfy"
|
||||
"avhwq.abc.io")));
|
||||
}
|
||||
|
||||
{
|
||||
EXPECT_FALSE(
|
||||
ipfs::TranslateToCurrentGatewayUrl(GURL("https://abc.io/ipfs/")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,6 @@ source_set("brave_ipfs_unit_tests") {
|
||||
"//services/data_decoder/public/cpp:test_support",
|
||||
"//services/network:test_support",
|
||||
"//testing/gtest",
|
||||
"//third_party/abseil-cpp:absl",
|
||||
"//url",
|
||||
]
|
||||
} # if (enable_ipfs)
|
||||
|
||||
Reference in New Issue
Block a user