Skip to content

Fix fromRlp accepting non-canonical RLP - #5506

Closed
gomesalexandre wants to merge 1 commit into
wevm:mainfrom
gomesalexandre:fix-rlp-non-canonical
Closed

gomesalexandre wants to merge 1 commit into
wevm:mainfrom
gomesalexandre:fix-rlp-non-canonical

Conversation

@gomesalexandre

Copy link
Copy Markdown
Contributor

fromRlp decodes several non-canonical encodings as if they were canonical. A single byte below 0x80 wrapped in a length prefix (fromRlp('0x8100') returns '0x00'), a short string or list using the long form ('0xb800' returns '0x', '0xf800' returns []), and a long-form length with a leading zero byte ('0xb90038' + '00'.repeat(56)) are all accepted. RLP gives every value exactly one encoding (Yellow Paper, Appendix B), and geth, @ethereumjs/rlp and ox's Rlp.toHex reject all of these.

Because parseTransaction goes through fromRlp, viem accepts a malleated transaction that nodes reject. A signed EIP-1559 transfer with its outer list header rewritten from f8 69 to f9 00 69 (same payload, against anvil):

canonical  0x02f869827a69...   keccak 0x91d9145c37b99559babacfc5153b0886650d864274967c775305ace48bf854be
malleated  0x02f90069827a69... keccak 0x438c805160240dbfd7c71ddb3c02cbc5a7231e905f96d80f0bf405a3fca0c90f

parseTransaction(malleated)          => nonce 0, value 1, to 0x1111...1111 (same fields as canonical)
recoverTransactionAddress(malleated) => 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 (same signer)
anvil eth_sendRawTransaction(malleated) => rejected: Failed to decode transaction

So viem reports a valid transaction signed by 0xf39F...2266, while keccak256 of the input differs from the canonical transaction hash and anvil (like geth, whose RLP decoder rejects non-minimal sizes) refuses it. Anything that keys on keccak256(serializedTransaction) after validating with viem (relayers, dedup, indexers) can be handed a second encoding of the same signed transaction.

fromRlp now throws RlpNonCanonicalError for a prefixed single byte below 0x80 and for a long-form length that fits a shorter prefix. These are the same checks ox already makes in Rlp.NonCanonicalError. The error class follows the shape of RlpTrailingBytesError from #4790. Canonical boundary encodings (0x7f, 0x8180, 55 and 56 byte strings, a 256 byte string, a 56 item list) still decode, and the new tests cover both cases, including the malleated transaction through parseTransaction. This only covers RLP-level encoding: a transaction integer field with leading zero bytes (0x820001 for nonce 1) is still valid RLP and is left to a separate change.

🤖 Generated with Claude Code

`fromRlp` accepted a single byte below `0x80` behind a length prefix and
long-form lengths that fit a shorter prefix, so `parseTransaction` and
`recoverTransactionAddress` returned the same fields and signer for a
malleated payload whose hash differs from the canonical one and which
nodes reject. Throw `RlpNonCanonicalError` for both, matching ox's
`Rlp.NonCanonicalError` and geth's RLP decoder.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Nov 13, 2026

Copy link
Copy Markdown

@gomesalexandre 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 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e43258a

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

Copy link
Copy Markdown
Member

Thanks @gomesalexandre! Superseded by #5513, which cherry-picks your commit unchanged (you remain the author) and adds TSDoc to the new error.

@jxom jxom closed this Nov 15, 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