Skip to content

[bug]: LND doens't reject unknown even (required) TLV types when decoding the tlv stream #10311

@erickcestari

Description

@erickcestari

The current TLV stream decoder doesn't properly handle unknown even TLV types according to the BOLT specification.

  • otherwise, if type is unknown:
    • if type is even:
      • MUST fail to parse the tlv_stream.
    • otherwise, if type is odd:
      • MUST discard the next length bytes.

https://github.com/lightning/bolts/blob/master/01-messaging.md#type-length-value-format

The stream decoder first try to find the type to see if its known or not.

        // Search the records known to the stream for this type. We'll
		// begin the search and recordIdx and walk forward until we find
		// it or the next record's type is larger.
		rec, newIdx, ok := s.getRecord(typ, recordIdx)
		switch {

If it's unknown (odd or even) it will treat as an unknown odd:

        // Otherwise, the record type is unknown and is odd, discard the 
		// number of bytes specified by length.
		default:
			// If the caller provided an initialized TypeMap, record
			// the encoded bytes.
			var b *bytes.Buffer
			writer := io.Discard
			if parsedTypes != nil {
				b = bytes.NewBuffer(make([]byte, 0, length))
				writer = b
			}

https://github.com/lightningnetwork/lnd/blob/790f8bd2ba76d15218061aee14f7ba5796ac5113/tlv/stream.go

This violates the spec because even types indicate required fields that must be understood by all implementations. If a decoder encounters an unknown even type, it should fail immediately rather than silently discarding it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementImprovements to existing features / behaviourtlv

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions