From 17af8d5f0954c85163f881ce1c9cc49efbc582b1 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Tue, 14 Nov 2023 15:05:05 -0600 Subject: [PATCH] Add NetworkGraph reference to DefaultMessageRouter When buffering onion messages for a node that is not connected as a peer, it's possible that the node does not exist. Include a NetworkGraph reference in DefaultMessageRouter so that it can be used to check if the node actually exists. Otherwise, an malicious node may send an onion message where the reply path's introduction node doesn't exist. This would result in buffering messages that may never be delivered. --- lightning/src/onion_message/messenger.rs | 27 ++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index c84cfc120..e507f0a21 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -25,6 +25,7 @@ use crate::ln::features::{InitFeatures, NodeFeatures}; use crate::ln::msgs::{self, OnionMessage, OnionMessageHandler}; use crate::ln::onion_utils; use crate::ln::peer_handler::IgnoringMessageHandler; +use crate::routing::gossip::NetworkGraph; pub use super::packet::OnionMessageContents; use super::packet::ParsedOnionMessageContents; use super::offers::OffersMessageHandler; @@ -256,9 +257,27 @@ pub trait MessageRouter { } /// A [`MessageRouter`] that can only route to a directly connected [`Destination`]. -pub struct DefaultMessageRouter; +pub struct DefaultMessageRouter>, L: Deref> +where + L::Target: Logger, +{ + network_graph: G, +} -impl MessageRouter for DefaultMessageRouter { +impl>, L: Deref> DefaultMessageRouter +where + L::Target: Logger, +{ + /// Creates a [`DefaultMessageRouter`] using the given [`NetworkGraph`]. + pub fn new(network_graph: G) -> Self { + Self { network_graph } + } +} + +impl>, L: Deref> MessageRouter for DefaultMessageRouter +where + L::Target: Logger, +{ fn find_path( &self, _sender: PublicKey, peers: Vec, destination: Destination ) -> Result { @@ -878,7 +897,7 @@ pub type SimpleArcOnionMessenger = OnionMessenger< Arc, Arc, Arc, - Arc, + Arc>>, Arc>>, Arc>, IgnoringMessageHandler >; @@ -897,7 +916,7 @@ pub type SimpleRefOnionMessenger< &'a KeysManager, &'a KeysManager, &'b L, - &'i DefaultMessageRouter, + &'i DefaultMessageRouter<&'g NetworkGraph<&'b L>, &'b L>, &'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L>, IgnoringMessageHandler >; -- 2.39.5