From 33762e0f81b8d8b4017ffd58fe3f79764be4615a Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Thu, 1 Mar 2018 00:39:26 +0100 Subject: [PATCH] htlcswitch/link: fail channel on lnwire.Error --- htlcswitch/link.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 528dd3538..e84d43b18 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -1319,7 +1319,21 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { l.fail("error receiving fee update: %v", err) return } + case *lnwire.Error: + // Error received from remote, MUST fail channel, but should + // only print the contents of the error message if all + // characters are printable ASCII. + errMsg := "non-ascii data" + if isASCII(msg.Data) { + errMsg = string(msg.Data) + } + l.fail("ChannelPoint(%v): recieved error from peer: %v", + l.channel.ChannelPoint(), errMsg) + default: + log.Warnf("ChannelPoint(%v): received unknown message of type %T", + l.channel.ChannelPoint(), msg) } + } // ackDownStreamPackets is responsible for removing htlcs from a link's @@ -2407,3 +2421,16 @@ func (l *channelLink) tracef(format string, a ...interface{}) { msg := fmt.Sprintf(format, a...) log.Tracef("ChannelLink(%s) %s", l.ShortChanID(), msg) } + +// isASCII is a helper method that checks whether all bytes in `data` would be +// printable ASCII characters if interpreted as a string. +func isASCII(data []byte) bool { + isASCII := true + for _, c := range data { + if c < 32 || c > 126 { + isASCII = false + break + } + } + return isASCII +}