Skip to content

fix(typedData): reject invalid field types in validateTypedData - #5366

Closed
haybinalim wants to merge 1 commit into
wevm:mainfrom
haybinalim:fix/typed-data-invalid-field-types
Closed

haybinalim wants to merge 1 commit into
wevm:mainfrom
haybinalim:fix/typed-data-invalid-field-types

Conversation

@haybinalim

Copy link
Copy Markdown
Contributor

Closes #5365

What

validateTypedData only matched a field type against integerRegex / bytesRegex to range-check its value. A type that matched neither, such as uint7, uint264, uint08, int0, bytes0 (or an array of one, like uint7[]), fell through every check, and hashTypedData then encoded the value as uint256 (the size = '256' fallback in encodeAbiParameters) under a type hash like Message(uint7 a). No Solidity struct can produce that type hash, so the resulting signature can't be verified on-chain, and a uint7 field accepted any value up to 2^256 - 1.

field type before after
uint7 (value 1000n) hashed InvalidTypedDataTypeError
uint264, uint08, int0, int7 hashed InvalidTypedDataTypeError
bytes0 hashed InvalidTypedDataTypeError
bytes33 passes validation, then Hex size (33) exceeds padding size (32) InvalidTypedDataTypeError
uint7[], uint264[2] hashed InvalidTypedDataTypeError
Person with no Person in types passes validation, then InvalidAbiEncodingTypeError InvalidTypedDataTypeError

ethers (invalid numeric width), eth-account (No definition of type `uint7` ) and MetaMask eth-sig-util V4 (Invalid number length, for uint7 / uint264 / int0) already reject these. viem itself already rejects the same names when they are used as a struct name (validateReference).

How

  • validateTypedData: after the existing int / uint alias check, a field's base type (array suffixes stripped) must be a struct in types or an EIP-712 atomic/dynamic type: address, bool, string, or a full match of bytesRegex / integerRegex. Otherwise it throws InvalidTypedDataTypeError. Since hashTypedData and the signTypedData action both call validateTypedData, they reject these types too.
  • InvalidTypedDataTypeError: the Use "uint256" instead. hint is now only added for the int / uint aliases; for uint7 it would have suggested "uint7256".

Tests

  • typedData.test.ts: bytes0, bytes33, int0, int7, uint08, uint7, uint7[], uint264, uint264[2] as field types, and a field that references an undefined struct.
  • hashTypedData.test.ts: uint7 and bytes0 (inline snapshots).
  • Changeset added (patch).

All new tests fail on main and pass with this change. Locally I ran typedData.test.ts, hashTypedData.test.ts, recoverTypedDataAddress.test.ts, accounts/utils/signTypedData.test.ts, the privateKeyToAccount / hdKeyToAccount / mnemonicToAccount / toAccount tests and getUserOperationTypedData.test.ts (all green), plus Biome and tsc on the changed files. Anvil-backed suites and the full typecheck are left to CI.

Regressions

Valid typed data is unaffected: every type that passes now is one that already hashed correctly before. The only inputs that change behavior are the invalid ones above, which now fail in validateTypedData instead of being hashed (or failing later with a less specific error). Struct types, including ones only referenced as array elements and recursive ones, are still looked up in types first.

Note

Related but out of scope here: encodeAbiParameters([{ type: 'uint7' }], [1000n]) also encodes as uint256 because of the same fallback. I kept this PR to typed data; happy to follow up on the ABI encoder if you want that tightened too.

AI assistance: I used an AI assistant to help find this through differential testing against ethers, eth-account and MetaMask's eth-sig-util and to draft the change. I reviewed the code, ran the tests and checked the behavior myself.

validateTypedData only matched a field type against the integer and bytes
regexes to check its value, so a type that matched neither, such as uint7,
uint264, uint08, int0 or bytes0, passed and hashTypedData encoded it as
uint256 under a type hash no Solidity struct can produce. A field's base type
must now be a struct in types or an EIP-712 atomic or dynamic type.
@vercel

vercel Bot commented Nov 1, 2026

Copy link
Copy Markdown

@haybinalim is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Nov 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9245f4f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

jxom commented Nov 1, 2026

Copy link
Copy Markdown
Member

Closing in favour of #5370. It keeps this PR's commit and adds two fixes: it also checks structs that are only used as array elements (e.g. Person[] with a uint7 field was still hashed), and it rejects field types like constructor or toString, which matched built-in object properties.


Generated by Claude Code

This branch was successfully deployed

1 active deployment
Preview — 9245f4f6 Deployed Nov 1, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

validateTypedData accepts invalid EIP-712 field types such as uint7, uint264 and bytes0

2 participants