Reject serializing blinded paths with more than 255 hops
What changed, and why it matters
This commit fixes a serialization bug in the way secret routing paths (blinded paths) are written to data messages. Previously, if a path had 256 or more hops, the hop count was silently truncated to fit in a single byte, causing the remaining hop data to spill into the next part of the message. That could corrupt offers/invoices or make them unparseable. The fix caps the written hops at 255 and adds a debug-only assertion to catch the condition during development.
Review whether capping at 255 hops silently is acceptable for all callers, or whether construction of oversized blinded paths should be prevented earlier. Consider adding a release-mode check or returning a serialization error if the writer API is later made fallible. No immediate user action is required beyond updating to the patched version.
Security signals we found
Integer narrowing/truncation bug (usize -> u8) in serialization
Potential TLV payload corruption in BOLT12 offers/invoices
Signed message parsing failure or misinterpretation of trailing hop bytes
Fix caps output rather than returning an error due to infallible writer constraints
Debug assertion added to catch oversized paths in development builds
Evidence from the diff
BlindedPath::write converted blinded_hops.len() from usize to u8 without bounds checking. A length >=256 wrapped modulo 256, so the serialized num_hops byte was wrong and the extra hop bodies were still emitted. In BOLT12 fields that concatenate blinded_path values without per-path framing, those trailing bytes could be misinterpreted as additional paths or break parsing of the signed TLV payload. The patch uses .min(u8::MAX as usize) to cap the written hop count and slice iteration, plus a debug_assert! for the invariant. It deliberately does not return an error because the surrounding TLV encoding uses an infallible length computation that would panic before the error could propagate.
Changed components
lightning/src/blinded_path/mod.rsBlindedPath::writeBOLT12 offer/invoice serializationInspect 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 58/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.