diff --git a/crates/redact/README.md b/crates/redact/README.md index d8f4b46e94..594110ac95 100644 --- a/crates/redact/README.md +++ b/crates/redact/README.md @@ -65,7 +65,10 @@ clear_marks(&mut doc, None)?; // remove marks without app nothing paints inside the areas; - **form XObjects**: rewritten recursively into new objects (pages sharing the original keep it); - **annotations** whose rectangle overlaps (with their pop-ups and their replies, `/IRT`), and **form fields** with a - widget under a mark; + 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; - 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 819cae99cd..9016417e48 100644 --- a/crates/redact/src/lib.rs +++ b/crates/redact/src/lib.rs @@ -70,6 +70,12 @@ pub enum RedactError { "{0} redaction mark(s) have no readable area (neither usable QuadPoints nor a usable Rect), so the text under them can't be known; nothing was redacted. Remove those marks or mark the text again, then apply again" )] UnreadableMark(usize), + /// An annotation's position cannot be judged. Keeping it could leave its text or appearance + /// in a supposedly redacted file, so the caller must repair or remove it before applying. + #[error( + "annotation {annotation} on page {page} has no readable Rect of four finite numbers, so its overlap with the redaction marks can't be checked; repair or remove that annotation, then apply again (nothing was changed)" + )] + UnreadableAnnotation { page: usize, annotation: usize }, #[error("redaction could not be verified: {0} item(s) were still found under the marks, so nothing was changed")] Residue(usize), /// A mark's overlay text has a character Helvetica can't draw; nothing was redacted. @@ -227,8 +233,11 @@ pub struct Report { } pub(crate) fn rect_of(doc: &Document, o: Option<&Object>) -> Option<[f64; 4]> { - let v: Vec = doc.resolve(o?).as_array()?.iter().filter_map(|x| doc.resolve(x).as_f64()).collect(); - (v.len() == 4 && v.iter().all(|x| x.is_finite())).then(|| [v[0].min(v[2]), v[1].min(v[3]), v[0].max(v[2]), v[1].max(v[3])]) + let obj = doc.resolve(o?); + let [x0, y0, x1, y1] = obj.as_array()?.as_slice() else { return None }; + let [x0, y0, x1, y1] = [x0, y0, x1, y1].map(|x| doc.resolve(x).as_f64()); + let (x0, y0, x1, y1) = (x0?, y0?, x1?, y1?); + [x0, y0, x1, y1].iter().all(|x| x.is_finite()).then(|| [x0.min(x1), y0.min(y1), x0.max(x1), y0.max(y1)]) } /// A mark's usable `/QuadPoints` quads ([`pdfcraft_geom::quad_points::usable_quads`]), the set the @@ -571,7 +580,7 @@ fn apply_within(doc: &mut Document, pages: Option<&[usize]>, images: pdfcraft_co let page = pdfcraft_model::pages(doc).swap_remove(pi); let rects: Vec<[f64; 4]> = page_marks.iter().flat_map(|m| m.rects.iter().copied()).collect(); redact_page_content(doc, &mut applying, pi, &page, &rects)?; - remove_page_annotations(doc, &mut applying, &page, &rects, page_marks)?; + remove_page_annotations(doc, &mut applying, pi, &page, &rects, page_marks)?; } if applying.images.refused() > 0 { return Err(RedactError::ImagesPastAllowance); @@ -946,6 +955,7 @@ fn redact_page_content( fn remove_page_annotations( doc: &mut Document, applying: &mut Applying, + pi: usize, page: &pdfcraft_model::Page, rects: &[[f64; 4]], page_marks: &[&Mark], @@ -997,7 +1007,10 @@ fn remove_page_annotations( if kind == b"Popup" { continue; } - if rect_of(doc, d.get(b"Rect")).is_some_and(|b| rects.iter().any(|x| pdfcraft_content::overlaps(*x, b, 0.0))) { + // 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 })?; + if rects.iter().any(|x| pdfcraft_content::overlaps(*x, bounds, 0.0)) { match r { Some(r) => removed.push(r), None => inline_gone[i] = true, diff --git a/crates/redact/src/tests.rs b/crates/redact/src/tests.rs index 43b6caaadc..edd04a3b57 100644 --- a/crates/redact/src/tests.rs +++ b/crates/redact/src/tests.rs @@ -6274,6 +6274,7 @@ fn unused_descriptions_in_shared_or_inherited_resources_are_retired() { } } +mod annotation_geometry; mod descriptions; mod elsewhere; mod form_matrix; diff --git a/crates/redact/src/tests/annotation_geometry.rs b/crates/redact/src/tests/annotation_geometry.rs new file mode 100644 index 0000000000..1bff27dd2b --- /dev/null +++ b/crates/redact/src/tests/annotation_geometry.rs @@ -0,0 +1,93 @@ +use super::*; + +#[test] +fn annotation_rectangles_do_not_skip_non_numeric_entries() { + let doc = one_page(b"BT ET", "", vec![]); + let rect = Object::Array(vec![Object::Int(10), Object::Int(100), Object::name("bad"), Object::Int(40), Object::Int(120)]); + assert_eq!(rect_of(&doc, Some(&rect)), None); +} + +fn with_annotation(rect: Option, inline: bool) -> Document { + let mut doc = one_page(b"BT /F1 10 Tf 10 100 Td (SECRET) Tj ET", "", vec![]); + let mut annotation = Dict::from_iter([ + (b"Type".to_vec(), Object::name("Annot")), + (b"Subtype".to_vec(), Object::name("Text")), + (b"Contents".to_vec(), Object::String(pdfcraft_cos::PdfString::text("PRIVATE-NOTE"))), + ]); + if let Some(rect) = rect { + annotation.set(b"Rect".to_vec(), rect); + } + let annotation = if inline { Object::Dict(annotation) } else { Object::Ref(doc.add(Object::Dict(annotation))) }; + let page = pdfcraft_model::pages(&doc)[0].obj; + doc.update_dict(page, |d| d.set(b"Annots".to_vec(), Object::Array(vec![annotation]))).unwrap(); + mark(&mut doc, 0, &[[0.0, 90.0, 100.0, 130.0]], ""); + doc +} + +#[test] +fn unreadable_annotation_rectangles_refuse_apply_without_changing_the_document() { + let number = |n| Object::Int(n); + let bad = [ + None, + Some(Object::Null), + Some(Object::name("not-a-rectangle")), + Some(Object::Ref(ObjRef::new(999, 0))), + Some(Object::Array(vec![number(10), number(100), number(40)])), + Some(Object::Array(vec![number(10), number(100), Object::name("bad"), number(120)])), + Some(Object::Array(vec![number(10), number(100), Object::name("bad"), number(40), number(120)])), + Some(Object::Array(vec![number(10), number(100), Object::Real(f64::INFINITY), number(120)])), + ]; + for inline in [false, true] { + for rect in &bad { + let mut doc = with_annotation(rect.clone(), inline); + // Keep non-finite objects in memory for the check: a writer need not preserve them. + let before_page = pdfcraft_model::pages(&doc)[0].dict.clone(); + let before_content = content(&doc, 0); + let before_marks = marks(&doc); + let result = apply(&mut doc, None); + assert!(matches!(result, Err(RedactError::UnreadableAnnotation { page: 1, annotation: 1 })), "{rect:?}, inline={inline}: {result:?}"); + assert_eq!(pdfcraft_model::pages(&doc)[0].dict, before_page); + assert_eq!(content(&doc, 0), before_content); + assert_eq!(marks(&doc), before_marks); + } + } +} + +#[test] +fn readable_annotation_rectangles_keep_their_geometry_and_save_without_the_note() { + for inline in [false, true] { + for indirect in [false, true] { + let mut doc = with_annotation(Some(Object::Array(vec![Object::Int(40), Object::Int(120), Object::Int(10), Object::Int(100)])), inline); + if indirect { + let coordinates: Vec = [40, 120, 10, 100].into_iter().map(|n| Object::Ref(doc.add(Object::Int(n)))).collect(); + let rect = doc.add(Object::Array(coordinates)); + let page = pdfcraft_model::pages(&doc)[0].clone(); + let mut annots = annots_of(&doc, &page.dict); + let mut annotation = doc.resolve(&annots[0]).as_dict().unwrap().clone(); + annotation.set(b"Rect".to_vec(), Object::Ref(rect)); + annots[0] = if inline { Object::Dict(annotation) } else { Object::Ref(doc.add(Object::Dict(annotation))) }; + doc.update_dict(page.obj, |d| d.set(b"Annots".to_vec(), Object::Array(annots))).unwrap(); + } + assert_eq!(apply(&mut doc, None).unwrap().annotations, 1); + let bytes = write_full(&doc, &SaveOptions { object_streams: false, ..SaveOptions::default() }).unwrap(); + assert!(!bytes.windows(b"PRIVATE-NOTE".len()).any(|w| w == b"PRIVATE-NOTE")); + let saved = Document::open(Arc::new(bytes)).unwrap(); + assert!(annots_of(&saved, &pdfcraft_model::pages(&saved)[0].dict).is_empty()); + } + } +} + +#[test] +fn malformed_annotations_on_unselected_pages_do_not_block_apply() { + let mut doc = pdf(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 >>".to_vec(), + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] /Contents 5 0 R /Annots [<< /Type /Annot /Subtype /Text /Contents (UNSELECTED-NOTE) >>] >>".to_vec(), + stream("", b"BT ET"), + ]); + mark(&mut doc, 0, &[[0.0, 0.0, 100.0, 100.0]], ""); + let before = pdfcraft_model::pages(&doc)[1].dict.clone(); + apply(&mut doc, Some(&[0])).unwrap(); + assert_eq!(pdfcraft_model::pages(&doc)[1].dict, before); +} diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml index d9ebb2af08..2257ec4bcd 100644 --- a/parity/acrobat-features.toml +++ b/parity/acrobat-features.toml @@ -5919,8 +5919,8 @@ milestone = "M8" status = "shipped" commands = ["redact.apply"] tools = ["redact_apply"] -evidence = ["crates/ui-egui/tests/it/redact.rs::marking_applying_and_clearing", "crates/engine/src/tests.rs::redaction_marks_apply_for_good_and_undo", "crates/automation/tests/it/automation.rs::redacting_through_tools", "crates/engine/src/tests.rs::the_renderer_check_refuses_text_still_shown_under_a_mark", "crates/redact/src/tests.rs::redacted_xobjects_do_not_survive_through_inherited_or_shared_resources", "crates/redact/src/tests.rs::redacted_text_does_not_survive_in_info_or_xmp", "crates/redact/src/tests.rs::metadata_is_cleared_when_the_removed_text_was_cut_short", "crates/redact/src/tests/metadata_fail_closed.rs::an_xmp_stream_whose_filter_data_is_corrupt_goes", "crates/redact/src/tests/elsewhere.rs::places_apply_leaves_that_still_hold_the_removed_text_are_counted_and_kept", "crates/engine/src/tests.rs::applied_redactions_report_a_bookmark_that_still_holds_the_removed_text", "crates/automation/tests/it/automation.rs::redact_apply_reports_places_that_still_hold_the_removed_text", "crates/ui-egui/tests/it/redact.rs::applying_says_where_the_removed_text_is_still_held", "crates/redact/src/tests/metadata_matching.rs::a_heading_tracked_with_tj_numbers_matches_the_metadata", "crates/redact/src/tests/metadata_matching.rs::unicode_forms_of_the_removed_text_match", "crates/redact/src/tests/metadata_matching.rs::a_utf16le_string_is_searched_as_utf16le", "crates/redact/src/tests/identification.rs::the_identification_of_a_removed_packet_is_written_back", "crates/automation/tests/it/automation.rs::redact_apply_reports_the_identification_written_back", "crates/ui-egui/tests/it/redact.rs::applying_says_the_pdfa_identification_was_written_back"] -notes = "confirmation; one undo step; next save is a full rewrite. Document information entries that contain the removed text, and the document's and pages' XMP streams that do, are removed (#2448; matched without regard to case or spacing, by the whole run of removed text and by its words of three letters or digits or more, and a value wholly part of a run as whole words, each also compared by letters and digits alone (so spacing, hyphens and TJ tracking don't hide it), after NFKC and lower case, with FF FE strings read as UTF-16LE; fail-closed: all of it goes when the removed text can't be known in full (too much to collect or search for, a glyph whose text isn't known, a form removed unread), and an XMP packet goes when it can't be decoded strictly, is over 16 MiB or isn't UTF-8 or UTF-16). When the document's XMP goes, its PDF/A and PDF/UA identification is written back in a packet holding only that, value by value when each is a valid code that doesn't hold the removed text, else reported lost (Report::identification, the notification, redact_apply's identification); /Info entries that remain aren't mirrored into that packet. Bookmark titles, attachments (names, file names, descriptions), the comments that remain and other objects' XMP are not changed: applying counts those that still hold the removed text, and the notification and redact_apply's still_holding say where (Remove hidden information removes them); when the removed text couldn't be searched for, every such place holding any text is counted. Refused, with nothing changed, when the page still shows text under a mark afterwards: text in some predefined CMaps and rotated Type 3 fonts is not removed yet, so those documents cannot be redacted. Also refused on a page with an inline image the renderer reads no image for, since the operators after it can't be known, or with bytes the content parser can't read in its content or in a form under a mark, which can hide text other readers draw. Removed images and forms are unlinked from page-tree resources (own, inherited or shared) and from surviving forms' resources, not yet from annotation appearances or patterns" +evidence = ["crates/redact/src/tests/annotation_geometry.rs::unreadable_annotation_rectangles_refuse_apply_without_changing_the_document", "crates/redact/src/tests/annotation_geometry.rs::readable_annotation_rectangles_keep_their_geometry_and_save_without_the_note", "crates/ui-egui/tests/it/redact.rs::marking_applying_and_clearing", "crates/engine/src/tests.rs::redaction_marks_apply_for_good_and_undo", "crates/automation/tests/it/automation.rs::redacting_through_tools", "crates/engine/src/tests.rs::the_renderer_check_refuses_text_still_shown_under_a_mark", "crates/redact/src/tests.rs::redacted_xobjects_do_not_survive_through_inherited_or_shared_resources", "crates/redact/src/tests.rs::redacted_text_does_not_survive_in_info_or_xmp", "crates/redact/src/tests.rs::metadata_is_cleared_when_the_removed_text_was_cut_short", "crates/redact/src/tests/metadata_fail_closed.rs::an_xmp_stream_whose_filter_data_is_corrupt_goes", "crates/redact/src/tests/elsewhere.rs::places_apply_leaves_that_still_hold_the_removed_text_are_counted_and_kept", "crates/engine/src/tests.rs::applied_redactions_report_a_bookmark_that_still_holds_the_removed_text", "crates/automation/tests/it/automation.rs::redact_apply_reports_places_that_still_hold_the_removed_text", "crates/ui-egui/tests/it/redact.rs::applying_says_where_the_removed_text_is_still_held", "crates/redact/src/tests/metadata_matching.rs::a_heading_tracked_with_tj_numbers_matches_the_metadata", "crates/redact/src/tests/metadata_matching.rs::unicode_forms_of_the_removed_text_match", "crates/redact/src/tests/metadata_matching.rs::a_utf16le_string_is_searched_as_utf16le", "crates/redact/src/tests/identification.rs::the_identification_of_a_removed_packet_is_written_back", "crates/automation/tests/it/automation.rs::redact_apply_reports_the_identification_written_back", "crates/ui-egui/tests/it/redact.rs::applying_says_the_pdfa_identification_was_written_back"] +notes = "A non-popup annotation on a selected page whose Rect is not exactly four finite numbers refuses Apply with page and annotation numbers, leaving the document unchanged; valid referenced coordinates and reversed corners still work. confirmation; one undo step; next save is a full rewrite. Document information entries that contain the removed text, and the document's and pages' XMP streams that do, are removed (#2448; matched without regard to case or spacing, by the whole run of removed text and by its words of three letters or digits or more, and a value wholly part of a run as whole words, each also compared by letters and digits alone (so spacing, hyphens and TJ tracking don't hide it), after NFKC and lower case, with FF FE strings read as UTF-16LE; fail-closed: all of it goes when the removed text can't be known in full (too much to collect or search for, a glyph whose text isn't known, a form removed unread), and an XMP packet goes when it can't be decoded strictly, is over 16 MiB or isn't UTF-8 or UTF-16). When the document's XMP goes, its PDF/A and PDF/UA identification is written back in a packet holding only that, value by value when each is a valid code that doesn't hold the removed text, else reported lost (Report::identification, the notification, redact_apply's identification); /Info entries that remain aren't mirrored into that packet. Bookmark titles, attachments (names, file names, descriptions), the comments that remain and other objects' XMP are not changed: applying counts those that still hold the removed text, and the notification and redact_apply's still_holding say where (Remove hidden information removes them); when the removed text couldn't be searched for, every such place holding any text is counted. Refused, with nothing changed, when the page still shows text under a mark afterwards: text in some predefined CMaps and rotated Type 3 fonts is not removed yet, so those documents cannot be redacted. Also refused on a page with an inline image the renderer reads no image for, since the operators after it can't be known, or with bytes the content parser can't read in its content or in a form under a mark, which can hide text other readers draw. Removed images and forms are unlinked from page-tree resources (own, inherited or shared) and from surviving forms' resources, not yet from annotation appearances or patterns" [[feature]] id = "protect.redact-glyph-removal"