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

+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)
}