Skip to content

Commit

Permalink
Fix UNSAFE_TODO for wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
supermassive committed Nov 7, 2024
1 parent 0693065 commit e875f58
Show file tree
Hide file tree
Showing 24 changed files with 191 additions and 317 deletions.
12 changes: 2 additions & 10 deletions browser/ui/webui/brave_wallet/android/android_wallet_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,8 @@ AndroidWalletPageUI::AndroidWalletPageUI(content::WebUI* web_ui,
}

// Add required resources.
if (url.host() == kWalletPageHost) {
webui::SetupWebUIDataSource(
source,
UNSAFE_TODO(base::make_span(kBraveWalletPageGenerated,
kBraveWalletPageGeneratedSize)),
IDR_WALLET_PAGE_HTML);
} else {
NOTREACHED_IN_MIGRATION()
<< "Failed to find page resources for:" << url.path();
}
webui::SetupWebUIDataSource(source, base::span(kBraveWalletPageGenerated),
IDR_WALLET_PAGE_HTML);

source->AddBoolean("isAndroid", true);
source->AddString("braveWalletLedgerBridgeUrl", kUntrustedLedgerURL);
Expand Down
3 changes: 1 addition & 2 deletions browser/ui/webui/brave_wallet/ledger/ledger_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ UntrustedLedgerUI::UntrustedLedgerUI(content::WebUI* web_ui)
auto* untrusted_source = content::WebUIDataSource::CreateAndAdd(
web_ui->GetWebContents()->GetBrowserContext(), kUntrustedLedgerURL);
untrusted_source->SetDefaultResource(IDR_BRAVE_WALLET_LEDGER_BRIDGE_HTML);
untrusted_source->AddResourcePaths(UNSAFE_TODO(
base::make_span(kLedgerBridgeGenerated, kLedgerBridgeGeneratedSize)));
untrusted_source->AddResourcePaths(base::span(kLedgerBridgeGenerated));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL));
untrusted_source->OverrideContentSecurityPolicy(
Expand Down
12 changes: 2 additions & 10 deletions browser/ui/webui/brave_wallet/line_chart/line_chart_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and
// convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "brave/browser/ui/webui/brave_wallet/line_chart/line_chart_ui.h"

#include <string>
Expand Down Expand Up @@ -40,13 +34,11 @@ UntrustedLineChartUI::UntrustedLineChartUI(content::WebUI* web_ui)

untrusted_source->SetDefaultResource(
IDR_BRAVE_WALLET_LINE_CHART_DISPLAY_HTML);
untrusted_source->AddResourcePaths(base::make_span(
kLineChartDisplayGenerated, kLineChartDisplayGeneratedSize));
untrusted_source->AddResourcePaths(base::span(kLineChartDisplayGenerated));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL));
webui::SetupWebUIDataSource(untrusted_source,
base::make_span(kLineChartDisplayGenerated,
kLineChartDisplayGeneratedSize),
base::span(kLineChartDisplayGenerated),
IDR_BRAVE_WALLET_LINE_CHART_DISPLAY_HTML);
untrusted_source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ScriptSrc,
Expand Down
16 changes: 4 additions & 12 deletions browser/ui/webui/brave_wallet/market/market_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and
// convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "brave/browser/ui/webui/brave_wallet/market/market_ui.h"

#include <string>
Expand Down Expand Up @@ -38,14 +32,12 @@ UntrustedMarketUI::UntrustedMarketUI(content::WebUI* web_ui)
untrusted_source->AddString(str.name, l10n_str);
}
untrusted_source->SetDefaultResource(IDR_BRAVE_WALLET_MARKET_DISPLAY_HTML);
untrusted_source->AddResourcePaths(
base::make_span(kMarketDisplayGenerated, kMarketDisplayGeneratedSize));
untrusted_source->AddResourcePaths(base::span(kMarketDisplayGenerated));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL));
webui::SetupWebUIDataSource(
untrusted_source,
base::make_span(kMarketDisplayGenerated, kMarketDisplayGeneratedSize),
IDR_BRAVE_WALLET_MARKET_DISPLAY_HTML);
webui::SetupWebUIDataSource(untrusted_source,
base::span(kMarketDisplayGenerated),
IDR_BRAVE_WALLET_MARKET_DISPLAY_HTML);

untrusted_source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ScriptSrc,
Expand Down
16 changes: 4 additions & 12 deletions browser/ui/webui/brave_wallet/nft/nft_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and
// convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "brave/browser/ui/webui/brave_wallet/nft/nft_ui.h"

#include <string>
Expand Down Expand Up @@ -39,14 +33,12 @@ UntrustedNftUI::UntrustedNftUI(content::WebUI* web_ui)
}

untrusted_source->SetDefaultResource(IDR_BRAVE_WALLET_NFT_DISPLAY_HTML);
untrusted_source->AddResourcePaths(
base::make_span(kNftDisplayGenerated, kNftDisplayGeneratedSize));
untrusted_source->AddResourcePaths(base::span(kNftDisplayGenerated));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL));
webui::SetupWebUIDataSource(
untrusted_source,
base::make_span(kNftDisplayGenerated, kNftDisplayGeneratedSize),
IDR_BRAVE_WALLET_NFT_DISPLAY_HTML);
webui::SetupWebUIDataSource(untrusted_source,
base::span(kNftDisplayGenerated),
IDR_BRAVE_WALLET_NFT_DISPLAY_HTML);
untrusted_source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ScriptSrc,
std::string("script-src 'self' chrome-untrusted://resources;"));
Expand Down
3 changes: 1 addition & 2 deletions browser/ui/webui/brave_wallet/trezor/trezor_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ UntrustedTrezorUI::UntrustedTrezorUI(content::WebUI* web_ui)
auto* untrusted_source = content::WebUIDataSource::CreateAndAdd(
web_ui->GetWebContents()->GetBrowserContext(), kUntrustedTrezorURL);
untrusted_source->SetDefaultResource(IDR_BRAVE_WALLET_TREZOR_BRIDGE_HTML);
untrusted_source->AddResourcePaths(UNSAFE_TODO(
base::make_span(kTrezorBridgeGenerated, kTrezorBridgeGeneratedSize)));
untrusted_source->AddResourcePaths(base::span(kTrezorBridgeGenerated));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL));
untrusted_source->OverrideContentSecurityPolicy(
Expand Down
7 changes: 2 additions & 5 deletions browser/ui/webui/brave_wallet/wallet_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ WalletPageUI::WalletPageUI(content::WebUI* web_ui)
"braveWalletPendingTransactions", IDS_BRAVE_WALLET_PENDING_TRANSACTIONS);
web_ui->AddMessageHandler(std::move(plural_string_handler));
NavigationBarDataProvider::Initialize(source, profile);
webui::SetupWebUIDataSource(
source,
UNSAFE_TODO(base::make_span(kBraveWalletPageGenerated,
kBraveWalletPageGeneratedSize)),
IDR_WALLET_PAGE_HTML);
webui::SetupWebUIDataSource(source, base::span(kBraveWalletPageGenerated),
IDR_WALLET_PAGE_HTML);
source->AddString("braveWalletLedgerBridgeUrl", kUntrustedLedgerURL);
source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ImgSrc,
Expand Down
7 changes: 2 additions & 5 deletions browser/ui/webui/brave_wallet/wallet_panel_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ WalletPanelUI::WalletPanelUI(content::WebUI* web_ui)
plural_string_handler->AddLocalizedString(
"braveWalletPendingTransactions", IDS_BRAVE_WALLET_PENDING_TRANSACTIONS);
web_ui->AddMessageHandler(std::move(plural_string_handler));
webui::SetupWebUIDataSource(
source,
UNSAFE_TODO(base::make_span(kBraveWalletPanelGenerated,
kBraveWalletPanelGeneratedSize)),
IDR_WALLET_PANEL_HTML);
webui::SetupWebUIDataSource(source, base::span(kBraveWalletPanelGenerated),
IDR_WALLET_PANEL_HTML);
source->AddString("braveWalletLedgerBridgeUrl", kUntrustedLedgerURL);
source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::FrameSrc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and
// convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "brave/components/brave_wallet/browser/bitcoin/bitcoin_discover_account_task.h"

#include <stdint.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <utility>

#include "base/containers/span.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "brave/components/brave_wallet/browser/bitcoin/bitcoin_hd_keyring.h"
Expand Down Expand Up @@ -74,7 +75,7 @@ class BitcoinKnapsackSolverUnitTest : public testing::Test {
tx_input.utxo_address = address;
std::string txid_fake = address + base::NumberToString(amount);
tx_input.utxo_outpoint.txid =
crypto::SHA256Hash(base::as_bytes(base::make_span(txid_fake)));
crypto::SHA256Hash(base::as_byte_span(txid_fake));
tx_input.utxo_outpoint.index = tx_input.utxo_outpoint.txid.back();
tx_input.utxo_value = amount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class BitcoinMaxSendSolverUnitTest : public testing::Test {
tx_input.utxo_address = address;
std::string txid_fake = address + base::NumberToString(amount);
tx_input.utxo_outpoint.txid =
crypto::SHA256Hash(base::as_bytes(base::make_span(txid_fake)));
crypto::SHA256Hash(base::as_byte_span(txid_fake));
tx_input.utxo_outpoint.index = tx_input.utxo_outpoint.txid.back();
tx_input.utxo_value = amount;

Expand Down
4 changes: 2 additions & 2 deletions components/brave_wallet/browser/ens_resolver_task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ EnsResolverTaskError::~EnsResolverTaskError() = default;
std::vector<uint8_t> MakeAddrCall(const std::string& domain) {
return eth_abi::TupleEncoder()
.AddFixedBytes(Namehash(domain))
.EncodeWithSelector(base::make_span(kAddrBytes32Selector));
.EncodeWithSelector(base::span(kAddrBytes32Selector));
}

std::vector<uint8_t> MakeContentHashCall(const std::string& domain) {
return eth_abi::TupleEncoder()
.AddFixedBytes(Namehash(domain))
.EncodeWithSelector(base::make_span(kContentHashBytes32Selector));
.EncodeWithSelector(base::span(kContentHashBytes32Selector));
}

OffchainLookupData::OffchainLookupData() = default;
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/eth_data_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ std::optional<std::vector<uint8_t>> Forward(const FilAddress& fil_address) {

return eth_abi::TupleEncoder()
.AddBytes(fil_address.GetBytes())
.EncodeWithSelector(base::make_span(kFilForwarderSelector));
.EncodeWithSelector(base::span(kFilForwarderSelector));
}

} // namespace filforwarder
Expand Down
4 changes: 2 additions & 2 deletions components/brave_wallet/browser/filecoin_keyring.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ std::string FilecoinKeyring::EncodePrivateKeyForExport(
const std::string& address) {
if (base::Contains(imported_bls_accounts_, address)) {
return GetExportEncodedJSON(
base::Base64Encode(base::make_span(*imported_bls_accounts_[address])),
base::Base64Encode(base::span(*imported_bls_accounts_[address])),
address);
}

Expand Down Expand Up @@ -209,7 +209,7 @@ std::optional<std::string> FilecoinKeyring::SignTransaction(

if (base::Contains(imported_bls_accounts_, address)) {
return tx->GetSignedTransaction(
fil_address, base::make_span(*imported_bls_accounts_[address]));
fil_address, base::span(*imported_bls_accounts_[address]));
}

HDKey* hd_key = GetHDKeyFromAddress(address);
Expand Down
21 changes: 8 additions & 13 deletions components/brave_wallet/browser/json_rpc_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -409,18 +409,16 @@ class GetAccountInfoHandler : public SolRpcCallHandler {

static std::vector<uint8_t> MakeMintData(int supply) {
std::vector<uint8_t> data(82);
base::as_writable_bytes(base::make_span(data))
.subspan(36)
.first<8u>()
.copy_from(base::U64ToNativeEndian(supply));
base::span(data).subspan(36).first<8u>().copy_from(
base::U64ToLittleEndian(supply));
return data;
}

static std::vector<uint8_t> MakeNameRegistryStateData(
const SolanaAddress& owner,
const std::vector<uint8_t>& data = {}) {
std::vector<uint8_t> result(96 + data.size());
auto result_span = base::as_writable_bytes(base::make_span(result));
auto result_span = base::span(result);
// Header.
base::ranges::copy(owner.bytes(), result_span.subspan(32, 32).begin());

Expand All @@ -435,7 +433,7 @@ class GetAccountInfoHandler : public SolRpcCallHandler {
const SolanaAddress& sol_record_address,
const std::vector<uint8_t>& signer_key) {
std::vector<uint8_t> result(32 + 64); // payload_address + signature.
auto result_span = base::as_writable_bytes(base::make_span(result));
auto result_span = base::span(result);

base::ranges::copy(sol_record_payload_address.bytes(), result_span.begin());

Expand Down Expand Up @@ -609,15 +607,12 @@ class GetProgramAccountsHandler : public SolRpcCallHandler {
static std::vector<uint8_t> MakeTokenAccountData(const SolanaAddress& mint,
const SolanaAddress& owner) {
std::vector<uint8_t> data(165);
auto mint_span =
base::as_writable_bytes(base::make_span(data)).subspan(0, 32);
auto mint_span = base::span(data).subspan(0, 32);
base::ranges::copy(mint.bytes(), mint_span.begin());
auto owner_span =
base::as_writable_bytes(base::make_span(data)).subspan(32, 32);
auto owner_span = base::span(data).subspan(32, 32);
base::ranges::copy(owner.bytes(), owner_span.begin());

auto amount_span =
base::as_writable_bytes(base::make_span(data)).subspan(64, 1);
auto amount_span = base::span(data).subspan(64, 1);
*amount_span.data() = 1;

return data;
Expand All @@ -632,7 +627,7 @@ class GetProgramAccountsHandler : public SolRpcCallHandler {
auto* filters = (*dict.FindList("params"))[1].GetDict().FindList("filters");
EXPECT_TRUE(filters);

auto data_span = base::make_span(token_account_data_);
auto data_span = base::span(token_account_data_);
base::Value::List expected_filters;
expected_filters.Append(base::Value::Dict());
expected_filters.back().GetDict().SetByDottedPath("memcmp.offset", 0);
Expand Down
8 changes: 4 additions & 4 deletions components/brave_wallet/browser/keyring_service_migrations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ void MaybeMigratePBKDF2Iterations(PrefService* profile_prefs,
/*force_create = */ true);

SetPrefForKeyring(profile_prefs, kEncryptedMnemonicDeprecated,
base::Value(base::Base64Encode(encryptor->Encrypt(
base::make_span(*mnemonic), nonce))),
base::Value(base::Base64Encode(
encryptor->Encrypt(base::span(*mnemonic), nonce))),
keyring_id);

if (keyring_id == mojom::kDefaultKeyringId) {
Expand Down Expand Up @@ -368,7 +368,7 @@ void MaybeMigratePBKDF2Iterations(PrefService* profile_prefs,
}

auto private_key = deprecated_encryptor->Decrypt(
base::make_span(*deprecated_private_key_decoded), *deprecated_nonce);
base::span(*deprecated_private_key_decoded), *deprecated_nonce);
if (!private_key) {
continue;
}
Expand Down Expand Up @@ -504,7 +504,7 @@ void MaybeMigrateToWalletMnemonic(PrefService* profile_prefs,
}

auto private_key = deprecated_encryptor->Decrypt(
base::make_span(*deprecated_private_key_decoded), *deprecated_nonce);
base::span(*deprecated_private_key_decoded), *deprecated_nonce);
if (!private_key) {
continue;
}
Expand Down
Loading

0 comments on commit e875f58

Please sign in to comment.