Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add length limit for invoice description #313

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/invoice/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub enum InvoiceError {
HexDecodeError(#[from] hex::FromHexError),
#[error("Duplicated inovice found: {0}")]
DuplicatedInvoice(String),
#[error("Description with length of {0} is too long, max length is 639")]
DescriptionTooLong(usize),
#[error("Invoice not found")]
InvoiceNotFound,
}
13 changes: 11 additions & 2 deletions src/invoice/invoice_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use serde_with::serde_as;
use std::{cmp::Ordering, str::FromStr};

pub(crate) const SIGNATURE_U5_SIZE: usize = 104;
pub(crate) const MAX_DESCRIPTION_LENGTH: usize = 639;

/// The currency of the invoice, can also used to represent the CKB network chain.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -652,7 +653,7 @@ impl InvoiceBuilder {
rand_sha256_hash()
};

self.check_duplicated_attrs()?;
self.check_attrs_valid()?;
let timestamp = std::time::UNIX_EPOCH
.elapsed()
.expect("Duration since unix epoch")
Expand All @@ -678,7 +679,7 @@ impl InvoiceBuilder {
Ok(invoice)
}

fn check_duplicated_attrs(&self) -> Result<(), InvoiceError> {
fn check_attrs_valid(&self) -> Result<(), InvoiceError> {
// check is there any duplicate attribute key set
for (i, attr) in self.attrs.iter().enumerate() {
for other in self.attrs.iter().skip(i + 1) {
Expand All @@ -687,6 +688,14 @@ impl InvoiceBuilder {
}
}
}

if let Some(len) = self.attrs.iter().find_map(|attr| match attr {
Attribute::Description(desc) if desc.len() > MAX_DESCRIPTION_LENGTH => Some(desc.len()),
_ => None,
}) {
return Err(InvoiceError::DescriptionTooLong(len));
}

Ok(())
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/invoice/tests/invoice_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,26 @@ fn test_invoice_builder_duplicated_attr() {
);
}

#[test]
fn test_invoice_check_description_length() {
let gen_payment_hash = rand_sha256_hash();
let private_key = gen_rand_private_key();
const MAX_DESCRIPTION_LEN: usize = 639;
let invoice = InvoiceBuilder::new(Currency::Fibb)
.amount(Some(1280))
.payment_hash(gen_payment_hash)
.description("a".repeat(MAX_DESCRIPTION_LEN + 1))
.add_attr(Attribute::FinalHtlcTimeout(5))
.build_with_sign(|hash| Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key));

assert!(invoice.is_err());
let message = invoice.err().unwrap().to_string();
assert_eq!(
message,
"Description with length of 640 is too long, max length is 639"
);
}

#[test]
fn test_invoice_builder_missing() {
let private_key = gen_rand_private_key();
Expand Down
Loading