Skip to content

Commit 1f74a24

Browse files
f test bad user-generated inbound pmt hashes
1 parent 00556af commit 1f74a24

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6422,8 +6422,10 @@ mod tests {
64226422
use bitcoin::hashes::Hash;
64236423
use bitcoin::hashes::sha256::Hash as Sha256;
64246424
use core::time::Duration;
6425+
use core::sync::atomic::Ordering;
64256426
use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
64266427
use ln::channelmanager::{PaymentId, PaymentSendFailure};
6428+
use ln::channelmanager::inbound_payment;
64276429
use ln::features::InitFeatures;
64286430
use ln::functional_test_utils::*;
64296431
use ln::msgs;
@@ -6438,7 +6440,7 @@ mod tests {
64386440
fn test_wait_timeout() {
64396441
use ln::channelmanager::PersistenceNotifier;
64406442
use sync::Arc;
6441-
use core::sync::atomic::{AtomicBool, Ordering};
6443+
use core::sync::atomic::AtomicBool;
64426444
use std::thread;
64436445

64446446
let persistence_notifier = Arc::new(PersistenceNotifier::new());
@@ -6867,6 +6869,35 @@ mod tests {
68676869
_ => panic!("unexpected error")
68686870
}
68696871
}
6872+
6873+
#[test]
6874+
fn bad_inbound_payment_hash() {
6875+
// Add coverage for checking that a user-provided payment hash matches the payment secret.
6876+
let chanmon_cfgs = create_chanmon_cfgs(2);
6877+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6878+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6879+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6880+
6881+
let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[0]);
6882+
let payment_data = msgs::FinalOnionHopData {
6883+
payment_secret,
6884+
total_msat: 100_000,
6885+
};
6886+
6887+
// Ensure that if the payment hash given to `inbound_payment::verify` differs from the original,
6888+
// payment verification fails as expected.
6889+
let mut bad_payment_hash = payment_hash.clone();
6890+
bad_payment_hash.0[0] += 1;
6891+
match inbound_payment::verify(bad_payment_hash, payment_data.clone(), nodes[0].node.highest_seen_timestamp.load(Ordering::Acquire) as u64, &nodes[0].node.inbound_payment_key, &nodes[0].logger) {
6892+
Ok(_) => panic!("Unexpected ok"),
6893+
Err(()) => {
6894+
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager::inbound_payment".to_string(), "Failing HTLC with user-generated payment_hash".to_string(), 1);
6895+
}
6896+
}
6897+
6898+
// Check that using the original payment hash succeeds.
6899+
assert!(inbound_payment::verify(payment_hash, payment_data, nodes[0].node.highest_seen_timestamp.load(Ordering::Acquire) as u64, &nodes[0].node.inbound_payment_key, &nodes[0].logger).is_ok());
6900+
}
68706901
}
68716902

68726903
#[cfg(all(any(test, feature = "_test_utils"), feature = "unstable"))]

0 commit comments

Comments
 (0)