Skip to content

fix: encoder out of bounds#59

Merged
viviveevee merged 4 commits intomainfrom
vivee/encoder-out-of-bounds
Apr 23, 2026
Merged

fix: encoder out of bounds#59
viviveevee merged 4 commits intomainfrom
vivee/encoder-out-of-bounds

Conversation

@viviveevee
Copy link
Copy Markdown
Contributor

@viviveevee viviveevee commented Apr 23, 2026

Fix: encoder buffer overflow when map value fills initial buffer exactly

Reported here

Summary

  • Fix RangeError: Offset is outside the bounds of the DataView thrown when a map value's encoded bytes land exactly on the 2 KB internal buffer boundary.
  • Move the buffer bounds check from encodeItem into encodeHeader, which is the common write path for all encoding flows. The previous check in encodeItem was bypassed when encodeMap called encodeTextString(key) directly.
  • Extract a growBuffer helper that doubles the buffer until it meets the required minimum size.

Root cause

encodeMap encodes keys via encodeTextString(key) without going through encodeItem, so the bounds check never ran. When a preceding value (e.g. a large Uint8Array) filled the buffer to its exact capacity, the next key's encodeHeader call wrote past the end of the DataView.

Test plan

  • Three new regression tests in encode.spec.ts covering encode, encodeWithSelfDescribedTag, and a realistic ICP envelope payload
  • All 606 existing tests continue to pass

Copilot AI review requested due to automatic review settings April 23, 2026 08:21
@viviveevee viviveevee requested a review from a team as a code owner April 23, 2026 08:21
Comment thread CHANGELOG.md
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an out-of-bounds write in the CBOR encoder that could throw RangeError: Offset is outside the bounds of the DataView when a map value lands exactly on the initial 2 KB buffer boundary. It centralizes buffer growth in the common header write path and adds regression coverage for the boundary case.

Changes:

  • Add a growBuffer(minSize) helper and move buffer-capacity checks into encodeHeader (plus reuse the helper in encodeBytes).
  • Add regression tests that reproduce the 2 KB boundary overflow for encode and encodeWithSelfDescribedTag, plus a realistic envelope-shaped payload case.
  • Update test TS config to use module: ESNext (and reformat include).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
tsconfig.test.json Sets module: ESNext for the test TS config and reformats includes.
src/encode/encode.ts Fixes encoder buffer growth by ensuring bounds checks happen on the shared header write path and by introducing growBuffer.
src/encode/encode.spec.ts Adds regression tests validating the encoder no longer overflows at the 2 KB boundary.
CHANGELOG.md Updates the Unreleased Fix entry to mention the encoder overflow fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CHANGELOG.md
Copy link
Copy Markdown
Contributor

@nikosxenakis nikosxenakis left a comment

Choose a reason for hiding this comment

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

LGTM

@viviveevee viviveevee enabled auto-merge April 23, 2026 08:30
@viviveevee viviveevee added this pull request to the merge queue Apr 23, 2026
Merged via the queue into main with commit e527e5a Apr 23, 2026
10 checks passed
@viviveevee viviveevee deleted the vivee/encoder-out-of-bounds branch April 23, 2026 08:31
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