Skip to content
5 changes: 5 additions & 0 deletions .changeset/quiet-shorts-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solana/codecs-numbers': patch
---

Fixed `getShortU16Decoder()` silently accepting malformed input: a truncated buffer decoded to garbage with an offset past the end of the byte array, and continuation chains longer than the documented three-byte encoding decoded to values outside the u16 domain. Malformed input now throws `SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH`, and decoded values above 65,535 now throw `SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE`, matching the guards every other number decoder already uses.
58 changes: 58 additions & 0 deletions packages/codecs-numbers/src/__tests__/short-u16-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH,
SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE,
SolanaError,
} from '@solana/errors';

import { getShortU16Codec } from '../short-u16';
import { assertRangeError, assertValid, assertValidEncode } from './__setup__';

Expand Down Expand Up @@ -54,6 +60,58 @@ describe('getShortU16Codec', () => {
}
});

it('throws when the buffer ends before the continuation chain terminates', () => {
expect.hasAssertions();
// Continuation bit set, but no further bytes.
expect(() => shortU16().decode(new Uint8Array([0x80]))).toThrow(
new SolanaError(SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, {
bytesLength: 1,
codecDescription: 'shortU16',
expected: 2,
}),
);
expect(() => shortU16().decode(new Uint8Array([0x80, 0x80]))).toThrow(
new SolanaError(SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, {
bytesLength: 2,
codecDescription: 'shortU16',
expected: 3,
}),
);
});

it('rejects continuation chains that exceed the three-byte encoding', () => {
expect.hasAssertions();
// A 4-byte chain needs a fourth byte, which violates the 3-byte encoding cap.
expect(() => shortU16().decode(new Uint8Array([0xff, 0xff, 0xff, 0x00]))).toThrow(
new SolanaError(SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, {
bytesLength: 4,
codecDescription: 'shortU16',
expected: 3,
}),
);
// Same rejection when the buffer ends exactly at three continuation bytes.
expect(() => shortU16().decode(new Uint8Array([0xff, 0xff, 0xff]))).toThrow(
new SolanaError(SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, {
bytesLength: 4,
codecDescription: 'shortU16',
expected: 3,
}),
);
});

it('rejects terminated three-byte encodings above the u16 domain', () => {
expect.hasAssertions();
// No continuation bit on the third byte, but 0x04 << 14 is 65536.
expect(() => shortU16().decode(new Uint8Array([0x80, 0x80, 0x04]))).toThrow(
new SolanaError(SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, {
codecDescription: 'shortU16',
max: MAX,
min: MIN,
value: 65536,
}),
);
});
Comment thread
lorisleiva marked this conversation as resolved.
Resolved

it('has the right sizes', () => {
expect(shortU16().maxSize).toBe(3);
expect(shortU16().getSizeFromValue(1)).toBe(1);
Expand Down
20 changes: 15 additions & 5 deletions packages/codecs-numbers/src/short-u16.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
assertByteArrayHasEnoughBytesForCodec,
combineCodec,
createDecoder,
createEncoder,
Expand All @@ -8,9 +9,12 @@ import {
VariableSizeDecoder,
VariableSizeEncoder,
} from '@solana/codecs-core';
import { SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, SolanaError } from '@solana/errors';

import { assertNumberIsBetweenForCodec } from './assertions';

const MAX_SHORT_U16_VALUE = 65535;

/**
* Returns an encoder for `shortU16` values.
*
Expand Down Expand Up @@ -90,18 +94,24 @@ export const getShortU16Decoder = (): VariableSizeDecoder<number> =>
read: (bytes: ReadonlyUint8Array | Uint8Array, offset): [number, Offset] => {
let value = 0;
let byteCount = 0;
while (++byteCount) {
while (++byteCount <= 3) {
assertByteArrayHasEnoughBytesForCodec('shortU16', byteCount, bytes, offset);
const byteIndex = byteCount - 1;
const currentByte = bytes[offset + byteIndex];
const nextSevenBits = 0b1111111 & currentByte;
// Insert the next group of seven bits into the correct slot of the output value.
value |= nextSevenBits << (byteIndex * 7);
if ((currentByte & 0b10000000) === 0) {
// This byte does not have its continuation bit set. We're done.
break;
assertNumberIsBetweenForCodec('shortU16', 0, MAX_SHORT_U16_VALUE, value);
return [value, offset + byteCount];
Comment thread
lorisleiva marked this conversation as resolved.
Resolved
}
}
return [value, offset + byteCount];
throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, {
bytesLength: 4,
codecDescription: 'shortU16',
expected: 3,
});
},
});

Expand All @@ -114,14 +124,14 @@ export const getShortU16Decoder = (): VariableSizeDecoder<number> =>
* - If the value is `<= 0x7f` (127), it is stored in a **single byte**
* and the first bit is set to `0` to indicate the end of the value.
* - Otherwise, the first bit is set to `1` to indicate that the value continues in the next byte, which follows the same pattern.
* - This process repeats until the value is fully encoded in up to 3 bytes. The third and last byte, if needed, uses all 8 bits to store the remaining value.
* - This process repeats until the value is fully encoded in up to 3 bytes. The third and last byte, if needed, stores the remaining value in its low 7 bits.
*
* In other words, the encoding scheme follows this structure:
*
* ```txt
* 0XXXXXXX <- Values 0 to 127 (1 byte)
* 1XXXXXXX 0XXXXXXX <- Values 128 to 16,383 (2 bytes)
* 1XXXXXXX 1XXXXXXX XXXXXXXX <- Values 16,384 to 4,194,303 (3 bytes)
* 1XXXXXXX 1XXXXXXX 0XXXXXXX <- Values 16,384 to 65,535 (3 bytes)
* ```
*
* @returns A `VariableSizeCodec<number | bigint, number>` for encoding and decoding `shortU16` values.
Expand Down
Loading