Skip to content

Commit ae22244

Browse files
canndrewknocte
authored andcommitted
Make GetFailureMsgData format non-printable error messages
Some lightning implementations will send non-printable data in error messages. For instance outdated versions of lnd will do this: lightningnetwork/lnd@ff37b71 Whereas these are the error codes (sent as single-byte error messages): https://github.com/lightningnetwork/lnd/blob/9b1ecbd3fa3fd93a6d95a5f71921c73777340760/lnwire/error.go#L12 These errors need to be printed as bytes when displayed to the user, rather than interpretted as strings. Error.GetFailureMsgData will now checks whether an error message is printable before returning the message as a string and, if it's not, returns a string containing the hex-encoded binary data in the error.
1 parent 0a76946 commit ae22244

File tree

1 file changed

+14
-1
lines changed
  • src/DotNetLightning.Core/Serialize/Msgs

1 file changed

+14
-1
lines changed

src/DotNetLightning.Core/Serialize/Msgs/Msgs.fs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,20 @@ type ErrorMessage =
12811281
ls.WriteWithLen(this.Data)
12821282

12831283
member this.GetFailureMsgData() =
1284-
System.Text.ASCIIEncoding.ASCII.GetString(this.Data)
1284+
let minPrintableAsciiChar = 32uy
1285+
let isPrintableAsciiChar (asciiChar: byte) =
1286+
asciiChar >= minPrintableAsciiChar
1287+
let isPrintableAsciiString =
1288+
not <| Seq.exists
1289+
(fun asciiChar -> not (isPrintableAsciiChar asciiChar))
1290+
this.Data
1291+
if isPrintableAsciiString then
1292+
System.Text.ASCIIEncoding.ASCII.GetString this.Data
1293+
else
1294+
Seq.fold
1295+
(fun msg (asciiChar: byte) -> sprintf "%s %02x" msg asciiChar)
1296+
"<error contains non-printable binary data>:"
1297+
this.Data
12851298

12861299
and WhichChannel =
12871300
| SpecificChannel of ChannelId

0 commit comments

Comments
 (0)