Serialize experimental TLV records in unsigned invoices
What changed, and why it matters
This commit fixes a bug in how an unsigned BOLT 12 invoice was serialized before being signed. The public serialization accidentally dropped experimental/custom data fields that were inherited from the payer's invoice request. Because those fields are still included in the cryptographic hash that gets signed, a signer that inspected only the public serialized bytes would see an incomplete picture and could not reliably enforce policies against unknown extensions. The fix makes the public serialization include both the standard bytes and the experimental bytes, matching what is actually signed.
Treat this as a security-relevant correctness fix and include it in the next maintenance release. Node operators and integrators using custom signers or HSM adapters for BOLT 12 invoices should upgrade so that policy checks operate on the complete authenticated invoice bytes. Review any signer code that serializes `UnsignedBolt12Invoice` to confirm it now receives the full TLV stream.
Security signals we found
Policy-signing boundary bypass risk: signer sees incomplete byte representation of authenticated data
Experimental/unknown odd TLV records silently omitted from public serialization
Mismatch between serialized view and tagged_hash/signature input
HSM or remote policy signer could fail to reject extensions it cannot see
Fix is narrowly scoped to one Writeable impl with regression test
Evidence from the diff
The Writeable implementation for UnsignedBolt12Invoice previously wrote only self.bytes, omitting self.experimental_bytes. Construction, parsing, and tagged_hash already included experimental_bytes, and sign appended it to the final Bolt12Invoice. This created a mismatch between the bytes a NodeSigner::sign_bolt12_invoice implementation could inspect via the public write() API and the bytes authenticated by the signature. The patch concatenates self.bytes and self.experimental_bytes without a length prefix, matching the unsigned TLV stream that is later signed. A regression test verifies that experimental TLVs inherited from a signed invoice request round-trip through serialization and preserve tagged_hash.
Changed components
lightning/src/offers/invoice.rsUnsignedBolt12Invoice::writeNodeSigner::sign_bolt12_invoice consumersBOLT 12 invoice request/response experimental TLV handlingInspect captured patch +42 / −1
### lightning/src/offers/invoice.rs
@@ -1487,7 +1487,8 @@ impl InvoiceFields {
impl Writeable for UnsignedBolt12Invoice {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
- WithoutLength(&self.bytes).write(writer)
+ WithoutLength(&self.bytes).write(writer)?;
+ WithoutLength(&self.experimental_bytes).write(writer)
}
}
@@ -1981,6 +1982,7 @@ mod tests {
let mut buffer = Vec::new();
unsigned_invoice.write(&mut buffer).unwrap();
+ assert!(unsigned_invoice.experimental_bytes.is_empty());
assert_eq!(unsigned_invoice.bytes, buffer.as_slice());
assert_eq!(unsigned_invoice.payer_metadata(), payer_metadata.as_slice());
assert_eq!(
@@ -3588,6 +3590,45 @@ mod tests {
}
}
+ #[test]
+ fn serializes_unsigned_invoice_with_experimental_tlv_records() {
+ let expanded_key = ExpandedKey::new([42; 32]);
+ let entropy = FixedEntropy {};
+ let nonce = Nonce::from_entropy_source(&entropy);
+ let secp_ctx = Secp256k1::new();
+ let payment_id = PaymentId([1; 32]);
+
+ // The payer signs the invoice request, so its experimental TLV records may be inherited by
+ // the invoice and must be included when the unsigned invoice is serialized for signing.
+ let invoice_request = OfferBuilder::new(recipient_pubkey())
+ .amount_msats(1000)
+ .build()
+ .unwrap()
+ .request_invoice(&expanded_key, nonce, &secp_ctx, payment_id)
+ .unwrap()
+ .experimental_bar(42)
+ .build_and_sign()
+ .unwrap();
+ let unsigned_invoice = invoice_request
+ .respond_with_no_std(payment_paths(), payment_hash(), now())
+ .unwrap()
+ .build()
+ .unwrap();
+ assert!(!unsigned_invoice.experimental_bytes.is_empty());
+
+ let mut encoded_invoice = Vec::new();
+ unsigned_invoice.write(&mut encoded_invoice).unwrap();
+
+ let mut expected = unsigned_invoice.bytes.clone();
+ expected.extend_from_slice(&unsigned_invoice.experimental_bytes);
+ assert_eq!(encoded_invoice, expected);
+
+ match UnsignedBolt12Invoice::try_from(encoded_invoice) {
+ Ok(reparsed) => assert_eq!(reparsed.tagged_hash, unsigned_invoice.tagged_hash),
+ Err(e) => panic!("error parsing invoice: {:?}", e),
+ }
+ }
+
#[test]
fn fails_parsing_invoice_with_out_of_range_tlv_records() {
let expanded_key = ExpandedKey::new([42; 32]);Why this scored 60/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.