Skip to content

fix: preserve bitlist input when reusing bytes - #560

Closed
GiHoon1123 wants to merge 2 commits into
ChainSafe:masterfrom
GiHoon1123:fix/bitlist-reuse-bytes
Closed

GiHoon1123 wants to merge 2 commits into
ChainSafe:masterfrom
GiHoon1123:fix/bitlist-reuse-bytes

Conversation

@GiHoon1123

@GiHoon1123 GiHoon1123 commented Oct 22, 2026 •

Copy link
Copy Markdown

Motivation

With reuseBytes: true, decoding a non-byte-aligned bitlist clears its padding bit in the input buffer. Decoding the same bytes again can then return a different length or throw.

Description

Check the bit length before copying the bitlist bytes and clearing the padding bit. Byte-aligned bitlists and other fields still reuse their buffers. The shared decoder covers both BitListType and ProgressiveBitListType.

Add regression tests for input preservation, repeated decoding, subarrays, length limits, and buffer reuse in mixed containers. Over-limit inputs are rejected before slicing in both value and tree decoding. Document the sharing behavior on deserialize().

Closes #559

Steps to test or reproduce

  • pnpm -r test:unit --coverage.enabled=false --reporter=dot
  • pnpm -r check-types
  • pnpm lint and pnpm lint:no-push-spread
  • 1,197 BitList spec cases passed after the limit-check change. The full generic suite, static tests with both minimal and mainnet presets, and EIP-4881 tests passed before that change.

The 86 regression cases also passed in Chrome and Firefox against the built JS. Direct browser execution of the TS sources hits the existing ByteViews type-export error, also reproduced with the existing bitlist tests.

@GiHoon1123
GiHoon1123 requested a review from a team as a code owner October 22, 2026 00:37
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T00:39:52.309042Z 816e49d 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 816e49d132

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

// the last byte is > 1, so a padding bit will exist in the last byte and need to be removed
const uint8Array = slice(data, start, end, reuseBytes);
// Copy even when reusing bytes so clearing the padding bit leaves the input intact.
const uint8Array = slice(data, start, end);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Validate the bitlist limit before copying

When reuseBytes is enabled and an untrusted non-byte-aligned bitlist exceeds the configured limit, this unconditional copy duplicates the entire field before BitListType or ProgressiveBitListType checks bitLen after the helper returns. A caller using reuse specifically to avoid allocations can therefore be forced into an input-sized allocation (and possible OOM) for data that is immediately rejected; compute and validate the bit length or serialized bound before taking this copy.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 083ea0f. Both bitlist types now pass their limit to the shared decoder, which checks the bit length before slicing. Added tests for value and tree decoding that verify over-limit inputs never reach slice or subarray.

@GiHoon1123

Copy link
Copy Markdown
Author

Could someone take a look at this PR when they have a chance? Happy to address any feedback. Thanks!

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.

BitList deserialization with reuseBytes: true mutates the input buffer

1 participant