Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/lib/converter/varint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
61 changes: 61 additions & 0 deletions src/tests/converters.js
Original file line number Diff line number Diff line change
@@ -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();
});
24 changes: 24 additions & 0 deletions src/tests/fixtures/fromBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
];
12 changes: 12 additions & 0 deletions src/tests/fixtures/varint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
],
};
8 changes: 8 additions & 0 deletions src/tests/varint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
22 changes: 16 additions & 6 deletions ts_src/lib/converter/varint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
65 changes: 65 additions & 0 deletions ts_src/tests/converters.ts
Original file line number Diff line number Diff line change
@@ -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();
});
24 changes: 24 additions & 0 deletions ts_src/tests/fixtures/fromBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
];
12 changes: 12 additions & 0 deletions ts_src/tests/fixtures/varint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
],
};
9 changes: 9 additions & 0 deletions ts_src/tests/varint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
Loading