Skip to content

Commit 926c039

Browse files
committed
Don't modify path when advance_path_by_one errors
When using advance_path_by_one when we are the introduction node, any error will result having the first hop of the input blinded path removed. Instead, only remove the first hop on success. Otherwise, the path will be invalid.
1 parent ac35043 commit 926c039

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
106106

107107
// Advance the blinded onion message path by one hop, so make the second hop into the new
108108
// introduction node.
109+
//
110+
// Will only modify `path` when returning `Ok`.
109111
pub(crate) fn advance_path_by_one<NS: Deref, NL: Deref, T>(
110112
path: &mut BlindedPath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
111113
) -> Result<(), ()>
@@ -116,8 +118,8 @@ where
116118
{
117119
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.blinding_point, None)?;
118120
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
119-
let encrypted_control_tlvs = path.blinded_hops.remove(0).encrypted_payload;
120-
let mut s = Cursor::new(&encrypted_control_tlvs);
121+
let encrypted_control_tlvs = &path.blinded_hops.get(0).ok_or(())?.encrypted_payload;
122+
let mut s = Cursor::new(encrypted_control_tlvs);
121123
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
122124
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
123125
Ok(ChaChaPolyReadAdapter {
@@ -139,6 +141,7 @@ where
139141
};
140142
mem::swap(&mut path.blinding_point, &mut new_blinding_point);
141143
path.introduction_node = IntroductionNode::NodeId(next_node_id);
144+
path.blinded_hops.remove(0);
142145
Ok(())
143146
},
144147
_ => Err(())

0 commit comments

Comments
 (0)