Skip to content

Commit d773b04

Browse files
OM pathfinding: add our id and first hop checks
Don't allow finding a path to ourselves and reject first hops that contain our node id as the counterparty node id.
1 parent 7230256 commit d773b04

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

lightning/src/routing/onion_message.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub fn find_path<L: Deref, GL: Deref>(
4848
let network_nodes = graph_lock.nodes();
4949
let our_node_id = NodeId::from_pubkey(our_node_pubkey);
5050
let dest_node_id = NodeId::from_pubkey(destination);
51+
if our_node_id == dest_node_id { return Err(Error::InvalidDestination) }
5152

5253
// Add our start and first-hops to `frontier`.
5354
let start = NodeId::from_pubkey(&our_node_pubkey);
@@ -56,6 +57,7 @@ pub fn find_path<L: Deref, GL: Deref>(
5657
frontier.push(PathBuildingHop { cost: 0, node_id: start, parent_node_id: start });
5758
if let Some(first_hops) = first_hops {
5859
for hop in first_hops {
60+
if hop.counterparty.node_id == *our_node_pubkey { return Err(Error::InvalidFirstHop) }
5961
if !hop.counterparty.features.supports_onion_messages() { continue; }
6062
let node_id = NodeId::from_pubkey(&hop.counterparty.node_id);
6163
frontier.push(PathBuildingHop { cost: 1, node_id, parent_node_id: start });
@@ -112,6 +114,10 @@ pub enum Error {
112114
PathNotFound,
113115
/// We failed to convert this node id into a [`PublicKey`].
114116
InvalidNodeId(secp256k1::Error),
117+
/// We attempted to generate a path to ourselves, which is not allowed.
118+
InvalidDestination,
119+
/// First hops cannot have our node id as a counterparty node id.
120+
InvalidFirstHop,
115121
}
116122

117123
impl fmt::Display for Error {
@@ -120,6 +126,8 @@ impl fmt::Display for Error {
120126
Error::PathNotFound => write!(f, "Failed to find a path to the destination"),
121127
Error::InvalidNodeId(e) =>
122128
write!(f, "Failed to convert a node id into a PublicKey with error: {}", e),
129+
Error::InvalidDestination => write!(f, "Cannot generate a route to ourselves"),
130+
Error::InvalidFirstHop => write!(f, "First hops cannot have our node id as a counterparty node id"),
123131
}
124132
}
125133
}
@@ -252,4 +260,21 @@ mod tests {
252260
assert_eq!(path.len(), 1);
253261
assert_eq!(path[0], node_pks[0]);
254262
}
263+
264+
#[test]
265+
fn invalid_first_hop() {
266+
// Check that we can't generate a path if first_hops contains a counterparty node id that
267+
// is equal to our node id.
268+
let mut features = InitFeatures::empty();
269+
features.set_onion_messages_optional();
270+
let (secp_ctx, network_graph, gossip_sync, _, logger) = build_graph_with_features(features.to_context());
271+
let (_, our_id, privkeys, node_pks) = get_nodes(&secp_ctx);
272+
273+
let bad_first_hop = vec![get_channel_details(Some(2), our_id, features, 100000)];
274+
let err = super::find_path(&our_id, &node_pks[2], &network_graph, Some(&bad_first_hop.iter().collect::<Vec<_>>()), Arc::clone(&logger)).unwrap_err();
275+
assert_eq!(err, super::Error::InvalidFirstHop);
276+
277+
let path = super::find_path(&our_id, &node_pks[2], &network_graph, None, Arc::clone(&logger)).unwrap();
278+
assert_eq!(path.len(), 2);
279+
}
255280
}

0 commit comments

Comments
 (0)