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
@@ -112,7 +112,7 @@ func encodeVertex(w io.Writer, val interface{}, _ *[8]byte) error {
}
func decodeVertex(r io.Reader, val interface{}, _ *[8]byte, l uint64) error {
if b, ok := val.(*Vertex); ok {
if b, ok := val.(*Vertex); ok && l == VertexSize {
_, err := io.ReadFull(r, b[:])
return err
}
+55
View File
@@ -8,6 +8,7 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record"
"github.com/lightningnetwork/lnd/tlv"
"github.com/stretchr/testify/require"
)
@@ -430,3 +431,57 @@ func TestBlindedHopFee(t *testing.T) {
require.Equal(t, lnwire.MilliSatoshi(0), route.HopFee(3))
require.Equal(t, lnwire.MilliSatoshi(0), route.HopFee(4))
}
// makeVertex creates a test Vertex with sequential byte values for testing
// TLV encoding/decoding.
func makeVertex() Vertex {
var v Vertex
for i := range v {
v[i] = byte(i)
}
return v
}
// TestVertexTLVEncodeDecode tests that we're able to properly encode and decode
// Vertex within TLV streams.
func TestVertexTLVEncodeDecode(t *testing.T) {
t.Parallel()
vertex := makeVertex()
var extraData lnwire.ExtraOpaqueData
require.NoError(t, extraData.PackRecords(&vertex))
var vertex2 Vertex
tlvs, err := extraData.ExtractRecords(&vertex2)
require.NoError(t, err)
require.Contains(t, tlvs, tlv.Type(0))
require.Equal(t, vertex, vertex2)
}
// TestVertexTypeDecodeInvalidLength ensures that decoding a Vertex TLV
// with an invalid length (anything other than 33) fails with an error.
func TestVertexTypeDecodeInvalidLength(t *testing.T) {
t.Parallel()
vertex := makeVertex()
var extraData lnwire.ExtraOpaqueData
require.NoError(t, extraData.PackRecords(&vertex))
// Corrupt the TLV length field to simulate malformed input.
// Byte 1 contains the varint size encoding. Since 33 bytes fits into
// a single varint byte, we can directly modify extraData[1].
extraData[1] = VertexSize + 1
var out Vertex
_, err := extraData.ExtractRecords(&out)
require.Error(t, err)
extraData[1] = VertexSize - 1
_, err = extraData.ExtractRecords(&out)
require.Error(t, err)
}