Skip to content

Commit 4c2d3d8

Browse files
committed
Substantially increase coverage in BOLT11 deserialization fuzzer
1 parent 6dcc0a0 commit 4c2d3d8

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

fuzz/src/bolt11_deser.rs

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,58 @@
88
// licenses.
99

1010
use bitcoin::bech32::{u5, FromBase32, ToBase32};
11+
use bitcoin::secp256k1::{Secp256k1, SecretKey};
1112
use crate::utils::test_logger;
12-
use lightning_invoice::RawDataPart;
13+
use std::str::FromStr;
14+
use lightning_invoice::{Bolt11Invoice, RawBolt11Invoice, RawDataPart, RawHrp, RawTaggedField, TaggedField};
1315

1416
#[inline]
1517
pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
16-
let bech32 = data.iter().map(|x| u5::try_from_u8(x % 32).unwrap()).collect::<Vec<_>>();
17-
let invoice = match RawDataPart::from_base32(&bech32) {
18-
Ok(invoice) => invoice,
19-
Err(_) => return,
20-
};
21-
22-
// Our encoding is not worse than the input
23-
assert!(invoice.to_base32().len() <= bech32.len());
24-
25-
// Our serialization is loss-less
26-
assert_eq!(
27-
RawDataPart::from_base32(&invoice.to_base32()).expect("faild parsing out own encoding"),
28-
invoice
29-
);
18+
// Read a fake HRP length byte
19+
let hrp_len = std::cmp::min(*data.get(0).unwrap_or(&0) as usize, data.len());
20+
if let Ok(s) = std::str::from_utf8(&data[..hrp_len]) {
21+
let hrp = match RawHrp::from_str(s) {
22+
Ok(hrp) => hrp,
23+
Err(_) => return,
24+
};
25+
let bech32 = data.iter()
26+
.skip(hrp_len)
27+
.map(|x| u5::try_from_u8(x % 32).unwrap())
28+
.collect::<Vec<_>>();
29+
let invoice_data = match RawDataPart::from_base32(&bech32) {
30+
Ok(invoice) => invoice,
31+
Err(_) => return,
32+
};
33+
34+
// Our data encoding is not worse than the input
35+
assert!(invoice_data.to_base32().len() <= bech32.len());
36+
37+
// Our data serialization is loss-less
38+
assert_eq!(
39+
RawDataPart::from_base32(&invoice_data.to_base32()).expect("faild parsing out own encoding"),
40+
invoice_data
41+
);
42+
43+
if invoice_data.tagged_fields.iter().any(|field| matches!(field, RawTaggedField::KnownSemantics(TaggedField::PayeePubKey(_)))) {
44+
// We could forge a signature using the fact that signing is insecure in fuzz mode, but
45+
// easier to just skip and rely on the fact that no-PayeePubKey invoices do pubkey
46+
// recovery
47+
return;
48+
}
49+
50+
let raw_invoice = RawBolt11Invoice { hrp, data: invoice_data };
51+
let signed_raw_invoice = match raw_invoice.sign(|hash| {
52+
let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
53+
Ok::<_, ()>(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key))
54+
}) {
55+
Ok(inv) => inv,
56+
Err(_) => return,
57+
};
58+
59+
if let Ok(invoice) = Bolt11Invoice::from_signed(signed_raw_invoice) {
60+
invoice.amount_milli_satoshis();
61+
}
62+
}
3063
}
3164

3265
pub fn bolt11_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {

0 commit comments

Comments
 (0)