diff --git a/.changeset/quiet-shorts-refuse.md b/.changeset/quiet-shorts-refuse.md new file mode 100644 index 000000000..09224e812 --- /dev/null +++ b/.changeset/quiet-shorts-refuse.md @@ -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. diff --git a/packages/codecs-numbers/src/__tests__/short-u16-test.ts b/packages/codecs-numbers/src/__tests__/short-u16-test.ts index 91c218766..90b98d374 100644 --- a/packages/codecs-numbers/src/__tests__/short-u16-test.ts +++ b/packages/codecs-numbers/src/__tests__/short-u16-test.ts @@ -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__'; @@ -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, + }), + ); + }); + it('has the right sizes', () => { expect(shortU16().maxSize).toBe(3); expect(shortU16().getSizeFromValue(1)).toBe(1); diff --git a/packages/codecs-numbers/src/short-u16.ts b/packages/codecs-numbers/src/short-u16.ts index 89ebb70f4..7563fbbf8 100644 --- a/packages/codecs-numbers/src/short-u16.ts +++ b/packages/codecs-numbers/src/short-u16.ts @@ -1,4 +1,5 @@ import { + assertByteArrayHasEnoughBytesForCodec, combineCodec, createDecoder, createEncoder, @@ -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. * @@ -90,7 +94,8 @@ export const getShortU16Decoder = (): VariableSizeDecoder => 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; @@ -98,10 +103,15 @@ export const getShortU16Decoder = (): VariableSizeDecoder => 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]; } } - return [value, offset + byteCount]; + throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, { + bytesLength: 4, + codecDescription: 'shortU16', + expected: 3, + }); }, }); @@ -114,14 +124,14 @@ export const getShortU16Decoder = (): VariableSizeDecoder => * - 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` for encoding and decoding `shortU16` values.