Skip to content

Commit 7e5c80f

Browse files
Require length limited reader for custom LN messages
When reading a raw LSP message specifically, it will consume the provided reader until it is out of bytes. Readable is not an appropriate trait for this situation, and over the past few commits we've been transitioning any struct that always reads-to-end to require a length limited reader. This is less error-prone because there is no risk of a bug being introduced because a custom message read more data than it was supposed to from the provided reader.
1 parent 7b1bb4a commit 7e5c80f

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

lightning-custom-message/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! use lightning::ln::peer_handler::CustomMessageHandler;
2525
//! use lightning::ln::wire::{CustomMessageReader, self};
2626
//! # use lightning::types::features::{InitFeatures, NodeFeatures};
27-
//! use lightning::util::ser::Writeable;
27+
//! use lightning::util::ser::{LengthLimitedRead, Writeable};
2828
//! # use lightning::util::ser::Writer;
2929
//!
3030
//! // Assume that `FooHandler` and `BarHandler` are defined in one crate and `BazHandler` is
@@ -52,7 +52,7 @@
5252
//! impl CustomMessageReader for FooHandler {
5353
//! // ...
5454
//! # type CustomMessage = Foo;
55-
//! # fn read<R: io::Read>(
55+
//! # fn read<R: LengthLimitedRead>(
5656
//! # &self, _message_type: u16, _buffer: &mut R
5757
//! # ) -> Result<Option<Self::CustomMessage>, DecodeError> {
5858
//! # unimplemented!()
@@ -104,7 +104,7 @@
104104
//! impl CustomMessageReader for BarHandler {
105105
//! // ...
106106
//! # type CustomMessage = Bar;
107-
//! # fn read<R: io::Read>(
107+
//! # fn read<R: LengthLimitedRead>(
108108
//! # &self, _message_type: u16, _buffer: &mut R
109109
//! # ) -> Result<Option<Self::CustomMessage>, DecodeError> {
110110
//! # unimplemented!()
@@ -156,7 +156,7 @@
156156
//! impl CustomMessageReader for BazHandler {
157157
//! // ...
158158
//! # type CustomMessage = Baz;
159-
//! # fn read<R: io::Read>(
159+
//! # fn read<R: LengthLimitedRead>(
160160
//! # &self, _message_type: u16, _buffer: &mut R
161161
//! # ) -> Result<Option<Self::CustomMessage>, DecodeError> {
162162
//! # unimplemented!()
@@ -340,7 +340,7 @@ macro_rules! composite_custom_message_handler {
340340

341341
impl $crate::lightning::ln::wire::CustomMessageReader for $handler {
342342
type CustomMessage = $message;
343-
fn read<R: $crate::lightning::io::Read>(
343+
fn read<R: $crate::lightning::util::ser::LengthLimitedRead>(
344344
&self, message_type: u16, buffer: &mut R
345345
) -> Result<Option<Self::CustomMessage>, $crate::lightning::ln::msgs::DecodeError> {
346346
match message_type {

lightning-liquidity/src/lsps0/ser.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ use crate::lsps2::msgs::{
1818
};
1919
use crate::prelude::{HashMap, String};
2020

21-
use lightning::ln::msgs::LightningError;
21+
use lightning::ln::msgs::{DecodeError, LightningError};
2222
use lightning::ln::wire;
23-
use lightning::util::ser::WithoutLength;
23+
use lightning::util::ser::{LengthLimitedRead, LengthReadable, WithoutLength};
2424

2525
use bitcoin::secp256k1::PublicKey;
2626

@@ -168,9 +168,10 @@ impl lightning::util::ser::Writeable for RawLSPSMessage {
168168
}
169169
}
170170

171-
impl lightning::util::ser::Readable for RawLSPSMessage {
172-
fn read<R: lightning::io::Read>(r: &mut R) -> Result<Self, lightning::ln::msgs::DecodeError> {
173-
let payload_without_length = WithoutLength::read(r)?;
171+
impl LengthReadable for RawLSPSMessage {
172+
fn read_from_fixed_length_buffer<R: LengthLimitedRead>(r: &mut R) -> Result<Self, DecodeError> {
173+
let payload_without_length: WithoutLength<String> =
174+
LengthReadable::read_from_fixed_length_buffer(r)?;
174175
Ok(Self { payload: payload_without_length.0 })
175176
}
176177
}

lightning-liquidity/src/manager.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use lightning::ln::peer_handler::CustomMessageHandler;
2727
use lightning::ln::wire::CustomMessageReader;
2828
use lightning::sign::EntropySource;
2929
use lightning::util::logger::Level;
30-
use lightning::util::ser::Readable;
30+
use lightning::util::ser::{LengthLimitedRead, LengthReadable};
3131

3232
use lightning_types::features::{InitFeatures, NodeFeatures};
3333

@@ -449,11 +449,13 @@ where
449449
{
450450
type CustomMessage = RawLSPSMessage;
451451

452-
fn read<RD: lightning::io::Read>(
452+
fn read<RD: LengthLimitedRead>(
453453
&self, message_type: u16, buffer: &mut RD,
454454
) -> Result<Option<Self::CustomMessage>, lightning::ln::msgs::DecodeError> {
455455
match message_type {
456-
LSPS_MESSAGE_TYPE_ID => Ok(Some(RawLSPSMessage::read(buffer)?)),
456+
LSPS_MESSAGE_TYPE_ID => {
457+
Ok(Some(RawLSPSMessage::read_from_fixed_length_buffer(buffer)?))
458+
},
457459
_ => Ok(None),
458460
}
459461
}

lightning/src/ln/wire.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub trait CustomMessageReader {
2525
/// implementation and the message could be decoded, must return `Ok(Some(message))`. If the
2626
/// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
2727
/// occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
28-
fn read<R: io::Read>(
28+
fn read<R: LengthLimitedRead>(
2929
&self, message_type: u16, buffer: &mut R,
3030
) -> Result<Option<Self::CustomMessage>, msgs::DecodeError>;
3131
}

0 commit comments

Comments
 (0)