From 8f1b20b67bbbaa6b37921141a72f73ae35a6f397 Mon Sep 17 00:00:00 2001 From: prikshitchawla300 Date: Wed, 11 Nov 2026 13:13:39 +0530 Subject: [PATCH] fix: reject non-canonical varint encodings varuint.decode() accepted non-minimal CompactSize encodings (e.g. fd 03 00 for 3), while callers advance by encodingLength(value). The two only agree for minimal encodings. decode() now throws "Format Error: non-canonical varint encoding" for fd/fe/ff-prefixed values that fit in a shorter encoding, matching Bitcoin Core's ReadCompactSize. --- src/lib/converter/varint.js | 19 ++++++--- src/tests/converters.js | 61 +++++++++++++++++++++++++++ src/tests/fixtures/fromBuffer.js | 24 +++++++++++ src/tests/fixtures/varint.js | 12 ++++++ src/tests/varint.js | 8 ++++ ts_src/lib/converter/varint.ts | 22 +++++++--- ts_src/tests/converters.ts | 65 +++++++++++++++++++++++++++++ ts_src/tests/fixtures/fromBuffer.ts | 24 +++++++++++ ts_src/tests/fixtures/varint.ts | 12 ++++++ ts_src/tests/varint.ts | 9 ++++ 10 files changed, 245 insertions(+), 11 deletions(-) create mode 100644 src/tests/converters.js create mode 100644 ts_src/tests/converters.ts diff --git a/src/lib/converter/varint.js b/src/lib/converter/varint.js index 72a4393..ec7255d 100644 --- a/src/lib/converter/varint.js +++ b/src/lib/converter/varint.js @@ -36,32 +36,41 @@ function encode(_number, buffer, offset) { return buffer; } exports.encode = encode; +function checkCanonical(n, minimum) { + // Reject non-minimal encodings, as Bitcoin Core's ReadCompactSize does. + if (n < minimum) + throw new RangeError('Format Error: non-canonical varint encoding'); +} function decode(buffer, offset) { if (!Buffer.isBuffer(buffer)) throw new TypeError('buffer must be a Buffer instance'); if (!offset) offset = 0; const first = buffer.readUInt8(offset); + let _number; // 8 bit if (first < 0xfd) { Object.assign(decode, { bytes: 1 }); return first; // 16 bit } else if (first === 0xfd) { + _number = buffer.readUInt16LE(offset + 1); + checkCanonical(_number, 0xfd); Object.assign(decode, { bytes: 3 }); - return buffer.readUInt16LE(offset + 1); // 32 bit } else if (first === 0xfe) { + _number = buffer.readUInt32LE(offset + 1); + checkCanonical(_number, 0x10000); Object.assign(decode, { bytes: 5 }); - return buffer.readUInt32LE(offset + 1); // 64 bit } else { - Object.assign(decode, { bytes: 9 }); const lo = buffer.readUInt32LE(offset + 1); const hi = buffer.readUInt32LE(offset + 5); - const _number = hi * 0x0100000000 + lo; + _number = hi * 0x0100000000 + lo; checkUInt53(_number); - return _number; + checkCanonical(_number, 0x100000000); + Object.assign(decode, { bytes: 9 }); } + return _number; } exports.decode = decode; function encodingLength(_number) { diff --git a/src/tests/converters.js b/src/tests/converters.js new file mode 100644 index 0000000..49dacdc --- /dev/null +++ b/src/tests/converters.js @@ -0,0 +1,61 @@ +'use strict'; +Object.defineProperty(exports, '__esModule', { value: true }); +const tape = require('tape'); +const convert = require('../lib/converter'); +const proprietaryKeyVal_1 = require('../lib/proprietaryKeyVal'); +const typeFields_1 = require('../lib/typeFields'); +// Nested decoders that read their own varint lengths must reject +// non-minimal encodings (CSHLD-2225). +const NON_CANONICAL = new RegExp( + '^RangeError: Format Error: non-canonical varint encoding$', +); +tape('witnessUtxo decode rejects non-canonical script length', t => { + // 8-byte amount, script length 1 encoded as fd 01 00, then 1 script byte + const value = Buffer.from('0100000000000000' + 'fd0100' + '51', 'hex'); + t.throws(() => { + convert.inputs.witnessUtxo.decode({ + key: Buffer.from([typeFields_1.InputTypes.WITNESS_UTXO]), + value, + }); + }, NON_CANONICAL); + t.end(); +}); +tape('tapTree decode rejects non-canonical leaf script length', t => { + // depth 0, leaf version c0, script length 1 encoded as fd 01 00, script 51 + const value = Buffer.from('00c0' + 'fd0100' + '51', 'hex'); + t.throws(() => { + convert.outputs.tapTree.decode({ + key: Buffer.from([typeFields_1.OutputTypes.TAP_TREE]), + value, + }); + }, NON_CANONICAL); + t.end(); +}); +tape('tapBip32Derivation decode rejects non-canonical leaf hash count', t => { + // leaf hash count 0 encoded as fd 00 00, then fingerprint (no path) + const key = Buffer.concat([ + Buffer.from([typeFields_1.InputTypes.TAP_BIP32_DERIVATION]), + Buffer.alloc(32, 1), + ]); + const value = Buffer.from('fd0000' + '00000000', 'hex'); + t.throws(() => { + convert.inputs.tapBip32Derivation.decode({ key, value }, true); + }, NON_CANONICAL); + t.end(); +}); +tape('decodeProprietaryKey rejects non-canonical identifier length', t => { + // fc, identifier length 3 encoded as fd 03 00, "abc", subtype 0 + const key = Buffer.from('fc' + 'fd0300' + '616263' + '00', 'hex'); + t.throws(() => { + proprietaryKeyVal_1.decodeProprietaryKey(key); + }, NON_CANONICAL); + t.end(); +}); +tape('decodeProprietaryKey rejects non-canonical subtype', t => { + // fc, identifier length 3, "abc", subtype 0 encoded as fd 00 00 + const key = Buffer.from('fc' + '03' + '616263' + 'fd0000', 'hex'); + t.throws(() => { + proprietaryKeyVal_1.decodeProprietaryKey(key); + }, NON_CANONICAL); + t.end(); +}); diff --git a/src/tests/fixtures/fromBuffer.js b/src/tests/fixtures/fromBuffer.js index 272d9f8..acbf2b2 100644 --- a/src/tests/fixtures/fromBuffer.js +++ b/src/tests/fixtures/fromBuffer.js @@ -14,4 +14,28 @@ exports.fixtures = [ exception: 'Format Error: Keys must be unique for each output: output index 0 key 08', }, + // Non-canonical key length (fd 01 00 = 1) + { + hex: '70736274fffd01000801ff00', + exception: 'Format Error: non-canonical varint encoding', + }, + // Non-canonical value length (fd 01 00 = 1) + { + hex: '70736274ff0108fd0100ff00', + exception: 'Format Error: non-canonical varint encoding', + }, + // Non-canonical key length (fe / ff prefixes) + { + hex: '70736274fffe010000000801ff00', + exception: 'Format Error: non-canonical varint encoding', + }, + { + hex: '70736274ffff01000000000000000801ff00', + exception: 'Format Error: non-canonical varint encoding', + }, + // Zero-length key via fd 00 00 (canonical 0x00 is the map terminator) + { + hex: '70736274fffd0000000100', + exception: 'Format Error: non-canonical varint encoding', + }, ]; diff --git a/src/tests/fixtures/varint.js b/src/tests/fixtures/varint.js index 12a9e6a..b5b5bb9 100644 --- a/src/tests/fixtures/varint.js +++ b/src/tests/fixtures/varint.js @@ -74,4 +74,16 @@ exports.fixtures = { msg: '^RangeError: value out of range$', }, ], + // Non-minimal encodings: decode must reject these (Bitcoin Core does) + nonCanonical: [ + { hex: 'fd0000' }, + { hex: 'fd0500' }, + { hex: 'fdfc00' }, + { hex: 'fe00000000' }, + { hex: 'fe05000000' }, + { hex: 'feffff0000' }, + { hex: 'ff0000000000000000' }, + { hex: 'ff0500000000000000' }, + { hex: 'ffffffffff00000000' }, + ], }; diff --git a/src/tests/varint.js b/src/tests/varint.js index f74ef0a..5de26fd 100644 --- a/src/tests/varint.js +++ b/src/tests/varint.js @@ -44,6 +44,14 @@ varint_1.fixtures.invalid.forEach((fixture, i) => { }); } }); +varint_1.fixtures.nonCanonical.forEach((fixture, i) => { + test('non-canonical decode #' + (i + 1), t => { + t.throws(() => { + varuint.decode(Buffer.from(fixture.hex, 'hex')); + }, new RegExp('^RangeError: Format Error: non-canonical varint encoding$')); + t.end(); + }); +}); test('encode', t => { t.test('write to buffer with offset', _t => { const buffer = Buffer.from([0x00, 0x00]); diff --git a/ts_src/lib/converter/varint.ts b/ts_src/lib/converter/varint.ts index b692128..9851dab 100644 --- a/ts_src/lib/converter/varint.ts +++ b/ts_src/lib/converter/varint.ts @@ -46,12 +46,19 @@ export function encode( return buffer; } +function checkCanonical(n: number, minimum: number): void { + // Reject non-minimal encodings, as Bitcoin Core's ReadCompactSize does. + if (n < minimum) + throw new RangeError('Format Error: non-canonical varint encoding'); +} + export function decode(buffer: Buffer, offset?: number): number { if (!Buffer.isBuffer(buffer)) throw new TypeError('buffer must be a Buffer instance'); if (!offset) offset = 0; const first = buffer.readUInt8(offset); + let _number: number; // 8 bit if (first < 0xfd) { @@ -60,24 +67,27 @@ export function decode(buffer: Buffer, offset?: number): number { // 16 bit } else if (first === 0xfd) { + _number = buffer.readUInt16LE(offset + 1); + checkCanonical(_number, 0xfd); Object.assign(decode, { bytes: 3 }); - return buffer.readUInt16LE(offset + 1); // 32 bit } else if (first === 0xfe) { + _number = buffer.readUInt32LE(offset + 1); + checkCanonical(_number, 0x10000); Object.assign(decode, { bytes: 5 }); - return buffer.readUInt32LE(offset + 1); // 64 bit } else { - Object.assign(decode, { bytes: 9 }); const lo = buffer.readUInt32LE(offset + 1); const hi = buffer.readUInt32LE(offset + 5); - const _number = hi * 0x0100000000 + lo; + _number = hi * 0x0100000000 + lo; checkUInt53(_number); - - return _number; + checkCanonical(_number, 0x100000000); + Object.assign(decode, { bytes: 9 }); } + + return _number; } export function encodingLength(_number: number): number { diff --git a/ts_src/tests/converters.ts b/ts_src/tests/converters.ts new file mode 100644 index 0000000..3c145be --- /dev/null +++ b/ts_src/tests/converters.ts @@ -0,0 +1,65 @@ +import * as tape from 'tape'; +import * as convert from '../lib/converter'; +import { decodeProprietaryKey } from '../lib/proprietaryKeyVal'; +import { InputTypes, OutputTypes } from '../lib/typeFields'; + +// Nested decoders that read their own varint lengths must reject +// non-minimal encodings (CSHLD-2225). +const NON_CANONICAL = new RegExp( + '^RangeError: Format Error: non-canonical varint encoding$', +); + +tape('witnessUtxo decode rejects non-canonical script length', t => { + // 8-byte amount, script length 1 encoded as fd 01 00, then 1 script byte + const value = Buffer.from('0100000000000000' + 'fd0100' + '51', 'hex'); + t.throws(() => { + convert.inputs.witnessUtxo.decode({ + key: Buffer.from([InputTypes.WITNESS_UTXO]), + value, + }); + }, NON_CANONICAL); + t.end(); +}); + +tape('tapTree decode rejects non-canonical leaf script length', t => { + // depth 0, leaf version c0, script length 1 encoded as fd 01 00, script 51 + const value = Buffer.from('00c0' + 'fd0100' + '51', 'hex'); + t.throws(() => { + convert.outputs.tapTree.decode({ + key: Buffer.from([OutputTypes.TAP_TREE]), + value, + }); + }, NON_CANONICAL); + t.end(); +}); + +tape('tapBip32Derivation decode rejects non-canonical leaf hash count', t => { + // leaf hash count 0 encoded as fd 00 00, then fingerprint (no path) + const key = Buffer.concat([ + Buffer.from([InputTypes.TAP_BIP32_DERIVATION]), + Buffer.alloc(32, 1), + ]); + const value = Buffer.from('fd0000' + '00000000', 'hex'); + t.throws(() => { + convert.inputs.tapBip32Derivation.decode({ key, value }, true); + }, NON_CANONICAL); + t.end(); +}); + +tape('decodeProprietaryKey rejects non-canonical identifier length', t => { + // fc, identifier length 3 encoded as fd 03 00, "abc", subtype 0 + const key = Buffer.from('fc' + 'fd0300' + '616263' + '00', 'hex'); + t.throws(() => { + decodeProprietaryKey(key); + }, NON_CANONICAL); + t.end(); +}); + +tape('decodeProprietaryKey rejects non-canonical subtype', t => { + // fc, identifier length 3, "abc", subtype 0 encoded as fd 00 00 + const key = Buffer.from('fc' + '03' + '616263' + 'fd0000', 'hex'); + t.throws(() => { + decodeProprietaryKey(key); + }, NON_CANONICAL); + t.end(); +}); diff --git a/ts_src/tests/fixtures/fromBuffer.ts b/ts_src/tests/fixtures/fromBuffer.ts index 1d1ca9b..e664066 100644 --- a/ts_src/tests/fixtures/fromBuffer.ts +++ b/ts_src/tests/fixtures/fromBuffer.ts @@ -12,4 +12,28 @@ export const fixtures = [ exception: 'Format Error: Keys must be unique for each output: output index 0 key 08', }, + // Non-canonical key length (fd 01 00 = 1) + { + hex: '70736274fffd01000801ff00', + exception: 'Format Error: non-canonical varint encoding', + }, + // Non-canonical value length (fd 01 00 = 1) + { + hex: '70736274ff0108fd0100ff00', + exception: 'Format Error: non-canonical varint encoding', + }, + // Non-canonical key length (fe / ff prefixes) + { + hex: '70736274fffe010000000801ff00', + exception: 'Format Error: non-canonical varint encoding', + }, + { + hex: '70736274ffff01000000000000000801ff00', + exception: 'Format Error: non-canonical varint encoding', + }, + // Zero-length key via fd 00 00 (canonical 0x00 is the map terminator) + { + hex: '70736274fffd0000000100', + exception: 'Format Error: non-canonical varint encoding', + }, ]; diff --git a/ts_src/tests/fixtures/varint.ts b/ts_src/tests/fixtures/varint.ts index 7fc6c6d..06df0d6 100644 --- a/ts_src/tests/fixtures/varint.ts +++ b/ts_src/tests/fixtures/varint.ts @@ -72,4 +72,16 @@ export const fixtures = { msg: '^RangeError: value out of range$', }, ], + // Non-minimal encodings: decode must reject these (Bitcoin Core does) + nonCanonical: [ + { hex: 'fd0000' }, + { hex: 'fd0500' }, + { hex: 'fdfc00' }, + { hex: 'fe00000000' }, + { hex: 'fe05000000' }, + { hex: 'feffff0000' }, + { hex: 'ff0000000000000000' }, + { hex: 'ff0500000000000000' }, + { hex: 'ffffffffff00000000' }, + ], }; diff --git a/ts_src/tests/varint.ts b/ts_src/tests/varint.ts index 7858e8b..f6fc758 100644 --- a/ts_src/tests/varint.ts +++ b/ts_src/tests/varint.ts @@ -50,6 +50,15 @@ fixtures.invalid.forEach((fixture, i) => { } }); +fixtures.nonCanonical.forEach((fixture, i) => { + test('non-canonical decode #' + (i + 1), t => { + t.throws(() => { + varuint.decode(Buffer.from(fixture.hex, 'hex')); + }, new RegExp('^RangeError: Format Error: non-canonical varint encoding$')); + t.end(); + }); +}); + test('encode', t => { t.test('write to buffer with offset', _t => { const buffer = Buffer.from([0x00, 0x00]);