PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(xfdf): never let a same-named imported comment replace a form widget
aionggf0e3880c3532
diff --git a/crates/xfdf/src/lib.rs b/crates/xfdf/src/lib.rsindex ab258990e..1e037b679 100644--- a/crates/xfdf/src/lib.rs+++ b/crates/xfdf/src/lib.rs@@ -1017,10 +1017,11 @@ const REDACT_UNSEEN: i64 = 1 | 2 | 32;/// `/F` Locked (ISO 32000-2 Table 167): the user protected this annotation from change.const LOCKED: i64 = 128;-/// An existing annotation an import must never replace, move or unlock: one the user locked, or a-/// redaction mark (replacing it would drop the region the user marked to remove).+/// An existing annotation an import must never replace, move or unlock: one the user locked, a+/// redaction mark (replacing it would drop the region the user marked to remove), or a form+/// widget or field (replacing it would set the field's value and policy around the form setter).fn import_protected(x: &Dict) -> bool {- x.int(b"F").unwrap_or(0) & LOCKED != 0 || x.name(b"Subtype") == Some(b"Redact".as_slice())+ x.int(b"F").unwrap_or(0) & LOCKED != 0 || matches!(x.name(b"Subtype"), Some(b"Redact" | b"Widget")) || x.contains(b"FT")}fn place(doc: &mut Document, page_ref: ObjRef, mut d: Dict) -> Result<ObjRef, DataError> {@@ -1042,8 +1043,8 @@ fn place(doc: &mut Document, page_ref: ObjRef, mut d: Dict) -> Result<ObjRef, Da.unwrap_or_default();let mut kept = Vec::new();let mut removed: Vec<ObjRef> = Vec::new();- // Never let a same-name import replace a locked comment or a redaction mark: keep it, and add- // the imported dict beside it under no name of its own.+ // Never let a same-name import replace a protected annotation (`import_protected`): keep it,+ // and add the imported dict beside it under no name of its own.let mut clash = false;for a in annots {let existing = a.as_ref().and_then(|r| doc.get(r).as_dict().cloned());diff --git a/crates/xfdf/src/tests.rs b/crates/xfdf/src/tests.rsindex 6c9824a24..57155e4b7 100644--- a/crates/xfdf/src/tests.rs+++ b/crates/xfdf/src/tests.rs@@ -1660,3 +1660,22 @@ fn tabular_import_keeps_tsv_quotes_literal_and_csv_value_tabs() {import(&mut doc, b"Name,Other\r\n\"Ada\tLovelace\",other field\r\n").unwrap();assert_eq!(fields(&doc).into_iter().find(|f| f.name == "Name").unwrap().value, vec!["Ada\tLovelace"]);}++#[test]+fn a_same_named_comment_import_never_replaces_a_form_widget() {+ let mut doc = blank();+ let page = pdfcraft_model::pages(&doc)[0].clone();+ let widget = doc+ .resolve(page.dict.get(b"Annots").unwrap())+ .as_array()+ .unwrap()+ .iter()+ .filter_map(Object::as_ref)+ .find(|r| doc.get(*r).as_dict().is_some_and(|d| d.name(b"Subtype") == Some(b"Widget".as_slice())))+ .unwrap();+ doc.update_dict(widget, |d| d.set(b"NM".to_vec(), PdfString::text("w1"))).unwrap();+ let before = doc.get(widget);+ let fdf = b"%FDF-1.2\n1 0 obj\n<< /FDF << /Annots [<< /Subtype /Text /NM (w1) /Page 0 /Rect [0 0 1 1] /FT /Tx /T (Name) /V (forged) >>] >> >>\nendobj\ntrailer\n<< /Root 1 0 R >>\n%%EOF\n";+ assert_eq!(import(&mut doc, fdf).unwrap().comments, 1);+ assert_eq!(doc.get(widget), before, "the widget was overwritten by an imported comment");+}