mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-25 15:37:02 +00:00
fix(sign): bound the depth and name bytes of signature field discovery
This commit is contained in:
@@ -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<Field> {
|
||||
let mut out = Vec::new();
|
||||
@@ -207,8 +214,12 @@ pub(super) fn sig_fields(doc: &Document) -> Vec<Field> {
|
||||
};
|
||||
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<Vec<u8>>)> = 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<Vec<u8>>, 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<Field> {
|
||||
let kids: Vec<Object> = 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<Object>, Vec<Object>) = 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<ObjRef>, Dict)> =
|
||||
|
||||
@@ -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::<Vec<_>>()
|
||||
);
|
||||
}
|
||||
|
||||
/// 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<u8>> = 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::<Vec<_>>())
|
||||
};
|
||||
let names = |bytes: &[u8]| signatures(&open(bytes), bytes, &TrustStore::default()).into_iter().map(|s| s.field).collect::<Vec<_>>();
|
||||
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")));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user