Merge PR 'Reject serializing blinded paths with more than 255 hops' (#5254)
What changed, and why it matters
This change hardens how Lightning blinded payment paths are saved or sent over the wire. Previously, if a path somehow contained more than 255 hops, the code would silently chop the count to fit in a single byte and then write out all hops, causing the receiver to read the wrong number of entries and likely crash or misparse the data. Now it still caps the count but also stops writing after 255 hops, and adds a debug-only assertion to catch the oversized path during development.
Treat as a low-severity hardening patch. Backport to maintained release branches if blinded path serialization is exposed to untrusted inputs. Review callers to ensure blinded paths cannot be constructed with >255 hops in production, since the debug_assert! is not active in release builds.
Security signals we found
Integer truncation / length mismatch in serialization
Potential denial-of-service or parse failure from malformed blinded path data
Defensive bounds enforcement added
No explicit security framing in commit message
Evidence from the diff
In BlindedPath’s Writeable implementation, the hop count was serialized as a u8 but computed as self.blinded_hops.len() as u8, which truncates for lengths >255. The loop then wrote all hops regardless of the truncated count, producing a malformed serialized stream (count disagrees with actual data). The patch replaces the truncation with an explicit min() and slices the loop to &self.blinded_hops[..num_hops], plus a debug_assert! documenting the 255-hop protocol limit. This is a defensive serialization fix; it does not change the on-wire format and only affects behavior when an invariant is violated.
Changed components
lightning/src/blinded_path/mod.rsBlindedPath serialization (Writeable impl)Inspect captured patch +7 / −2
### lightning/src/blinded_path/mod.rs
@@ -140,8 +140,13 @@ impl Writeable for BlindedPath {
}
self.blinding_point.write(w)?;
- (self.blinded_hops.len() as u8).write(w)?;
- for hop in &self.blinded_hops {
+ debug_assert!(
+ self.blinded_hops.len() <= u8::MAX as usize,
+ "Blinded paths may not have more than 255 hops",
+ );
+ let num_hops = self.blinded_hops.len().min(u8::MAX as usize);
+ (num_hops as u8).write(w)?;
+ for hop in &self.blinded_hops[..num_hops] {
hop.write(w)?;
}
Ok(())Why this scored 35/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.