PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(redact,sign): address review — pop-up replies, signed-role deletions, widget role from signed revision
Dylan M. Taylor39ee93fe70d7
diff --git a/crates/redact/src/lib.rs b/crates/redact/src/lib.rsindex 81b975a0c..29f23d3c9 100644--- a/crates/redact/src/lib.rs+++ b/crates/redact/src/lib.rs@@ -566,6 +566,13 @@ fn apply_to(doc: &mut Document, pages: Option<&[usize]>) -> Result<Report, Redac}}removed_everywhere.extend(removed.iter().copied());+ // Pop-ups dropped with their parent below count too: a reply on another page may answer one.+ for a in &annots {+ let Some(r) = a.as_ref() else { continue };+ if doc.get(r).as_dict().and_then(|d| d.get(b"Parent")).and_then(Object::as_ref).is_some_and(|p| removed.contains(&p)) {+ removed_everywhere.insert(r);+ }+ }let kept: Vec<Object> = annots.into_iter().filter(|a| {diff --git a/crates/redact/src/tests.rs b/crates/redact/src/tests.rsindex 1ccfeff39..b95237710 100644--- a/crates/redact/src/tests.rs+++ b/crates/redact/src/tests.rs@@ -2821,3 +2821,26 @@ fn hidden_text_removal_drops_the_source_text_of_added_content() {let (list, _) = page_streams(&doc, &p.dict, 0).unwrap();assert!(list.iter().all(|o| doc.resolve(o).as_dict().is_some_and(|d| !d.contains(b"PCAdded"))));}++#[test]+fn a_reply_on_another_page_to_a_removed_popup_goes_too() {+ let objs: Vec<Vec<u8>> = vec![+ b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(),+ b"<< /Type /Pages /Kids [3 0 R 4 0 R] /Count 2 >>".to_vec(),+ b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] /Contents 5 0 R /Annots [6 0 R 8 0 R] >>".to_vec(),+ b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] /Contents 5 0 R /Annots [7 0 R] >>".to_vec(),+ stream("", b"BT ET"),+ b"<< /Type /Annot /Subtype /Text /Rect [20 20 40 40] /Contents (PARENT-NOTE) /Popup 8 0 R >>".to_vec(),+ b"<< /Type /Annot /Subtype /Text /Rect [250 250 270 270] /IRT 8 0 R /Contents (POPUP-REPLY) >>".to_vec(),+ b"<< /Type /Annot /Subtype /Popup /Rect [200 150 290 190] /Parent 6 0 R /Contents (POPUP-TEXT) >>".to_vec(),+ ];+ let mut doc = pdf(objs);+ mark(&mut doc, 0, &[[10.0, 10.0, 70.0, 50.0]], "");+ apply(&mut doc, None).unwrap();+ let second = pdfcraft_model::pages(&doc).swap_remove(1);+ assert!(annots_of(&doc, &second.dict).is_empty());+ let bytes = write_full(&doc, &SaveOptions { object_streams: false, ..SaveOptions::default() }).unwrap();+ for gone in [&b"POPUP-REPLY"[..], b"POPUP-TEXT", b"PARENT-NOTE"] {+ assert!(!bytes.windows(gone.len()).any(|w| w == gone), "{} survived", String::from_utf8_lossy(gone));+ }+}diff --git a/crates/sign/src/pdf.rs b/crates/sign/src/pdf.rsindex 6df73927c..c53e08917 100644--- a/crates/sign/src/pdf.rs+++ b/crates/sign/src/pdf.rs@@ -1471,11 +1471,12 @@ fn classify_changes(doc: &Document, old: Option<Document>, p: Option<u8>) -> Modfor 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);- if matches!(&*was, Object::Stream(s) if matches!(s.dict.name(b"Type"), Some(b"XRef" | b"ObjStm"))) {+ let signed_role = old_content.contains(&r) || signed_pages.contains(&r) || signed_resources.contains_key(&r);+ // 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 old_content.contains(&r) || signed_pages.contains(&r) || signed_resources.contains_key(&r) { kind::PAGE_CONTENT } else { kind::OTHER };+ let what = if signed_role { kind::PAGE_CONTENT } else { kind::OTHER };if !disallowed.contains(&what) {disallowed.push(what);}@@ -1784,6 +1785,12 @@ fn change_kind(doc: &Document, old: &Document, d: &Dict, before: Option<&Dict>)// 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.contains(b"V") && b.name(b"FT") == Some(b"Sig")) && before != Some(d) {+ return kind::PAGE_CONTENT;+ }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,@@ -1820,11 +1827,6 @@ fn change_kind(doc: &Document, old: &Document, d: &Dict, before: Option<&Dict>)_ => {}}match d.name(b"Subtype") {- // 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.- Some(b"Widget") if before.is_some_and(|b| b.contains(b"V") && b.name(b"FT") == Some(b"Sig")) && before != Some(d) => {- return kind::PAGE_CONTENT;- }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,Some(_) if ty == Some(b"Annot") || d.contains(b"Rect") => return kind::COMMENTS,