diff --git a/browser/ipfs/test/ipfs_service_browsertest.cc b/browser/ipfs/test/ipfs_service_browsertest.cc index 681de4fe2187..2450a9e4ba9c 100644 --- a/browser/ipfs/test/ipfs_service_browsertest.cc +++ b/browser/ipfs/test/ipfs_service_browsertest.cc @@ -395,20 +395,6 @@ class IpfsServiceBrowserTest : public InProcessBrowserTest { return http_response; } - std::unique_ptr HandlePublicGatewayRequest( - const net::test_server::HttpRequest& request) { - auto http_response = - std::make_unique(); - 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 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(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(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(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(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(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); - browser()->profile()->GetPrefs()->SetInteger( - kIPFSResolveMethod, - static_cast(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY)); GURL gateway = GetURL("b.com", "/"); SetIPFSDefaultGatewayForTest(gateway); - - auto tab_url = - GetURL("a.com", - "/ipfs/" - "bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq"); - + 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(), tab_url); -} + EXPECT_EQ(contents->GetURL().host(), tab_url.host()); -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(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( - contents->GetURL(), - GetURL( - "b.com", - "/ipfs/bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/" - "simple.html?a=b")); -} - -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(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(ipfs::IPFSResolveMethodTypes::IPFS_ASK)); - GURL gateway = GetURL("b.com", "/"); - SetIPFSDefaultGatewayForTest(gateway); - - auto tab_url = GetURL( - "bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.a.com", - "/simple.html?a=b"); - + 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( + auto domain = GetDomainAndRegistry( contents->GetURL(), - GURL("ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/" - "simple.html?a=b")); -} - -IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, - TopLevelAutoRedirectsOn_DoNotTranslateSimpleUrls) { - ResetTestServer( - base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest, - base::Unretained(this))); - browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true); - browser()->profile()->GetPrefs()->SetInteger( - kIPFSResolveMethod, - static_cast(ipfs::IPFSResolveMethodTypes::IPFS_ASK)); - GURL gateway = GetURL("b.com", "/"); - SetIPFSDefaultGatewayForTest(gateway); + net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); - auto tab_url = GetURL("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(domain, gateway.host()); } IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, - TopLevelAutoRedirectsOn_DoNotTranslateIncompleteUrls) { + 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(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)); + 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); + 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(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) { diff --git a/browser/net/ipfs_redirect_network_delegate_helper.cc b/browser/net/ipfs_redirect_network_delegate_helper.cc index 74c5fcb33a33..46795328738f 100644 --- a/browser/net/ipfs_redirect_network_delegate_helper.cc +++ b/browser/net/ipfs_redirect_network_delegate_helper.cc @@ -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 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; diff --git a/browser/net/ipfs_redirect_network_delegate_helper_unittest.cc b/browser/net/ipfs_redirect_network_delegate_helper_unittest.cc index ed8db916803e..f390e829ddca 100644 --- a/browser/net/ipfs_redirect_network_delegate_helper_unittest.cc +++ b/browser/net/ipfs_redirect_network_delegate_helper_unittest.cc @@ -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(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(url); request_info->browser_context = profile(); request_info->ipfs_gateway_url = GetPublicGateway(); @@ -332,10 +333,10 @@ TEST_F(IPFSRedirectNetworkDelegateHelperTest, scoped_refptr orig_response_headers = new net::HttpResponseHeaders(std::string()); + orig_response_headers->AddHeader("x-ipfs-path", "/test"); scoped_refptr 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 orig_response_headers = new net::HttpResponseHeaders(std::string()); + orig_response_headers->AddHeader("x-ipfs-path", "/test"); scoped_refptr overwrite_response_headers = new net::HttpResponseHeaders(std::string()); GURL allowed_unsafe_redirect_url; diff --git a/components/ipfs/ipfs_utils.cc b/components/ipfs/ipfs_utils.cc index 2f2889ee279d..c8a083559a26 100644 --- a/components/ipfs/ipfs_utils.cc +++ b/components/ipfs/ipfs_utils.cc @@ -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 TranslateToCurrentGatewayUrl(const GURL& url) { - if (!url.is_valid() || !url.SchemeIsHTTPOrHTTPS()) { - return absl::nullopt; - } - - std::vector 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 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 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 diff --git a/components/ipfs/ipfs_utils.h b/components/ipfs/ipfs_utils.h index 75d9d2f9b034..cfce863d7ae1 100644 --- a/components/ipfs/ipfs_utils.h +++ b/components/ipfs/ipfs_utils.h @@ -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 TranslateToCurrentGatewayUrl(const GURL& url); } // namespace ipfs diff --git a/components/ipfs/ipfs_utils_unittest.cc b/components/ipfs/ipfs_utils_unittest.cc index 8fc4e10aabd9..3fcce883c053 100644 --- a/components/ipfs/ipfs_utils_unittest.cc +++ b/components/ipfs/ipfs_utils_unittest.cc @@ -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/"))); - } -} diff --git a/components/ipfs/test/BUILD.gn b/components/ipfs/test/BUILD.gn index 65b150155d76..21e7a69cad29 100644 --- a/components/ipfs/test/BUILD.gn +++ b/components/ipfs/test/BUILD.gn @@ -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)