Skip to content

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

fix(audit): count only the covered text a finding reports against the output bound

aiongg3e16c71a51be
diff --git a/crates/audit/src/lib.rs b/crates/audit/src/lib.rs
index 3ef8f6720..509ae977e 100644
--- a/crates/audit/src/lib.rs
+++ b/crates/audit/src/lib.rs
@@ -115,7 +115,7 @@ struct Limits {
/// Cover-against-line comparisons per page, counting a line once per word box it has (at
/// least once), since [`covers`] walks them.
comparisons: usize,
- /// Bytes of covered text across all findings.
+ /// Bytes of covered text the findings report.
covered_text: usize,
}
@@ -150,18 +150,24 @@ impl Budget {
}
let mut covered = String::new();
for line in lines.iter().filter(|l| hides(l)) {
- let text = line.text.trim();
- self.covered_text = self.covered_text.saturating_add(text.len() + 1);
- if self.covered_text > self.limits.covered_text {
- return Err(AuditError::TooMuchOutput);
- }
if !covered.is_empty() {
covered.push(' ');
}
- covered.push_str(text);
+ covered.push_str(line.text.trim());
}
Ok(covered)
}
+
+ /// The text a finding reports for `covered`: none when copying is withheld. Charges what it
+ /// reports against the output bound.
+ fn report_text(&mut self, covered: String, copying: bool) -> Result<String, AuditError> {
+ let reported = if copying { covered } else { String::new() };
+ self.covered_text = self.covered_text.saturating_add(reported.len());
+ if self.covered_text > self.limits.covered_text {
+ return Err(AuditError::TooMuchOutput);
+ }
+ Ok(reported)
+ }
}
fn audit(doc: &Document, limits: Limits) -> Result<Vec<AuditFinding>, AuditError> {
@@ -175,7 +181,7 @@ fn audit(doc: &Document, limits: Limits) -> Result<Vec<AuditFinding>, AuditError
for r in cover::filled_rects(doc, &page.dict)? {
let covered = budget.hidden_text(&lines, |line| is_cover(&r, line))?;
if !covered.trim().is_empty() {
- let covered_text = if copying { covered } else { String::new() };
+ let covered_text = budget.report_text(covered, copying)?;
out.push(AuditFinding { page: n, kind: FindingKind::CoveredText, rect: r.rect, covered_text });
}
}
@@ -190,7 +196,7 @@ fn audit(doc: &Document, limits: Limits) -> Result<Vec<AuditFinding>, AuditError
AnnotationCover::Redact(rect) => (FindingKind::UnappliedMark, rect),
AnnotationCover::Square { rect, .. } => (FindingKind::CoveredText, rect),
};
- let covered_text = if copying { covered } else { String::new() };
+ let covered_text = budget.report_text(covered, copying)?;
out.push(AuditFinding { page: n, kind, rect, covered_text });
}
}
diff --git a/crates/audit/src/tests.rs b/crates/audit/src/tests.rs
index e0ec3888c..659f8de60 100644
--- a/crates/audit/src/tests.rs
+++ b/crates/audit/src/tests.rs
@@ -697,8 +697,15 @@ fn copy_restricted_documents_withhold_covered_text() {
let findings = audit_redactions(&user).expect("audit must run");
assert_eq!(findings.len(), 1, "the cover is still reported: {findings:?}");
assert_eq!(findings[0].covered_text, "");
+ // The output bound counts the text reported, so withheld text costs nothing.
+ let nothing = crate::Limits { comparisons: usize::MAX, covered_text: 0 };
+ assert_eq!(crate::audit(&user, nothing).expect("withheld text is not output").len(), 1);
let owner = Document::open_with_password(bytes, Some("owner")).expect("open");
let findings = audit_redactions(&owner).expect("audit must run");
assert_eq!(findings.len(), 1, "{findings:?}");
assert_eq!(findings[0].covered_text, "SECRET DATA 123");
+ let exact = crate::Limits { comparisons: usize::MAX, covered_text: "SECRET DATA 123".len() };
+ assert!(crate::audit(&owner, exact).is_ok());
+ let short = crate::Limits { covered_text: exact.covered_text - 1, ..exact };
+ assert!(matches!(crate::audit(&owner, short), Err(crate::AuditError::TooMuchOutput)));
}