Skip to content

fix(calldata): reject non-canonical wire data in decoder - #233

Open
feafel wants to merge 2 commits into
genlayerlabs:v2-devfrom
feafel:fix/calldata-decoder-validation
Open

feafel wants to merge 2 commits into
genlayerlabs:v2-devfrom
feafel:fix/calldata-decoder-validation

Conversation

@feafel

@feafel feafel commented Oct 11, 2026 •

Copy link
Copy Markdown

Summary

Fixes #227: calldata.decode was more permissive than the GenVM reference decoder in several wire-level cases:

  1. readULeb128 accepted overlong/non-canonical encodings such as [0x80, 0x00]
  2. readULeb128 read past the end of input as undefined
  3. Map decoding did not enforce canonical key order or reject duplicate keys
  4. Strings and map keys used non-fatal TextDecoder, so malformed UTF-8 was accepted with replacement characters

Changes

  • src/abi/calldata/decoder.ts: reject overlong ULEB128 encodings
  • src/abi/calldata/decoder.ts: fail immediately on unexpected end-of-input
  • src/abi/calldata/decoder.ts: enforce strictly increasing map key order
  • src/abi/calldata/decoder.ts: use fatal TextDecoder for UTF-8 validation
  • src/abi/calldata/decoder.ts: add bounds checking for strings and map keys
  • Added 9 regression tests in tests/calldata-decoder.test.ts

Testing

  • All 102 tests pass (93 existing + 9 new)
  • npx vitest run — clean

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Calldata decoding now rejects truncated or overlong values, invalid UTF-8, incomplete strings or map keys, and unordered or duplicate map keys.
    • Calldata encoding now rejects unsafe numeric values and strings or map keys with invalid Unicode. Use bigint for integers outside the safe-integer range.
  • Tests
    • Added coverage for valid and invalid calldata encoding and decoding cases.

calldata.encode silently accepted:
1. Unsafe integer numbers (outside Number.isSafeInteger range) — BigInt(data) encoded the already-rounded Number
2. Strings with unpaired UTF-16 surrogates — TextEncoder replaced them with U+FFFD, causing decode(encode(value)) !== value and map key collisions

Now rejects both with clear error messages, matching the GenVM Python reference encoder behavior.

Closes genlayerlabs#229
calldata.decode was more permissive than the GenVM reference decoder:
1. readULeb128 accepted overlong/non-canonical encodings
2. readULeb128 read past end of input as undefined
3. Map decoding did not enforce canonical key order or reject duplicates
4. Strings and map keys used non-fatal TextDecoder for invalid UTF-8

Now rejects all of these at the point they are encountered.

Closes genlayerlabs#227
@github-actions
github-actions Bot changed the base branch from main to v2-dev October 11, 2026 01:33
@github-actions

Copy link
Copy Markdown

This PR targeted main, which is only the default/static branch.

I retargeted it to v2-dev, the active development branch. Pushes to v2-dev automatically fast-forward main.

@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 26a11d46-ed67-4dbc-a4f0-dc2f1f51210c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR



  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

calldata.decode accepts non-canonical encodings and delays truncation failures

2 participants