Repository navigation
fix: reject non-canonical RLP length encodings [0.x] - #597
Merged
Merged
Conversation
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Contributor
|
Published pnpm add ox@pr-597
pnpm add ox@0.0.0-pr-597-7b42891 |
Merged
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #596 to
0.x; supersedes #444 by @gomesalexandre, whose commits are kept with original authorship.Rlp.toBytes/Rlp.toHexnow throwRlp.NonCanonicalErrorinstead of accepting alternate encodings of the same value, matching geth's canonical-size rules: a single byte below0x80with 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.xhas none ofmain's other RLP error classes, soNonCanonicalErroris the only one added and the exports snapshot lists just it.Tests: new
Rlp.test.tscases and the updatedAccountSimulation.test.tssnapshot fail against0.x'sRlp.tsand pass here;bun test vectors/src/rlp.vectors.test.tspasses 14640/14640;tsc --noEmitand Biome pass;core+tempo-unithave the same 34 environment-only failures (Anvil/RPC, slow KZG) as a clean0.x, with 5 more passing tests.