Skip to content

Commit bda54b1

Browse files
Disallow sending invalid custom OM TLVs
Onion message data TLV types must be >= 64, enforce this on send
1 parent 4720a7a commit bda54b1

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lightning/src/onion_message/functional_tests.rs

+27
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,33 @@ fn reply_path() {
217217
format!("Received an onion message with path_id None and a reply_path").to_string(), 2);
218218
}
219219

220+
#[test]
221+
fn invalid_custom_message_type() {
222+
let nodes = create_nodes(2);
223+
224+
struct InvalidCustomMessage{}
225+
impl CustomOnionMessageContents for InvalidCustomMessage {
226+
fn tlv_type(&self) -> u64 {
227+
// Onion message contents must have a TLV >= 64.
228+
63
229+
}
230+
}
231+
232+
impl Writeable for InvalidCustomMessage {
233+
fn write<W: Writer>(&self, _w: &mut W) -> Result<(), io::Error> { unreachable!() }
234+
}
235+
236+
impl MaybeReadableArgs<u64> for InvalidCustomMessage {
237+
fn read<R: io::Read>(_buffer: &mut R, _message_type: u64) -> Result<Option<Self>, DecodeError> where Self: Sized {
238+
unreachable!()
239+
}
240+
}
241+
242+
let test_msg = OnionMessageContents::Custom(InvalidCustomMessage {});
243+
let err = nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), test_msg, None).unwrap_err();
244+
assert_eq!(err, SendError::InvalidMessage);
245+
}
246+
220247
#[test]
221248
fn peer_buffer_full() {
222249
let nodes = create_nodes(2);

lightning/src/onion_message/messenger.rs

+5
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ pub enum SendError {
156156
TooFewBlindedHops,
157157
/// Our next-hop peer was offline or does not support onion message forwarding.
158158
InvalidFirstHop,
159+
/// Onion message contents must have a TLV type >= 64.
160+
InvalidMessage,
159161
/// Our next-hop peer's buffer was full or our total outbound buffer was full.
160162
BufferFull,
161163
}
@@ -205,6 +207,9 @@ impl<Signer: Sign, K: Deref, L: Deref, CMH: Deref> OnionMessenger<Signer, K, L,
205207
return Err(SendError::TooFewBlindedHops);
206208
}
207209
}
210+
let OnionMessageContents::Custom(ref msg) = message;
211+
if msg.tlv_type() < 64 { return Err(SendError::InvalidMessage) }
212+
208213
let blinding_secret_bytes = self.keys_manager.get_secure_random_bytes();
209214
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
210215
let (introduction_node_id, blinding_point) = if intermediate_nodes.len() != 0 {

0 commit comments

Comments
 (0)