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

SNS should resolve domains like b.sol (uplift to 1.62.x) #21700

Merged
merged 1 commit into from
Jan 23, 2024
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
21 changes: 15 additions & 6 deletions components/brave_wallet/browser/json_rpc_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ using decentralized_dns::ResolveMethodTypes;
// The domain name should not start or end with hyphen (-).
// The domain name can be a subdomain.
// TLD & TLD-1 must be at least two characters.
constexpr char kDomainPattern[] =
constexpr char kEnsDomainPattern[] =
"(?:[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]\\.)+[A-Za-z]{2,}$";

// Dot separated alpha-numeric-hyphen strings ending with sol
constexpr char kSnsDomainPattern[] = R"(^(?:[a-z0-9-]+\.)+sol$)";

// Non empty group of symbols of a-z | 0-9 | hyphen(-).
// Then a dot.
// Then one of fixed suffixes(should match `supportedUDExtensions` array from
Expand Down Expand Up @@ -1524,7 +1527,7 @@ void JsonRpcService::SetSnsResolveMethod(mojom::ResolveMethod method) {

void JsonRpcService::EnsGetEthAddr(const std::string& domain,
EnsGetEthAddrCallback callback) {
if (!IsValidDomain(domain)) {
if (!IsValidEnsDomain(domain)) {
std::move(callback).Run(
"", false, mojom::ProviderError::kInvalidParams,
l10n_util::GetStringUTF8(IDS_WALLET_INVALID_PARAMETERS));
Expand Down Expand Up @@ -1595,7 +1598,7 @@ void JsonRpcService::OnEnsGetEthAddrTaskDone(

void JsonRpcService::SnsGetSolAddr(const std::string& domain,
SnsGetSolAddrCallback callback) {
if (!IsValidDomain(domain)) {
if (!IsValidSnsDomain(domain)) {
std::move(callback).Run(
"", mojom::SolanaProviderError::kInvalidParams,
l10n_util::GetStringUTF8(IDS_WALLET_INVALID_PARAMETERS));
Expand Down Expand Up @@ -1648,7 +1651,7 @@ void JsonRpcService::OnSnsGetSolAddrTaskDone(

void JsonRpcService::SnsResolveHost(const std::string& domain,
SnsResolveHostCallback callback) {
if (!IsValidDomain(domain)) {
if (!IsValidSnsDomain(domain)) {
std::move(callback).Run(
absl::nullopt, mojom::SolanaProviderError::kInvalidParams,
l10n_util::GetStringUTF8(IDS_WALLET_INVALID_PARAMETERS));
Expand Down Expand Up @@ -2075,8 +2078,14 @@ void JsonRpcService::OnGetBlockByNumber(GetBlockByNumberCallback callback,
}

/*static*/
bool JsonRpcService::IsValidDomain(const std::string& domain) {
static const base::NoDestructor<re2::RE2> kDomainRegex(kDomainPattern);
bool JsonRpcService::IsValidEnsDomain(const std::string& domain) {
static const base::NoDestructor<re2::RE2> kDomainRegex(kEnsDomainPattern);
return re2::RE2::FullMatch(domain, *kDomainRegex);
}

/*static*/
bool JsonRpcService::IsValidSnsDomain(const std::string& domain) {
static const base::NoDestructor<re2::RE2> kDomainRegex(kSnsDomainPattern);
return re2::RE2::FullMatch(domain, *kDomainRegex);
}

Expand Down
6 changes: 4 additions & 2 deletions components/brave_wallet/browser/json_rpc_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,14 @@ class JsonRpcService : public KeyedService, public mojom::JsonRpcService {
AddChainCallback callback,
APIRequestResult api_request_result);

FRIEND_TEST_ALL_PREFIXES(JsonRpcServiceUnitTest, IsValidDomain);
FRIEND_TEST_ALL_PREFIXES(JsonRpcServiceUnitTest, IsValidEnsDomain);
FRIEND_TEST_ALL_PREFIXES(JsonRpcServiceUnitTest, IsValidSnsDomain);
FRIEND_TEST_ALL_PREFIXES(JsonRpcServiceUnitTest, IsValidUnstoppableDomain);
FRIEND_TEST_ALL_PREFIXES(JsonRpcServiceUnitTest, Reset);
friend class JsonRpcServiceUnitTest;

static bool IsValidDomain(const std::string& domain);
static bool IsValidEnsDomain(const std::string& domain);
static bool IsValidSnsDomain(const std::string& domain);
static bool IsValidUnstoppableDomain(const std::string& domain);

void OnGetERC721OwnerOf(GetERC721OwnerOfCallback callback,
Expand Down
41 changes: 38 additions & 3 deletions components/brave_wallet/browser/json_rpc_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3609,19 +3609,54 @@ TEST_F(JsonRpcServiceUnitTest, GetWalletAddrInvalidDomain) {
}
}

TEST_F(JsonRpcServiceUnitTest, IsValidDomain) {
TEST_F(JsonRpcServiceUnitTest, IsValidEnsDomain) {
std::vector<std::string> valid_domains = {"brave.eth", "test.brave.eth",
"brave-test.test-dev.eth"};
for (const auto& domain : valid_domains) {
EXPECT_TRUE(JsonRpcService::IsValidDomain(domain))
EXPECT_TRUE(JsonRpcService::IsValidEnsDomain(domain))
<< domain << " should be valid";
}

std::vector<std::string> invalid_domains = {
"", ".eth", "-brave.eth", "brave-.eth", "brave.e-th",
"b.eth", "brave.e", "-brave.test.eth", "brave-.test.eth"};
for (const auto& domain : invalid_domains) {
EXPECT_FALSE(JsonRpcService::IsValidDomain(domain))
EXPECT_FALSE(JsonRpcService::IsValidEnsDomain(domain))
<< domain << " should be invalid";
}
}

TEST_F(JsonRpcServiceUnitTest, IsValidSnsDomain) {
std::vector<std::string> valid_domains = {
"brave.sol", //
"test.brave.sol", //
"brave-test.test-dev.sol", //
"b.sol", //
"w.sol", //
"-.sol", //
"-brave.sol", //
"brave-.sol", //
"---.sol", //
"-.-.sol", //
"-brave.test.sol", //
"brave-.test.sol" //
};
for (const auto& domain : valid_domains) {
EXPECT_TRUE(JsonRpcService::IsValidSnsDomain(domain))
<< domain << " should be valid";
}

std::vector<std::string> invalid_domains = {
"", //
"b.eth", //
".sol", //
"brave.s-ol", //
"B.sol", //
"brave.s", //
"b.Sol" //
};
for (const auto& domain : invalid_domains) {
EXPECT_FALSE(JsonRpcService::IsValidSnsDomain(domain))
<< domain << " should be invalid";
}
}
Expand Down