Skip to content

fix: reject non-canonical RLP length encodings - #596

Merged
jxom merged 3 commits into
mainfrom
fix/rlp-noncanonical-length
Nov 8, 2026
Merged

jxom merged 3 commits into
mainfrom
fix/rlp-noncanonical-length

Conversation

@jxom

@jxom jxom commented Nov 8, 2026

Copy link
Copy Markdown
Member

Supersedes #444 by @gomesalexandre: same fix, rebased onto main, plus review follow-ups. Their commits are kept with original authorship.

Rlp.toBytes / Rlp.toHex now throw Rlp.NonCanonicalError instead of accepting alternate encodings of the same value, matching geth's canonical-size rules (ErrCanonSize):

  • a single byte below 0x80 wrapped in a length-1 string prefix (0x8100, 0x817f)
  • a long-form length below 56 (0xb800, 0xb80141, 0xf8020102)
  • a long-form length with leading zero length bytes (0xb9000141, 0xba0000ff…)

Follow-ups on top of #444:

  • Dropped the MultisigSimulation.test.ts edit (renamed on main) and updated the matching AccountSimulation.test.ts snapshot, which now fails at the RLP layer.
  • Added NonCanonicalError to the readLength and decodeRlpCursor error unions.
  • Tightened the changeset to one sentence and used inline snapshots for the new positive cases.

Tests:

  • ethereum/tests RLPTests/invalidRLPTest.json: main accepts 16/26 invalid vectors, this branch accepts 1/26 (emptyEncoding, the empty 0x input, left for a follow-up).
  • New Rlp.test.ts cases fail against main's Rlp.ts and pass here; bun test vectors/src/rlp.vectors.test.ts passes 14640/14640.
  • tempo-unit passes; core passes apart from Anvil/network tests that fail identically on a clean main in this environment; tsc -b and vp check pass.

gomesalexandre and others added 3 commits November 8, 2026 19:46
RLP is defined as a bijection (Yellow Paper, Appendix B): every value
has exactly one valid encoding. The decoder previously accepted
alternate, non-canonical encodings of the same value:

- a single byte < 0x80 wrapped in an explicit one-byte length prefix
  (e.g. `0x8100` decoding to the same value as the canonical `0x00`)
- a long-form length that fits the short form, or that uses more
  length-bytes than necessary (e.g. `0xb800`, `0xb9000141`)

Accepting these means two different byte strings decode to the same
value, and decoding then re-encoding a non-canonical input produces
different bytes than the input -- a hash/identity malleability issue
for any code that hashes or compares raw RLP bytes assuming a 1:1
mapping to decoded values.

Adds `Rlp.NonCanonicalError` and validates both cases in
`readLength`/`decodeRlpCursor`. Verified against all 14,640 official
RLP test vectors (`bun test vectors/src/rlp.vectors.test.ts`) with no
regressions, plus new unit tests for the non-canonical cases and the
55/56 and 255/256 byte-count boundaries.

Co-authored-by: jxom <jake@tempo.xyz>
Co-authored-by: jxom <jake@tempo.xyz>
Adds NonCanonicalError to the readLength and decodeRlpCursor error
unions, updates the AccountSimulation snapshot that now fails at the RLP
layer, and tightens the changeset to one sentence.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1bc8f-0483-70f9-9462-97c636407bca
Co-authored-by: jxom <jake@tempo.xyz>
@vercel

vercel Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
ox Error Error Nov 8, 2026 7:58pm UTC

Request Review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 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-08T19:56:53.760506Z caaff64 PR opened
ℹ️ 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

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Published ox@0.0.0-pr-596-caaff64 under the pr-596 dist-tag, which follows this pull request until it closes.

pnpm add ox@pr-596
pnpm add ox@0.0.0-pr-596-caaff64

@jxom
jxom merged commit 80d74c1 into main Nov 8, 2026
15 of 16 checks passed
@jxom
jxom deleted the fix/rlp-noncanonical-length branch November 8, 2026 20:06
@github-actions github-actions Bot mentioned this pull request Nov 8, 2026

This branch had an error being deployed

1 failed deployment
Preview — caaff647 Deployed Nov 8, 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