From b2a5f23f7616ec13c446a0268c512239001c3018 Mon Sep 17 00:00:00 2001 From: oisupov Date: Tue, 16 Aug 2022 02:44:27 +0700 Subject: [PATCH 1/2] Remove DNS.Resolvers from IPNS config if DOH is disabled Fixes https://github.com/brave/brave-browser/issues/24678 --- .../services/ipfs/ipfs_service_utils.cc | 2 + .../ipfs/ipfs_service_utils_unittest.cc | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/components/services/ipfs/ipfs_service_utils.cc b/components/services/ipfs/ipfs_service_utils.cc index a6eab363745c..7898930ccb9c 100644 --- a/components/services/ipfs/ipfs_service_utils.cc +++ b/components/services/ipfs/ipfs_service_utils.cc @@ -59,6 +59,8 @@ bool UpdateConfigJSON(const std::string& source, base::Value::Dict dns_resolvers; dns_resolvers.Set(".", *config->doh_server_url); dict->SetByDottedPath("DNS.Resolvers", std::move(dns_resolvers)); + } else { + dict->RemoveByDottedPath("DNS.Resolvers"); } base::Value::List list; diff --git a/components/services/ipfs/ipfs_service_utils_unittest.cc b/components/services/ipfs/ipfs_service_utils_unittest.cc index 8d5ccdd60a68..bb38cfe76cec 100644 --- a/components/services/ipfs/ipfs_service_utils_unittest.cc +++ b/components/services/ipfs/ipfs_service_utils_unittest.cc @@ -65,6 +65,49 @@ TEST_F(IPFSServiceUtils, UpdateConfigJSONTest) { EXPECT_EQ(updated, ""); } +TEST_F(IPFSServiceUtils, DNSResolversRemove) { + std::string updated; + { + std::string json = R"({})"; + + auto config = ipfs::mojom::IpfsConfig::New( + base::FilePath(), base::FilePath(), base::FilePath(), "GatewayPort", + "APIPort", "SwarmPort", "StorageSize", + "https://cloudflare.com/dns-query"); + + std::string expect = + "{\"Addresses\":{\"API\":\"/ip4/127.0.0.1/tcp/APIPort\"," + "\"Gateway\":\"/ip4/127.0.0.1/tcp/GatewayPort\",\"Swarm\":" + "[\"/ip4/0.0.0.0/tcp/SwarmPort\",\"/ip6/::/tcp/SwarmPort\"" + "]},\"DNS\":{\"Resolvers\":{\".\":\"https://cloudflare.com/" + "dns-query\"}}," + "\"Datastore\":{\"GCPeriod\":\"1h\",\"StorageMax\":" + "\"StorageSize\"},\"Swarm\":{\"ConnMgr\":{\"GracePeriod\":\"20s\"," + "\"HighWater\":40,\"LowWater\":20}}}"; + + EXPECT_TRUE(UpdateConfigJSON(json, config.get(), &updated)); + EXPECT_EQ(updated, expect); + } + + std::string json = updated; + + auto config = ipfs::mojom::IpfsConfig::New( + base::FilePath(), base::FilePath(), base::FilePath(), "GatewayPort", + "APIPort", "SwarmPort", "StorageSize", absl::nullopt); + + std::string expect = + "{\"Addresses\":{\"API\":\"/ip4/127.0.0.1/tcp/APIPort\"," + "\"Gateway\":\"/ip4/127.0.0.1/tcp/GatewayPort\",\"Swarm\":" + "[\"/ip4/0.0.0.0/tcp/SwarmPort\",\"/ip6/::/tcp/SwarmPort\"" + "]}," + "\"Datastore\":{\"GCPeriod\":\"1h\",\"StorageMax\":" + "\"StorageSize\"},\"Swarm\":{\"ConnMgr\":{\"GracePeriod\":\"20s\"," + "\"HighWater\":40,\"LowWater\":20}}}"; + + EXPECT_TRUE(UpdateConfigJSON(json, config.get(), &updated)); + ASSERT_EQ(updated, expect); +} + TEST_F(IPFSServiceUtils, DNSResolversUpdate) { std::string json = R"({})"; std::string updated; From 3beffc0740bbfdde3f45fd626360fd5efe6123b3 Mon Sep 17 00:00:00 2001 From: oisupov Date: Tue, 16 Aug 2022 22:20:14 +0700 Subject: [PATCH 2/2] Remove {?dns} template from DNS.Resolvers Fixes https://github.com/brave/brave-browser/issues/24706 Currently Kubo doesn't support such template and only uses POST requests --- .../services/ipfs/ipfs_service_utils.cc | 6 +++++- .../ipfs/ipfs_service_utils_unittest.cc | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/components/services/ipfs/ipfs_service_utils.cc b/components/services/ipfs/ipfs_service_utils.cc index 7898930ccb9c..fb87a095a25b 100644 --- a/components/services/ipfs/ipfs_service_utils.cc +++ b/components/services/ipfs/ipfs_service_utils.cc @@ -12,6 +12,7 @@ #include "base/json/json_reader.h" #include "base/json/json_writer.h" #include "base/strings/strcat.h" +#include "base/strings/string_util.h" #include "brave/components/services/ipfs/public/mojom/ipfs_service.mojom.h" #include "third_party/abseil-cpp/absl/types/optional.h" #include "third_party/re2/src/re2/re2.h" @@ -57,7 +58,10 @@ bool UpdateConfigJSON(const std::string& source, if (config->doh_server_url) { base::Value::Dict dns_resolvers; - dns_resolvers.Set(".", *config->doh_server_url); + std::string doh_url = *(config->doh_server_url); + // Kubo doesn't support RFC-8484 DOH url format + base::ReplaceSubstringsAfterOffset(&doh_url, 0, "{?dns}", ""); + dns_resolvers.Set(".", std::move(doh_url)); dict->SetByDottedPath("DNS.Resolvers", std::move(dns_resolvers)); } else { dict->RemoveByDottedPath("DNS.Resolvers"); diff --git a/components/services/ipfs/ipfs_service_utils_unittest.cc b/components/services/ipfs/ipfs_service_utils_unittest.cc index bb38cfe76cec..c936c001f6ea 100644 --- a/components/services/ipfs/ipfs_service_utils_unittest.cc +++ b/components/services/ipfs/ipfs_service_utils_unittest.cc @@ -128,6 +128,26 @@ TEST_F(IPFSServiceUtils, DNSResolversUpdate) { EXPECT_EQ(updated, expect); } +TEST_F(IPFSServiceUtils, DNSResolversUpdate_DnsHasRFC8484Template) { + std::string json = R"({})"; + std::string updated; + auto config = ipfs::mojom::IpfsConfig::New( + base::FilePath(), base::FilePath(), base::FilePath(), "GatewayPort", + "APIPort", "SwarmPort", "StorageSize", + "https://cloudflare.com/dns-query{?dns}"); + + std::string expect = + "{\"Addresses\":{\"API\":\"/ip4/127.0.0.1/tcp/APIPort\"," + "\"Gateway\":\"/ip4/127.0.0.1/tcp/GatewayPort\",\"Swarm\":" + "[\"/ip4/0.0.0.0/tcp/SwarmPort\",\"/ip6/::/tcp/SwarmPort\"" + "]},\"DNS\":{\"Resolvers\":{\".\":\"https://cloudflare.com/dns-query\"}}," + "\"Datastore\":{\"GCPeriod\":\"1h\",\"StorageMax\":" + "\"StorageSize\"},\"Swarm\":{\"ConnMgr\":{\"GracePeriod\":\"20s\"," + "\"HighWater\":40,\"LowWater\":20}}}"; + ASSERT_TRUE(UpdateConfigJSON(json, config.get(), &updated)); + EXPECT_EQ(updated, expect); +} + TEST_F(IPFSServiceUtils, GetVerisonFromNodeFilename) { EXPECT_EQ( ipfs::GetVersionFromNodeFilename("go-ipfs_v0.9.0-rc1_windows-amd64"),