Skip to content

fix: reject non-canonical RLP in fromRlp - #5513

Merged
jxom merged 3 commits into
mainfrom
fix-rlp-non-canonical
Nov 15, 2026
Merged

jxom merged 3 commits into
mainfrom
fix-rlp-non-canonical

Conversation

@jxom

@jxom jxom commented Nov 15, 2026

Copy link
Copy Markdown
Member

Supersedes #5506 by @gomesalexandre. Their commit is cherry-picked unchanged, with authorship preserved.

fromRlp accepted non-canonical encodings, so a payload could decode to the same value under a different hash. It now throws RlpNonCanonicalError for:

  • a single byte below 0x80 wrapped in a length prefix (0x8100, 0x817f)
  • long-form string or list lengths of 55 or less (0xb837…, 0xf800)
  • long-form lengths with leading zero bytes (0xb900ff…, 0xba000100…)

These match Yellow Paper appendix B, geth's ErrCanonSize / ErrNonCanonicalSize, and ox's Rlp.NonCanonicalError. viem keeps its own decoder because ox's Rlp.to does not check for trailing bytes, list overruns, or depth.

On top of the cherry-pick, this PR adds TSDoc to the new public error.

Verification

  • Boundary inputs on each side of every rule: main accepts all 18 non-canonical cases, and this branch rejects them while still decoding the canonical ones.
  • 48 raw mainnet transactions (legacy including pre-EIP-155, EIP-2930, EIP-1559, EIP-4844, EIP-7702) still parse, and recoverTransactionAddress returns the original sender.
  • The RLP and transaction vector suites (134,640 tests) pass.
  • fromRlp, parseTransaction, and index tests pass, Biome is clean, and tsc -b src passes.

gomesalexandre and others added 2 commits November 15, 2026 04:57
`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 <noreply@anthropic.com>
Co-authored-by: jxom <jake@tempo.xyz>
@changeset-bot

changeset-bot Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6960b19

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

@vercel

vercel Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
viem Error Error Nov 15, 2026 5:16am UTC

Request Review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T05:13:37.224531Z 6960b19 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

Published viem@0.0.0-pr-5513-ce0e56b under the pr-5513 dist-tag, which follows this pull request until it closes.

pnpm add viem@pr-5513
pnpm add viem@0.0.0-pr-5513-ce0e56b

@jxom
jxom merged commit 1759adf into main Nov 15, 2026
36 of 38 checks passed
@jxom
jxom deleted the fix-rlp-non-canonical branch November 15, 2026 05:18

This branch had an error being deployed

1 failed deployment
Preview — 6960b195 Deployed Nov 15, 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.

3 participants