Skip to content

fix: reject non-canonical RLP length encodings [0.x] - #597

Merged
jxom merged 3 commits into
0.xfrom
fix/rlp-noncanonical-length-0.x
Nov 8, 2026
Merged

jxom merged 3 commits into
0.xfrom
fix/rlp-noncanonical-length-0.x

Conversation

@jxom

@jxom jxom commented Nov 8, 2026

Copy link
Copy Markdown
Member

Backport of #596 to 0.x; supersedes #444 by @gomesalexandre, whose 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: a single byte below 0x80 with a length-1 prefix (0x8100), a long-form length below 56 (0xb800, 0xf8020102), or leading zero length bytes (0xb9000141).

Same source change as #596. 0.x has none of main's other RLP error classes, so NonCanonicalError is the only one added and the exports snapshot lists just it.

Tests: new Rlp.test.ts cases and the updated AccountSimulation.test.ts snapshot fail against 0.x's Rlp.ts and pass here; bun test vectors/src/rlp.vectors.test.ts passes 14640/14640; tsc --noEmit and Biome pass; core + tempo-unit have the same 34 environment-only failures (Anvil/RPC, slow KZG) as a clean 0.x, with 5 more passing tests.

gomesalexandre and others added 3 commits November 8, 2026 19:48
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>
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 Ready Ready Preview Nov 8, 2026 8:11pm 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-08T20:03:33.195317Z 7b42891 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-597-7b42891 under the pr-597 dist-tag, which follows this pull request until it closes.

pnpm add ox@pr-597
pnpm add ox@0.0.0-pr-597-7b42891

@jxom
jxom merged commit 0253792 into 0.x Nov 8, 2026
14 checks passed
@jxom
jxom deleted the fix/rlp-noncanonical-length-0.x branch November 8, 2026 20:12
@github-actions github-actions Bot mentioned this pull request Nov 8, 2026

This branch was successfully deployed

1 active deployment
Preview — 7b42891d 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