From 6ea8098c50acc5f7467d8e80905e657496ada4d9 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 21:03:29 +0000 Subject: [PATCH] fix(sign): bound the depth and name bytes of signature field discovery --- crates/sign/src/pdf/listing.rs | 21 ++++++++++++++++++--- crates/sign/tests/it/pdf.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/crates/sign/src/pdf/listing.rs b/crates/sign/src/pdf/listing.rs index cbcf339715..c30403da40 100644 --- a/crates/sign/src/pdf/listing.rs +++ b/crates/sign/src/pdf/listing.rs @@ -198,6 +198,13 @@ impl Field { } } +/// Deepest field hierarchy `sig_fields` follows; fields below it are not listed. +const MAX_FIELD_DEPTH: usize = 64; + +/// Bytes of qualified field names `sig_fields` builds for one document. Each name repeats its +/// ancestors', so a long chain would otherwise cost the square of its length. +const MAX_FIELD_NAME_BYTES: usize = 32 << 20; + /// Every signature field, depth first in `/Fields` order. pub(super) fn sig_fields(doc: &Document) -> Vec { let mut out = Vec::new(); @@ -207,8 +214,12 @@ pub(super) fn sig_fields(doc: &Document) -> Vec { }; let Some(fields) = af.get(b"Fields").map(|f| doc.resolve(f)).and_then(|f| f.as_array().cloned()) else { return out }; let mut seen = HashSet::new(); - let mut stack: Vec<(Object, String, Option>)> = fields.into_iter().rev().map(|f| (f, String::new(), None)).collect(); - while let Some((o, prefix, inherited_ft)) = stack.pop() { + let mut stack: Vec<(Object, String, Option>, usize)> = fields.into_iter().rev().map(|f| (f, String::new(), None, 0)).collect(); + let mut name_bytes = 0usize; + while let Some((o, prefix, inherited_ft, depth)) = stack.pop() { + if depth > MAX_FIELD_DEPTH { + continue; + } if let Some(r) = o.as_ref() && !seen.insert(r) { @@ -225,8 +236,12 @@ pub(super) fn sig_fields(doc: &Document) -> Vec { let kids: Vec = d.get(b"Kids").map(|k| doc.resolve(k)).and_then(|k| k.as_array().cloned()).unwrap_or_default(); // Kids with a /T are fields; others are this field's widgets. let (sub, widgets): (Vec, Vec) = kids.into_iter().partition(|k| doc.resolve(k).as_dict().is_some_and(|kd| kd.contains(b"T"))); + name_bytes = name_bytes.saturating_add(name.len()); + if name_bytes > MAX_FIELD_NAME_BYTES { + break; + } for k in sub.into_iter().rev() { - stack.push((k, name.clone(), ft.clone())); + stack.push((k, name.clone(), ft.clone(), depth + 1)); } if ft.as_deref() == Some(b"Sig") && part.is_some() { let mut ws: Vec<(Option, Dict)> = diff --git a/crates/sign/tests/it/pdf.rs b/crates/sign/tests/it/pdf.rs index 869a4bebd5..3719f19dbd 100644 --- a/crates/sign/tests/it/pdf.rs +++ b/crates/sign/tests/it/pdf.rs @@ -2954,3 +2954,34 @@ fn signing_a_field_with_a_null_value_after_another_signature_keeps_the_first_val all.iter().map(|s| (&s.status, &s.modification)).collect::>() ); } + +/// Signature fields are found through a field hierarchy of bounded depth: a chain of ordinary +/// depth lists its leaf, one thousands deep does not build a name per level. +#[test] +fn signature_discovery_bounds_the_field_hierarchy_depth() { + let chain = |levels: usize| { + let mut objs: Vec> = vec![ + b"<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [7 0 R] >> >>".to_vec(), + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(), + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] >>".to_vec(), + b"<< /Length 0 >>\nstream\n\nendstream".to_vec(), + b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".to_vec(), + b"<< /Length 0 >>\nstream\n\nendstream".to_vec(), + ]; + for i in 0..levels { + let num = 7 + i; + objs.push( + if i + 1 == levels { + format!("<< /T (leaf) /FT /Sig /Parent {} 0 R >>", num - 1) + } else { + format!("<< /T (g) /FT /Sig /Kids [{} 0 R] >>", num + 1) + } + .into_bytes(), + ); + } + write_pdf(&objs.iter().map(Vec::as_slice).collect::>()) + }; + let names = |bytes: &[u8]| signatures(&open(bytes), bytes, &TrustStore::default()).into_iter().map(|s| s.field).collect::>(); + assert!(names(&chain(4)).iter().any(|n| n == "g.g.g.leaf"), "{:?}", names(&chain(4))); + assert!(names(&chain(2_000)).iter().all(|n| !n.ends_with("leaf"))); +}