Skip to content

Commit 519efae

Browse files
committed
Require a non-0 number of non-empty paths when deserializing routes
When we read a `Route` (or a list of `RouteHop`s), we should never have zero paths or zero `RouteHop`s in a path. As such, its fine to simply reject these at deserialization-time. Technically this could lead to something which we can generate not round-trip'ing serialization, but that seems okay here.
1 parent 558b2f2 commit 519efae

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6786,7 +6786,7 @@ impl Readable for HTLCSource {
67866786
0 => {
67876787
let mut session_priv: crate::util::ser::OptionDeserWrapper<SecretKey> = crate::util::ser::OptionDeserWrapper(None);
67886788
let mut first_hop_htlc_msat: u64 = 0;
6789-
let mut path = Some(Vec::new());
6789+
let mut path: Option<Vec<RouteHop>> = Some(Vec::new());
67906790
let mut payment_id = None;
67916791
let mut payment_secret = None;
67926792
let mut payment_params = None;
@@ -6803,10 +6803,14 @@ impl Readable for HTLCSource {
68036803
// instead.
68046804
payment_id = Some(PaymentId(*session_priv.0.unwrap().as_ref()));
68056805
}
6806+
if path.is_none() || path.as_ref().unwrap().is_empty() {
6807+
return Err(DecodeError::InvalidValue);
6808+
}
6809+
let path = path.unwrap();
68066810
Ok(HTLCSource::OutboundRoute {
68076811
session_priv: session_priv.0.unwrap(),
68086812
first_hop_htlc_msat,
6809-
path: path.unwrap(),
6813+
path,
68106814
payment_id: payment_id.unwrap(),
68116815
payment_secret,
68126816
payment_params,

lightning/src/routing/router.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,15 @@ impl Readable for Route {
313313
fn read<R: io::Read>(reader: &mut R) -> Result<Route, DecodeError> {
314314
let _ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
315315
let path_count: u64 = Readable::read(reader)?;
316+
if path_count == 0 { return Err(DecodeError::InvalidValue); }
316317
let mut paths = Vec::with_capacity(cmp::min(path_count, 128) as usize);
317318
for _ in 0..path_count {
318319
let hop_count: u8 = Readable::read(reader)?;
319320
let mut hops = Vec::with_capacity(hop_count as usize);
320321
for _ in 0..hop_count {
321322
hops.push(Readable::read(reader)?);
322323
}
324+
if hops.is_empty() { return Err(DecodeError::InvalidValue); }
323325
paths.push(hops);
324326
}
325327
let mut payment_params = None;

0 commit comments

Comments
 (0)