Skip to content

Commit

Permalink
fix: Prevent invalid gas limit (#5093)
Browse files Browse the repository at this point in the history
## Explanation

Add hex validation to the `gas` and `gasLimit` properties on `txParams`.

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

## References

Fixes: MetaMask/MetaMask-planning#3826

<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?

For example:

* Fixes #12345
* Related to #67890
-->

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/transaction-controller`

- **ADDED**: Hex validation for `gas` and `gasLimit`

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
- [ ] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
  • Loading branch information
pedronfigueiredo authored Dec 20, 2024
1 parent f8e5f2e commit ceca030
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/transaction-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Validate `gas` and `gasLimit` are hexadecimal strings ([#5093](https://github.com/MetaMask/core/pull/5093))

### Changed

- Bump `@metamask/base-controller` from `^7.0.0` to `^7.1.0` ([#5079](https://github.com/MetaMask/core/pull/5079))
Expand Down
55 changes: 55 additions & 0 deletions packages/transaction-controller/src/utils/validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { rpcErrors } from '@metamask/rpc-errors';

import { TransactionEnvelopeType } from '../types';
import type { TransactionParams } from '../types';
import { validateTxParams } from './validation';

describe('validation', () => {
Expand Down Expand Up @@ -362,6 +363,60 @@ describe('validation', () => {
} as any),
).not.toThrow();
});

it('throws if gasLimit is defined but not a valid hexadecimal', () => {
expect(() =>
validateTxParams({
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
to: '0xfbb5595c18ca76bab52d66188e4ca50c7d95f77a',
maxFeePerGas: '0x01',
gasLimit: 'zzzzz',
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any),
).toThrow(
rpcErrors.invalidParams(
'Invalid transaction params: gasLimit is not a valid hexadecimal. got: (zzzzz)',
),
);
expect(() =>
validateTxParams({
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
to: '0xfbb5595c18ca76bab52d66188e4ca50c7d95f77a',
maxFeePerGas: '0x01',
gasLimit: '0x0',
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any),
).not.toThrow();
});

it('throws if gas is defined but not a valid hexadecimal', () => {
expect(() =>
validateTxParams({
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
to: '0xfbb5595c18ca76bab52d66188e4ca50c7d95f77a',
maxFeePerGas: '0x01',
gas: 'zzzzz',
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as unknown as TransactionParams),
).toThrow(
rpcErrors.invalidParams(
'Invalid transaction params: gas is not a valid hexadecimal. got: (zzzzz)',
),
);
expect(() =>
validateTxParams({
from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
to: '0xfbb5595c18ca76bab52d66188e4ca50c7d95f77a',
maxFeePerGas: '0x01',
gas: '0x0',
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as unknown as TransactionParams),
).not.toThrow();
});
});
});
});
15 changes: 14 additions & 1 deletion packages/transaction-controller/src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { isStrictHexString } from '@metamask/utils';
import { TransactionEnvelopeType, type TransactionParams } from '../types';
import { isEIP1559Transaction } from './utils';

type GasFieldsToValidate = 'gasPrice' | 'maxFeePerGas' | 'maxPriorityFeePerGas';
type GasFieldsToValidate =
| 'gasPrice'
| 'maxFeePerGas'
| 'maxPriorityFeePerGas'
| 'gas'
| 'gasLimit';

/**
* Validates whether a transaction initiated by a specific 'from' address is permitted by the origin.
Expand Down Expand Up @@ -281,6 +286,14 @@ function validateGasFeeParams(txParams: TransactionParams) {
);
ensureFieldIsValidHex(txParams, 'maxPriorityFeePerGas');
}

if (txParams.gasLimit) {
ensureFieldIsValidHex(txParams, 'gasLimit');
}

if (txParams.gas) {
ensureFieldIsValidHex(txParams, 'gas');
}
}

/**
Expand Down

0 comments on commit ceca030

Please sign in to comment.