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: Prevent invalid gas limit #5093

Merged
merged 2 commits into from
Dec 20, 2024
Merged

fix: Prevent invalid gas limit #5093

merged 2 commits into from
Dec 20, 2024

Conversation

pedronfigueiredo
Copy link
Contributor

@pedronfigueiredo pedronfigueiredo commented Dec 20, 2024

Explanation

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

References

Fixes: https://github.com/MetaMask/MetaMask-planning/issues/3826

Changelog

@metamask/transaction-controller

  • ADDED: Hex validation for gas and gasLimit

Checklist

  • 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

@pedronfigueiredo
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "20.0.2-preview-2983d87f",
  "@metamask-previews/address-book-controller": "6.0.2-preview-2983d87f",
  "@metamask-previews/announcement-controller": "7.0.2-preview-2983d87f",
  "@metamask-previews/approval-controller": "7.1.1-preview-2983d87f",
  "@metamask-previews/assets-controllers": "45.1.2-preview-2983d87f",
  "@metamask-previews/base-controller": "7.1.0-preview-2983d87f",
  "@metamask-previews/build-utils": "3.0.2-preview-2983d87f",
  "@metamask-previews/chain-controller": "0.2.2-preview-2983d87f",
  "@metamask-previews/composable-controller": "10.0.0-preview-2983d87f",
  "@metamask-previews/controller-utils": "11.4.4-preview-2983d87f",
  "@metamask-previews/ens-controller": "15.0.1-preview-2983d87f",
  "@metamask-previews/eth-json-rpc-provider": "4.1.7-preview-2983d87f",
  "@metamask-previews/gas-fee-controller": "22.0.2-preview-2983d87f",
  "@metamask-previews/json-rpc-engine": "10.0.2-preview-2983d87f",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.6-preview-2983d87f",
  "@metamask-previews/keyring-controller": "19.0.2-preview-2983d87f",
  "@metamask-previews/logging-controller": "6.0.3-preview-2983d87f",
  "@metamask-previews/message-manager": "11.0.3-preview-2983d87f",
  "@metamask-previews/multichain": "2.0.0-preview-2983d87f",
  "@metamask-previews/name-controller": "8.0.2-preview-2983d87f",
  "@metamask-previews/network-controller": "22.1.1-preview-2983d87f",
  "@metamask-previews/notification-services-controller": "0.15.0-preview-2983d87f",
  "@metamask-previews/permission-controller": "11.0.4-preview-2983d87f",
  "@metamask-previews/permission-log-controller": "3.0.2-preview-2983d87f",
  "@metamask-previews/phishing-controller": "12.3.1-preview-2983d87f",
  "@metamask-previews/polling-controller": "12.0.2-preview-2983d87f",
  "@metamask-previews/preferences-controller": "15.0.1-preview-2983d87f",
  "@metamask-previews/profile-sync-controller": "3.1.1-preview-2983d87f",
  "@metamask-previews/queued-request-controller": "8.0.2-preview-2983d87f",
  "@metamask-previews/rate-limit-controller": "6.0.2-preview-2983d87f",
  "@metamask-previews/remote-feature-flag-controller": "1.2.0-preview-2983d87f",
  "@metamask-previews/selected-network-controller": "20.0.2-preview-2983d87f",
  "@metamask-previews/signature-controller": "23.1.0-preview-2983d87f",
  "@metamask-previews/transaction-controller": "42.0.0-preview-2983d87f",
  "@metamask-previews/user-operation-controller": "21.0.0-preview-2983d87f"
}

Comment on lines +373 to +375
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know these anys all over this file but does it make sense to remove all these todo's and mark them as TransactionParams

Suggested change
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any),
} as unknown as TransactionParams),

OGPoyraz
OGPoyraz previously approved these changes Dec 20, 2024
matthewwalsh0
matthewwalsh0 previously approved these changes Dec 20, 2024
@@ -281,6 +286,14 @@ function validateGasFeeParams(txParams: TransactionParams) {
);
ensureFieldIsValidHex(txParams, 'maxPriorityFeePerGas');
}

if (txParams.gasLimit) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a future ticket, but we should make sure we also normalize these together to a single property so we can rely on a single value in the rest of the logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a huge relief, these optional picks are all over the client code

Copy link
Member

@matthewwalsh0 matthewwalsh0 Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could even make that a caller problem and only define gas for example in TransactionParams.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great idea and I agree with normalizing these. Shouldn't we leave gasLimit in the transaction params since the incoming payload from the dApps still might include it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant "caller" as in the client rather than the dApp. So the client middleware could merge the properties and give us just one.

@@ -281,6 +286,14 @@ function validateGasFeeParams(txParams: TransactionParams) {
);
ensureFieldIsValidHex(txParams, 'maxPriorityFeePerGas');
}

if (txParams.gasLimit) {
ensureFieldIsValidHex(txParams, 'gasLimit');
Copy link
Member

@matthewwalsh0 matthewwalsh0 Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there implications of this for existing dApps, do any rely on being able to provide decimals directly? I recall discussing this in the past.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there are some but shouldn't we take this current API as base?
https://docs.metamask.io/wallet/reference/json-rpc-methods/eth_sendtransaction/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a good question, I don't want to break dApps with this update...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if I pass gas limit as a decimal number it isn't currently picked up by the UI. A different gas limit is shown. So I think it's safe to validate this as an hexadecimal.

@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add hex validation for gas and gasLimit ([#5093](https://github.com/MetaMask/core/pull/5093))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@pedronfigueiredo pedronfigueiredo merged commit ceca030 into main Dec 20, 2024
120 checks passed
@pedronfigueiredo pedronfigueiredo deleted the pnf/3826 branch December 20, 2024 12:45
github-merge-queue bot pushed a commit to MetaMask/metamask-extension that referenced this pull request Dec 20, 2024
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

Updates from v42 to v42.1 in order to get the validation of the gas
limit hexadecimal string properties. See
MetaMask/core#5093 for more details.

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/29395?quickstart=1)

## **Related issues**

Fixes: MetaMask/MetaMask-planning#3826

## **Manual testing steps**

1. Go to this page...
2.
3.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: MetaMask Bot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants