Skip to content

Commit 3bdf795

Browse files
Error when attempting to send an OM to a blinded route with 0 hops
1 parent b1817c5 commit 3bdf795

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lightning/src/onion_message/functional_tests.rs

+21
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,24 @@ fn too_big_packet_error() {
119119
let err = nodes[0].messenger.send_onion_message(&hops, Destination::Node(hop_node_id)).unwrap_err();
120120
assert_eq!(err, SendError::TooBigPacket);
121121
}
122+
123+
#[test]
124+
fn invalid_blinded_route_error() {
125+
// Make sure we error as expected if a provided blinded route has 0 or 1 hops.
126+
let mut nodes = create_nodes(3);
127+
let (node1, node2, node3) = (nodes.remove(0), nodes.remove(0), nodes.remove(0));
128+
129+
// 0 hops
130+
let secp_ctx = Secp256k1::new();
131+
let mut blinded_route = BlindedRoute::new::<EnforcingSigner, _, _>(&[node2.get_node_pk(), node3.get_node_pk()], &*node3.keys_manager, &secp_ctx).unwrap();
132+
blinded_route.blinded_hops.clear();
133+
let err = node1.messenger.send_onion_message(&[], Destination::BlindedRoute(blinded_route)).unwrap_err();
134+
assert_eq!(err, SendError::TooFewBlindedHops);
135+
136+
// 1 hop
137+
let mut blinded_route = BlindedRoute::new::<EnforcingSigner, _, _>(&[node2.get_node_pk(), node3.get_node_pk()], &*node3.keys_manager, &secp_ctx).unwrap();
138+
blinded_route.blinded_hops.remove(0);
139+
assert_eq!(blinded_route.blinded_hops.len(), 1);
140+
let err = node1.messenger.send_onion_message(&[], Destination::BlindedRoute(blinded_route)).unwrap_err();
141+
assert_eq!(err, SendError::TooFewBlindedHops);
142+
}

lightning/src/onion_message/messenger.rs

+8
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ 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 [`BlindedRoute`], due to having fewer than two
122+
/// blinded hops.
123+
TooFewBlindedHops,
121124
}
122125

123126
impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
@@ -140,6 +143,11 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
140143
/// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
141144
/// See [`OnionMessenger`] for example usage.
142145
pub fn send_onion_message(&self, intermediate_nodes: &[PublicKey], destination: Destination) -> Result<(), SendError> {
146+
if let Destination::BlindedRoute(BlindedRoute { ref blinded_hops, .. }) = destination {
147+
if blinded_hops.len() < 2 {
148+
return Err(SendError::TooFewBlindedHops);
149+
}
150+
}
143151
let blinding_secret_bytes = self.keys_manager.get_secure_random_bytes();
144152
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
145153
let (introduction_node_id, blinding_point) = if intermediate_nodes.len() != 0 {

0 commit comments

Comments
 (0)