Skip to content

fix(codecs-numbers): reject truncated and overlong shortU16 encodings - #1986

Closed
latent-9 wants to merge 2 commits into
anza-xyz:mainfrom
latent-9:fix-shortu16-decoder-bounds
Closed

latent-9 wants to merge 2 commits into
anza-xyz:mainfrom
latent-9:fix-shortu16-decoder-bounds

Conversation

@latent-9

Copy link
Copy Markdown

Problem

The shortU16 decoder loop had no upper bound, violating the codec's own documented contract of a 1 to 3 byte encoding (maxSize: 3). Two concrete failure modes:

  1. Overlong chains accepted: an input like 80 80 80 80 ... with continuation bits keeps decoding, producing values far outside the 0..65535 domain the encoder asserts via assertNumberIsBetweenForCodec.
  2. Truncated input silently misread: once the chain ran past the end of the buffer, bytes[offset + byteIndex] returned undefined; undefined & 0b10000000 === 0 terminated the loop, so truncated input decoded to garbage and returned an offset beyond bytes.length instead of failing. For example, decode(new Uint8Array([0x80])) currently returns [0, 2].

Every other number decoder guards its reads through assertByteArrayHasEnoughBytesForCodec; shortU16 was the odd one out.

Change

  • Bound the read loop at three bytes and assert per-byte availability with assertByteArrayHasEnoughBytesForCodec('shortU16', ...), so truncation throws INVALID_BYTE_LENGTH exactly like the fixed-size number decoders.
  • If a continuation bit is still set after three bytes, throw NUMBER_OUT_OF_RANGE, mirroring the encoder's own range assertion for the same domain violation.

Test plan

Two regression tests fail on current main and pass with this change:

  • decode([0x80]) and decode([0x80, 0x80]) (truncated chains) now throw SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH.
  • decode([0xff, 0xff, 0xff, 0x00]) (four-byte chain) now throws SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE.

Full codecs-numbers suite passes (28/28), including the existing round-trip sweep over all \[0, 0xffff\] values; package typecheck is clean.

The shortU16 decoder loop had no upper bound, so a continuation chain
longer than the documented three-byte encoding was accepted, producing
values far outside the u16 domain the encoder enforces. Worse, once the
chain ran past the end of the buffer, the out-of-bounds byte read as
undefined terminated the loop silently: truncated input decoded to
garbage and returned an offset beyond bytes.length instead of failing.

Bound the loop at three bytes and assert per-byte availability through
assertByteArrayHasEnoughBytesForCodec, matching every other number
decoder. A chain that still continues after three bytes now throws
NUMBER_OUT_OF_RANGE, mirroring the encoder's own range assertion.
@changeset-bot

changeset-bot Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a3e06ae

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 48 packages
Name Type
@solana/codecs-numbers Patch
@solana/codecs-data-structures Patch
@solana/codecs-strings Patch
@solana/codecs Patch
@solana/offchain-messages Patch
@solana/options Patch
@solana/program-client-core Patch
@solana/rpc-types Patch
@solana/sysvars Patch
@solana/transaction-messages Patch
@solana/transactions Patch
@solana/accounts Patch
@solana/addresses Patch
@solana/keys Patch
@solana/rpc-api Patch
@solana/rpc-graphql Patch
@solana/transaction-confirmation Patch
@solana/transaction-introspection Patch
@solana/instruction-plans Patch
@solana/kit Patch
@solana/signers Patch
@solana/plugin-interfaces Patch
@solana/rpc-parsed-types Patch
@solana/rpc-subscriptions-api Patch
@solana/rpc-subscriptions Patch
@solana/rpc-transformers Patch
@solana/rpc Patch
@solana/programs Patch
@solana/react Patch
@solana/wallet-account-signer Patch
@solana/compat Patch
@solana/instructions Patch
@solana/assertions Patch
@solana/codecs-core Patch
@solana/errors Patch
@solana/fast-stable-stringify Patch
@solana/fixed-points Patch
@solana/functional Patch
@solana/nominal-types Patch
@solana/plugin-core Patch
@solana/promises Patch
@solana/rpc-spec-types Patch
@solana/rpc-spec Patch
@solana/rpc-subscriptions-channel-websocket Patch
@solana/rpc-subscriptions-spec Patch
@solana/rpc-transport-http Patch
@solana/subscribable Patch
@solana/webcrypto-ed25519-polyfill Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the stale label Sep 10, 2026
@github-actions github-actions Bot closed this Sep 17, 2026
@latent-9

Copy link
Copy Markdown
Author

Reopening — this was closed by the stale bot, not reviewed. I re-verified every claim in the PR body by executing the actual pre/post code with the real @solana/codecs-core + @solana/errors packages (10/10 checks):

Old code reproduces exactly as described:

  • read([0x80]) → [0, 2] (garbage value, offset past end of buffer)
  • read([0x80, 0x80]) → silently accepted
  • read([0xff, 0xff, 0xff, 0x00]) → accepted with value 2097151 (far outside u16)

Fixed code:

  • [0x80] → INVALID_BYTE_LENGTH {bytesLength: 1, expected: 2}
  • [0x80, 0x80] → INVALID_BYTE_LENGTH {bytesLength: 2, expected: 3}
  • overlong chain → NUMBER_OUT_OF_RANGE {min: 0, max: 65535, value: 2097151}
  • Valid 1–3 byte decodes unchanged; full 0..65535 encode→decode sweep intact.

Happy to address any review feedback. 🙏

@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant