Skip to content

fix: reject fixed-length typed data arrays with the wrong length - #5555

Closed
erkancamli wants to merge 1 commit into
wevm:mainfrom
erkancamli:fix/typed-data-fixed-array-length
Closed

erkancamli wants to merge 1 commit into
wevm:mainfrom
erkancamli:fix/typed-data-fixed-array-length

Conversation

@erkancamli

Copy link
Copy Markdown
Contributor

hashTypedData encodes a fixed-length array field such as uint256[2] from however many elements the value has. A message with three elements (or one) hashes and signs without an error, and the digest is one that no verifier for that type can produce:

hashTypedData({
  types: { Message: [{ name: 'values', type: 'uint256[2]' }] },
  primaryType: 'Message',
  message: { values: [1n, 2n, 3n] },
})
// viem: 0xc2e43d2c...   ox: InvalidArrayLengthError   ethers: "array length mismatch"

ox checks the declared length in TypedData.encodeField (InvalidArrayLengthError), and ethers' TypedDataEncoder does too. This adds the same check to encodeField for every dimension, so uint256[2][] with a one-element inner array is caught as well, and throws AbiEncodingArrayLengthMismatchError, the error encodeAbiParameters already uses for the same mismatch. Dynamic arrays and correctly sized fixed arrays hash exactly as before.

Tests

hashTypedData.test.ts gets cases for a uint256[2] with too many and too few elements, a wrong inner dimension in uint256[2][], and a Person[1] with two structs. They fail on main (596dcc3) and pass with the change. The hash of the correct uint256[2] message is unchanged and matches ox and ethers. I also hashed 400 random well-formed messages with nested fixed and dynamic arrays of primitives and structs: the results are the same before and after, and match go-ethereum's apitypes. biome and tsc are clean, the ESM size limits pass (viem 94.07 kB, viem/account-abstraction 62.96 kB), and a changeset is included.

hashTypedData encoded a fixed-length array field such as uint256[2]
from however many elements the value had, so a message with three
elements hashed and signed without error, giving a digest no verifier
for that type can produce. ox (TypedData.encodeField) and ethers reject
it. Check the declared length for every dimension and throw
AbiEncodingArrayLengthMismatchError, as encodeAbiParameters does.
@vercel

vercel Bot commented Nov 17, 2026

Copy link
Copy Markdown

@erkancamli 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 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2a5e730

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 17, 2026

Copy link
Copy Markdown
Member

Thanks @erkancamli! Superseded by #5556, which keeps your tests (you're credited as co-author) and moves the length check into validateTypedData, so JSON-RPC signTypedData and verifyTypedData reject the same inputs as hashTypedData.

@jxom jxom closed this Nov 17, 2026
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.

2 participants