@@ -61,6 +61,16 @@ fn check_blinded_forward(
61
61
Ok ( ( amt_to_forward, outgoing_cltv_value) )
62
62
}
63
63
64
+ fn check_trampoline_sanity ( outer_hop_data : & msgs:: InboundTrampolineEntrypointPayload , trampoline_cltv_value : u32 , trampoline_amount : u64 ) -> Result < ( ) , ( ) > {
65
+ if outer_hop_data. outgoing_cltv_value < trampoline_cltv_value {
66
+ return Err ( ( ) ) ;
67
+ }
68
+ if outer_hop_data. amt_to_forward < trampoline_amount {
69
+ return Err ( ( ) ) ;
70
+ }
71
+ Ok ( ( ) )
72
+ }
73
+
64
74
enum RoutingInfo {
65
75
Direct {
66
76
short_channel_id : u64 ,
@@ -121,7 +131,15 @@ pub(super) fn create_fwd_pending_htlc_info(
121
131
err_code : 0x4000 | 22 ,
122
132
err_data : Vec :: new ( ) ,
123
133
} ) ,
124
- onion_utils:: Hop :: TrampolineForward { next_trampoline_hop_data, next_trampoline_hop_hmac, new_trampoline_packet_bytes, trampoline_shared_secret, .. } => {
134
+ onion_utils:: Hop :: TrampolineForward { ref outer_hop_data, next_trampoline_hop_data, next_trampoline_hop_hmac, new_trampoline_packet_bytes, trampoline_shared_secret, .. } => {
135
+ check_trampoline_sanity ( outer_hop_data, next_trampoline_hop_data. outgoing_cltv_value , next_trampoline_hop_data. amt_to_forward ) . map_err ( |( ) | {
136
+ // The Trampoline onion's amt and CLTV values cannot exceed the outer onion's
137
+ InboundHTLCErr {
138
+ msg : "Underflow calculating outbound amount or CLTV value for Trampoline forward" ,
139
+ err_code : 0x2000 | 26 ,
140
+ err_data : Vec :: new ( ) ,
141
+ }
142
+ } ) ?;
125
143
(
126
144
RoutingInfo :: Trampoline {
127
145
next_trampoline : next_trampoline_hop_data. next_trampoline ,
@@ -136,7 +154,7 @@ pub(super) fn create_fwd_pending_htlc_info(
136
154
None
137
155
)
138
156
} ,
139
- onion_utils:: Hop :: TrampolineBlindedForward { outer_hop_data, next_trampoline_hop_data, next_trampoline_hop_hmac, new_trampoline_packet_bytes, trampoline_shared_secret, .. } => {
157
+ onion_utils:: Hop :: TrampolineBlindedForward { ref outer_hop_data, next_trampoline_hop_data, next_trampoline_hop_hmac, new_trampoline_packet_bytes, trampoline_shared_secret, .. } => {
140
158
let ( amt_to_forward, outgoing_cltv_value) = check_blinded_forward (
141
159
msg. amount_msat , msg. cltv_expiry , & next_trampoline_hop_data. payment_relay , & next_trampoline_hop_data. payment_constraints , & next_trampoline_hop_data. features
142
160
) . map_err ( |( ) | {
@@ -148,6 +166,15 @@ pub(super) fn create_fwd_pending_htlc_info(
148
166
err_data : vec ! [ 0 ; 32 ] ,
149
167
}
150
168
} ) ?;
169
+ check_trampoline_sanity ( outer_hop_data, outgoing_cltv_value, amt_to_forward) . map_err ( |( ) | {
170
+ // The Trampoline onion's amt and CLTV values cannot exceed the outer onion's, but
171
+ // we're inside a blinded path
172
+ InboundHTLCErr {
173
+ msg : "Underflow calculating outbound amount or CLTV value for Trampoline forward" ,
174
+ err_code : INVALID_ONION_BLINDING ,
175
+ err_data : vec ! [ 0 ; 32 ] ,
176
+ }
177
+ } ) ?;
151
178
(
152
179
RoutingInfo :: Trampoline {
153
180
next_trampoline : next_trampoline_hop_data. next_trampoline ,
@@ -266,14 +293,25 @@ pub(super) fn create_recv_pending_htlc_info(
266
293
intro_node_blinding_point. is_none ( ) , true , invoice_request)
267
294
}
268
295
onion_utils:: Hop :: TrampolineReceive {
296
+ ref outer_hop_data,
269
297
trampoline_hop_data : msgs:: InboundOnionReceivePayload {
270
298
payment_data, keysend_preimage, custom_tlvs, sender_intended_htlc_amt_msat,
271
299
cltv_expiry_height, payment_metadata, ..
272
300
} , ..
273
- } =>
301
+ } => {
302
+ check_trampoline_sanity ( outer_hop_data, cltv_expiry_height, sender_intended_htlc_amt_msat) . map_err ( |( ) | {
303
+ // The Trampoline onion's amt and CLTV values cannot exceed the outer onion's
304
+ InboundHTLCErr {
305
+ msg : "Underflow calculating skimmable amount or CLTV value for Trampoline receive" ,
306
+ err_code : 0x2000 | 26 ,
307
+ err_data : Vec :: new ( ) ,
308
+ }
309
+ } ) ?;
274
310
( payment_data, keysend_preimage, custom_tlvs, sender_intended_htlc_amt_msat,
275
- cltv_expiry_height, payment_metadata, None , false , keysend_preimage. is_none ( ) , None ) ,
311
+ cltv_expiry_height, payment_metadata, None , false , keysend_preimage. is_none ( ) , None )
312
+ } ,
276
313
onion_utils:: Hop :: TrampolineBlindedReceive {
314
+ ref outer_hop_data,
277
315
trampoline_hop_data : msgs:: InboundOnionBlindedReceivePayload {
278
316
sender_intended_htlc_amt_msat, total_msat, cltv_expiry_height, payment_secret,
279
317
intro_node_blinding_point, payment_constraints, payment_context, keysend_preimage,
@@ -290,6 +328,15 @@ pub(super) fn create_recv_pending_htlc_info(
290
328
msg : "Amount or cltv_expiry violated blinded payment constraints within Trampoline onion" ,
291
329
}
292
330
} ) ?;
331
+ check_trampoline_sanity ( outer_hop_data, cltv_expiry_height, sender_intended_htlc_amt_msat) . map_err ( |( ) | {
332
+ // The Trampoline onion's amt and CLTV values cannot exceed the outer onion's, but
333
+ // we're inside a blinded path
334
+ InboundHTLCErr {
335
+ msg : "Underflow calculating skimmable amount or CLTV value for Trampoline receive" ,
336
+ err_code : INVALID_ONION_BLINDING ,
337
+ err_data : vec ! [ 0 ; 32 ] ,
338
+ }
339
+ } ) ?;
293
340
let payment_data = msgs:: FinalOnionHopData { payment_secret, total_msat } ;
294
341
( Some ( payment_data) , keysend_preimage, custom_tlvs,
295
342
sender_intended_htlc_amt_msat, cltv_expiry_height, None , Some ( payment_context) ,
0 commit comments