Skip to content

Commit 5309176

Browse files
committed
Fuzz fetching InvoiceRequestFields from VerifiedInvoiceRequests
This should allow us to reach the panic from two commits ago from the fuzzer.
1 parent eb7f6d9 commit 5309176

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

fuzz/src/invoice_request_deser.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,26 @@ fn build_response<T: secp256k1::Signing + secp256k1::Verification>(
8585
let expanded_key = ExpandedKey::new([42; 32]);
8686
let entropy_source = Randomness {};
8787
let nonce = Nonce::from_entropy_source(&entropy_source);
88+
89+
let invoice_request_fields =
90+
if let Ok(ver) = invoice_request.clone().verify_using_metadata(&expanded_key, secp_ctx) {
91+
// Previously we had a panic where we'd truncate the payer note possibly cutting a
92+
// Unicode character in two here, so try to fetch fields if we can validate.
93+
ver.fields()
94+
} else {
95+
InvoiceRequestFields {
96+
payer_signing_pubkey: invoice_request.payer_signing_pubkey(),
97+
quantity: invoice_request.quantity(),
98+
payer_note_truncated: invoice_request
99+
.payer_note()
100+
.map(|s| UntrustedString(s.to_string())),
101+
human_readable_name: None,
102+
}
103+
};
104+
88105
let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
89106
offer_id: OfferId([42; 32]),
90-
invoice_request: InvoiceRequestFields {
91-
payer_signing_pubkey: invoice_request.payer_signing_pubkey(),
92-
quantity: invoice_request.quantity(),
93-
payer_note_truncated: invoice_request
94-
.payer_note()
95-
.map(|s| UntrustedString(s.to_string())),
96-
human_readable_name: None,
97-
},
107+
invoice_request: invoice_request_fields,
98108
});
99109
let payee_tlvs = UnauthenticatedReceiveTlvs {
100110
payment_secret: PaymentSecret([42; 32]),

lightning/src/offers/invoice_request.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,14 @@ impl VerifiedInvoiceRequest {
989989
InvoiceWithDerivedSigningPubkeyBuilder
990990
);
991991

992-
pub(crate) fn fields(&self) -> InvoiceRequestFields {
992+
/// Fetch the [`InvoiceRequestFields`] for this verified invoice.
993+
///
994+
/// These are fields which we expect to be useful when receiving a payment for this invoice
995+
/// request, and include the returned [`InvoiceRequestFields`] in the
996+
/// [`PaymentContext::Bolt12Offer`].
997+
///
998+
/// [`PaymentContext::Bolt12Offer`]: crate::blinded_path::payment::PaymentContext::Bolt12Offer
999+
pub fn fields(&self) -> InvoiceRequestFields {
9931000
let InvoiceRequestContents {
9941001
payer_signing_pubkey,
9951002
inner: InvoiceRequestContentsWithoutPayerSigningPubkey { quantity, payer_note, .. },
@@ -1404,8 +1411,13 @@ pub struct InvoiceRequestFields {
14041411
}
14051412

14061413
/// The maximum number of characters included in [`InvoiceRequestFields::payer_note_truncated`].
1414+
#[cfg(not(fuzzing))]
14071415
pub const PAYER_NOTE_LIMIT: usize = 512;
14081416

1417+
/// The maximum number of characters included in [`InvoiceRequestFields::payer_note_truncated`].
1418+
#[cfg(fuzzing)]
1419+
pub const PAYER_NOTE_LIMIT: usize = 8;
1420+
14091421
impl Writeable for InvoiceRequestFields {
14101422
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
14111423
write_tlv_fields!(writer, {

0 commit comments

Comments
 (0)