]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Use PrintableString for displaying errors in PeerManager
authorVladimir Fomene <vladimirfomene@gmail.com>
Wed, 7 Jun 2023 04:48:28 +0000 (07:48 +0300)
committerVladimir Fomene <vladimirfomene@gmail.com>
Wed, 7 Jun 2023 18:38:23 +0000 (21:38 +0300)
We currently just print "with non-ASCII error message"
to log when we see non-ASCII chars, but should instead
use our fancy PrintableString type to display the
untrusted string and ignore control chars.

lightning/src/ln/peer_handler.rs

index a7f35a59c275baeb70ca6fdb607fe85283c4e965..2f569c52b9834ffdb66b4ed8e9445aee010c1560 100644 (file)
@@ -31,6 +31,7 @@ use crate::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler
 use crate::routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, NodeAlias};
 use crate::util::atomic_counter::AtomicCounter;
 use crate::util::logger::Logger;
+use crate::util::string::PrintableString;
 
 use crate::prelude::*;
 use crate::io;
@@ -1536,38 +1537,14 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                                // Handled above
                        },
                        wire::Message::Error(msg) => {
-                               let mut data_is_printable = true;
-                               for b in msg.data.bytes() {
-                                       if b < 32 || b > 126 {
-                                               data_is_printable = false;
-                                               break;
-                                       }
-                               }
-
-                               if data_is_printable {
-                                       log_debug!(self.logger, "Got Err message from {}: {}", log_pubkey!(their_node_id), msg.data);
-                               } else {
-                                       log_debug!(self.logger, "Got Err message from {} with non-ASCII error message", log_pubkey!(their_node_id));
-                               }
+                               log_debug!(self.logger, "Got Err message from {}: {}", log_pubkey!(their_node_id), PrintableString(&msg.data));
                                self.message_handler.chan_handler.handle_error(&their_node_id, &msg);
                                if msg.channel_id == [0; 32] {
                                        return Err(PeerHandleError { }.into());
                                }
                        },
                        wire::Message::Warning(msg) => {
-                               let mut data_is_printable = true;
-                               for b in msg.data.bytes() {
-                                       if b < 32 || b > 126 {
-                                               data_is_printable = false;
-                                               break;
-                                       }
-                               }
-
-                               if data_is_printable {
-                                       log_debug!(self.logger, "Got warning message from {}: {}", log_pubkey!(their_node_id), msg.data);
-                               } else {
-                                       log_debug!(self.logger, "Got warning message from {} with non-ASCII error message", log_pubkey!(their_node_id));
-                               }
+                               log_debug!(self.logger, "Got warning message from {}: {}", log_pubkey!(their_node_id), PrintableString(&msg.data));
                        },
 
                        wire::Message::Ping(msg) => {