Disallow sending invalid custom OM TLVs
authorValentine Wallace <vwallace@protonmail.com>
Tue, 18 Oct 2022 17:33:45 +0000 (13:33 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Wed, 19 Oct 2022 20:07:01 +0000 (16:07 -0400)
Onion message data TLV types must be >= 64, enforce this on send

lightning/src/onion_message/functional_tests.rs
lightning/src/onion_message/messenger.rs

index 356e1f3cdafafe6fec5d25bcaeada4da7bc5cedc..66ea62bbcec05a7f989f611c8876637822718c6f 100644 (file)
@@ -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<W: Writer>(&self, _w: &mut W) -> Result<(), io::Error> { unreachable!() }
+       }
+
+       impl MaybeReadableArgs<u64> for InvalidCustomMessage {
+               fn read<R: io::Read>(_buffer: &mut R, _message_type: u64) -> Result<Option<Self>, 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);
index 9c86a44e0a0a6a80b0ded58c2d26c376c3f38f9b..5cf2e1e0dceba6e10ed990c82257c91d0062cb6f 100644 (file)
@@ -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<Signer: Sign, K: Deref, L: Deref, CMH: Deref> OnionMessenger<Signer, K, L,
                                return Err(SendError::TooFewBlindedHops);
                        }
                }
+               let OnionMessageContents::Custom(ref msg) = message;
+               if msg.tlv_type() < 64 { return Err(SendError::InvalidMessage) }
+
                let blinding_secret_bytes = self.keys_manager.get_secure_random_bytes();
                let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
                let (introduction_node_id, blinding_point) = if intermediate_nodes.len() != 0 {