Skip to content

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

Merge PR #2854: Redaction audit: an opaque filled Square annotation over text is a cover

aiongg7bef6fb7b482
diff --cc crates/audit/src/tests.rs
index 63d9220cc,13f347006..4ca3b0bf5
--- a/crates/audit/src/tests.rs
+++ b/crates/audit/src/tests.rs
@@@ -175,47 -175,69 +175,110 @@@ fn unapplied_redact_mark_is_found()
assert!(findings[0].covered_text.contains("DO NOT SHARE"));
}
+/// #2662: a box over one number of a long line hides a few percent of the line, and all of the
+/// number. Found as a cover, and as an unapplied mark over the same word.
+#[test]
+fn a_cover_over_one_word_of_a_long_line_is_found() {
+ // Helvetica 11: "The contract value is " ends near x 132, "98765" spans about 132-163.
+ let line = b"BT /F1 11 Tf 30 100 Td (The contract value is 98765 and the rest of this long line continues here.) Tj ET
+";
+ let mut content = line.to_vec();
+ content.extend_from_slice(
+ b"0 g
+131 96 33 14 re f
+",
+ );
+ let findings = audit_redactions(&open(one_page(&content, ""))).expect("audit must run");
+ assert_eq!(findings.len(), 1, "{findings:?}");
+ assert_eq!(findings[0].kind, FindingKind::CoveredText);
+ assert!(findings[0].covered_text.contains("98765"), "{findings:?}");
+
+ let mut b = Builder::new();
+ b.add("<< /Type /Catalog /Pages 2 0 R >>");
+ b.add("<< /Type /Pages /Kids [3 0 R] /Count 1 >>");
+ b.add("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> /Annots [6 0 R] >>");
+ b.add_stream(line);
+ b.add(FONT);
+ b.add("<< /Type /Annot /Subtype /Redact /Rect [131 96 164 110] >>");
+ let findings = audit_redactions(&open(b.build())).expect("audit must run");
+ assert_eq!(findings.len(), 1, "{findings:?}");
+ assert_eq!(findings[0].kind, FindingKind::UnappliedMark);
+}
+
+/// A box that only grazes the words beside it (less than half of each) is not a cover.
+#[test]
+fn a_box_grazing_a_word_is_not_flagged() {
+ let content = b"BT /F1 11 Tf 30 100 Td (The contract value is 98765 and the rest of this long line continues here.) Tj ET
+0 g
+131 96 6 14 re f
+";
+ let findings = audit_redactions(&open(one_page(content, ""))).expect("audit must run");
+ assert!(findings.is_empty(), "{findings:?}");
+}
+
+ #[test]
+ fn opaque_filled_square_annotation_over_text_is_found() {
+ let content = b"BT /F1 24 Tf 100 700 Td (SECRET DATA 123) Tj ET\n";
+ let mut b = Builder::new();
+ b.add("<< /Type /Catalog /Pages 2 0 R >>");
+ b.add("<< /Type /Pages /Kids [3 0 R] /Count 1 >>");
+ b.add("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> /Annots [6 0 R] >>");
+ b.add_stream(content);
+ b.add(FONT);
+ b.add("<< /Type /Annot /Subtype /Square /Rect [95 685 325 725] /C [0 0 0] /IC [0 0 0] >>");
+ let findings = audit_redactions(&open(b.build())).expect("audit must run");
+ assert_eq!(findings.len(), 1, "{findings:?}");
+ assert_eq!(findings[0].kind, FindingKind::CoveredText);
+ assert_eq!(findings[0].rect, [95.0, 685.0, 325.0, 725.0]);
+ assert_eq!(findings[0].covered_text, "SECRET DATA 123");
+ }
+
+ /// A Hidden Square (`/F 2`) is never drawn, so the text under it is not a finding.
+ #[test]
+ fn hidden_square_annotation_is_not_a_finding() {
+ let content = b"BT /F1 24 Tf 100 700 Td (SECRET DATA 123) Tj ET\n";
+ let mut b = Builder::new();
+ b.add("<< /Type /Catalog /Pages 2 0 R >>");
+ b.add("<< /Type /Pages /Kids [3 0 R] /Count 1 >>");
+ b.add("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> /Annots [6 0 R] >>");
+ b.add_stream(content);
+ b.add(FONT);
+ b.add("<< /Type /Annot /Subtype /Square /Rect [95 685 325 725] /C [0 0 0] /IC [0 0 0] /F 2 >>");
+ let findings = audit_redactions(&open(b.build())).expect("audit must run");
+ assert!(findings.is_empty(), "{findings:?}");
+ }
+
+ /// A NoView Square (`/F 32`) is never drawn on screen, so the text under it is not a finding.
+ #[test]
+ fn no_view_square_annotation_is_not_a_finding() {
+ let content = b"BT /F1 24 Tf 100 700 Td (SECRET DATA 123) Tj ET\n";
+ let mut b = Builder::new();
+ b.add("<< /Type /Catalog /Pages 2 0 R >>");
+ b.add("<< /Type /Pages /Kids [3 0 R] /Count 1 >>");
+ b.add("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> /Annots [6 0 R] >>");
+ b.add_stream(content);
+ b.add(FONT);
+ b.add("<< /Type /Annot /Subtype /Square /Rect [95 685 325 725] /C [0 0 0] /IC [0 0 0] /F 32 >>");
+ let findings = audit_redactions(&open(b.build())).expect("audit must run");
+ assert!(findings.is_empty(), "{findings:?}");
+ }
+
+ /// A Print-only Square (`/F 4`) is still drawn on screen, so the text under it is a finding.
+ #[test]
+ fn print_flag_square_annotation_is_still_a_finding() {
+ let content = b"BT /F1 24 Tf 100 700 Td (SECRET DATA 123) Tj ET\n";
+ let mut b = Builder::new();
+ b.add("<< /Type /Catalog /Pages 2 0 R >>");
+ b.add("<< /Type /Pages /Kids [3 0 R] /Count 1 >>");
+ b.add("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> /Annots [6 0 R] >>");
+ b.add_stream(content);
+ b.add(FONT);
+ b.add("<< /Type /Annot /Subtype /Square /Rect [95 685 325 725] /C [0 0 0] /IC [0 0 0] /F 4 >>");
+ let findings = audit_redactions(&open(b.build())).expect("audit must run");
+ assert_eq!(findings.len(), 1, "{findings:?}");
+ assert_eq!(findings[0].kind, FindingKind::CoveredText);
+ }
+
#[test]
fn background_before_text_is_not_flagged() {
// An opaque dark rectangle painted *before* the text is a background, not a cover.