Skip to content

fix: reject Union input without a selector byte - #568

Merged
spiral-ladder merged 2 commits into
ChainSafe:masterfrom
HereForTheTechNFT:fix/union-empty-selector
Nov 24, 2026
Merged

spiral-ladder merged 2 commits into
ChainSafe:masterfrom
HereForTheTechNFT:fix/union-empty-selector

Conversation

@HereForTheTechNFT

Copy link
Copy Markdown
Contributor

Motivation

UnionType.deserialize on an empty buffer throws TypeError: Cannot read properties of undefined instead of a decode error (#567).

Description

The selector read from an empty buffer is undefined. It passes the selector > this.maxSelector check, and this.types[undefined] fails later. value_deserializeFromBytes and tree_deserializeFromBytes now throw Union requires a selector byte when end <= start, as CompatibleUnionType already does ("CompatibleUnion requires a selector byte").

Closes #567

Steps to test or reproduce

The new No selector byte case in test/unit/byType/union/invalid.test.ts runs through runTypeTestInvalid, so it covers struct deserialize, tree deserialize, toView and toViewDU. Its 4 tests fail without the fix and pass with it.

In packages/ssz, the full unit suite passes (2429 passed, 3 skipped). biome check on the two changed files and tsc --noEmit report nothing.

🤖 Generated with Claude Code

https://claude.ai/code/session_014cRzwuyJe8BGwXowsQ3kpw

HereForTheTechNFT and others added 2 commits November 24, 2026 06:49
UnionType read the selector from an empty buffer as undefined, which passed the
maxSelector check and failed later with a TypeError. Throw a decode error
instead, as CompatibleUnionType does.

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
@HereForTheTechNFT
HereForTheTechNFT requested a review from a team as a code owner November 24, 2026 05:50
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 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-24T05:51:41.966375Z 91968b7 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.

@kutluhaneth46 kutluhaneth46 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up #567. The empty-range guard in both value_deserializeFromBytes and tree_deserializeFromBytes is the right place. Public deserialize / deserializeFromBytes go through the tree path on CompositeType, so UnionType.deserialize(new Uint8Array()) should now throw a real decode Error instead of TypeError on types[undefined].

One small test hardening idea: in the new "No selector byte" case, assert error instanceof Error (or that the message is not a TypeError-shaped stack). That locks the original bug class so a future refactor cannot silently regress to Cannot read properties of undefined while still matching the "selector byte" substring.

Otherwise the fix looks complete for the empty-buffer path.

@spiral-ladder spiral-ladder left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks for the PR

@spiral-ladder
spiral-ladder merged commit d6eaeda into ChainSafe:master Nov 24, 2026
12 checks passed
@HereForTheTechNFT

Copy link
Copy Markdown
Contributor Author

Thanks for the review, and to spiral-ladder for merging.

Good point about the test: the new case matches on the message, so a regression that threw a TypeError mentioning "selector byte" would still pass. Asserting that the error is not a TypeError would pin the original bug class. It's a one-line follow-up if the maintainers want it.

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.

Union deserialize on empty input throws TypeError instead of a decode error

3 participants