From 834c44fc6a8e07904feb73e5535ec780cb3772ad Mon Sep 17 00:00:00 2001 From: jonathanwxh-cell Date: Sun, 25 Oct 2026 09:28:27 +0800 Subject: [PATCH 1/3] sign: validate signature dicts and post-signing changes against signed revision (#3998) - Resolve /ByteRange, /Contents, /Reference (DocMDP), /SubFilter, /Cert, and signer metadata from the revision covered by the signature or standalone timestamp, and verify that /Contents matches the byte-range gap - Classify post-signing modifications against the signed revision's catalog, page tree (/Pages and /Kids), page list, AcroForm, and signed/unsigned signature objects so dropping /Type or adding form/signature keys cannot downgrade disallowed changes - Disallow kind::SIGNATURE modifications under DocMDP P = 1 and add regression tests for catalog/page-tree relabelling, post-signing signature dictionary tampering, and P = 1 counter-signing Fixes #3998 --- crates/sign/src/pdf.rs | 456 +++++++++++++++++++++++++++--------- crates/sign/tests/it/pdf.rs | 124 ++++++++++ 2 files changed, 475 insertions(+), 105 deletions(-) diff --git a/crates/sign/src/pdf.rs b/crates/sign/src/pdf.rs index 279feee096..3ee69e474c 100644 --- a/crates/sign/src/pdf.rs +++ b/crates/sign/src/pdf.rs @@ -620,7 +620,7 @@ pub fn list_cached(doc: &Document, bytes: &[u8], trust: &TrustStore, cache: &Dig details: Vec::new(), }; if let Some(v) = v { - validate_into(doc, bytes, trust, &v, &mut info, cache); + validate_into(doc, bytes, trust, Some(&f), None, &v, &mut info, cache); } out.push(info); } @@ -662,7 +662,7 @@ pub fn list_cached(doc: &Document, bytes: &[u8], trust: &TrustStore, cache: &Dig modification: Modification::None, details: Vec::new(), }; - validate_doc_timestamp(doc, bytes, d, &mut info, cache, trust); + validate_doc_timestamp(doc, bytes, None, Some(r), d, &mut info, cache, trust); out.push(info); } out @@ -713,30 +713,90 @@ fn docmdp(doc: &Document, v: &Dict) -> Option { .map(|r| r.get(b"TransformParams").map(|p| doc.resolve(p)).and_then(|p| p.as_dict().and_then(|p| p.int(b"P"))).unwrap_or(2).clamp(1, 3) as u8) } -fn validate_into(doc: &Document, bytes: &[u8], trust: &TrustStore, v: &Dict, info: &mut SignatureInfo, cache: &DigestCache) { +fn whole_file(bytes: &[u8], covered: usize) -> bool { + covered == bytes.len() || bytes.get(covered..).is_some_and(|rest| rest.iter().all(|b| b.is_ascii_whitespace() || *b == 0)) +} + +fn dict_contents(doc: &Document, d: &Dict) -> Option> { + let contents = d.get(b"Contents").map(|c| doc.resolve(c))?; + let s = contents.as_string()?; + if s.hex { Some(s.bytes.clone()) } else { unhex(&s.bytes) } +} + +fn find_sig_in_revision(rev: &Document, field: Option<&Field>, stamp_ref: Option) -> Option { + if let Some(f) = field { + let matched = sig_fields(rev).into_iter().find(|of| (f.r.is_some() && of.r == f.r) || of.name == f.name)?; + matched.signature(rev) + } else if let Some(r) = stamp_ref { + rev.try_get(r.num).ok().and_then(|o| o.as_dict().cloned()).filter(is_doc_timestamp) + } else { + None + } +} + +/// The revision in which this signature or timestamp first appeared (when an earlier revision +/// already holds it), or the current document. +fn earliest_signed_dict( + doc: &Document, + bytes: &[u8], + cache: &DigestCache, + field: Option<&Field>, + stamp_ref: Option, + v: &Dict, +) -> (Document, Dict) { + let ends = doc.revision_ends(); + if ends.len() > 1 { + for &end in &ends[..ends.len() - 1] { + if end < bytes.len() + && !whole_file(bytes, end) + && let Some(old) = cache.revision(doc, bytes, end) + && let Some(old_v) = find_sig_in_revision(&old, field, stamp_ref) + { + return (old, old_v); + } + } + } + (doc.clone(), v.clone()) +} + +#[allow(clippy::too_many_arguments)] +fn validate_into( + doc: &Document, + bytes: &[u8], + trust: &TrustStore, + field: Option<&Field>, + stamp_ref: Option, + v: &Dict, + info: &mut SignatureInfo, + cache: &DigestCache, +) { + let (mut sig_doc, mut sig_v) = earliest_signed_dict(doc, bytes, cache, field, stamp_ref, v); // A document timestamp can also be the value of a signature field (Documenso writes // `Timestamp_1` that way). Its `/Contents` is a bare RFC 3161 token, not a signature over the // document, so it must not be read as one. - if is_doc_timestamp(v) { + if is_doc_timestamp(&sig_v) || is_doc_timestamp(v) { info.sub_filter = Some("ETSI.RFC3161".to_string()); info.doc_timestamp = true; info.timestamp = true; - validate_doc_timestamp(doc, bytes, v, info, cache, trust); + validate_doc_timestamp(doc, bytes, field, stamp_ref, v, info, cache, trust); return; } - info.date = text(doc, v, b"M"); - info.reason = text(doc, v, b"Reason"); - info.location = text(doc, v, b"Location"); - info.contact = text(doc, v, b"ContactInfo"); - info.sub_filter = v.name(b"SubFilter").map(|s| String::from_utf8_lossy(s).into_owned()); - info.signer = text(doc, v, b"Name"); - info.certify = docmdp(doc, v); + let fill_metadata = |d_doc: &Document, d_v: &Dict, info: &mut SignatureInfo| { + info.date = text(d_doc, d_v, b"M"); + info.reason = text(d_doc, d_v, b"Reason"); + info.location = text(d_doc, d_v, b"Location"); + info.contact = text(d_doc, d_v, b"ContactInfo"); + info.sub_filter = d_v.name(b"SubFilter").map(|s| String::from_utf8_lossy(s).into_owned()); + info.signer = text(d_doc, d_v, b"Name"); + info.certify = docmdp(d_doc, d_v); + }; + fill_metadata(&sig_doc, &sig_v, info); let invalid = |info: &mut SignatureInfo, why: Detail| { info.status = Status::Invalid; info.details.push(why); }; // Byte ranges: [0 a b c] with the gap holding exactly the hex /Contents. - let Some(br) = nums(doc, v, b"ByteRange").filter(|b| b.len() == 4 && b.iter().all(|x| *x >= 0.0)) else { + let Some(br) = nums(&sig_doc, &sig_v, b"ByteRange").filter(|b| b.len() == 4 && b.iter().all(|x| *x >= 0.0)) else { return invalid(info, Detail::plain("The signature has no valid byte range.")); }; let [o0, l0, o1, l1] = [br[0] as usize, br[1] as usize, br[2] as usize, br[3] as usize]; @@ -749,6 +809,24 @@ fn validate_into(doc: &Document, bytes: &[u8], trust: &TrustStore, v: &Dict, inf } let Some(contents) = unhex(&gap[1..gap.len() - 1]) else { return invalid(info, Detail::plain("The signature contents are not hexadecimal.")) }; let covered = o1 + l1; + if !whole_file(bytes, covered) { + let Some(old) = cache.revision(doc, bytes, covered) else { + return invalid(info, Detail::plain("The signature's byte range does not match the file.")); + }; + let Some(old_v) = find_sig_in_revision(&old, field, stamp_ref) else { + return invalid(info, Detail::plain("The signature's byte range does not match the file.")); + }; + if nums(&old, &old_v, b"ByteRange") != Some(br) { + return invalid(info, Detail::plain("The signature's byte range does not match the file.")); + } + fill_metadata(&old, &old_v, info); + (sig_doc, sig_v) = (old, old_v); + } + let expected_contents = + sig_doc.bytes().get(l0..o1).filter(|g| g.first() == Some(&b'<') && g.last() == Some(&b'>')).and_then(|g| unhex(&g[1..g.len() - 1])); + if dict_contents(&sig_doc, &sig_v) != expected_contents { + return invalid(info, Detail::plain("The signature's byte range does not exclude exactly its contents.")); + } info.signed_len = covered; // The signed revision's number: its cross-reference sections (1 for a reconstructed file). info.revision = cache.revision(doc, bytes, covered).map_or(1, |d| d.revisions().len().max(1)); @@ -760,7 +838,7 @@ fn validate_into(doc: &Document, bytes: &[u8], trust: &TrustStore, v: &Dict, inf info.details.push(Detail::of("PdfCraft can't check this signature yet: {}.", Fill::Reason(why.to_owned()))); }; if info.sub_filter.as_deref() == Some("adbe.x509.rsa_sha1") { - return validate_x509_rsa_sha1(doc, bytes, trust, v, info, cache, (l0, o1, covered), &contents); + return validate_x509_rsa_sha1(doc, &sig_doc, bytes, trust, &sig_v, info, cache, (l0, o1, covered), &contents); } let sd = match SignedData::parse(&contents) { Ok(sd) => sd, @@ -909,20 +987,10 @@ fn finish_validation( let revocation = check_dss_revocation(doc, signer.as_ref(), &at, info); let revoked = revocation == DssRevocation::Revoked; // Changes after signing. - info.modification = if covered == bytes.len() || bytes[covered..].iter().all(|b| b.is_ascii_whitespace() || *b == 0) { + info.modification = if whole_file(bytes, covered) { Modification::None } else { - // The permissions are the signed revision's: a later one can rewrite `/P` (#1788). This - // signature is the one whose byte range ends that revision. let old = cache.revision(doc, bytes, covered); - if let Some(old) = &old - && let Some(v) = sig_fields(old) - .iter() - .filter_map(|f| f.signature(old)) - .find(|v| nums(old, v, b"ByteRange").is_some_and(|b| b.len() == 4 && b[2] + b[3] == covered as f64)) - { - info.certify = docmdp(old, &v); - } classify_changes(doc, old, info.certify) }; let mut problems = false; @@ -1140,13 +1208,24 @@ fn check_dss_revocation(doc: &Document, signer: Option<&Certificate>, at: &Optio /// Validate a standalone document timestamp dictionary (`/ETSI.RFC3161`): the token must be /// cryptographically valid and its message imprint must cover the file's signed bytes. -fn validate_doc_timestamp(doc: &Document, bytes: &[u8], v: &Dict, info: &mut SignatureInfo, cache: &DigestCache, trust: &TrustStore) { - info.date = text(doc, v, b"M"); +#[allow(clippy::too_many_arguments)] +fn validate_doc_timestamp( + doc: &Document, + bytes: &[u8], + field: Option<&Field>, + stamp_ref: Option, + v: &Dict, + info: &mut SignatureInfo, + cache: &DigestCache, + trust: &TrustStore, +) { + let (mut sig_doc, mut sig_v) = earliest_signed_dict(doc, bytes, cache, field, stamp_ref, v); + info.date = text(&sig_doc, &sig_v, b"M"); let invalid = |info: &mut SignatureInfo, why: Detail| { info.status = Status::Invalid; info.details.push(why); }; - let Some(br) = nums(doc, v, b"ByteRange").filter(|b| b.len() == 4 && b.iter().all(|x| *x >= 0.0)) else { + let Some(br) = nums(&sig_doc, &sig_v, b"ByteRange").filter(|b| b.len() == 4 && b.iter().all(|x| *x >= 0.0)) else { return invalid(info, Detail::plain("The timestamp has no valid byte range.")); }; let [o0, l0, o1, l1] = [br[0] as usize, br[1] as usize, br[2] as usize, br[3] as usize]; @@ -1161,6 +1240,24 @@ fn validate_doc_timestamp(doc: &Document, bytes: &[u8], v: &Dict, info: &mut Sig return invalid(info, Detail::plain("The timestamp contents are not hexadecimal.")); }; let covered = o1 + l1; + if !whole_file(bytes, covered) { + let Some(old) = cache.revision(doc, bytes, covered) else { + return invalid(info, Detail::plain("The timestamp's byte range does not match the file.")); + }; + let Some(old_v) = find_sig_in_revision(&old, field, stamp_ref) else { + return invalid(info, Detail::plain("The timestamp's byte range does not match the file.")); + }; + if nums(&old, &old_v, b"ByteRange") != Some(br) { + return invalid(info, Detail::plain("The timestamp's byte range does not match the file.")); + } + info.date = text(&old, &old_v, b"M"); + (sig_doc, sig_v) = (old, old_v); + } + let expected_contents = + sig_doc.bytes().get(l0..o1).filter(|g| g.first() == Some(&b'<') && g.last() == Some(&b'>')).and_then(|g| unhex(&g[1..g.len() - 1])); + if dict_contents(&sig_doc, &sig_v) != expected_contents { + return invalid(info, Detail::plain("The timestamp's byte range does not exclude exactly its contents.")); + } info.signed_len = covered; info.revision = cache.revision(doc, bytes, covered).map_or(1, |d| d.revisions().len().max(1)); let token = match crate::timestamp::parse_token(&contents) { @@ -1270,6 +1367,7 @@ pub(crate) fn signature_contents(doc: &Document) -> Vec> { #[allow(clippy::too_many_arguments)] fn validate_x509_rsa_sha1( doc: &Document, + sig_doc: &Document, bytes: &[u8], trust: &TrustStore, v: &Dict, @@ -1282,8 +1380,8 @@ fn validate_x509_rsa_sha1( info.status = Status::Invalid; info.details.push(why); }; - let certs: Vec = match v.get(b"Cert").map(|c| doc.resolve(c)).as_deref() { - Some(Object::Array(a)) => a.iter().filter_map(|c| doc.resolve(c).as_string().map(|s| s.bytes.clone())).collect::>(), + let certs: Vec = match v.get(b"Cert").map(|c| sig_doc.resolve(c)).as_deref() { + Some(Object::Array(a)) => a.iter().filter_map(|c| sig_doc.resolve(c).as_string().map(|s| s.bytes.clone())).collect::>(), Some(Object::String(s)) => vec![s.bytes.clone()], _ => Vec::new(), } @@ -1333,9 +1431,10 @@ fn classify_changes(doc: &Document, old: Option, p: Option) -> Mod let Some(old) = old else { return Modification::Disallowed(vec![kind::UNREADABLE]); }; - let old_content: HashSet = pdfcraft_annot::page_refs(&old) - .unwrap_or_default() - .into_iter() + let old_page_list = pdfcraft_annot::page_refs(&old).unwrap_or_default(); + let old_content: HashSet = old_page_list + .iter() + .copied() .flat_map(|pg| { let c = old.get(pg).as_dict().and_then(|d| d.get(b"Contents").cloned()); match c { @@ -1350,18 +1449,50 @@ fn classify_changes(doc: &Document, old: Option, p: Option) -> Mod .collect(); // The signed revision's document-level XMP stream. let old_metadata = old.root().and_then(|root| old.get(root).as_dict().and_then(|c| c.get(b"Metadata").and_then(Object::as_ref))); + // The signed revision's AcroForm object (if indirect). + let old_acroform = old.root().and_then(|root| old.get(root).as_dict().and_then(|c| c.get(b"AcroForm").and_then(Object::as_ref))); // The signed revision's XFA data packets: filling the form rewrites them. let old_xfa_data = xfa_data_packets(&old); - // What each object was in the signed revision: its pages, and its trailer /Info. A changed - // object is judged by the role it held when signed, never by the labels or keys its new, - // attacker-written version carries (relabelling a signed page /Type /Sig, or dropping its - // /Type and adding /ModDate, must not reclassify it away from page content). - let signed_pages: HashSet = pdfcraft_annot::page_refs(&old).unwrap_or_default().into_iter().collect(); + // What each object was in the signed revision: its pages, its page tree, its signature + // fields and widgets, and its trailer /Info. A changed object is judged by the role it held + // when signed, never by the labels or keys its new, attacker-written version carries + // (relabelling a signed page /Type /Sig, or dropping its /Type and adding /ModDate, must not + // reclassify it away from page content). + let signed_pages: HashSet = old_page_list.iter().copied().collect(); + let signed_page_tree = signed_page_tree(&old, &signed_pages); // The signed pages' /Annots arrays that are objects of their own. Adding an annotation updates // such an array in place (the page dictionary does not change), so it is judged like a // page's inline /Annots: by the annotations it gained. let signed_annots: HashSet = signed_pages.iter().filter_map(|pg| old.get(*pg).as_dict().and_then(|d| d.get(b"Annots").and_then(Object::as_ref))).collect(); + // Signature fields and widgets in the signed revision: those already signed must never be + // altered later, whereas an unsigned signature field or widget in the signed revision may be + // signed later (under P != 1). + let mut signed_sig_widgets: HashSet = HashSet::new(); + let mut signed_sig_fields: HashSet = HashSet::new(); + let mut unsigned_sig_objects: HashSet = HashSet::new(); + for f in sig_fields(&old) { + if let Some(v) = f.signature(&old) { + signed_sig_fields.extend(f.r); + if let Some(vr) = f.dict.get(b"V").and_then(Object::as_ref) { + signed_sig_fields.insert(vr); + } + if let Some(refs) = v.get(b"Reference").map(|o| old.resolve(o)).and_then(|o| o.as_array().cloned()) { + for entry in &refs { + if let Some(er) = entry.as_ref() { + signed_sig_fields.insert(er); + } + if let Some(tr) = old.resolve(entry).as_dict().and_then(|d| d.get(b"TransformParams").and_then(Object::as_ref)) { + signed_sig_fields.insert(tr); + } + } + } + signed_sig_widgets.extend(f.widgets.iter().filter_map(|(r, _)| *r)); + } else { + unsigned_sig_objects.extend(f.r); + unsigned_sig_objects.extend(f.widgets.iter().filter_map(|(r, _)| *r)); + } + } // What each signed page draws from, by the role it held when signed (#1787). let signed_resources = signed_page_resources(&old, &signed_pages); let old_info = old.trailer().get(b"Info").and_then(Object::as_ref); @@ -1398,6 +1529,9 @@ fn classify_changes(doc: &Document, old: Option, p: Option) -> Mod if doc.root() != old.root() { disallowed.push(kind::STRUCTURE); } + if pdfcraft_annot::page_refs(doc).ok().as_ref() != Some(&old_page_list) && !disallowed.contains(&kind::PAGES) { + disallowed.push(kind::PAGES); + } for num in doc.object_numbers() { if same_place(num) { continue; @@ -1430,6 +1564,13 @@ fn classify_changes(doc: &Document, old: Option, p: Option) -> Mod (Some(k), _) => k, // The Document Security Store's own arrays and dictionaries, when they only grew. (_, _) if dss.contains(&num) => kind::SECURITY_STORE, + (_, _) if signed_page_tree.contains(&r) => kind::PAGES, + (_, _) if signed_sig_widgets.contains(&r) => kind::PAGE_CONTENT, + (_, _) if signed_sig_fields.contains(&r) => kind::OTHER, + (_, Object::Dict(d)) if Some(r) == old.root() => catalog_change_kind(doc, &old, d, before.as_deref().and_then(Object::as_dict)), + (_, _) if Some(r) == old.root() => kind::STRUCTURE, + (_, Object::Dict(d)) if signed_pages.contains(&r) => page_change_kind(doc, &old, d, before.as_deref().and_then(Object::as_dict)), + (_, _) if signed_pages.contains(&r) => kind::PAGE_CONTENT, // A signed page's indirect /Annots array, still an array: what it lost, then what it // gained. Dropping a signed annotation is never hidden behind an added signature. (_, Object::Array(now)) if signed_annots.contains(&r) && before.as_deref().is_some_and(|b| b.as_array().is_some()) => { @@ -1450,15 +1591,13 @@ fn classify_changes(doc: &Document, old: Option, p: Option) -> Mod } (_, Object::Dict(d)) => { let was = before.as_deref().and_then(Object::as_dict); - if signed_pages.contains(&r) { - // A signed page is always judged by what the page shows, whatever it is now - // relabelled: a rewritten /Contents or /Resources is a page content change. - page_change_kind(doc, &old, d, was) + if was.is_some() && Some(r) == old_acroform { + acroform_kind(doc, &old, d, was) } else if was.is_some() && Some(r) == old_info { // Only the signed revision's own /Info dictionary is a metadata change. kind::METADATA } else { - change_kind(doc, &old, d, was) + change_kind(doc, &old, d, was, unsigned_sig_objects.contains(&r)) } } (_, Object::Stream(s)) => { @@ -1489,8 +1628,8 @@ fn classify_changes(doc: &Document, old: Option, p: Option) -> Mod _ => kind::OTHER, }; let ok = match (what, p) { - (kind::SIGNATURE | kind::SECURITY_STORE, _) => true, - (kind::FORM_FILL, Some(2 | 3) | None) => true, + (kind::SECURITY_STORE, _) => true, + (kind::SIGNATURE | kind::FORM_FILL, Some(2 | 3) | None) => true, (kind::COMMENTS, Some(3) | None) => true, // Every save updates /Info (ModDate), signing included. (kind::METADATA, _) => true, @@ -1508,12 +1647,23 @@ fn classify_changes(doc: &Document, old: Option, p: Option) -> Mod for num in old.object_numbers().into_iter().filter(|n| !current.contains(n)) { let r = ObjRef { num, generation: old.generation(num) }; let was = old.get(r); - let signed_role = old_content.contains(&r) || signed_pages.contains(&r) || signed_resources.contains_key(&r); + let page_role = old_content.contains(&r) || signed_pages.contains(&r) || signed_resources.contains_key(&r) || signed_sig_widgets.contains(&r); + let struct_role = Some(r) == old.root(); + let pages_role = signed_page_tree.contains(&r); + let signed_role = page_role || struct_role || pages_role; // A signed page's content or resource is judged by that role, whatever /Type its stream carried. if !signed_role && matches!(&*was, Object::Stream(s) if matches!(s.dict.name(b"Type"), Some(b"XRef" | b"ObjStm"))) { continue; } - let what = if signed_role { kind::PAGE_CONTENT } else { kind::OTHER }; + let what = if struct_role { + kind::STRUCTURE + } else if pages_role { + kind::PAGES + } else if page_role { + kind::PAGE_CONTENT + } else { + kind::OTHER + }; if !disallowed.contains(&what) { disallowed.push(what); } @@ -1521,6 +1671,47 @@ fn classify_changes(doc: &Document, old: Option, p: Option) -> Mod if disallowed.is_empty() { Modification::Allowed(allowed) } else { Modification::Disallowed(disallowed) } } +/// The page-tree nodes (`/Pages` dictionaries and indirect `/Kids` arrays) of the signed revision, +/// reached from the signed catalog's `/Pages` entry or from any signed page's `/Parent` chain. +fn signed_page_tree(old: &Document, pages: &HashSet) -> HashSet { + const LIMIT: usize = 50_000; + let mut tree = HashSet::new(); + let mut stack = Vec::new(); + if let Some(root_pages) = old.root().and_then(|r| old.get(r).as_dict().and_then(|c| c.get(b"Pages").and_then(Object::as_ref))) { + stack.push(root_pages); + } + for pg in pages { + if let Some(parent) = old.get(*pg).as_dict().and_then(|d| d.get(b"Parent").and_then(Object::as_ref)) { + stack.push(parent); + } + } + while let Some(r) = stack.pop() { + if pages.contains(&r) || Some(r) == old.root() || tree.len() >= LIMIT || !tree.insert(r) { + continue; + } + if let Some(d) = old.get(r).as_dict() { + if let Some(parent) = d.get(b"Parent").and_then(Object::as_ref) { + stack.push(parent); + } + if let Some(kids) = d.get(b"Kids") { + if let Some(kr) = kids.as_ref() { + tree.insert(kr); + } + if let Some(arr) = old.resolve(kids).as_array() { + for kid in arr { + if let Some(kr) = kid.as_ref() + && !pages.contains(&kr) + { + stack.push(kr); + } + } + } + } + } + } + tree +} + /// The streams of `doc`'s XFA form that hold its data and state: the `datasets` and `form` /// packets of the AcroForm's `/XFA` array (name, stream, name, stream, …). A form given as one /// XFA stream holds its template too, and is not listed. @@ -1702,7 +1893,11 @@ fn acroform_kind(doc: &Document, old: &Document, now: &Dict, before: Option<&Dic return kind::FORM_FILL; } let old_fields = refs(old, before.get(b"Fields")); - let added: Vec = refs(doc, now.get(b"Fields")).into_iter().filter(|r| !old_fields.contains(r)).collect(); + let now_fields = refs(doc, now.get(b"Fields")); + if old_fields.iter().any(|r| !now_fields.contains(r)) { + return kind::OTHER; + } + let added: Vec = now_fields.into_iter().filter(|r| !old_fields.contains(r)).collect(); if added.is_empty() || added_annots(doc, &added) == kind::SIGNATURE { kind::SIGNATURE } else { kind::FORM_FILL } } @@ -1713,11 +1908,18 @@ fn without(x: &Dict, keys: &[&[u8]]) -> Vec<(Vec, Object)> { v } -/// Whether a dictionary was a signature object: a signature or timestamp value, a signature -/// reference or its transform parameters, or a signature form field. Used to decide from the -/// signed revision, so a later relabel of some other object cannot pose as a signature change. -fn is_signature_dict(d: &Dict) -> bool { - matches!(d.name(b"Type"), Some(b"Sig" | b"DocTimeStamp" | b"SigRef" | b"TransformParams")) || d.name(b"FT") == Some(b"Sig") +/// Whether `b` in the signed revision was a signature dictionary, a timestamp, a signature +/// reference, or transform parameters — an immutable signature object once signed. +fn is_signed_sig_value_or_ref(b: &Dict) -> bool { + matches!(b.name(b"Type"), Some(b"Sig" | b"DocTimeStamp" | b"SigRef" | b"TransformParams")) + || b.contains(b"ByteRange") + || b.contains(b"TransformMethod") +} + +/// Whether `b` in the signed revision was an unsigned signature form field (`/FT /Sig` without a +/// signature dictionary in `/V`). +fn is_unsigned_sig_dict(old: &Document, b: &Dict) -> bool { + b.name(b"FT") == Some(b"Sig") && !is_signed_sig_value_or_ref(b) && !b.get(b"V").is_some_and(|v| old.resolve(v).as_dict().is_some()) } /// The objects the signed pages draw from, by reference, with the role each held when signed @@ -1802,6 +2004,31 @@ fn signed_page_resources(old: &Document, pages: &HashSet) -> HashMap) -> &'static str { + let keys: &[&[u8]] = &[b"AcroForm", b"DSS", b"Perms", b"Metadata", b"NeedsRendering", b"Extensions"]; + match before { + Some(b) if without(d, keys) != without(b, keys) => kind::STRUCTURE, + // Developer extensions (ISO 32000-2 §7.12) only declare what the file uses. A + // writer adding a /DSS also declares the ESIC extension it belongs to, so a + // declaration may be added, but none that was signed may change or go. + Some(b) if !extensions_kept(doc, old, d, b) => kind::STRUCTURE, + Some(b) => { + if d.get(b"AcroForm") != b.get(b"AcroForm") { + // Compare the forms themselves, inline or not. + let now = acroform(doc).unwrap_or_default(); + acroform_kind(doc, old, &now, None) + } else if b.get(b"Perms") != d.get(b"Perms") { + kind::SIGNATURE + } else { + kind::METADATA + } + } + None => kind::STRUCTURE, + } +} + /// A changed object that was a page in the signed revision, judged by what the page shows rather /// than by any `/Type` its new version carries. Only added annotations are permitted; any other /// difference (content, resources, a relabelled or dropped `/Type`) is a page content change. @@ -1810,8 +2037,16 @@ fn page_change_kind(doc: &Document, old: &Document, d: &Dict, before: Option<&Di Some(b) if without(d, &[b"Annots"]) != without(b, &[b"Annots"]) => kind::PAGE_CONTENT, Some(b) => { let was = refs(old, b.get(b"Annots")); - let added: Vec = refs(doc, d.get(b"Annots")).into_iter().filter(|r| !was.contains(r)).collect(); - added_annots(doc, &added) + let now = refs(doc, d.get(b"Annots")); + let removed: Vec = was.iter().filter(|r| !now.contains(r)).copied().collect(); + if removed.is_empty() { + let added: Vec = now.into_iter().filter(|r| !was.contains(r)).collect(); + added_annots(doc, &added) + } else if removed.iter().any(|r| old.get(*r).as_dict().is_some_and(|d| d.name(b"Subtype") == Some(b"Widget"))) { + kind::OTHER + } else { + kind::COMMENTS + } } None => kind::PAGE_CONTENT, } @@ -1829,59 +2064,74 @@ fn extensions_kept(doc: &Document, old: &Document, d: &Dict, b: &Dict) -> bool { } } -fn change_kind(doc: &Document, old: &Document, d: &Dict, before: Option<&Dict>) -> &'static str { +fn change_kind(doc: &Document, old: &Document, d: &Dict, before: Option<&Dict>, was_unsigned_sig: bool) -> &'static str { let ty = d.name(b"Type"); - // A later revision cannot make an existing object a signature by relabelling it: a signature - // change is only a brand-new object, or one that was already a signature object when signed. - let signature_ok = before.is_none_or(is_signature_dict); - // A signature widget that already held a signature is what the signature shows: moving it or - // swapping its appearance can hide the signed page under it. Judged by the signed revision, - // so relabelling the widget now does not change that. - if before.is_some_and(|b| { - b.name(b"Subtype") == Some(b"Widget") && b.name(b"FT") == Some(b"Sig") && b.get(b"V").is_some_and(|v| old.resolve(v).as_dict().is_some()) - }) && before != Some(d) - { - return kind::PAGE_CONTENT; + // A signature widget or field that already held a signature when signed, or a signed + // signature/timestamp/SigRef/TransformParams dictionary, must never be rewritten later. + if let Some(b) = before { + if b.name(b"Subtype") == Some(b"Widget") + && b.name(b"FT") == Some(b"Sig") + && b.get(b"V").is_some_and(|v| old.resolve(v).as_dict().is_some()) + && before != Some(d) + { + return kind::PAGE_CONTENT; + } + if is_signed_sig_value_or_ref(b) || (b.name(b"FT") == Some(b"Sig") && b.get(b"V").is_some_and(|v| old.resolve(v).as_dict().is_some())) { + return kind::OTHER; + } + if b.name(b"Type") == Some(b"Pages") { + return kind::PAGES; + } + if b.name(b"Type") == Some(b"Catalog") { + return catalog_change_kind(doc, old, d, Some(b)); + } + if b.name(b"Type") == Some(b"Page") { + return page_change_kind(doc, old, d, Some(b)); + } } + // A later revision cannot make an existing object a signature by relabelling it: a signature + // change is only a brand-new object, or one that was an unsigned signature field/widget when signed. + let signature_ok = before.is_none_or(|b| was_unsigned_sig || is_unsigned_sig_dict(old, b)); match ty { // Only a new signature value or policy: rewriting a signed one is not a signature (#1788). Some(b"Sig" | b"DocTimeStamp" | b"SigRef" | b"TransformParams") if before.is_none() => return kind::SIGNATURE, - Some(b"Catalog") => { - let keys: &[&[u8]] = &[b"AcroForm", b"DSS", b"Perms", b"Metadata", b"NeedsRendering", b"Extensions"]; - return match before { - Some(b) if without(d, keys) != without(b, keys) => kind::STRUCTURE, - // Developer extensions (ISO 32000-2 §7.12) only declare what the file uses. A - // writer adding a /DSS also declares the ESIC extension it belongs to, so a - // declaration may be added, but none that was signed may change or go. - Some(b) if !extensions_kept(doc, old, d, b) => kind::STRUCTURE, - Some(b) => { - if d.get(b"AcroForm") != b.get(b"AcroForm") { - // Compare the forms themselves, inline or not. - let now = acroform(doc).unwrap_or_default(); - acroform_kind(doc, old, &now, None) - } else if b.get(b"Perms") != d.get(b"Perms") { - kind::SIGNATURE - } else { - kind::METADATA - } - } - None => kind::FORM_FILL, - }; - } - Some(b"Page") => { - return match before { - Some(b) if without(d, &[b"Annots"]) != without(b, &[b"Annots"]) => kind::PAGE_CONTENT, - Some(b) => { - let was = refs(old, b.get(b"Annots")); - let added: Vec = refs(doc, d.get(b"Annots")).into_iter().filter(|r| !was.contains(r)).collect(); - added_annots(doc, &added) - } - None => kind::COMMENTS, - }; - } + Some(b"Sig" | b"DocTimeStamp" | b"SigRef" | b"TransformParams") => return kind::OTHER, + Some(b"Catalog") => return if before.is_some() { catalog_change_kind(doc, old, d, before) } else { kind::STRUCTURE }, + Some(b"Page") => return if before.is_some() { page_change_kind(doc, old, d, before) } else { kind::COMMENTS }, Some(b"Pages") => return if before.is_some() { kind::PAGES } else { kind::STRUCTURE }, _ => {} } + if let Some(b) = before { + // An existing dictionary is judged by the role it already held in the signed revision: + // dropping `/Type` or adding `/FT`, `/Subtype`, `/Fields`, or `/DR` later cannot turn + // an arbitrary signed dictionary into a form fill or comment annotation. + if b.name(b"Type").is_some() && d.name(b"Type") != b.name(b"Type") { + return kind::OTHER; + } + match b.name(b"Subtype") { + Some(b"Widget") if d.name(b"Subtype") == Some(b"Widget") => { + return if d.name(b"FT") == Some(b"Sig") && signature_ok { kind::SIGNATURE } else { kind::FORM_FILL }; + } + Some(b"Link") if d.name(b"Subtype") == Some(b"Link") => return kind::LINKS, + Some(st) if d.name(b"Subtype") == Some(st) && (b.name(b"Type") == Some(b"Annot") || b.contains(b"Rect")) => { + return kind::COMMENTS; + } + _ => {} + } + if d.name(b"FT") == Some(b"Sig") && signature_ok { + return kind::SIGNATURE; + } + if b.contains(b"Fields") && d.contains(b"Fields") { + return acroform_kind(doc, old, d, Some(b)); + } + if (b.contains(b"FT") && d.name(b"FT") == b.name(b"FT")) + || (b.contains(b"T") && b.contains(b"Kids") && d.contains(b"T") && d.contains(b"Kids")) + || (b.contains(b"DR") && d.contains(b"DR")) + { + return kind::FORM_FILL; + } + return kind::OTHER; + } match d.name(b"Subtype") { Some(b"Widget") => return if d.name(b"FT") == Some(b"Sig") && signature_ok { kind::SIGNATURE } else { kind::FORM_FILL }, Some(b"Link") => return kind::LINKS, @@ -1897,11 +2147,7 @@ fn change_kind(doc: &Document, old: &Document, d: &Dict, before: Option<&Dict>) if d.contains(b"FT") || (d.contains(b"T") && d.contains(b"Kids")) || d.contains(b"DR") { return kind::FORM_FILL; } - if before.is_none() { - // New objects referenced by changed ones (fonts, appearance resources). - return kind::FORM_FILL; - } - kind::OTHER + kind::FORM_FILL } // ── signing ───────────────────────────────────────────────────────────────────────────────── diff --git a/crates/sign/tests/it/pdf.rs b/crates/sign/tests/it/pdf.rs index 7234e782ed..5daffee7a2 100644 --- a/crates/sign/tests/it/pdf.rs +++ b/crates/sign/tests/it/pdf.rs @@ -2931,3 +2931,127 @@ fn signing_a_field_with_a_null_value_after_another_signature_keeps_the_first_val all.iter().map(|s| (&s.status, &s.modification)).collect::>() ); } + +/// The signed catalog and page-tree nodes keep their structural roles even if a later revision +/// drops `/Type /Catalog` or `/Type /Pages` and adds form keys (`/FT /Tx`, `/DR`) while swapping +/// the page tree (#3998). +#[test] +fn signed_catalog_and_page_tree_keep_their_role_when_relabelled_or_dropped() { + use pdfcraft_cos::{ObjRef, Stream}; + // Rewriting the signed catalog without `/Type /Catalog` while pointing `/Pages` at a new page tree + // and adding `/FT /Tx` is refused as a structure change at every DocMDP level. + assert_refused_at_every_level(&fixture(), kind::STRUCTURE, |doc| { + let new_content = doc.add(Object::Stream(Stream::from_raw(Dict::new(), b"BT /F1 14 Tf 20 250 Td (Forged) Tj ET".to_vec()))); + let new_page = doc.add(Object::Dict(dict_of(vec![ + ("Type", Object::name("Page")), + ("Parent", oref(2)), + ("MediaBox", Object::Array(vec![Object::Int(0), Object::Int(0), Object::Int(300), Object::Int(300)])), + ("Contents", Object::Ref(new_content)), + ]))); + let new_pages = doc.add(Object::Dict(dict_of(vec![ + ("Type", Object::name("Pages")), + ("Kids", Object::Array(vec![Object::Ref(new_page)])), + ("Count", Object::Int(1)), + ]))); + let af = doc.get(ObjRef { num: 1, generation: 0 }).as_dict().and_then(|c| c.get(b"AcroForm").cloned()).unwrap(); + doc.set( + ObjRef { num: 1, generation: 0 }, + Object::Dict(dict_of(vec![("Pages", Object::Ref(new_pages)), ("AcroForm", af), ("FT", Object::name("Tx"))])), + ); + }); + + // Rewriting the signed `/Pages` dictionary without `/Type /Pages` while replacing `/Kids` and + // adding `/FT /Tx` is refused as a page-tree change (`kind::PAGES`) at every DocMDP level. + assert_refused_at_every_level(&fixture(), kind::PAGES, |doc| { + let new_content = doc.add(Object::Stream(Stream::from_raw(Dict::new(), b"BT /F1 14 Tf 20 250 Td (Forged) Tj ET".to_vec()))); + let new_page = doc.add(Object::Dict(dict_of(vec![ + ("Type", Object::name("Page")), + ("Parent", oref(2)), + ("MediaBox", Object::Array(vec![Object::Int(0), Object::Int(0), Object::Int(300), Object::Int(300)])), + ("Contents", Object::Ref(new_content)), + ]))); + doc.set( + ObjRef { num: 2, generation: 0 }, + Object::Dict(dict_of(vec![ + ("Kids", Object::Array(vec![Object::Ref(new_page), oref(3)])), + ("Count", Object::Int(2)), + ("FT", Object::name("Tx")), + ])), + ); + }); +} + +/// Signature dictionaries (`/ByteRange`, `/Contents`, `/Reference`) are read from the revision +/// covered by the signature, and rewriting a signed signature object or attaching a signature in a +/// later revision whose `/ByteRange` only covers an earlier revision is refused (#3998). +#[test] +fn signed_signature_objects_and_byte_ranges_are_read_from_the_signed_revision() { + let id = pkcs12::open(&data("ec-p256.p12"), "test").unwrap(); + let trust = TrustStore { certs: vec![id.certificate.clone()] }; + let signed = pdfcraft_sign::sign(&open(&fixture()), &id, &SignOptions { certify: Some(1), ..opts() }).unwrap(); + let sig_ref = open(&signed) + .object_numbers() + .into_iter() + .map(|num| pdfcraft_cos::ObjRef { num, generation: 0 }) + .find(|r| open(&signed).get(*r).as_dict().is_some_and(|d| d.name(b"Type") == Some(b"Sig"))) + .unwrap(); + + // Rewriting the signed `/Type /Sig` dictionary in a later revision (trying to relax `/P` from + // 1 to 3 and adding `/FT /Sig` so the rewrite looks like a signature field) keeps `certify` as + // `Some(1)` from the signed revision and invalidates the signature. + let relaxed = edit_after(&signed, |doc| { + doc.update_dict(sig_ref, |d| { + d.remove(b"Type"); + d.set(b"FT".to_vec(), Object::name("Sig")); + d.set( + b"Reference".to_vec(), + Object::Array(vec![Object::Dict(dict_of(vec![ + ("Type", Object::name("SigRef")), + ("TransformMethod", Object::name("DocMDP")), + ( + "TransformParams", + Object::Dict(dict_of(vec![("Type", Object::name("TransformParams")), ("P", Object::Int(3)), ("V", Object::name("1.2"))])), + ), + ]))]), + ); + }) + .unwrap(); + }); + let s = signatures(&open(&relaxed), &relaxed, &trust).into_iter().find(|s| s.signed).unwrap(); + assert_eq!(s.certify, Some(1), "DocMDP level must come from the signed revision"); + assert_eq!(s.status, Status::Invalid, "{:?}", s.modification); + assert!(matches!(&s.modification, Modification::Disallowed(k) if k.contains(&kind::OTHER)), "{:?}", s.modification); + + // A signature object added in a later revision whose `/ByteRange` only covers the earlier + // revision (where that signature field did not hold that signature) is refused. + let sig_dict = open(&signed).get(sig_ref).as_dict().cloned().unwrap(); + let replayed = edit_after(&signed, |doc| { + let replayed_v = doc.add(Object::Dict(sig_dict.clone())); + doc.update_dict(pdfcraft_cos::ObjRef { num: 6, generation: 0 }, |f| { + f.set(b"V".to_vec(), Object::Ref(replayed_v)); + }) + .unwrap(); + }); + let all = signatures(&open(&replayed), &replayed, &trust); + let approval = all.iter().find(|s| s.field == "Approval").unwrap(); + assert_eq!(approval.status, Status::Invalid, "a later signature whose ByteRange only covers an earlier revision must be invalid: {approval:?}"); +} + +/// Under DocMDP `P = 1` (no changes permitted), adding a signature field or counter-signing an +/// existing unsigned signature field invalidates the certifying signature (#3998). +#[test] +fn certify_level_one_forbids_adding_signature_fields_or_counter_signing() { + let id = pkcs12::open(&data("ec-p256.p12"), "test").unwrap(); + let trust = TrustStore { certs: vec![id.certificate.clone()] }; + let certified_p1 = pdfcraft_sign::sign(&open(&fixture()), &id, &SignOptions { certify: Some(1), ..opts() }).unwrap(); + let counter_p1 = pdfcraft_sign::sign(&open(&certified_p1), &id, &SignOptions { field: Some("Approval".into()), ..opts() }).unwrap(); + let first_p1 = signatures(&open(&counter_p1), &counter_p1, &trust).into_iter().find(|s| s.field == "Signature1").unwrap(); + assert_eq!(first_p1.status, Status::Invalid, "{:?}", first_p1.modification); + assert_eq!(first_p1.modification, Modification::Disallowed(vec![kind::SIGNATURE])); + + // Under P = 2 (filling forms and signing allowed), the same counter-signature is permitted. + let certified_p2 = pdfcraft_sign::sign(&open(&fixture()), &id, &SignOptions { certify: Some(2), ..opts() }).unwrap(); + let counter_p2 = pdfcraft_sign::sign(&open(&certified_p2), &id, &SignOptions { field: Some("Approval".into()), ..opts() }).unwrap(); + let first_p2 = signatures(&open(&counter_p2), &counter_p2, &trust).into_iter().find(|s| s.field == "Signature1").unwrap(); + assert_eq!(first_p2.status, Status::Valid, "{:?}", first_p2.modification); +} From 9ed2bbc98d99953b7d21760e78316fed7dd01b33 Mon Sep 17 00:00:00 2001 From: jonathanwxh-cell <258340642+jonathanwxh-cell@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:44:00 +0000 Subject: [PATCH 2/3] fix(sign): match a signature by object and treat NeedsRendering as structure A later widget that reuses a signed field's name was reported as that signature. Changing /NeedsRendering is a structure change at every certification level. --- crates/sign/src/pdf.rs | 17 +++++++++-- crates/sign/tests/it/pdf.rs | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/crates/sign/src/pdf.rs b/crates/sign/src/pdf.rs index 3ee69e474c..a845035e53 100644 --- a/crates/sign/src/pdf.rs +++ b/crates/sign/src/pdf.rs @@ -725,8 +725,18 @@ fn dict_contents(doc: &Document, d: &Dict) -> Option> { fn find_sig_in_revision(rev: &Document, field: Option<&Field>, stamp_ref: Option) -> Option { if let Some(f) = field { - let matched = sig_fields(rev).into_iter().find(|of| (f.r.is_some() && of.r == f.r) || of.name == f.name)?; - matched.signature(rev) + // A later field that reuses the name is a different field. Match the object when we + // have one, and the name only when neither side has an object. Two candidates is not + // a match: the signature must not be reported on an attacker-chosen widget. + let mut matched = sig_fields(rev).into_iter().filter(|of| match f.r { + Some(want) => of.r == Some(want), + None => of.r.is_none() && of.name == f.name, + }); + let found = matched.next()?; + if matched.next().is_some() { + return None; + } + found.signature(rev) } else if let Some(r) = stamp_ref { rev.try_get(r.num).ok().and_then(|o| o.as_dict().cloned()).filter(is_doc_timestamp) } else { @@ -2007,7 +2017,8 @@ fn signed_page_resources(old: &Document, pages: &HashSet) -> HashMap) -> &'static str { - let keys: &[&[u8]] = &[b"AcroForm", b"DSS", b"Perms", b"Metadata", b"NeedsRendering", b"Extensions"]; + // /NeedsRendering selects dynamic XFA rendering. A change of it is structure, not metadata. + let keys: &[&[u8]] = &[b"AcroForm", b"DSS", b"Perms", b"Metadata", b"Extensions"]; match before { Some(b) if without(d, keys) != without(b, keys) => kind::STRUCTURE, // Developer extensions (ISO 32000-2 §7.12) only declare what the file uses. A diff --git a/crates/sign/tests/it/pdf.rs b/crates/sign/tests/it/pdf.rs index 5daffee7a2..78a041842f 100644 --- a/crates/sign/tests/it/pdf.rs +++ b/crates/sign/tests/it/pdf.rs @@ -1135,6 +1135,65 @@ fn xfa_page_swap(doc: &mut Document) { .unwrap(); } +/// A second widget that reuses the signed field's name and `/V` is not that signature. +#[test] +fn a_later_field_with_the_same_name_does_not_inherit_the_signature() { + let id = pkcs12::open(&data("rsa-aes.p12"), "test").unwrap(); + let trust = TrustStore { certs: vec![id.certificate.clone()] }; + let signed = pdfcraft_sign::sign(&open(&fixture()), &id, &SignOptions { field: Some("Approval".into()), ..opts() }).unwrap(); + let edited = edit_after(&signed, |doc| { + let root = doc.root().unwrap(); + let form = doc.get(root).as_dict().and_then(|c| c.get(b"AcroForm")).map(|a| doc.resolve(a)).and_then(|a| a.as_dict().cloned()).unwrap(); + let fields = form.get(b"Fields").map(|f| doc.resolve(f)).and_then(|f| f.as_array().cloned()).unwrap(); + let original = fields.iter().find_map(Object::as_ref).unwrap(); + let value = doc.get(original).as_dict().unwrap().get(b"V").cloned().expect("the signed field has /V"); + let mut widget = pdfcraft_cos::Dict::new(); + widget.set(b"Type".to_vec(), Object::name("Annot")); + widget.set(b"Subtype".to_vec(), Object::name("Widget")); + widget.set(b"FT".to_vec(), Object::name("Sig")); + widget.set(b"T".to_vec(), PdfString::text("Approval")); + widget.set(b"Rect".to_vec(), Object::Array(vec![Object::Int(10), Object::Int(10), Object::Int(80), Object::Int(40)])); + widget.set(b"P".to_vec(), Object::Ref(pdfcraft_cos::ObjRef { num: 3, generation: 0 })); + widget.set(b"V".to_vec(), value); + let planted = doc.add(Object::Dict(widget)); + doc.update_dict(root, |c| { + if let Some(Object::Dict(form)) = c.get_mut(b"AcroForm") + && let Some(Object::Array(list)) = form.get_mut(b"Fields") + { + list.push(Object::Ref(planted)); + } + }) + .unwrap(); + doc.update_dict(pdfcraft_cos::ObjRef { num: 3, generation: 0 }, |p| { + if let Some(Object::Array(annots)) = p.get_mut(b"Annots") { + annots.push(Object::Ref(planted)); + } + }) + .unwrap(); + }); + let listed = signatures(&open(&edited), &edited, &trust); + let original = listed.iter().find(|s| s.rect == Some([150.0, 20.0, 280.0, 70.0])).expect("original field"); + let planted = listed.iter().find(|s| s.rect == Some([10.0, 10.0, 80.0, 40.0])).expect("planted field"); + assert_eq!(original.status, Status::Valid, "{:?}", original.details); + assert_ne!(planted.status, Status::Valid, "the planted widget inherited the signature: {:?}", planted.details); +} + +#[test] +fn toggling_needs_rendering_is_a_structure_change() { + let id = pkcs12::open(&data("rsa-aes.p12"), "test").unwrap(); + let trust = TrustStore { certs: vec![id.certificate.clone()] }; + let check = |bytes: &[u8]| signatures(&open(bytes), bytes, &trust).into_iter().find(|s| s.signed).unwrap(); + let certified = pdfcraft_sign::sign(&open(&fixture()), &id, &SignOptions { certify: Some(1), ..opts() }).unwrap(); + assert_eq!(check(&certified).status, Status::Valid); + let edited = edit_after(&certified, |doc| { + let root = doc.root().unwrap(); + doc.update_dict(root, |c| c.set(b"NeedsRendering".to_vec(), Object::Bool(true))).unwrap(); + }); + let s = check(&edited); + assert_eq!(s.status, Status::Invalid, "{:?}", s.details); + assert!(matches!(&s.modification, Modification::Disallowed(k) if k.contains(&kind::STRUCTURE)), "{:?}", s.modification); +} + #[test] fn declaring_a_form_xfa_does_not_excuse_replacing_the_pages() { let id = pkcs12::open(&data("rsa-aes.p12"), "test").unwrap(); From 7e8fb1fd6c689dba80c3c373b34220b5d2ae948a Mon Sep 17 00:00:00 2001 From: jonathanwxh-cell Date: Sat, 31 Oct 2026 17:09:04 +0800 Subject: [PATCH 3/3] test(sign): cover relabelled signed action dict under DocMDP --- crates/sign/tests/it/pdf.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/crates/sign/tests/it/pdf.rs b/crates/sign/tests/it/pdf.rs index 811eb3498c..30135148b7 100644 --- a/crates/sign/tests/it/pdf.rs +++ b/crates/sign/tests/it/pdf.rs @@ -3061,6 +3061,26 @@ fn signed_catalog_and_page_tree_keep_their_role_when_relabelled_or_dropped() { ])), ); }); + + // Rewriting a signed non-form dictionary (such as an `/Action` dictionary) by adding `/FT /Tx` + // or `/DR` in a later revision is classified as `kind::OTHER` rather than `kind::FORM_FILL`, + // and is refused even under DocMDP `P = 2` (and `P = 3`). + let with_action = fixture_with( + b"<< /Type /Catalog /Pages 2 0 R /OpenAction 7 0 R /AcroForm << /Fields [6 0 R] >> >>", + &[b"<< /Type /Action /S /URI /URI (https://example.test/signed) >>"], + ); + assert_refused_at_every_level(&with_action, kind::OTHER, |doc| { + doc.set( + ObjRef { num: 7, generation: 0 }, + Object::Dict(dict_of(vec![ + ("Type", Object::name("Action")), + ("S", Object::name("URI")), + ("URI", Object::String(PdfString::text("https://attacker.invalid/phish"))), + ("FT", Object::name("Tx")), + ("DR", Object::Dict(Dict::new())), + ])), + ); + }); } /// Signature dictionaries (`/ByteRange`, `/Contents`, `/Reference`) are read from the revision