diff --git a/crates/automation/tests/it/automation.rs b/crates/automation/tests/it/automation.rs index 5251bf96ce..62c9fab14b 100644 --- a/crates/automation/tests/it/automation.rs +++ b/crates/automation/tests/it/automation.rs @@ -5853,6 +5853,65 @@ fn redacting_through_tools() { assert!(matches!(a.call("redact_mark", &json!({ "doc": doc })), Err(ToolError::InvalidArgs(_)))); } +#[test] +fn redact_apply_refuses_unreadable_annotations_then_recovers_through_tools() { + for inline in [false, true] { + for rect in ["", "/Rect [10 140 /Bad 80 160]"] { + let temp = tempfile::tempdir().unwrap(); + let dir = temp.path(); + let note = format!("<< /Type /Annot /Subtype /Text /NM (problem-note) /Contents (PRIVATE-NOTE) {rect} >>"); + let annots = if inline { note.clone() } else { "6 0 R".into() }; + let page = format!( + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Contents 5 0 R \ + /Resources << /Font << /F1 4 0 R >> >> /Annots [{annots}] >>" + ); + let content = "BT /F1 12 Tf 20 150 Td (SECRET) Tj ET BT /F1 12 Tf 20 50 Td (Public line) Tj ET"; + let stream = format!("<< /Length {} >>\nstream\n{content}\nendstream", content.len()); + let bytes = pdf_of(&[ + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + &page, + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", + &stream, + ¬e, + ]); + std::fs::write(dir.join("source.pdf"), bytes).unwrap(); + let mut a = auto(dir); + let doc = ok(&mut a, "doc_open", json!({ "path": "source.pdf" }))["doc"].as_u64().unwrap(); + assert_eq!(ok(&mut a, "redact_mark", json!({ "doc": doc, "find": "SECRET" }))["marked"], 1); + ok(&mut a, "doc_save", json!({ "doc": doc, "path": "pending.pdf", "full": true })); + let state = |a: &Automation| { + let d = a.session().get(pdfcraft_engine::DocId(doc)).unwrap(); + (d.bytes.clone(), d.dirty, d.edit_generation(), d.can_undo().map(str::to_owned), d.can_redo().map(str::to_owned)) + }; + let before = state(&a); + let pending = std::fs::read(dir.join("pending.pdf")).unwrap(); + let comments = ok(&mut a, "comment_list", json!({ "doc": doc })); + for _ in 0..2 { + let failure = err(&mut a, "redact_apply", json!({ "doc": doc })).to_string(); + assert!(failure.contains("annotation 1 on page 1") && failure.contains("nothing was changed"), "{failure}"); + assert_eq!(state(&a), before, "refusal preserves working bytes, dirty state, generation and history"); + assert_eq!(ok(&mut a, "comment_list", json!({ "doc": doc })), comments, "the note and pending mark stay"); + assert_eq!(std::fs::read(dir.join("pending.pdf")).unwrap(), pending); + assert!(page_text(&mut a, doc)[0].contains("SECRET"), "refusal leaves the original page intact"); + } + // The error's remedy is reachable through the ordinary comment tool, even without a + // usable rectangle. The original pending mark can then be applied and saved. + ok(&mut a, "comment_delete", json!({ "doc": doc, "id": "problem-note" })); + let applied = ok(&mut a, "redact_apply", json!({ "doc": doc })); + assert_eq!((applied["applied"].as_u64(), applied["marks_pending"].as_u64()), (Some(1), Some(0))); + ok(&mut a, "doc_save", json!({ "doc": doc, "path": "redacted.pdf", "full": true })); + let mut reader = auto(dir); + let reopened = ok(&mut reader, "doc_open", json!({ "path": "redacted.pdf" }))["doc"].as_u64().unwrap(); + let text = page_text(&mut reader, reopened)[0].clone(); + assert!(text.contains("Public line") && !text.contains("SECRET"), "{text}"); + assert_eq!(ok(&mut reader, "comment_list", json!({ "doc": reopened }))["comments"], json!([])); + let saved = std::fs::read(dir.join("redacted.pdf")).unwrap(); + assert!(!saved.windows(b"PRIVATE-NOTE".len()).any(|w| w == b"PRIVATE-NOTE"), "the removed note must not survive a full save"); + } + } +} + /// #2539 review: `redact_apply` says where the removed text is still held that applying leaves as /// it is (a bookmark title and a note's text here), and changes neither. #[test] diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml index 2257ec4bcd..2cb118c621 100644 --- a/parity/acrobat-features.toml +++ b/parity/acrobat-features.toml @@ -5919,7 +5919,7 @@ milestone = "M8" status = "shipped" commands = ["redact.apply"] tools = ["redact_apply"] -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"] +evidence = ["crates/automation/tests/it/automation.rs::redact_apply_refuses_unreadable_annotations_then_recovers_through_tools", "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]]