Skip to content

Commit c652170

Browse files
authored
Merge pull request #7785 from brave/change-ipfs-scheme-handling
Change ipfs scheme handling
2 parents 6801d67 + cd2b323 commit c652170

6 files changed

+94
-1
lines changed

browser/ipfs/content_browser_client_helper.cc

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ bool HandleIPFSURLReverseRewrite(
7575
if (ipfs_pos == std::string::npos && ipns_pos == std::string::npos)
7676
return false;
7777

78+
GURL configured_gateway =
79+
GetConfiguredBaseGateway(browser_context, chrome::GetChannel());
80+
if (configured_gateway.port() != url->port())
81+
return false;
7882
GURL::Replacements scheme_replacements;
7983
GURL::Replacements host_replacements;
8084
if (ipfs_pos != std::string::npos) {

browser/ipfs/content_browser_client_helper_unittest.cc

+72
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
#include "brave/browser/ipfs/content_browser_client_helper.h"
77

88
#include <memory>
9+
#include <string>
910
#include <vector>
1011

1112
#include "base/test/bind.h"
1213
#include "base/test/scoped_feature_list.h"
1314
#include "brave/components/ipfs/features.h"
1415
#include "brave/components/ipfs/ipfs_constants.h"
16+
#include "brave/components/ipfs/ipfs_ports.h"
17+
#include "brave/components/ipfs/ipfs_utils.h"
1518
#include "brave/components/ipfs/pref_names.h"
1619
#include "chrome/browser/prefs/browser_prefs.h"
1720
#include "chrome/common/channel_info.h"
@@ -137,4 +140,73 @@ TEST_F(ContentBrowserClientHelperUnitTest, HandleIPNSURLRewriteLocal) {
137140
ASSERT_TRUE(HandleIPFSURLRewrite(&ipns_uri, browser_context()));
138141
}
139142

143+
TEST_F(ContentBrowserClientHelperUnitTest, HandleIPFSURLReverseRewriteLocal) {
144+
profile()->GetPrefs()->SetInteger(
145+
kIPFSResolveMethod, static_cast<int>(IPFSResolveMethodTypes::IPFS_LOCAL));
146+
147+
GURL gateway_url("http://localhost/");
148+
GURL::Replacements replacements;
149+
const std::string& port = ipfs::GetGatewayPort(chrome::GetChannel());
150+
replacements.SetPortStr(port);
151+
gateway_url = gateway_url.ReplaceComponents(replacements);
152+
153+
ASSERT_EQ(
154+
ipfs::GetConfiguredBaseGateway(browser_context(), chrome::GetChannel()),
155+
gateway_url);
156+
157+
std::string source = "http://test.com.ipns.localhost:8000/";
158+
GURL ipns_uri(source);
159+
ASSERT_FALSE(HandleIPFSURLReverseRewrite(&ipns_uri, browser_context()));
160+
ASSERT_EQ(ipns_uri.spec(), source);
161+
162+
ipns_uri = GURL("http://test.com.ipns.localhost/");
163+
ipns_uri = ipns_uri.ReplaceComponents(replacements);
164+
ASSERT_TRUE(HandleIPFSURLReverseRewrite(&ipns_uri, browser_context()));
165+
ASSERT_EQ(ipns_uri.spec(), "ipns://test.com/");
166+
}
167+
168+
TEST_F(ContentBrowserClientHelperUnitTest, HandleIPFSURLReverseRewriteGateway) {
169+
profile()->GetPrefs()->SetInteger(
170+
kIPFSResolveMethod,
171+
static_cast<int>(IPFSResolveMethodTypes::IPFS_GATEWAY));
172+
ASSERT_EQ(ipfs::GetConfiguredBaseGateway(browser_context(),
173+
version_info::Channel::UNKNOWN),
174+
GURL("https://dweb.link/"));
175+
176+
std::string source = "http://test.com.ipns.localhost:8000/";
177+
GURL ipns_uri(source);
178+
ASSERT_FALSE(HandleIPFSURLReverseRewrite(&ipns_uri, browser_context()));
179+
ASSERT_EQ(ipns_uri.spec(), source);
180+
181+
source = "https://ku2jvrakgpiqgx4j6fe.ipfs.dweb.link/";
182+
ipns_uri = GURL(source);
183+
ASSERT_FALSE(HandleIPFSURLReverseRewrite(&ipns_uri, browser_context()));
184+
ASSERT_EQ(ipns_uri.spec(), source);
185+
186+
profile()->GetPrefs()->SetString(kIPFSPublicGatewayAddress,
187+
"http://localhost:8080");
188+
ASSERT_EQ(ipfs::GetConfiguredBaseGateway(browser_context(),
189+
version_info::Channel::UNKNOWN),
190+
GURL("http://localhost:8080"));
191+
192+
source = "http://test.com.ipns.localhost:8000/";
193+
ipns_uri = GURL(source);
194+
ASSERT_FALSE(HandleIPFSURLReverseRewrite(&ipns_uri, browser_context()));
195+
ASSERT_EQ(ipns_uri.spec(), source);
196+
197+
source = "https://ku2jvrakgpiqgx4j6fe.ipfs.dweb.link/";
198+
ipns_uri = GURL(source);
199+
ASSERT_FALSE(HandleIPFSURLReverseRewrite(&ipns_uri, browser_context()));
200+
ASSERT_EQ(ipns_uri.spec(), source);
201+
202+
source = "https://ku2jvrakgpiqgx4j6fe.ipfs.dweb.link:8080/";
203+
ipns_uri = GURL(source);
204+
ASSERT_FALSE(HandleIPFSURLReverseRewrite(&ipns_uri, browser_context()));
205+
ASSERT_EQ(ipns_uri.spec(), source);
206+
207+
ipns_uri = GURL("http://test.com.ipns.localhost:8080/");
208+
ASSERT_TRUE(HandleIPFSURLReverseRewrite(&ipns_uri, browser_context()));
209+
ASSERT_EQ(ipns_uri.spec(), "ipns://test.com/");
210+
}
211+
140212
} // namespace ipfs

components/ipfs/ipfs_constants.cc

+2
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ const char kRepoStatsPath[] = "/api/v0/repo/stat";
2323
const char kRepoStatsHumanReadableParamName[] = "human";
2424
const char kRepoStatsHumanReadableParamValue[] = "true";
2525
const char kNodeInfoPath[] = "/api/v0/id";
26+
const char kLocalhostIP[] = "127.0.0.1";
27+
const char kLocalhostDomain[] = "localhost";
2628
} // namespace ipfs

components/ipfs/ipfs_constants.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ extern const char kRepoStatsPath[];
2222
extern const char kRepoStatsHumanReadableParamName[];
2323
extern const char kRepoStatsHumanReadableParamValue[];
2424
extern const char kNodeInfoPath[];
25+
extern const char kLocalhostIP[];
26+
extern const char kLocalhostDomain[];
2527

2628
enum class IPFSResolveMethodTypes {
2729
IPFS_ASK,

components/ipfs/ipfs_utils.cc

+7-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,13 @@ GURL GetDefaultIPFSGateway(content::BrowserContext* context) {
183183

184184
DCHECK(context);
185185
PrefService* prefs = user_prefs::UserPrefs::Get(context);
186-
return GURL(prefs->GetString(kIPFSPublicGatewayAddress));
186+
GURL gateway_url(prefs->GetString(kIPFSPublicGatewayAddress));
187+
if (gateway_url.DomainIs(kLocalhostIP)) {
188+
GURL::Replacements replacements;
189+
replacements.SetHostStr(kLocalhostDomain);
190+
return gateway_url.ReplaceComponents(replacements);
191+
}
192+
return gateway_url;
187193
}
188194

189195
GURL GetAPIServer(version_info::Channel channel) {

components/ipfs/ipfs_utils_unittest.cc

+7
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ TEST_F(IpfsUtilsUnitTest, GetDefaultIPFSGateway) {
217217
prefs()->SetString(kIPFSPublicGatewayAddress, "https://example.com/");
218218
EXPECT_EQ(ipfs::GetDefaultIPFSGateway(context()),
219219
GURL("https://example.com/"));
220+
prefs()->SetString(kIPFSPublicGatewayAddress, "https://127.0.0.1:8888/");
221+
EXPECT_EQ(ipfs::GetDefaultIPFSGateway(context()),
222+
GURL("https://localhost:8888/"));
223+
prefs()->SetString(kIPFSPublicGatewayAddress, "https://127.0.0.1/");
224+
EXPECT_EQ(ipfs::GetDefaultIPFSGateway(context()), GURL("https://localhost/"));
225+
prefs()->SetString(kIPFSPublicGatewayAddress, "https://localhost/");
226+
EXPECT_EQ(ipfs::GetDefaultIPFSGateway(context()), GURL("https://localhost/"));
220227
}
221228

222229
TEST_F(IpfsUtilsUnitTest, TranslateIPFSURINotIPFSScheme) {

0 commit comments

Comments
 (0)