From a98744974a34762dc9884a028cd2cf9e6804469c Mon Sep 17 00:00:00 2001 From: FredApps Date: Sat, 14 Nov 2026 21:32:15 +0100 Subject: [PATCH] redact: an entry of /Annots that names another type does not block Apply A graphics state or a font listed in /Annots (PDFBOX-5775 Export.pdf) has no /Rect, so Apply refused and pointed at an annotation that is not there. Such an entry is kept and skipped. One with an annotation subtype is still an annotation whatever its /Type says, and still refuses without a readable /Rect. --- crates/redact/README.md | 5 +- crates/redact/src/lib.rs | 46 +++++++++++++++++++ .../redact/src/tests/annotation_geometry.rs | 32 +++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/crates/redact/README.md b/crates/redact/README.md index 594110ac95..4a24c15693 100644 --- a/crates/redact/README.md +++ b/crates/redact/README.md @@ -68,7 +68,10 @@ clear_marks(&mut doc, None)?; // remove marks without app widget under a mark. On a selected page, a non-popup annotation with no readable `/Rect` (exactly four finite numbers, including referenced coordinates) refuses the entire operation with its page and annotation number. Repair or remove it before applying again; nothing changes - on refusal. An unreadable rectangle is not treated as evidence that it misses the marks; + on refusal. An unreadable rectangle is not treated as evidence that it misses the marks. An + entry of `/Annots` that says it is something else (a `/Type` other than `/Annot` and no + annotation `/Subtype`, such as a font a writer listed there) is not an annotation: it is kept + and does not block Apply; - the marks themselves, replaced by boxes in their fill colour (and overlay text) drawn into the page as a `/PCMark /Redaction` stream; - **metadata** that repeats the removed text (#2448): `/Info` entries containing it or wholly diff --git a/crates/redact/src/lib.rs b/crates/redact/src/lib.rs index 9016417e48..73378ac426 100644 --- a/crates/redact/src/lib.rs +++ b/crates/redact/src/lib.rs @@ -268,6 +268,47 @@ pub(crate) fn subtype(doc: &Document, d: &Dict) -> Option> { d.get(b"Subtype").and_then(|s| doc.resolve(s).as_name().map(<[u8]>::to_vec)) } +/// The annotation types (ISO 32000-2 Table 171). +const ANNOTATION_SUBTYPES: [&[u8]; 28] = [ + b"Text", + b"Link", + b"FreeText", + b"Line", + b"Square", + b"Circle", + b"Polygon", + b"PolyLine", + b"Highlight", + b"Underline", + b"Squiggly", + b"StrikeOut", + b"Caret", + b"Stamp", + b"Ink", + b"Popup", + b"FileAttachment", + b"Sound", + b"Movie", + b"Screen", + b"Widget", + b"PrinterMark", + b"TrapNet", + b"Watermark", + b"3D", + b"Redact", + b"Projection", + b"RichMedia", +]; + +/// Whether `d`, an entry of `/Annots` with `/Subtype` `kind`, says it is something other than an +/// annotation: its `/Type` is a name that isn't `/Annot` (an annotation's, "if present, shall be +/// Annot": ISO 32000-2 Table 166) and `kind` is no annotation type. A dictionary with an +/// annotation type is an annotation whatever its `/Type` says, since a viewer may still show it. +fn names_another_type(doc: &Document, d: &Dict, kind: &[u8]) -> bool { + let other = d.get(b"Type").is_some_and(|t| doc.resolve(t).as_name().is_some_and(|n| n != b"Annot".as_slice())); + other && !ANNOTATION_SUBTYPES.contains(&kind) +} + /// The pop-up `d` names (`/Popup`), when it is one: it goes with `d`. Anything else it might name /// (a mark on another page, say) is left alone. fn named_popup(doc: &Document, d: &Dict) -> Option { @@ -1007,6 +1048,11 @@ fn remove_page_annotations( if kind == b"Popup" { continue; } + // A graphics state or a font that a writer listed in `/Annots` is not an annotation: + // it has no `/Rect` to read and nothing a viewer shows, so it stays and Apply goes on. + if names_another_type(doc, d, &kind) { + continue; + } // Unknown geometry is not evidence that the annotation misses every mark. Refuse rather // than keep potentially sensitive annotation text or appearances after a successful apply. let bounds = rect_of(doc, d.get(b"Rect")).ok_or(RedactError::UnreadableAnnotation { page: pi + 1, annotation: i + 1 })?; diff --git a/crates/redact/src/tests/annotation_geometry.rs b/crates/redact/src/tests/annotation_geometry.rs index 1bff27dd2b..e08ac68ff7 100644 --- a/crates/redact/src/tests/annotation_geometry.rs +++ b/crates/redact/src/tests/annotation_geometry.rs @@ -91,3 +91,35 @@ fn malformed_annotations_on_unselected_pages_do_not_block_apply() { apply(&mut doc, Some(&[0])).unwrap(); assert_eq!(pdfcraft_model::pages(&doc)[1].dict, before); } + +/// A page whose `/Annots` is `annots`, with a mark over the lower left of the page. +fn page_with_annots(annots: &str) -> Document { + let mut doc = pdf(vec![ + b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(), + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(), + format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] /Contents 4 0 R /Annots [{annots}] >>").into_bytes(), + stream("", b"BT ET"), + ]); + mark(&mut doc, 0, &[[0.0, 90.0, 100.0, 130.0]], ""); + doc +} + +/// A graphics state and a font listed in `/Annots` (PDFBOX-5775's `Export.pdf` has both) are not +/// annotations: Apply goes on, removes the note under the mark and leaves them where they were. +#[test] +fn dictionaries_of_another_type_in_annots_do_not_block_apply() { + let others = "<< /Type /ExtGState /CA 1 >> << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>"; + let mut doc = page_with_annots(&format!("{others} << /Type /Annot /Subtype /Text /Rect [10 100 40 120] /Contents (PRIVATE-NOTE) >>")); + assert_eq!(apply(&mut doc, None).unwrap().annotations, 1); + let left = annots_of(&doc, &pdfcraft_model::pages(&doc)[0].dict); + let type_of = |a: &Object| doc.resolve(a).as_dict().and_then(|d| d.get(b"Type")?.as_name().map(<[u8]>::to_vec)); + assert_eq!(left.iter().filter_map(type_of).collect::>(), [b"ExtGState".to_vec(), b"Font".to_vec()]); +} + +/// An annotation type under another `/Type` is still an annotation: without a `/Rect` it refuses. +#[test] +fn an_annotation_subtype_under_another_type_still_refuses_apply() { + let mut doc = page_with_annots("<< /Type /Font /Subtype /Text /Contents (PRIVATE-NOTE) >>"); + let result = apply(&mut doc, None); + assert!(matches!(result, Err(RedactError::UnreadableAnnotation { page: 1, annotation: 1 })), "{result:?}"); +}