Skip to content

Commit 5dac003

Browse files
committed
fixup! multi: decode zero-length onion message payloads
1 parent 6726150 commit 5dac003

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

payload.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ type HopPayload struct {
4444
// HMAC is an HMAC computed over the entire per-hop payload that also
4545
// includes the higher-level (optional) associated data bytes.
4646
HMAC [HMACSize]byte
47+
48+
// TLVPayloadGuaranteed is set to true if the payload is guaranteed to
49+
// be a TLVPayload. E.g. in the case of an onion message.
50+
TLVPayloadGuaranteed bool
4751
}
4852

4953
// NewTLVHopPayload creates a new TLV encoded HopPayload. The payload will be
@@ -87,13 +91,8 @@ func (hp *HopPayload) Encode(w io.Writer) error {
8791
}
8892

8993
// Decode unpacks an encoded HopPayload from the passed reader into the target
90-
// HopPayload. The isMessage boolean should be set to true if we're parsing a
91-
// payload that is known to be for an onion message.
92-
func (hp *HopPayload) Decode(r io.Reader, isMessage ...bool) error {
93-
// To preserve backwards compatibility, we'll default to isMessage being
94-
// false if it is not provided.
95-
isMsg := len(isMessage) > 0 && isMessage[0]
96-
94+
// HopPayload.
95+
func (hp *HopPayload) Decode(r io.Reader) error {
9796
bufReader := bufio.NewReader(r)
9897

9998
// In order to properly parse the payload, we'll need to check the
@@ -104,12 +103,9 @@ func (hp *HopPayload) Decode(r io.Reader, isMessage ...bool) error {
104103
return err
105104
}
106105

107-
// Per BOLT 7, onion messages MUST use the TLV format.
108-
if isMsg {
109-
return decodeTLVHopPayload(hp, bufReader)
110-
}
111-
112-
if isLegacyPayloadByte(peekByte[0]) {
106+
// If the HopPayload is guaranteed to be a TLV payload, we can skip the
107+
// check for the legacy payload byte.
108+
if !hp.TLVPayloadGuaranteed && isLegacyPayloadByte(peekByte[0]) {
113109
return decodeLegacyHopPayload(hp, bufReader)
114110
}
115111

sphinx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,8 @@ func unwrapPacket(onionPkt *OnionPacket, sharedSecret *Hash256,
750750
// With the MAC checked, and the payload decrypted, we can now parse
751751
// out the payload so we can derive the specified forwarding
752752
// instructions.
753-
var hopPayload HopPayload
754-
err := hopPayload.Decode(bytes.NewReader(hopInfo), isOnionMessage)
753+
hopPayload := HopPayload{TLVPayloadGuaranteed: isOnionMessage}
754+
err := hopPayload.Decode(bytes.NewReader(hopInfo))
755755
if err != nil {
756756
return nil, nil, err
757757
}

0 commit comments

Comments
 (0)