Skip to content

Commit 6fb46db

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 46fd703 commit 6fb46db

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
@@ -6745,7 +6745,7 @@ impl Readable for HTLCSource {
67456745
0 => {
67466746
let mut session_priv: crate::util::ser::OptionDeserWrapper<SecretKey> = crate::util::ser::OptionDeserWrapper(None);
67476747
let mut first_hop_htlc_msat: u64 = 0;
6748-
let mut path = Some(Vec::new());
6748+
let mut path: Option<Vec<RouteHop>> = Some(Vec::new());
67496749
let mut payment_id = None;
67506750
let mut payment_secret = None;
67516751
let mut payment_params = None;
@@ -6762,10 +6762,14 @@ impl Readable for HTLCSource {
67626762
// instead.
67636763
payment_id = Some(PaymentId(*session_priv.0.unwrap().as_ref()));
67646764
}
6765+
if path.is_none() || path.as_ref().unwrap().is_empty() {
6766+
return Err(DecodeError::InvalidValue);
6767+
}
6768+
let path = path.unwrap();
67656769
Ok(HTLCSource::OutboundRoute {
67666770
session_priv: session_priv.0.unwrap(),
67676771
first_hop_htlc_msat,
6768-
path: path.unwrap(),
6772+
path,
67696773
payment_id: payment_id.unwrap(),
67706774
payment_secret,
67716775
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)