diff --git a/crates/xfa/src/pdf.rs b/crates/xfa/src/pdf.rs index fd80949e8..bc5b4d2da 100644 --- a/crates/xfa/src/pdf.rs +++ b/crates/xfa/src/pdf.rs @@ -44,7 +44,7 @@ fn hex32(h: &[u8; 32]) -> Vec { fn parse_hex32(s: &[u8]) -> Option<[u8; 32]> { let mut out = [0u8; 32]; - if s.len() != 64 { + if s.len() != 64 || !s.iter().all(u8::is_ascii_hexdigit) { return None; } for (o, pair) in out.iter_mut().zip(s.as_chunks::<2>().0) { @@ -1294,3 +1294,20 @@ pub fn existing_layout(doc: &Document) -> Option { pub fn pdf_rect(r: Rect, page_height: f64) -> [f64; 4] { [r.x, page_height - r.bottom(), r.right(), page_height - r.y] } + +#[cfg(test)] +mod tests { + use super::parse_hex32; + + #[test] + fn parse_hex32_rejects_non_hex_digits() { + for prefix in ["+f", "-f", " f"] { + let mut s = prefix.as_bytes().to_vec(); + s.resize(64, b'0'); + assert!(parse_hex32(&s).is_none(), "{prefix:?}"); + } + let mut ok = b"Ff".to_vec(); + ok.resize(64, b'0'); + assert_eq!(parse_hex32(&ok).map(|h| h[0]), Some(0xff)); + } +}