Skip to content

Commit cdca641

Browse files
Error when attempting to send an OM to a blinded route with 0 hops
1 parent 059e39c commit cdca641

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lightning/src/onion_message/functional_tests.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use util::test_utils;
1717
use bitcoin::network::constants::Network;
1818
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
1919

20+
use core::mem;
2021
use sync::Arc;
2122

2223
struct MessengerNode {
@@ -123,3 +124,18 @@ fn too_big_packet_error() {
123124
let err = nodes[0].messenger.send_onion_message(hops, Destination::Node(hop_node_id)).unwrap_err();
124125
assert_eq!(err, SendError::TooBigPacket);
125126
}
127+
128+
#[test]
129+
fn invalid_blinded_route_error() {
130+
// Make sure we error as expected if a provided blinded route has 0 hops.
131+
let mut nodes = create_nodes(3);
132+
let (node1, node2, node3) = (nodes.remove(0), nodes.remove(0), nodes.remove(0));
133+
134+
let secp_ctx = Secp256k1::new();
135+
let mut blinded_route = BlindedRoute::new(vec![node2.get_node_pk(), node3.get_node_pk()], &node3.keys_manager, &secp_ctx).unwrap();
136+
let mut empty_hops = Vec::new();
137+
mem::swap(&mut empty_hops, &mut blinded_route.blinded_hops);
138+
139+
let err = node1.messenger.send_onion_message(vec![], Destination::BlindedRoute(blinded_route)).unwrap_err();
140+
assert_eq!(err, SendError::MissingBlindedHops);
141+
}

lightning/src/onion_message/messenger.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ pub enum SendError {
118118
/// Because implementations such as Eclair will drop onion messages where the message packet
119119
/// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
120120
TooBigPacket,
121+
/// The provided [destination] was an invalid [blinded route], due to having 0 blinded hops.
122+
///
123+
/// [destination]: Destination
124+
/// [blinded route]: super::blinded_route::BlindedRoute
125+
MissingBlindedHops,
121126
}
122127

123128
impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
@@ -139,6 +144,11 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
139144

140145
/// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
141146
pub fn send_onion_message(&self, intermediate_nodes: Vec<PublicKey>, destination: Destination) -> Result<(), SendError> {
147+
if let Destination::BlindedRoute(BlindedRoute { ref blinded_hops, .. }) = destination {
148+
if blinded_hops.len() == 0 {
149+
return Err(SendError::MissingBlindedHops);
150+
}
151+
}
142152
let blinding_secret_bytes = self.keys_manager.get_secure_random_bytes();
143153
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
144154
let (introduction_node_id, blinding_point) = if intermediate_nodes.len() != 0 {

0 commit comments

Comments
 (0)