Skip to content

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

Redact: operators split across a page's content streams are redacted whole (#153)

EthanSuquet7920f99f7a9c
diff --git a/crates/redact/src/interp.rs b/crates/redact/src/interp.rs
index e0c64a3d0..3876c6861 100644
--- a/crates/redact/src/interp.rs
+++ b/crates/redact/src/interp.rs
@@ -207,20 +207,46 @@ pub(crate) fn process(doc: &mut Document, scope: &mut Scope<'_>, streams: &[Vec<
let mut clip = false;
let mut forced_change = false;
+ // The streams are one content stream in pieces, which may be split between any two tokens
+ // (ISO 32000-2 §7.8.2): an operator's operands can end one piece and the operator start the
+ // next. Parse them joined, then give each operator back to the piece its keyword is in.
+ let mut joined: Vec<u8> = Vec::new();
+ let mut ends: Vec<usize> = Vec::with_capacity(streams.len());
for data in streams {
- let mut parsed = parse(data);
- if !scope.hidden_layers.is_empty() {
- let props = res_dict(doc, resources, b"Properties");
- let (kept, removed) = strip_hidden_layers(doc, scope, parsed.ops, &props);
- parsed.ops = kept;
- if removed > 0 {
- scope.layer_blocks += removed;
- forced_change = true;
+ joined.extend_from_slice(data);
+ joined.push(b'\n');
+ ends.push(joined.len());
+ }
+ let piece_of = |at: usize| ends.iter().position(|&e| at < e).unwrap_or(ends.len().saturating_sub(1));
+ let mut parsed = parse(&joined);
+ if !scope.hidden_layers.is_empty() {
+ let props = res_dict(doc, resources, b"Properties");
+ let (kept, removed) = strip_hidden_layers(doc, scope, parsed.ops, &props);
+ parsed.ops = kept;
+ if removed > 0 {
+ scope.layer_blocks += removed;
+ forced_change = true;
+ }
+ }
+ let mut pieces: Vec<Vec<Op>> = streams.iter().map(|_| Vec::new()).collect();
+ let mut rewrite: Vec<bool> = vec![forced_change; streams.len()];
+ for op in parsed.ops {
+ let (first, last) = (piece_of(op.span.start), piece_of(op.span.end.saturating_sub(1)));
+ // An operator split across pieces is written whole into its keyword's piece, so every
+ // piece it spans is rewritten.
+ if first < last {
+ for r in rewrite.iter_mut().take(last + 1).skip(first) {
+ *r = true;
}
}
- let mut ops: Vec<Op> = Vec::with_capacity(parsed.ops.len());
- let mut changed = std::mem::take(&mut forced_change);
- for op in parsed.ops {
+ if let Some(p) = pieces.get_mut(last) {
+ p.push(op);
+ }
+ }
+
+ for (piece, mut changed) in pieces.into_iter().zip(rewrite) {
+ let mut ops: Vec<Op> = Vec::with_capacity(piece.len());
+ for op in piece {
let o = op.op.as_slice();
// Path construction.
if matches!(o, b"m" | b"l" | b"c" | b"v" | b"y" | b"h" | b"re" | b"W" | b"W*") {
diff --git a/crates/redact/src/tests.rs b/crates/redact/src/tests.rs
index 1f4175fa0..e42749e9a 100644
--- a/crates/redact/src/tests.rs
+++ b/crates/redact/src/tests.rs
@@ -117,6 +117,42 @@ fn added_text_parameters_do_not_keep_redacted_text() {
assert!(!bytes.windows(4).any(|w| w == b"1234"), "redacted text left in the saved file");
}
+#[test]
+fn operators_split_across_content_streams_are_redacted_whole() {
+ // One content stream in three pieces, split between tokens: a marked-content dictionary
+ // ends in the second piece, and the TJ array that ends it shows its glyphs with the
+ // operator in the third.
+ 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(),
+ b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] /Contents [4 0 R 7 0 R 8 0 R] /Resources << /Font << /F1 5 0 R >> >> >>".to_vec(),
+ stream("", b"/P << /MCID 0"),
+ FONT.replace("95 0 R", "6 0 R").into_bytes(),
+ widths(),
+ stream("", b">> BDC BT /F1 10 Tf 10 100 Td [(AB1234CD)]"),
+ stream("", b"TJ ET EMC BT /F1 10 Tf 10 200 Td (KEEP) Tj ET"),
+ ]);
+ assert_eq!(under(&mut doc, 0, &[[20.0, 95.0, 40.0, 110.0]]), 4, "the verifier sees the split TJ");
+ mark(&mut doc, 0, &[[20.0, 95.0, 40.0, 110.0]], "");
+ let r = apply(&mut doc, None).unwrap();
+ assert_eq!(r.glyphs, 4, "{r:?}");
+ let c = content(&doc, 0);
+ assert!(!c.contains("1234"), "redacted glyphs left in the page: {c}");
+ assert!(c.contains("KEEP"), "{c}");
+ assert_eq!(under(&mut doc, 0, &[[40.0, 95.0, 50.0, 110.0]]), 2, "C and D are where they were");
+ // Every operator still has its operands: none was cut off from them.
+ for op in pdfcraft_content::parse(c.as_bytes()).ops {
+ let want = match op.op.as_slice() {
+ b"TJ" | b"Tj" => 1,
+ b"BDC" | b"Tf" | b"Td" => 2,
+ _ => continue,
+ };
+ assert_eq!(op.operands.len(), want, "{} lost its operands in {c}", String::from_utf8_lossy(&op.op));
+ }
+ let doc = reopen(&doc);
+ assert!(!content(&doc, 0).contains("1234"));
+}
+
#[test]
fn kerning_spacing_scaling_and_line_operators_are_honoured() {
// TJ kerning, character and word spacing, 50% horizontal scaling, ' and ".