Ecosyste.ms: Advisories
An open API service providing security vulnerability metadata for many open source software ecosystems.
Security Advisories: GSA_kwCzR0hTQS0zamNnLXZ4N2YtajZxZs4AA-RP
The fuels-ts typescript SDK has no awareness of to-be-spent transactions
Brief/Intro
The typescript SDK has no awareness of to-be-spent transactions causing some transactions to fail or silently get pruned as they are funded with already used UTXOs.
The Typescript SDK
provides the fund
function which retrieves UTXOs
, which belong to the owner and can be used to fund the request in question, from fuel's graphql api. These then get added to the request making it possible to send it to the network as it now has inputs which can be spent by its outputs. Now this works when a user only wants to fund one transaction per block as in the next block, the spent UTXO will not exist anymore. However if a user wants to fund multiple transactions within one block, the following can happen:
It is important to note, that the graphql API will return a random UTXO which has enough value to fund the transaction in question.
- user has 2 spendable
UTXOs
in their wallet which can cover all expenses - user funds transaction
tA
with an input gotten from the APIiA
- user submits
tA
to fuel iA
is still in possession of the user as no new block has been produced- user funds a transaction
tB
and gets the same inputiA
from the API - user tries to submit transaction
tB
to fuel but now one of the following can happen:- if the recipient and all other parameters are the same as in
tA
, submission will fail astB
will have the sametxHash
astA
- if the parameters are different, there will be a collision in the
txpool
andtA
will be removed from thetxpool
- if the recipient and all other parameters are the same as in
Vulnerability Details
The problem occurs, because the fund
function in fuels-ts/packages/account/src/account.ts
gets the needed ressources statelessly with the function getResourcesToSpend
without taking into consideration already used UTXOs:
async fund<T extends TransactionRequest>(request: T, params: EstimatedTxParams): Promise<T> {
// [...]
let missingQuantities: CoinQuantity[] = [];
Object.entries(quantitiesDict).forEach(([assetId, { owned, required }]) => {
if (owned.lt(required)) {
missingQuantities.push({
assetId,
amount: required.sub(owned),
});
}
});
let needsToBeFunded = missingQuantities.length > 0;
let fundingAttempts = 0;
while (needsToBeFunded && fundingAttempts < MAX_FUNDING_ATTEMPTS) {
const resources = await this.getResourcesToSpend(
missingQuantities,
cacheRequestInputsResourcesFromOwner(request.inputs, this.address)
); // @audit-issue here we do not exclude ids we already got and used for another transaction in the current block
request.addResources(resources);
// [...]
}
// [...]
return request;
}
Impact Details
This issue will lead to unexpected SDK behaviour. Looking at the scenario in Brief/Intro
, it could have the following impacts for users:
- A transaction does not get included in the
txpool
/ in a block - A previous transaction silently gets removed from the
txpool
and replaced with a new one
Recommendation
I would recommend adding a buffer to the Account
class, in which retrieved resources
are saved. These can then be provided to getResourcesToSpend
to be excluded from future queries but need to be removed from the buffer if their respective transaction fails to be included, in order to be able to use those resources
again in such cases.
Proof of Concept
The following PoC transfers 100 coins from wallet2
to wallet
after which wallet2
has two UTXOs
one with value 100
and one with a very high value (this is printed to the console). Afterwards, wallet
will attempt transfering 80
coins back to wallet2
twice in one block, each in a separate transaction. This should work perfectly fine as wallet
has two UTXOs
where each can cover the cost of each respective transaction. Now when running this one of the following will happen:
- both transfers from
wallet
towallet2
get a differentUTXO
. This is the case if execution is successful andwallet2
has80
coins more thanwallet
in the end. - both transfers get the same
UTXO
. In this case the script will fail and throw an error as then both transactions will have the same hash
In order to execute this PoC, please deploy a local node with a blocktime of 5secs
as I wrote my PoC for that blocktime. Note that with a small change it will also work with other blocktimes. Then add the PoC to a file poc_resources.ts
and compile it with tsc poc_resources.ts
. Finally execute it with node poc_resources.js
.
Since the choice which UTXO
is taken as input is random, it might take a few tries to trigger the bug!
import { JsonAbi, Script, Provider, WalletUnlocked, Account, Predicate, Wallet, CoinQuantityLike, coinQuantityfy, EstimatedTxParams, BN, Coin, AbstractAddress, Address, Contract, ScriptTransactionRequest } from 'fuels';
const abi: JsonAbi = {
'encoding': '1',
'types': [
{
'typeId': 0,
'type': '()',
'components': [],
'typeParameters': null
}
],
'functions': [
{
'inputs': [],
'name': 'main',
'output': {
'name': '',
'type': 0,
'typeArguments': null
},
'attributes': null
}
],
'loggedTypes': [],
'messagesTypes': [],
'configurables': []
};
const FUEL_NETWORK_URL = 'http://127.0.0.1:4000/v1/graphql';
async function executeTransaction() {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet: WalletUnlocked = Wallet.fromPrivateKey('0x37fa81c84ccd547c30c176b118d5cb892bdb113e8e80141f266519422ef9eefd', provider);
const wallet2: WalletUnlocked = Wallet.fromPrivateKey('0xde97d8624a438121b86a1956544bd72ed68cd69f2c99555b08b1e8c51ffd511c', provider);
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
console.log("Balance wallet before: ", await wallet.getBalance());
console.log("Balance wallet2 before: ", await wallet2.getBalance());
wallet2.transfer(wallet.address, 100);
await sleep(5500);
await wallet.transfer(wallet2.address, 80);
console.log('wallet -> wallet2');
await wallet.transfer(wallet2.address, 80);
console.log('wallet -> wallet2');
console.log("Balance wallet after: ", await wallet.getBalance());
console.log("Balance wallet2 after: ", await wallet2.getBalance());
};
executeTransaction().catch(console.error);
Permalink: https://github.com/advisories/GHSA-3jcg-vx7f-j6qfJSON: https://advisories.ecosyste.ms/api/v1/advisories/GSA_kwCzR0hTQS0zamNnLXZ4N2YtajZxZs4AA-RP
Source: GitHub Advisory Database
Origin: Unspecified
Severity: Low
Classification: General
Published: 4 months ago
Updated: 4 months ago
CVSS Score: 3.1
CVSS vector: CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:U/C:N/I:L/A:N
Identifiers: GHSA-3jcg-vx7f-j6qf, CVE-2024-41945
References:
- https://github.com/FuelLabs/fuels-ts/security/advisories/GHSA-3jcg-vx7f-j6qf
- https://github.com/FuelLabs/fuels-ts/commit/16ee1bfe66733551d00f0a76c21e8a09ea33006f
- https://nvd.nist.gov/vuln/detail/CVE-2024-41945
- https://github.com/advisories/GHSA-3jcg-vx7f-j6qf
Blast Radius: 1.0
Affected Packages
npm:@fuel-ts/account
Dependent packages: 0Dependent repositories: 0
Downloads: 39,546 last month
Affected Version Ranges: < 0.93.0
Fixed in: 0.93.0
All affected versions: 0.74.0, 0.75.0, 0.76.0, 0.76.1, 0.76.2, 0.76.3, 0.76.4, 0.76.5, 0.76.7, 0.76.8, 0.76.9, 0.76.10, 0.76.11, 0.76.12, 0.77.0, 0.78.0, 0.79.0, 0.80.0, 0.81.0, 0.82.0, 0.83.0, 0.84.0, 0.85.0, 0.86.0, 0.87.0, 0.88.0, 0.88.1, 0.89.0, 0.89.1, 0.89.2, 0.90.0, 0.91.0, 0.92.0, 0.92.1
All unaffected versions: 0.93.0, 0.94.0, 0.94.1, 0.94.2, 0.94.3, 0.94.4, 0.94.5, 0.94.6, 0.94.7, 0.94.8, 0.94.9, 0.95.0, 0.96.0, 0.96.1, 0.97.0