Skip to content

fix(rlp): reject trailing bytes in fromRlp - #4778

Closed
spokodev wants to merge 1 commit into
wevm:mainfrom
spokodev:fix/fromrlp-reject-trailing-bytes
Closed

spokodev wants to merge 1 commit into
wevm:mainfrom
spokodev:fix/fromrlp-reject-trailing-bytes

Conversation

@spokodev

Copy link
Copy Markdown
Contributor

Summary

fromRlp decodes a single item via fromRlpCursor(cursor, to) and returns without asserting that the cursor consumed all input. Any bytes after the first RLP item are silently dropped.

RLP is a bijection (Ethereum Yellow Paper, Appendix B): a payload encodes exactly one item, with no leftover bytes. Both geth and ethers reject over-long input; viem instead returns a partial value, which is unsafe in the transaction-decode path (parseTransaction).

Reproduction

fromRlp('0x80deadbeef', 'hex') // before: '0x'             (4 trailing bytes dropped)
fromRlp('0xc2010203', 'hex')   // before: ['0x01','0x02']  (trailing 0x03 dropped)
fromRlp('0xc000', 'hex')       // before: []               (trailing 0x00 dropped)

Oracle

  • geth: rlp: input contains more than one value
  • ethers decodeRlp: unexpected junk after rlp payload

Both reject all three inputs above.

Fix

In the public fromRlp wrapper (not in the fromRlpCursor recursion, so nested lists are unaffected), after decoding assert that the cursor reached the end of the input, otherwise throw a BaseError naming the count of leftover bytes.

fromRlp('0x80deadbeef', 'hex')
// BaseError: Unexpected trailing bytes: RLP payload encodes a single item but 4 byte(s) remain after it.

Valid single-item inputs (0x80, the empty list 0xc0, nested lists 0xc1c0, byte strings) keep decoding unchanged, and the full parseTransaction suite still passes (well-formed transactions consume all bytes).

Added tests cover all three trailing-byte cases plus a control that valid single items still decode.

This is a separate root cause from #4770 (Infinity recursiveReadLimit / deep-nesting stack overflow) and from the ABI zero-width encode fix in a sibling PR.

`fromRlp` decoded a single item via `fromRlpCursor` and returned without
checking that the cursor consumed all input, so any bytes after the first
RLP item were silently dropped.

RLP is a bijection (Ethereum Yellow Paper, Appendix B): a payload encodes
exactly one item with no leftover bytes. geth rejects over-long input with
`rlp: input contains more than one value` and ethers with `unexpected junk
after rlp payload`. viem instead returned a value:

  fromRlp('0x80deadbeef','hex') // before: '0x'            after: throws
  fromRlp('0xc2010203','hex')   // before: ['0x01','0x02'] after: throws
  fromRlp('0xc000','hex')       // before: []              after: throws

Assert in the public `fromRlp` wrapper (not in the `fromRlpCursor`
recursion, so nested lists are unaffected) that the cursor reached the end
of the input. Valid single-item inputs (`0x80`, `0xc0`, nested lists) and
the transaction-decode path (parseTransaction) are unchanged.

Separate root cause from the abi-encode zero-width fix on
fix/abi-encode-zero-width-static-aggregate.
@vercel

vercel Bot commented Jun 30, 2026

Copy link
Copy Markdown

@spokodev 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 Jun 30, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5992eb8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@spokodev

Copy link
Copy Markdown
Contributor Author

Sibling PR for a separate root cause: #4777 (abi zero-width static aggregates).

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