Skip to content

Commit

Permalink
Fix #pragma allow_unsafe_buffers for wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
supermassive committed Oct 31, 2024
1 parent 0f2f592 commit b2b95b6
Show file tree
Hide file tree
Showing 43 changed files with 515 additions and 711 deletions.
13 changes: 3 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,12 @@ 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::make_span(kLineChartDisplayGenerated));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPageURL));
untrusted_source->AddFrameAncestor(GURL(kBraveUIWalletPanelURL));
webui::SetupWebUIDataSource(untrusted_source,
base::make_span(kLineChartDisplayGenerated,
kLineChartDisplayGeneratedSize),
base::make_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::make_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::make_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::make_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::make_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
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 @@ -6,7 +6,9 @@
#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BITCOIN_BITCOIN_DISCOVER_ACCOUNT_TASK_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BITCOIN_BITCOIN_DISCOVER_ACCOUNT_TASK_H_

#include <memory>
#include <string>
#include <utility>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
Expand Down Expand Up @@ -71,7 +73,7 @@ class DiscoverAccountTaskBase {
uint32_t active_requests_ = 0;
// Indexed by 0 and 1 for receive and change addresses discovery states
// respectively.
State states_[2];
std::array<State, 2> states_;
bool account_is_used_ = false;
mojom::BitcoinBalancePtr balance_;

Expand Down
13 changes: 7 additions & 6 deletions components/brave_wallet/browser/eip1559_transaction_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <optional>
#include <utility>

#include "base/containers/span.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/values.h"
Expand Down Expand Up @@ -125,10 +126,10 @@ TEST(Eip1559TransactionUnitTest, GetSignedTransactionAndHash) {
"0x863c02549182b91f1764714b93d7e882f010539c0907adaf4de761f7b06a713c"}};
for (const auto& entry : cases) {
SCOPED_TRACE(entry.signed_tx);
std::vector<uint8_t> private_key;
EXPECT_TRUE(base::HexStringToBytes(
std::array<uint8_t, 32> private_key;
EXPECT_TRUE(base::HexStringToSpan(
"8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63",
&private_key));
private_key));

HDKey key;
key.SetPrivateKey(private_key);
Expand All @@ -142,9 +143,9 @@ TEST(Eip1559TransactionUnitTest, GetSignedTransactionAndHash) {
nullptr));

int recid;
const std::vector<uint8_t> signature =
key.SignCompact(tx.GetHashedMessageToSign(0), &recid);
tx.ProcessSignature(signature, recid);
auto signature = key.SignCompact(tx.GetHashedMessageToSign(0), &recid);
ASSERT_TRUE(signature);
tx.ProcessSignature(*signature, recid, 0);
EXPECT_EQ(tx.GetSignedTransaction(), entry.signed_tx);
EXPECT_EQ(tx.GetTransactionHash(), entry.hash);
}
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/eip2930_transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ std::string Eip2930Transaction::GetTransactionHash() const {
return ToHex(KeccakHash(Serialize()));
}

void Eip2930Transaction::ProcessSignature(const std::vector<uint8_t> signature,
void Eip2930Transaction::ProcessSignature(base::span<const uint8_t> signature,
int recid,
uint256_t chain_id) {
EthTransaction::ProcessSignature(signature, recid, chain_id_);
Expand Down
4 changes: 2 additions & 2 deletions components/brave_wallet/browser/eip2930_transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ class Eip2930Transaction : public EthTransaction {
// accessList, signatureYParity, signatureR, signatureS]))
std::string GetTransactionHash() const override;

void ProcessSignature(const std::vector<uint8_t> signature,
void ProcessSignature(base::span<const uint8_t> signature,
int recid,
uint256_t chain_id = 0) override;
uint256_t chain_id) override;

bool IsSigned() const override;

Expand Down
13 changes: 7 additions & 6 deletions components/brave_wallet/browser/eip2930_transaction_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <optional>
#include <utility>

#include "base/containers/span.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/values.h"
Expand Down Expand Up @@ -117,19 +118,19 @@ TEST(Eip2930TransactionUnitTest, GetSignedTransactionAndHash) {

access_list->push_back(item);

std::vector<uint8_t> private_key;
EXPECT_TRUE(base::HexStringToBytes(
std::array<uint8_t, 32> private_key;
EXPECT_TRUE(base::HexStringToSpan(
"fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19",
&private_key));
private_key));

HDKey key;
key.SetPrivateKey(private_key);
int recid;
const std::vector<uint8_t> signature =
key.SignCompact(tx.GetHashedMessageToSign(0), &recid);
auto signature = key.SignCompact(tx.GetHashedMessageToSign(0), &recid);
ASSERT_TRUE(signature);

ASSERT_FALSE(tx.IsSigned());
tx.ProcessSignature(signature, recid);
tx.ProcessSignature(*signature, recid, 0);
ASSERT_TRUE(tx.IsSigned());
EXPECT_EQ(
tx.GetSignedTransaction(),
Expand Down
37 changes: 13 additions & 24 deletions components/brave_wallet/browser/eth_abi_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@
* 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/eth_abi_decoder.h"

#include <limits>
#include <map>
#include <memory>
#include <optional>
#include <utility>

#include "base/containers/span.h"
#include "base/strings/strcat.h"
#include "base/containers/span_reader.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "brave/components/brave_wallet/common/eth_abi_utils.h"
Expand Down Expand Up @@ -96,8 +89,8 @@ std::optional<DecoderResult<base::Value>> GetAddressFromData(ByteView input) {
}

return DecoderResult<base::Value>(
base::Value("0x" + HexEncodeLower(input.data() + kWordSize - kAddressSize,
kAddressSize)),
base::Value("0x" +
HexEncodeLower(input.first(kWordSize).last(kAddressSize))),
GetSubByteView(input, kWordSize), kWordSize);
}

Expand Down Expand Up @@ -472,37 +465,35 @@ std::optional<std::vector<std::string>> UniswapEncodedPathDecode(
if (!PrefixedHexStringToBytes(encoded_path, &data)) {
return std::nullopt;
}
size_t offset = 0;
std::vector<std::string> path;

auto reader = base::SpanReader(base::as_byte_span(data));

// The path should be long enough to encode a single-hop swap.
// 43 = 20(address) + 3(fee) + 20(address)
if (data.size() < 43) {
if (reader.remaining() < 43) {
return std::nullopt;
}

// Parse first hop address.
path.push_back("0x" + HexEncodeLower(data.data(), 20));
offset += 20;
path.push_back("0x" + HexEncodeLower(*reader.Read(20u)));

while (true) {
if (offset == data.size()) {
if (!reader.remaining()) {
break;
}

// Parse the pool fee, and ignore.
if (data.size() - offset < 3) {
if (!reader.Skip(3u)) {
return std::nullopt;
}

offset += 3;

// Parse next hop.
if (data.size() - offset < 20) {
if (auto address = reader.Read(20u)) {
path.push_back("0x" + HexEncodeLower(*address));
} else {
return std::nullopt;
}
path.push_back("0x" + HexEncodeLower(data.data() + offset, 20));
offset += 20;
}

// Require a minimum of 2 addresses for a single-hop swap.
Expand All @@ -515,9 +506,7 @@ std::optional<std::vector<std::string>> UniswapEncodedPathDecode(

std::optional<base::Value::List> ABIDecode(const eth_abi::Type& type,
const ByteArray& data) {
ByteView input = base::make_span(data.data(), data.size());

auto decoded = DecodeParam(type, input);
auto decoded = DecodeParam(type, data);
if (!decoded) {
return std::nullopt;
}
Expand Down
43 changes: 17 additions & 26 deletions components/brave_wallet/browser/eth_gas_utils.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/components/brave_wallet/browser/eth_gas_utils.h"

#include <algorithm>
Expand Down Expand Up @@ -62,9 +56,9 @@ std::optional<uint256_t> ScaleBaseFeePerGas(const std::string& value) {
// base_fee_per_gas (last element) * 33%
// - avg_priority_fee will be the 0.4 * length's element of the sorted
// reward array.
// - The same applies to low_prirority_fee, but if it was equal to avg
// - The same applies to low_priority_fee, but if it was equal to avg
// then we walk it back to the next smallest element if possible.
// - The same applies to high_prirority_fee, but if it was equal to avg
// - The same applies to high_priority_fee, but if it was equal to avg
// then we walk it forward to the next biggest element if possible.
bool GetSuggested1559Fees(const std::vector<std::string>& base_fee_per_gas,
const std::vector<double>& gas_used_ratio,
Expand Down Expand Up @@ -104,40 +98,37 @@ bool GetSuggested1559Fees(const std::vector<std::string>& base_fee_per_gas,
return true;
}

std::vector<uint256_t> priority_fee_uints[3];
uint256_t* priority_fees[3] = {low_priority_fee, avg_priority_fee,
high_priority_fee};
std::array<std::vector<uint256_t>, 3> priority_fee_uints;
std::array<uint256_t*, 3> priority_fees = {low_priority_fee, avg_priority_fee,
high_priority_fee};
for (size_t i = 0; i < 3; i++) {
uint256_t& current_priority_fee = *(priority_fees[i]);
std::vector<uint256_t>& current_priority_fee_uints = priority_fee_uints[i];
bool invalid_data = false;
// Convert the string priority fees to uints
std::transform(reward.begin(), reward.end(),
std::back_inserter(current_priority_fee_uints),
[&](const std::vector<std::string>& v) -> uint256_t {
uint256_t val = fallback_priority_fee;
if (v.size() != 3) {
invalid_data = true;
} else if (!HexValueToUint256(v[i], &val)) {
invalid_data = true;
}
return val;
});
for (auto& v : reward) {
uint256_t val = fallback_priority_fee;
if (v.size() != 3) {
invalid_data = true;
} else if (!HexValueToUint256(v[i], &val)) {
invalid_data = true;
}
current_priority_fee_uints.push_back(val);
}

// We allow no reward info but we don't allow invalid reward info
if (invalid_data) {
return false;
}

// Sort the priroirty fee uints
// Sort the priority fee uints
std::sort(current_priority_fee_uints.begin(),
current_priority_fee_uints.end());
// Calculate the avg priorty fee first to be the 40th percentile of the avg
// Calculate the avg priority fee first to be the 40th percentile of the avg
// percentiles. We use this same method as the initial value for low
// and high too.
size_t percentile_index = current_priority_fee_uints.size() * 0.4;

current_priority_fee = current_priority_fee_uints[percentile_index];
*(priority_fees[i]) = current_priority_fee_uints[percentile_index];
}

// Re-adjust the percentiles for low down to the next non-equal value if
Expand Down
Loading

0 comments on commit b2b95b6

Please sign in to comment.