PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(redact): clear descriptions above removed content and annotations
aiongg7db37911ed7f
diff --git a/crates/redact/src/tags.rs b/crates/redact/src/tags.rsindex 854881060..60c158b18 100644--- a/crates/redact/src/tags.rs+++ b/crates/redact/src/tags.rs@@ -183,20 +183,21 @@ pub(crate) fn clean_within(// also from the parent tree and the ID tree: one listed only there still owns marked content// (#3302). The stack pops /K first, so an element reached through /K gets the page it inherits// from its ancestors before a tree reaches it without one.- let mut todo: Vec<(Object, Option<ObjRef>)> =- [&b"IDTree"[..], b"ParentTree", b"K"].iter().filter_map(|key| root.get(key).cloned()).map(|k| (k, None)).collect();+ let mut todo: Vec<(Object, Option<ObjRef>, Above)> =+ [&b"IDTree"[..], b"ParentTree", b"K"].iter().filter_map(|key| root.get(key).cloned()).map(|k| (k, None, Above::default())).collect();let mut seen = HashSet::new();let mut pages = HashMap::new();+ let mut parents = Parents::new();let mut hits: Vec<(ObjRef, Dict, Option<ObjRef>, bool)> = Vec::new();// Whether marked content `x` (a kid on page `pg`) is one this cleanup must change.let touches = |doc: &Document, x: &Object, pg: Option<ObjRef>| {mcid_of(doc, x, pg, page, stream).is_some_and(|m| stream.is_some() || changed.contains(&m) || empty.contains(&m))};- while let Some((k, pg)) = todo.pop() {+ while let Some((k, pg, above)) = todo.pop() {match k {- Object::Array(a) => todo.extend(a.into_iter().map(|x| (x, pg))),+ Object::Array(a) => todo.extend(a.into_iter().map(|x| (x, pg, above))),// A number or name tree node written in place.- Object::Dict(d) if !d.contains(b"S") => todo.extend(tree_node_entries(&d).into_iter().map(|x| (x, pg))),+ Object::Dict(d) if !d.contains(b"S") => todo.extend(tree_node_entries(&d).into_iter().map(|x| (x, pg, above))),// An element written in place: its kids are followed, but if it owns marked content// this cleanup changes, only rewriting what holds it could change it.Object::Dict(d) => {@@ -208,9 +209,11 @@ pub(crate) fn clean_within(if kids.iter().any(|x| touches(doc, x, pg)) {return Err(RedactError::Tags);}- todo.extend(kids.into_iter().map(|x| (x, pg)));+ let above = above.through(&d);+ todo.extend(kids.into_iter().map(|x| (x, pg, above)));}Object::Ref(r) => {+ parents.record(r, above);if !seen.insert(r) {continue;}@@ -219,13 +222,13 @@ pub(crate) fn clean_within(}let object = doc.get(r);if let Object::Array(a) = &*object {- todo.extend(a.iter().cloned().map(|x| (x, pg)));+ todo.extend(a.iter().cloned().map(|x| (x, pg, above)));continue;}let Some(d) = object.as_dict().cloned() else { continue };if !d.contains(b"S") {// A parent tree or ID tree node: its values may be elements.- todo.extend(tree_node_entries(&d).into_iter().map(|x| (x, pg)));+ todo.extend(tree_node_entries(&d).into_iter().map(|x| (x, pg, above)));continue;}let pg = match d.get(b"Pg").and_then(Object::as_ref).or(pg) {@@ -238,11 +241,12 @@ pub(crate) fn clean_within(if sensitive || remap {hits.push((r, d.clone(), pg, sensitive));}- todo.extend(kids.into_iter().map(|x| (x, pg)));+ todo.extend(kids.into_iter().map(|x| (x, pg, Above::element(r))));}_ => {}}}+ let sensitive: Vec<ObjRef> = hits.iter().filter(|h| h.3).map(|h| h.0).collect();let mut n = 0;for (r, d, pg, sensitive) in hits {let kids = d.get(b"K").map(|k| doc.resolve(k));@@ -291,9 +295,88 @@ pub(crate) fn clean_within(})?;n += usize::from(sensitive && doc.get(r).as_dict() != Some(&d));}+ // An element above may describe the changed content too (`/ActualText` on a span's parent+ // covers the span, §14.9.4).+ n += clear_ancestors(doc, &sensitive, &parents, limit)?;Ok(n)}+/// Where the walk reached an object from: the nearest element above it that is an object of its+/// own, and whether an element written in place between them carries a description (which only+/// rewriting the object holding it could remove).+#[derive(Clone, Copy, Default)]+struct Above {+ element: Option<ObjRef>,+ described_in_place: bool,+}++impl Above {+ fn element(r: ObjRef) -> Self {+ Self { element: Some(r), described_in_place: false }+ }++ /// Below an element written in place.+ fn through(self, d: &Dict) -> Self {+ Self { described_in_place: self.described_in_place || DESCRIPTIONS.iter().any(|k| d.contains(k)), ..self }+ }+}++/// The keys of an element that describe its content.+const DESCRIPTIONS: [&[u8]; 3] = [b"Alt", b"ActualText", b"E"];++/// Each element's parents as the walk found them: through `/K`, which `/P` may not agree with.+struct Parents(HashMap<ObjRef, Vec<Above>>);++impl Parents {+ fn new() -> Self {+ Self(HashMap::new())+ }++ fn record(&mut self, r: ObjRef, above: Above) {+ if above.element.is_some() || above.described_in_place {+ self.0.entry(r).or_default().push(above);+ }+ }+}++/// Remove the descriptions of every element above `elements`, through both `/P` and the walk's+/// `parents`; returns how many elements changed. [`RedactError::Tags`] when an element written in+/// place above them carries one, or when there are more than `limit` ancestors.+fn clear_ancestors(doc: &mut Document, elements: &[ObjRef], parents: &Parents, limit: usize) -> Result<usize, RedactError> {+ let mut todo: Vec<ObjRef> = elements.to_vec();+ let mut seen: HashSet<ObjRef> = elements.iter().copied().collect();+ let mut cleared = 0;+ while let Some(r) = todo.pop() {+ let mut up: Vec<ObjRef> = doc.get(r).as_dict().and_then(|d| d.get(b"P")).and_then(Object::as_ref).into_iter().collect();+ for above in parents.0.get(&r).into_iter().flatten() {+ if above.described_in_place {+ return Err(RedactError::Tags);+ }+ up.extend(above.element);+ }+ for a in up {+ if !seen.insert(a) {+ continue;+ }+ if seen.len() > limit {+ return Err(RedactError::Tags);+ }+ // The structure tree root (no /S) ends the climb.+ let Some(d) = doc.get(a).as_dict().filter(|d| d.contains(b"S")).cloned() else { continue };+ if DESCRIPTIONS.iter().any(|k| d.contains(k)) {+ doc.update_dict(a, |e| {+ for key in DESCRIPTIONS {+ e.remove(key);+ }+ })?;+ cleared += 1;+ }+ todo.push(a);+ }+ }+ Ok(cleared)+}+/// The page an element inherits (§14.7.4.4): the nearest `/Pg` among its ancestors, following/// `/P`. `Ok(None)` when none has one; [`RedactError::Tags`] when the lookup gives up (more than/// [`MAX_PARENTS`] ancestors, or a loop), which isn't the same as having no page. `pages` keeps@@ -384,12 +467,14 @@ pub(crate) fn drop_object_refs_within(doc: &mut Document, gone: &HashSet<ObjRef>};// Elements are reached from the root's /K, and also from the parent tree and the ID tree:// one listed only there (left behind by an editor, say) still points at its annotation.- let mut todo: Vec<Object> = [&b"K"[..], b"ParentTree", b"IDTree"].iter().filter_map(|key| root.get(key).cloned()).collect();+ let mut todo: Vec<(Object, Above)> =+ [&b"K"[..], b"ParentTree", b"IDTree"].iter().filter_map(|key| root.get(key).cloned()).map(|k| (k, Above::default())).collect();let mut seen = HashSet::new();+ let mut parents = Parents::new();let mut hits: Vec<ObjRef> = Vec::new();- while let Some(k) = todo.pop() {+ while let Some((k, above)) = todo.pop() {match k {- Object::Array(a) => todo.extend(a),+ Object::Array(a) => todo.extend(a.into_iter().map(|x| (x, above))),// A structure element written in place: its kids are followed; one that tags a// removed annotation can't be changed here (see above).Object::Dict(d) if d.contains(b"S") => {@@ -397,11 +482,13 @@ pub(crate) fn drop_object_refs_within(doc: &mut Document, gone: &HashSet<ObjRef>if kids.iter().any(|x| refers_to(doc, x, gone)) {return Err(RedactError::Tags);}- todo.extend(kids);+ let above = above.through(&d);+ todo.extend(kids.into_iter().map(|x| (x, above)));}// A number or name tree node written in place (§7.9.6, §7.9.7): its values and kids.- Object::Dict(d) => todo.extend(tree_node_entries(&d)),+ Object::Dict(d) => todo.extend(tree_node_entries(&d).into_iter().map(|x| (x, above))),Object::Ref(r) => {+ parents.record(r, above);if !seen.insert(r) {continue;}@@ -410,26 +497,26 @@ pub(crate) fn drop_object_refs_within(doc: &mut Document, gone: &HashSet<ObjRef>}let object = doc.get(r);if let Object::Array(a) = &*object {- todo.extend(a.iter().cloned());+ todo.extend(a.iter().cloned().map(|x| (x, above)));continue;}let Some(d) = object.as_dict() else { continue };if !d.contains(b"S") {// A parent tree or ID tree node: its values may be elements.- todo.extend(tree_node_entries(d));+ todo.extend(tree_node_entries(d).into_iter().map(|x| (x, above)));continue;}let kids = kids_of(doc, d);if kids.iter().any(|x| refers_to(doc, x, gone)) {hits.push(r);}- todo.extend(kids);+ todo.extend(kids.into_iter().map(|x| (x, Above::element(r))));}_ => {}}}let mut n = 0;- for r in hits {+ for &r in &hits {let Some(d) = doc.get(r).as_dict().cloned() else { continue };let kids = d.get(b"K").map(|k| doc.resolve(k));let array = kids.as_ref().is_some_and(|k| matches!(&**k, Object::Array(_)));@@ -449,15 +536,15 @@ pub(crate) fn drop_object_refs_within(doc: &mut Document, gone: &HashSet<ObjRef>} else {e.remove(b"K");}- // An element left with nothing to tag keeps no text describing what it tagged:- // a link's alternate text often repeats the URL, an annotation's its comment.- if keep.is_empty() {- for key in [&b"Alt"[..], b"ActualText", b"T", b"E"] {- e.remove(key);- }+ // An element that lost an annotation keeps no text describing it, even with other+ // kids left: a link's alternate text often repeats the URL, an annotation's its+ // comment. Elements above it may describe it too.+ for key in [&b"Alt"[..], b"ActualText", b"T", b"E"] {+ e.remove(key);}})?;}+ clear_ancestors(doc, &hits, &parents, limit)?;Ok(n)}diff --git a/crates/redact/src/tests.rs b/crates/redact/src/tests.rsindex 94081f9e3..ebc95f451 100644--- a/crates/redact/src/tests.rs+++ b/crates/redact/src/tests.rs@@ -2461,6 +2461,7 @@ fn unused_descriptions_in_shared_or_inherited_resources_are_retired() {}}+mod descriptions;mod limits;mod property_retirement;mod surviving_references;diff --git a/crates/redact/src/tests/descriptions.rs b/crates/redact/src/tests/descriptions.rsnew file mode 100644index 000000000..df6b8555d--- /dev/null+++ b/crates/redact/src/tests/descriptions.rs@@ -0,0 +1,64 @@+//! Descriptions in the structure tree (alternate text, actual text, expansions) go with the+//! content they describe, on the element that owns it and on every element above it.++use super::surviving_references::full_save_holds;+use super::*;++/// A tagged page: "SECRET" (MCID 0) inside a section whose descriptions repeat it, and "Public"+/// (MCID 1). `paragraph_parent` is the secret paragraph's /P.+fn sectioned(paragraph_parent: &str) -> Document {+ let content = b"/P <</MCID 0>> BDC BT /F1 10 Tf 10 200 Td (SECRET) Tj ET EMC /P <</MCID 1>> BDC BT /F1 10 Tf 10 100 Td (Public) Tj ET EMC";+ pdf(vec![+ b"<< /Type /Catalog /Pages 2 0 R /StructTreeRoot 7 0 R /MarkInfo << /Marked true >> >>".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 /StructParents 0 /Resources << /Font << /F1 5 0 R >> >> >>".to_vec(),+ stream("", content),+ FONT.replace("95 0 R", "6 0 R").into_bytes(),+ widths(),+ b"<< /Type /StructTreeRoot /K 8 0 R >>".to_vec(),+ b"<< /S /Document /P 7 0 R /K [11 0 R 10 0 R] >>".to_vec(),+ format!("<< /S /P /P {paragraph_parent} /Pg 3 0 R /K 0 >>").into_bytes(),+ b"<< /S /P /P 8 0 R /Pg 3 0 R /ActualText (Public) /K 1 >>".to_vec(),+ b"<< /S /Sect /P 8 0 R /ActualText (LEAK_ACTUAL) /Alt (LEAK_ALT) /E (LEAK_EXPANSION) /K 9 0 R >>".to_vec(),+ ])+}++#[test]+fn an_ancestor_describing_redacted_content_loses_its_descriptions() {+ // The paragraph names its section as parent, or (wrongly) the document: the section is found+ // either way, by its /K.+ for parent in ["11 0 R", "8 0 R"] {+ let mut doc = sectioned(parent);+ mark(&mut doc, 0, &[[5.0, 195.0, 60.0, 212.0]], "");+ apply(&mut doc, None).unwrap();+ assert!(!full_save_holds(&doc, b"LEAK_"), "a description of the redacted word survived (paragraph /P {parent})");+ let public = doc.get(ObjRef::new(10, 0)).as_dict().cloned().unwrap();+ assert!(public.contains(b"ActualText"), "an element beside it keeps its own");+ }+}++#[test]+fn a_link_element_that_keeps_its_text_loses_the_description_of_its_removed_annotation() {+ use crate::sanitize::{Hidden, remove_hidden};+ let mut doc = pdf(vec![+ b"<< /Type /Catalog /Pages 2 0 R /MarkInfo << /Marked true >> /StructTreeRoot 8 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 /Resources << /Font << /F1 5 0 R >> >> /Annots [6 0 R] /StructParents 0 >>"+ .to_vec(),+ stream("", b"/Link << /MCID 0 >> BDC BT /F1 10 Tf 10 230 Td (See the site) Tj ET EMC"),+ FONT.replace("95 0 R", "12 0 R").into_bytes(),+ b"<< /Type /Annot /Subtype /Link /Rect [10 228 80 240] /StructParent 1 /A << /S /URI /URI (https://example.org/) >> >>".to_vec(),+ b"null".to_vec(),+ b"<< /Type /StructTreeRoot /K 9 0 R /ParentTree << /Nums [0 [11 0 R] 1 11 0 R] >> /ParentTreeNextKey 2 >>".to_vec(),+ b"<< /Type /StructElem /S /Document /P 8 0 R /K 10 0 R >>".to_vec(),+ b"<< /Type /StructElem /S /P /P 9 0 R /ActualText (See LEAK_PARENT) /K 11 0 R >>".to_vec(),+ // The link element tags its text and the annotation; its descriptions repeat the address.+ b"<< /Type /StructElem /S /Link /P 10 0 R /Pg 3 0 R /Alt (LEAK_ALT) /ActualText (LEAK_ACTUAL) /E (LEAK_E) /T (LEAK_T) /K [0 << /Type /OBJR /Obj 6 0 R /Pg 3 0 R >>] >>".to_vec(),+ widths(),+ ]);+ let done = remove_hidden(&mut doc, &[Hidden::LinksActionsScripts]).unwrap();+ assert!(done.contains(&(Hidden::LinksActionsScripts, 1)), "{done:?}");+ assert!(!full_save_holds(&doc, b"LEAK_"), "a description of the removed link survived");+ let link = doc.get(ObjRef::new(11, 0)).as_dict().cloned().unwrap();+ assert_eq!(link.get(b"K"), Some(&Object::Array(vec![Object::Int(0)])), "the element keeps its text");+}diff --git a/crates/redact/src/tests/surviving_references.rs b/crates/redact/src/tests/surviving_references.rsindex 2c8bdb09c..d3aa98af6 100644--- a/crates/redact/src/tests/surviving_references.rs+++ b/crates/redact/src/tests/surviving_references.rs@@ -3,7 +3,7 @@use super::*;/// Whether `secret` survives a full save of `doc`, in the file's bytes or in a decoded stream.-fn full_save_holds(doc: &Document, secret: &[u8]) -> bool {+pub(super) fn full_save_holds(doc: &Document, secret: &[u8]) -> bool {let bytes = write_full(doc, &SaveOptions::default()).unwrap();let found = |data: &[u8]| data.windows(secret.len()).any(|w| w == secret);if found(&bytes) {