Skip to content

Commit 5816794

Browse files
f test bad user-generated inbound pmt hashes
1 parent 4857b57 commit 5816794

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
@@ -6419,8 +6419,10 @@ mod tests {
64196419
use bitcoin::hashes::Hash;
64206420
use bitcoin::hashes::sha256::Hash as Sha256;
64216421
use core::time::Duration;
6422+
use core::sync::atomic::Ordering;
64226423
use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
64236424
use ln::channelmanager::{PaymentId, PaymentSendFailure};
6425+
use ln::channelmanager::inbound_payment;
64246426
use ln::features::InitFeatures;
64256427
use ln::functional_test_utils::*;
64266428
use ln::msgs;
@@ -6435,7 +6437,7 @@ mod tests {
64356437
fn test_wait_timeout() {
64366438
use ln::channelmanager::PersistenceNotifier;
64376439
use sync::Arc;
6438-
use core::sync::atomic::{AtomicBool, Ordering};
6440+
use core::sync::atomic::AtomicBool;
64396441
use std::thread;
64406442

64416443
let persistence_notifier = Arc::new(PersistenceNotifier::new());
@@ -6864,6 +6866,35 @@ mod tests {
68646866
_ => panic!("unexpected error")
68656867
}
68666868
}
6869+
6870+
#[test]
6871+
fn bad_inbound_payment_hash() {
6872+
// Add coverage for checking that a user-provided payment hash matches the payment secret.
6873+
let chanmon_cfgs = create_chanmon_cfgs(2);
6874+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
6875+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
6876+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
6877+
6878+
let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[0]);
6879+
let payment_data = msgs::FinalOnionHopData {
6880+
payment_secret,
6881+
total_msat: 100_000,
6882+
};
6883+
6884+
// Ensure that if the payment hash given to `inbound_payment::verify` differs from the original,
6885+
// payment verification fails as expected.
6886+
let mut bad_payment_hash = payment_hash.clone();
6887+
bad_payment_hash.0[0] += 1;
6888+
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) {
6889+
Ok(_) => panic!("Unexpected ok"),
6890+
Err(()) => {
6891+
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager::inbound_payment".to_string(), "Failing HTLC with user-generated payment_hash".to_string(), 1);
6892+
}
6893+
}
6894+
6895+
// Check that using the original payment hash succeeds.
6896+
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());
6897+
}
68676898
}
68686899

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

0 commit comments

Comments
 (0)