-
Notifications
You must be signed in to change notification settings - Fork 925
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
Support resolution of unstoppable domains with IPFS records via Ethereum #8456
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a189c89
Support resolution of unstoppable domains with IPFS records via Ethereum
yrliou 39802d2
Support getting resolve method list by providers
yrliou 64f5277
Add tests
yrliou a858ae7
Add more comments
yrliou e61a547
Add learn more link for Unstoppable Domains setting
yrliou 220df7b
lint fix
yrliou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
browser/net/decentralized_dns_network_delegate_helper.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* Copyright (c) 2021 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/browser/net/decentralized_dns_network_delegate_helper.h" | ||
|
||
#include <vector> | ||
|
||
#include "net/base/net_errors.h" | ||
|
||
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h" | ||
#include "brave/components/brave_wallet/browser/brave_wallet_service.h" | ||
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h" | ||
#include "brave/components/brave_wallet/browser/eth_json_rpc_controller.h" | ||
#include "brave/components/decentralized_dns/constants.h" | ||
#include "brave/components/decentralized_dns/utils.h" | ||
#include "chrome/browser/browser_process.h" | ||
#include "content/public/browser/browser_context.h" | ||
|
||
namespace decentralized_dns { | ||
|
||
namespace { | ||
|
||
std::string GetValue(const std::vector<std::string>& arr, RecordKeys key) { | ||
return arr[static_cast<size_t>(key)]; | ||
} | ||
|
||
} // namespace | ||
|
||
int OnBeforeURLRequest_DecentralizedDnsPreRedirectWork( | ||
const brave::ResponseCallback& next_callback, | ||
std::shared_ptr<brave::BraveRequestInfo> ctx) { | ||
if (!ctx->browser_context || !IsDecentralizedDnsEnabled() || | ||
ctx->browser_context->IsOffTheRecord() || !g_browser_process) { | ||
return net::OK; | ||
} | ||
|
||
if (IsUnstoppableDomainsTLD(ctx->request_url) && | ||
IsUnstoppableDomainsResolveMethodEthereum( | ||
g_browser_process->local_state())) { | ||
auto* service = BraveWalletServiceFactory::GetInstance()->GetForContext( | ||
ctx->browser_context); | ||
if (!service) { | ||
return net::OK; | ||
} | ||
|
||
service->controller()->UnstoppableDomainsProxyReaderGetMany( | ||
kProxyReaderContractAddress, ctx->request_url.host(), | ||
std::vector<std::string>(std::begin(kRecordKeys), | ||
std::end(kRecordKeys)), | ||
base::BindOnce(&OnBeforeURLRequest_DecentralizedDnsRedirectWork, | ||
next_callback, ctx)); | ||
|
||
return net::ERR_IO_PENDING; | ||
} | ||
|
||
return net::OK; | ||
} | ||
|
||
void OnBeforeURLRequest_DecentralizedDnsRedirectWork( | ||
const brave::ResponseCallback& next_callback, | ||
std::shared_ptr<brave::BraveRequestInfo> ctx, | ||
bool success, | ||
const std::string& result) { | ||
if (!success) { | ||
if (!next_callback.is_null()) | ||
next_callback.Run(); | ||
return; | ||
} | ||
|
||
std::vector<std::string> output; | ||
size_t offset = 2 /* len of "0x" */ + 64 /* len of offset to array */; | ||
if (offset > result.size() || | ||
!brave_wallet::DecodeStringArray(result.substr(offset), &output)) { | ||
if (!next_callback.is_null()) | ||
next_callback.Run(); | ||
return; | ||
} | ||
|
||
// Redirect to ipfs URI if content hash is set, otherwise, fallback to the | ||
// set redirect URL. If no records available to use, do nothing. See | ||
// https://docs.unstoppabledomains.com/browser-resolution/browser-resolution-algorithm | ||
// for more details. | ||
// | ||
// TODO(jocelyn): Do not fallback to the set redirect URL if dns.A or | ||
// dns.AAAA is not empty once we support the classical DNS records case. | ||
std::string ipfs_uri = GetValue(output, RecordKeys::DWEB_IPFS_HASH); | ||
yrliou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (ipfs_uri.empty()) { // Try legacy value. | ||
ipfs_uri = GetValue(output, RecordKeys::IPFS_HTML_VALUE); | ||
} | ||
|
||
std::string fallback_url = GetValue(output, RecordKeys::BROWSER_REDIRECT_URL); | ||
if (fallback_url.empty()) { // Try legacy value. | ||
fallback_url = GetValue(output, RecordKeys::IPFS_REDIRECT_DOMAIN_VALUE); | ||
} | ||
|
||
if (!ipfs_uri.empty()) { | ||
ctx->new_url_spec = GURL("ipfs://" + ipfs_uri).spec(); | ||
} else if (!fallback_url.empty()) { | ||
ctx->new_url_spec = GURL(fallback_url).spec(); | ||
} | ||
|
||
if (!next_callback.is_null()) | ||
next_callback.Run(); | ||
} | ||
|
||
} // namespace decentralized_dns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* Copyright (c) 2021 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_BROWSER_NET_DECENTRALIZED_DNS_NETWORK_DELEGATE_HELPER_H_ | ||
#define BRAVE_BROWSER_NET_DECENTRALIZED_DNS_NETWORK_DELEGATE_HELPER_H_ | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "brave/browser/net/url_context.h" | ||
#include "net/base/completion_once_callback.h" | ||
|
||
namespace decentralized_dns { | ||
|
||
// Issue eth_call requests via Ethereum provider such as Infura to query | ||
// decentralized DNS records, and redirect URL requests based on them. | ||
int OnBeforeURLRequest_DecentralizedDnsPreRedirectWork( | ||
yrliou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const brave::ResponseCallback& next_callback, | ||
std::shared_ptr<brave::BraveRequestInfo> ctx); | ||
|
||
void OnBeforeURLRequest_DecentralizedDnsRedirectWork( | ||
const brave::ResponseCallback& next_callback, | ||
std::shared_ptr<brave::BraveRequestInfo> ctx, | ||
bool success, | ||
const std::string& result); | ||
|
||
} // namespace decentralized_dns | ||
|
||
#endif // BRAVE_BROWSER_NET_DECENTRALIZED_DNS_NETWORK_DELEGATE_HELPER_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really clear: "many" of what? and the method doesn't explain anything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also curious whether we consider the endpoint as trustworthy.. In theory, we'd better to follow Rule of 2: https://chromium.googlesource.com/chromium/src/+/master/docs/security/rule-of-2.md
probably something to consider during sec review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's calling getMany function defined by Unstoppable Domains's ProxyReader smart contract.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We issue these calls to Unstoppable Domains smart contract via Infura to the Ethereum network.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some more comments in 1cdb4f081decc67ac46ac53ed9bee83fcf3ebb34, and created security review issue.