Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M9: sign_list lists a hidden or NoView signature field as not visible (#4141)

Eric Kaden1b247a481254
diff --git a/crates/sign/src/pdf.rs b/crates/sign/src/pdf.rs
index f4fe2c601..cb0069330 100644
--- a/crates/sign/src/pdf.rs
+++ b/crates/sign/src/pdf.rs
@@ -587,7 +587,10 @@ pub fn list_cached(doc: &Document, bytes: &[u8], trust: &TrustStore, cache: &Dig
.and_then(|(_, w)| nums(doc, w, b"Rect"))
.filter(|r| r.len() == 4)
.map(|r| [r[0].min(r[2]), r[1].min(r[3]), r[0].max(r[2]), r[1].max(r[3])]);
- let visible = rect.is_some_and(|r| r[2] - r[0] > 0.5 && r[3] - r[1] > 0.5);
+ // Shown when it has a box, and its widget isn't Hidden (2) or NoView (32), as the forms
+ // crate reads a widget's flags.
+ let flags = widget.and_then(|(_, w)| w.get(b"F")).and_then(|o| doc.resolve(o).as_int()).unwrap_or(0);
+ let visible = rect.is_some_and(|r| r[2] - r[0] > 0.5 && r[3] - r[1] > 0.5) && flags & (2 | 32) == 0;
let v = f.signature(doc);
let mut info = SignatureInfo {
field: f.name.clone(),
diff --git a/crates/sign/tests/it/pdf.rs b/crates/sign/tests/it/pdf.rs
index 3cad0fa75..41f9c58e1 100644
--- a/crates/sign/tests/it/pdf.rs
+++ b/crates/sign/tests/it/pdf.rs
@@ -163,6 +163,24 @@ fn a_field_with_a_null_value_is_unsigned_and_can_be_signed() {
}
}
+/// A signature field whose widget is hidden (annotation flag Hidden, 2) or not shown on screen
+/// (NoView, 32) is listed as not visible, whatever its rectangle, as form widgets are: a form's
+/// requestor box with flags 96 (NoView and ReadOnly) has no box on the page to show. A widget
+/// that is only printable (4) stays visible.
+#[test]
+fn a_hidden_signature_field_is_listed_as_not_visible() {
+ let field = pdfcraft_cos::ObjRef { num: 6, generation: 0 };
+ let visible_with = |flags: i64| {
+ let bytes = edit_after(&fixture(), |doc| doc.update_dict(field, |d| d.set(b"F".to_vec(), Object::Int(flags))).unwrap());
+ let listed = signatures(&open(&bytes), &bytes, &TrustStore::default());
+ listed.iter().find(|s| s.field == "Approval").map(|s| s.visible).expect("listed")
+ };
+ assert!(visible_with(4), "printable only: shown");
+ for flags in [2, 32, 96, 34] {
+ assert!(!visible_with(flags), "flags {flags}: not shown");
+ }
+}
+
/// Follow-up to #1374: a signature field whose /V is neither null nor a signature dictionary is
/// refused by name with an error saying so, instead of being overwritten or called signed.
#[test]
diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml
index 6a2d0ccf9..6200bac36 100644
--- a/parity/acrobat-features.toml
+++ b/parity/acrobat-features.toml
@@ -6241,8 +6241,8 @@ milestone = "M9"
status = "shipped"
commands = ["sign.panel"]
tools = ["sign_list"]
-evidence = ["crates/ui-egui/tests/it/signing.rs::drawing_a_signature_creating_an_id_signing_and_trusting"]
-notes = ""
+evidence = ["crates/ui-egui/tests/it/signing.rs::drawing_a_signature_creating_an_id_signing_and_trusting", "crates/sign/tests/it/pdf.rs::a_hidden_signature_field_is_listed_as_not_visible"]
+notes = "sign_list reports a signature as visible only when its widget has a box and isn't Hidden or NoView, as form widgets are read, so a hidden field gets no badge on the page"
[[feature]]
id = "sign.validation-banner"