From: Valentine Wallace Date: Tue, 18 Oct 2022 17:33:45 +0000 (-0400) Subject: Disallow sending invalid custom OM TLVs X-Git-Tag: v0.0.112~6^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=4905268b65b36d2d1468e2763408cb056cf63b36;p=rust-lightning Disallow sending invalid custom OM TLVs Onion message data TLV types must be >= 64, enforce this on send --- diff --git a/lightning/src/onion_message/functional_tests.rs b/lightning/src/onion_message/functional_tests.rs index 356e1f3cd..66ea62bbc 100644 --- a/lightning/src/onion_message/functional_tests.rs +++ b/lightning/src/onion_message/functional_tests.rs @@ -217,6 +217,33 @@ fn reply_path() { format!("Received an onion message with path_id None and a reply_path").to_string(), 2); } +#[test] +fn invalid_custom_message_type() { + let nodes = create_nodes(2); + + struct InvalidCustomMessage{} + impl CustomOnionMessageContents for InvalidCustomMessage { + fn tlv_type(&self) -> u64 { + // Onion message contents must have a TLV >= 64. + 63 + } + } + + impl Writeable for InvalidCustomMessage { + fn write(&self, _w: &mut W) -> Result<(), io::Error> { unreachable!() } + } + + impl MaybeReadableArgs for InvalidCustomMessage { + fn read(_buffer: &mut R, _message_type: u64) -> Result, DecodeError> where Self: Sized { + unreachable!() + } + } + + let test_msg = OnionMessageContents::Custom(InvalidCustomMessage {}); + let err = nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), test_msg, None).unwrap_err(); + assert_eq!(err, SendError::InvalidMessage); +} + #[test] fn peer_buffer_full() { let nodes = create_nodes(2); diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 9c86a44e0..5cf2e1e0d 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -156,6 +156,8 @@ pub enum SendError { TooFewBlindedHops, /// Our next-hop peer was offline or does not support onion message forwarding. InvalidFirstHop, + /// Onion message contents must have a TLV type >= 64. + InvalidMessage, /// Our next-hop peer's buffer was full or our total outbound buffer was full. BufferFull, } @@ -205,6 +207,9 @@ impl OnionMessenger