From 399b57e707218bd14be8ab892dc2686a1dab86c2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 20:43:48 +0000 Subject: [PATCH] fix(sign): refuse to sign a field whose ReadOnly flag is set --- crates/sign/src/pdf/listing.rs | 13 +++++++++++++ crates/sign/src/pdf/signing.rs | 3 +++ crates/sign/tests/it/pdf.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/crates/sign/src/pdf/listing.rs b/crates/sign/src/pdf/listing.rs index cbcf339715..31b722809d 100644 --- a/crates/sign/src/pdf/listing.rs +++ b/crates/sign/src/pdf/listing.rs @@ -185,6 +185,19 @@ pub(super) struct Field { } impl Field { + /// The field's ReadOnly flag (`/Ff` bit 1, inheritable from its ancestors). + pub(super) fn read_only(&self, doc: &Document) -> bool { + let mut d = self.dict.clone(); + for _ in 0..64 { + if let Some(ff) = d.get(b"Ff").map(|f| doc.resolve(f)).and_then(|f| f.as_int()) { + return ff & 1 != 0; + } + let Some(parent) = d.get(b"Parent").map(|p| doc.resolve(p)).and_then(|p| p.as_dict().cloned()) else { return false }; + d = parent; + } + false + } + /// The signature dictionary in /V. A field without one is unsigned, whether /V is missing, /// null or not a dictionary. pub(super) fn signature(&self, doc: &Document) -> Option { diff --git a/crates/sign/src/pdf/signing.rs b/crates/sign/src/pdf/signing.rs index 71dd10b302..010d4c3653 100644 --- a/crates/sign/src/pdf/signing.rs +++ b/crates/sign/src/pdf/signing.rs @@ -208,6 +208,9 @@ fn sign_inner( if f.signature(&doc).is_some() { return Err(SignError::Pdf(format!("the field {:?} is already signed", f.name))); } + if f.read_only(&doc) { + return Err(SignError::Pdf(format!("the field {:?} is read-only", f.name))); + } if f.has_foreign_value(&doc) { return Err(SignError::Pdf(format!("the value of the field {:?} is not a signature", f.name))); } diff --git a/crates/sign/tests/it/pdf.rs b/crates/sign/tests/it/pdf.rs index 869a4bebd5..2091f58bb7 100644 --- a/crates/sign/tests/it/pdf.rs +++ b/crates/sign/tests/it/pdf.rs @@ -2954,3 +2954,35 @@ fn signing_a_field_with_a_null_value_after_another_signature_keeps_the_first_val all.iter().map(|s| (&s.status, &s.modification)).collect::>() ); } + +/// A field the form marks ReadOnly (its own `/Ff`, or inherited from a parent) is not signed +/// through the shared signer, as ordinary form writes refuse it too. +#[test] +fn a_read_only_signature_field_is_not_signed() { + let id = pkcs12::open(&data("rsa-aes.p12"), "test").unwrap(); + let page: &[u8] = b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] /Contents 4 0 R /Annots [6 0 R] >>"; + let own = write_pdf(&[ + b"<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [6 0 R] >> >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + page, + b"<< /Length 0 >>\nstream\n\nendstream", + b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", + b"<< /Type /Annot /Subtype /Widget /FT /Sig /Ff 1 /T (Approval) /Rect [150 20 280 70] /P 3 0 R /F 4 >>", + ]); + let inherited = write_pdf(&[ + b"<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [7 0 R] >> >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + page, + b"<< /Length 0 >>\nstream\n\nendstream", + b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", + b"<< /Type /Annot /Subtype /Widget /T (Approval) /Parent 7 0 R /Rect [150 20 280 70] /P 3 0 R /F 4 >>", + b"<< /FT /Sig /Ff 1 /T (Group) /Kids [6 0 R] >>", + ]); + for (what, bytes, field) in [("own flag", own, "Approval"), ("inherited flag", inherited, "Group.Approval")] { + let r = pdfcraft_sign::sign(&open(&bytes), &id, &SignOptions { field: Some(field.into()), ..opts() }); + assert!(matches!(&r, Err(pdfcraft_sign::SignError::Pdf(m)) if m.contains("read-only")), "{what}: {r:?}"); + } + // The same fixture without the flag signs. + let ok = pdfcraft_sign::sign(&open(&fixture()), &id, &SignOptions { field: Some("Approval".into()), ..opts() }); + assert!(ok.is_ok()); +}