multi: enforce strict TLV length checks

This commit improves TLV decoding safety and consistency across multiple
packages by enforcing fixed-length requirements and adding unit tests to
prevent malformed TLV records from being accepted.

Changes include:

- lnwire:
  * Enforce 8-byte length in Fee TLV decoder.
  * Enforce PubNonceSize in Musig2Nonce TLV decoder.
  * Enforce 8-byte length in ShortChannelID TLV decoder.
  * Added roundtrip and invalid length tests for Fee, Musig2Nonce,
    and ShortChannelID records.

- routing/route:
  * Enforce Vertex TLV length (33 bytes).
  * Added encode/decode and invalid length tests for Vertex.

- tlv:
  * Enforce correct length in DBytes33 decoder (33 bytes).
  * Added tests ensuring all fixed-size primitive decoders reject
    incorrect TLV lengths.

By strictly validating TLV lengths, we prevent malformed or corrupted
TLV records from being silently accepted, improving protocol safety.
This commit is contained in:
Erick Cestari committed 2025-12-29 19:18:49 -03:00
1 parent 013f5c93ed
commit 5a7925008e
10 files changed
+260 -5

No files matched your search

+1 -1
View File
@@ -51,7 +51,7 @@ func nonceTypeEncoder(w io.Writer, val interface{}, _ *[8]byte) error {
func nonceTypeDecoder(r io.Reader, val interface{}, _ *[8]byte,
l uint64) error {
if v, ok := val.(*Musig2Nonce); ok {
if v, ok := val.(*Musig2Nonce); ok && l == musig2.PubNonceSize {
_, err := io.ReadFull(r, v[:])
return err
}
+62
View File
@@ -0,0 +1,62 @@
package lnwire
import (
"testing"
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
"github.com/stretchr/testify/require"
)
// makeNonce creates a test Musig2Nonce with sequential byte values for testing
// TLV encoding/decoding.
func makeNonce() Musig2Nonce {
var n Musig2Nonce
for i := range n {
n[i] = byte(i)
}
return n
}
// TestMusig2NonceEncodeDecode tests that we're able to properly encode and
// decode Musig2Nonce within TLV streams.
func TestMusig2NonceEncodeDecode(t *testing.T) {
t.Parallel()
nonce := makeNonce()
var extraData ExtraOpaqueData
require.NoError(t, extraData.PackRecords(&nonce))
var extractedNonce Musig2Nonce
_, err := extraData.ExtractRecords(&extractedNonce)
require.NoError(t, err)
require.Equal(t, nonce, extractedNonce)
}
// TestMusig2NonceTypeDecodeInvalidLength ensures that decoding a Musig2Nonce
// TLV with an invalid length (anything other than 66 bytes) fails with an
// error.
func TestMusig2NonceTypeDecodeInvalidLength(t *testing.T) {
t.Parallel()
nonce := makeNonce()
var extraData ExtraOpaqueData
require.NoError(t, extraData.PackRecords(&nonce))
// Corrupt the TLV length field to simulate malformed input.
// Byte 1 contains the varint size encoding. Since 66 bytes fits into
// a single varint byte, we can directly modify extraData[1].
extraData[1] = musig2.PubNonceSize + 1
var out Musig2Nonce
_, err := extraData.ExtractRecords(&out)
require.Error(t, err)
extraData[1] = musig2.PubNonceSize - 1
_, err = extraData.ExtractRecords(&out)
require.Error(t, err)
}
+2 -1
View File
@@ -92,7 +92,8 @@ func DShortChannelID(r io.Reader, val interface{}, buf *[8]byte,
if v, ok := val.(*ShortChannelID); ok {
var scid uint64
err := tlv.DUint64(r, &scid, buf, 8)
// tlv.DUint64 forces the length to be 8 bytes.
err := tlv.DUint64(r, &scid, buf, l)
if err != nil {
return err
}
+25
View File
@@ -62,3 +62,28 @@ func TestScidTypeEncodeDecode(t *testing.T) {
require.Contains(t, tlvs, AliasScidRecordType)
require.Equal(t, aliasScid, aliasScid2)
}
// TestScidTypeDecodeInvalidLength ensures that decoding a ShortChannelID TLV
// with an invalid length (anything other than 8 bytes) fails with an error.
func TestScidTypeDecodeInvalidLength(t *testing.T) {
t.Parallel()
aliasScid := ShortChannelID{
BlockHeight: 1, TxIndex: 1, TxPosition: 1,
}
var extraData ExtraOpaqueData
require.NoError(t, extraData.PackRecords(&aliasScid))
// Corrupt the TLV length field to simulate malformed input.
extraData[1] = 8 + 1
var out ShortChannelID
_, err := extraData.ExtractRecords(&out)
require.Error(t, err)
extraData[1] = 8 - 1
_, err = extraData.ExtractRecords(&out)
require.Error(t, err)
}
+1 -1
View File
@@ -41,7 +41,7 @@ func feeEncoder(w io.Writer, val interface{}, buf *[8]byte) error {
// feeDecoder is a custom TLV decoder for the fee record.
func feeDecoder(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
v, ok := val.(*Fee)
if !ok {
if !ok || l != 8 {
return tlv.NewTypeForDecodingErr(val, "lnwire.Fee", l, 8)
}
+25
View File
@@ -38,3 +38,28 @@ func testTypedFee(t *testing.T, fee Fee) { //nolint: thelper
require.Equal(t, fee, extractedFee)
}
// TestTypedFeeTypeDecodeInvalidLength ensures that decoding a Fee TLV
// with an invalid length (anything other than 8 bytes) fails with an error.
func TestTypedFeeTypeDecodeInvalidLength(t *testing.T) {
t.Parallel()
fee := Fee{
BaseFee: 1, FeeRate: 1,
}
var extraData ExtraOpaqueData
require.NoError(t, extraData.PackRecords(&fee))
// Corrupt the TLV length field to simulate malformed input.
extraData[3] = 8 + 1
var out Fee
_, err := extraData.ExtractRecords(&out)
require.Error(t, err)
extraData[3] = 8 - 1
_, err = extraData.ExtractRecords(&out)
require.Error(t, err)
}