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

fix: Add validation to ensure gas fields are valid hexadecimal #4854

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Changes from 1 commit
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
Next Next commit
fix: validate gas fields are a valid hex
  • Loading branch information
vinistevam committed Oct 28, 2024
commit 7a0b88e4cc8f5960e28e99202313b6924178929d
24 changes: 13 additions & 11 deletions packages/transaction-controller/src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Interface } from '@ethersproject/abi';
import { ORIGIN_METAMASK, isValidHexAddress } from '@metamask/controller-utils';
import { abiERC20 } from '@metamask/metamask-eth-abis';
import { providerErrors, rpcErrors } from '@metamask/rpc-errors';
import { isStrictHexString } from '@metamask/utils';

import { TransactionEnvelopeType, type TransactionParams } from '../types';
import { isEIP1559Transaction } from './utils';
Expand Down Expand Up @@ -243,7 +244,7 @@ function validateGasFeeParams(txParams: TransactionParams) {
'gasPrice',
'maxPriorityFeePerGas',
);
ensureFieldIsString(txParams, 'gasPrice');
ensureFieldIsValidHex(txParams, 'gasPrice');
}

if (txParams.maxFeePerGas) {
Expand All @@ -253,7 +254,7 @@ function validateGasFeeParams(txParams: TransactionParams) {
'maxFeePerGas',
'gasPrice',
);
ensureFieldIsString(txParams, 'maxFeePerGas');
ensureFieldIsValidHex(txParams, 'maxFeePerGas');
}

if (txParams.maxPriorityFeePerGas) {
Expand All @@ -266,7 +267,7 @@ function validateGasFeeParams(txParams: TransactionParams) {
'maxPriorityFeePerGas',
'gasPrice',
);
ensureFieldIsString(txParams, 'maxPriorityFeePerGas');
ensureFieldIsValidHex(txParams, 'maxPriorityFeePerGas');
}
}

Expand Down Expand Up @@ -332,22 +333,23 @@ function ensureMutuallyExclusiveFieldsNotProvided(
}

/**
* Ensures that the provided value for field is a string, throws an
* invalidParams error if field is not a string.
* Ensures that the provided value for field is a valid hexadecimal.
* Throws an invalidParams error if field is not a valid hexadecimal.
*
* @param txParams - The transaction parameters object
* @param field - The current field being validated
* @throws {rpcErrors.invalidParams} Throws if field is not a string
* @throws {rpcErrors.invalidParams} Throws if field is not a valid hexadecimal
*/
function ensureFieldIsString(
function ensureFieldIsValidHex(
txParams: TransactionParams,
field: GasFieldsToValidate,
) {
if (typeof txParams[field] !== 'string') {
const value = txParams[field];
if (typeof value !== 'string' || !isStrictHexString(value)) {
throw rpcErrors.invalidParams(
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Invalid transaction params: ${field} is not a string. got: (${txParams[field]})`,
`Invalid transaction params: ${field} is not a valid hexadecimal string. got: (${String(
value,
)})`,
);
}
}
Loading