fix(redact): redact an XObject or font whose /Subtype is a reference (#6750)

## What this changes

Fixes #6750 (audit F-9).

Redaction kept an image under a mark when the image's `/Subtype` was an indirect reference (`/Subtype 8 0 R`, object 8 being `/Image`). `xobject()` in `crates/redact/src/interp.rs` matched on `s.dict.name(b"Subtype")`, and `Dict::name` does not follow references. The image fell through to `_ => None`, so the `Do`, the resource entry and the samples stayed in the saved file under the black box. The renderer still draws such an image.

- `xobject()` resolves `/Subtype` before matching, so an image or form named by reference is redacted like a direct one.
- `is_vertical()` does the same for a Type0 font, so vertical text in a font with a referenced `/Subtype` still makes Apply refuse rather than pass.
- The `_` arm now fails closed. An XObject with no `/Subtype`, or one the redactor doesn't read, is removed in Apply when its placement hits a mark. Verify counts it as residue there. Its placement is the unit square an image fills, or the `/BBox` under its `/Matrix` when it has one. Apply counts it in `forms_removed` and sets `removed_cut`, as it does for a form it cannot read. A placement outside every mark is left alone.

Related open PRs, checked for overlap: #5849 resolves `/Subtype` on annotations in `lib.rs`, and #5662 resolves an indirect `/Subtype /Form` in `prune.rs`. Neither touches `xobject()` or `is_vertical()`.

## How you verified it

- [x] `cargo fmt --check` (clean)
- [ ] clippy with `-D warnings` on `pdfcraft-redact`: **not run locally**
- [ ] tests pass on `pdfcraft-redact`: **not run locally with the fix**, see below

**What ran.** With the three new or extended tests in place and `interp.rs` reverted to `main`, I ran `cargo test -p pdfcraft-redact --lib -- an_image_whose_subtype an_xobject_of_unknown vertical_writing_text`. All three failed on the expected assertions:
- `an_image_whose_subtype_is_a_reference_is_redacted`: the direct `/Subtype /Image` control passed, then the `8 0 R` case failed with `images_removed: 0`.
- `an_xobject_of_unknown_subtype_under_a_mark_goes`: the verifier counted 0, expected 1.
- `vertical_writing_text_under_a_mark_fails_closed`: the referenced `/Subtype /Type0` case did not refuse.

**What did not run.** Local builds were stopped for this change before the run with the fix completed. I have **not** seen these tests pass with the fix, nor run the rest of the crate's tests or clippy. I'm relying on CI for those. This PR is a draft until CI passes.

## Never-crash

- [x] No new `unwrap`/`expect`/`panic!`/`unreachable!`/`todo!` outside tests
- [x] No indexing with input-derived positions (the `/BBox` is read with a slice pattern)
- [x] No `unsafe`

## Parity and reach

No parity change: this hardens the existing Apply Redactions behaviour.

## Anything a reviewer should look at first

The fail-closed `_` arm. It removes an unclassifiable XObject only where a placement hits a mark, never elsewhere. Verify counts it as residue, so the post-apply check would refuse if Apply ever left one.
This commit is contained in:
Michael Higgins
2026-11-18 16:35:49 +07:00
committed by GitHub
parent d5bcb41099
commit 6da8fa27ed
4 changed files with 165 additions and 11 deletions
+34 -3
View File
@@ -342,7 +342,9 @@ fn res_dict(doc: &Document, res: &Dict, key: &[u8]) -> Dict {
/// predefined CMap name ending in `-V` (Identity-V and the like), or an embedded CMap stream
/// whose `/WMode` is 1 (ISO 32000-2 §9.7.5).
fn is_vertical(doc: &Document, font: &Dict) -> bool {
if font.name(b"Subtype") != Some(b"Type0".as_slice()) {
// `/Subtype` may be written by reference (#6750).
let subtype = font.get(b"Subtype").map(|s| doc.resolve(s));
if subtype.as_deref().and_then(Object::as_name) != Some(b"Type0".as_slice()) {
return false;
}
match font.get(b"Encoding").map(|e| doc.resolve(e)).as_deref() {
@@ -1120,7 +1122,10 @@ fn xobject(
let r = xobjects.get(&name)?.as_ref()?;
let obj = doc.get(r);
let Object::Stream(s) = &*obj else { return None };
match s.dict.name(b"Subtype") {
// `/Subtype` may be written by reference, and the renderer follows it: an image named that way
// is still drawn, so it must still be redacted (#6750).
let subtype = s.dict.get(b"Subtype").map(|t| doc.resolve(t));
match subtype.as_deref().and_then(Object::as_name) {
_ if s.dict.get(b"OC").is_some_and(|oc| scope.xobject_layer_hidden(doc, r, oc)) => {
// Verify mode must count hidden XObjects too: scan uses that count to decide
// whether the HiddenLayers pass is necessary.
@@ -1349,7 +1354,33 @@ fn xobject(
scope.report.forms_rewritten += 1;
Some(Some(Op::new("Do", vec![Object::Name(n)])))
}
_ => None,
// No `/Subtype`, or one the redactor does not read: what it draws is unknown, so a
// placement under a mark fails closed. Apply removes it, as it does a form it cannot read,
// and verification counts it as what may remain. Its place is the unit square an image
// fills or, when it has a `/BBox`, the box a form would draw in: either one under a mark
// counts. Both are read as the proof reads a form's, entries by reference included; a
// `/Matrix` or `/BBox` that is present but malformed leaves its place unknown, so it counts
// as under a mark rather than as outside every one.
_ => {
let unit = gs.ctm.bbox([0.0, 0.0, 1.0, 1.0]);
let framed = match (crate::verify::matrix_of(doc, &s.dict), crate::verify::bbox_of(doc, &s.dict)) {
(Ok(m), Ok(bbox)) => bbox.is_some_and(|b| scope.hits(m.then(&gs.ctm).bbox(b))),
_ => true,
};
if !scope.hits(unit) && !framed {
return None;
}
if scope.mode == Mode::Verify {
out.residue += 1;
return None;
}
scope.report.forms_removed += 1;
// Any text it held was never read, so the metadata check can't know what went with it.
scope.removed_cut = true;
out.replaced.push(name);
out.doomed.push(r);
Some(None)
}
}
}
+103
View File
@@ -817,6 +817,16 @@ fn vertical_writing_text_under_a_mark_fails_closed() {
mark(&mut hdoc, 0, &[[85.0, 55.5, 105.0, 61.0]], "");
let r = apply(&mut hdoc, None).unwrap();
assert_eq!(r.glyphs, 2, "the horizontal control is redacted: {r:?}");
// A Type0 font whose /Subtype is a reference still writes vertically (#6750).
let mut rdoc = make("Identity-V");
let page = pdfcraft_model::pages(&rdoc)[0].obj;
let res = rdoc.get(page).as_dict().and_then(|d| d.get(b"Resources")).and_then(Object::as_dict).cloned().unwrap();
let font_ref = res.get(b"Font").and_then(Object::as_dict).and_then(|f| f.get(b"F2")).and_then(Object::as_ref).unwrap();
let name_ref = rdoc.add(Object::Name(b"Type0".to_vec()));
rdoc.update_dict(font_ref, |d| d.set(b"Subtype".to_vec(), Object::Ref(name_ref))).unwrap();
mark(&mut rdoc, 0, &[[85.0, 55.5, 105.0, 61.0]], "");
assert_eq!(apply(&mut rdoc, None).unwrap_err(), RedactError::VerticalText(1), "a referenced /Subtype /Type0 is still vertical");
}
#[test]
@@ -840,6 +850,99 @@ fn images_are_removed_or_have_their_pixels_cleared() {
assert_eq!(orig.decoded().unwrap(), [255; 4], "the shared original is untouched");
}
/// `/Subtype 8 0 R` naming `/Image` is still an image, and the renderer draws it. Redaction read
/// only a direct name, so it kept the `Do` and the samples under the box (#6750). The direct
/// `/Subtype /Image` is the control.
#[test]
fn an_image_whose_subtype_is_a_reference_is_redacted() {
const MARKER: &[u8; 16] = b"F9-SECRET-PIXELS";
for subtype in ["/Image", "8 0 R"] {
let img = stream(&format!("/Type /XObject /Subtype {subtype} /Width 16 /Height 1 /ColorSpace /DeviceGray /BitsPerComponent 8"), MARKER);
let mut doc = one_page(b"q 10 0 0 10 10 10 cm /Im1 Do Q", "/XObject << /Im1 7 0 R >>", vec![img, b"/Image".to_vec()]);
mark(&mut doc, 0, &[[5.0, 5.0, 25.0, 25.0]], "");
let r = apply(&mut doc, None).unwrap();
assert_eq!(r.images_removed, 1, "{subtype}: {r:?}");
let c = content(&doc, 0);
assert!(!c.contains("/Im1 Do"), "{subtype}: {c}");
let bytes = write_full(&doc, &SaveOptions { object_streams: false, ..SaveOptions::default() }).unwrap();
assert!(!bytes.windows(MARKER.len()).any(|w| w == MARKER), "{subtype}: the image samples stay in the saved file");
}
}
/// An XObject the redactor cannot classify (no `/Subtype`, or one it does not know) may still draw
/// something under a mark. Apply removes it there and verification counts it; one elsewhere stays.
#[test]
fn an_xobject_of_unknown_subtype_under_a_mark_goes() {
for subtype in ["", "/Subtype /PS", "/Subtype 42"] {
let x = stream(&format!("/Type /XObject {subtype} /Width 1 /Height 1 /ColorSpace /DeviceGray /BitsPerComponent 8"), [0]);
let src = b"q 10 0 0 10 10 10 cm /X1 Do Q q 10 0 0 10 200 200 cm /X1 Do Q";
let mut doc = one_page(src, "/XObject << /X1 7 0 R >>", vec![x]);
let rects = [[5.0, 5.0, 25.0, 25.0]];
assert_eq!(under(&mut doc, 0, &rects), 1, "{subtype:?}: verification counts it");
mark(&mut doc, 0, &rects, "");
let r = apply(&mut doc, None).unwrap();
assert_eq!(r.forms_removed, 1, "{subtype:?}: {r:?}");
let c = content(&doc, 0);
assert_eq!(c.matches("/X1 Do").count(), 1, "{subtype:?}: only the placement outside the mark stays: {c}");
assert!(c.contains("200 200 cm"), "{subtype:?}: {c}");
}
}
/// An unclassifiable XObject's `/Matrix` and `/BBox` are read with their entries by reference, as
/// the proof reads a form's. Read only directly, a box moved onto the mark was taken for the
/// identity or for no box, and the placement was kept and not counted. Present but malformed, its
/// place can't be told, so it counts. The direct entries are the control.
#[test]
fn an_xobject_of_unknown_subtype_is_placed_by_referenced_geometry() {
for geometry in [
"/Matrix [1 0 0 1 100 100] /BBox [0 0 10 10]",
"/Matrix [1 0 0 1 8 0 R 9 0 R] /BBox [0 0 10 10]",
"/BBox [100 100 10 0 R 10 0 R]",
"/Matrix [1 0 0 1 100] /BBox [0 0 10 10]",
"/BBox [0 0 10]",
] {
let x = stream(&format!("/Type /XObject /Subtype /PS {geometry}"), [0]);
let mut doc = one_page(b"/X1 Do", "/XObject << /X1 7 0 R >>", vec![x, b"100".to_vec(), b"100".to_vec(), b"110".to_vec()]);
let rects = [[105.0, 105.0, 110.0, 110.0]];
assert_eq!(under(&mut doc, 0, &rects), 1, "{geometry}: verification counts it");
mark(&mut doc, 0, &rects, "");
let r = apply(&mut doc, None).unwrap();
assert_eq!(r.forms_removed, 1, "{geometry}: {r:?}");
let c = content(&doc, 0);
assert!(!c.contains("/X1 Do"), "{geometry}: {c}");
}
}
/// A `/BBox` of null is no entry (ISO 32000-2 §7.3.9), directly or by reference. It was read as a
/// malformed box, so an unclassifiable XObject placed outside every mark was removed and counted.
/// No `/BBox` at all is the control.
#[test]
fn an_xobject_of_unknown_subtype_with_a_null_bbox_is_placed_as_without_one() {
for bbox in ["", "/BBox null", "/BBox 8 0 R"] {
let x = stream(&format!("/Type /XObject /Subtype /PS {bbox}"), [0]);
let src = b"q 10 0 0 10 200 200 cm /X1 Do Q";
let mut doc = one_page(src, "/XObject << /X1 7 0 R >>", vec![x, b"null".to_vec()]);
let rects = [[5.0, 5.0, 25.0, 25.0]];
assert_eq!(under(&mut doc, 0, &rects), 0, "{bbox:?}: verification leaves it out");
mark(&mut doc, 0, &rects, "");
let r = apply(&mut doc, None).unwrap();
assert_eq!(r.forms_removed, 0, "{bbox:?}: {r:?}");
let c = content(&doc, 0);
assert!(c.contains("/X1 Do"), "{bbox:?}: the placement outside the mark stays: {c}");
}
}
/// A `/BBox` is read only when it has four entries; any other length is malformed whatever its
/// entries, so the placement's place can't be told and it counts as under a mark.
#[test]
fn an_xobject_of_unknown_subtype_with_a_bbox_of_the_wrong_length_counts() {
for bbox in ["/BBox []", "/BBox [0 0 10 10 10]", "/BBox [0 0 10 10 8 0 R]"] {
let x = stream(&format!("/Type /XObject /Subtype /PS {bbox}"), [0]);
let mut doc = one_page(b"q 10 0 0 10 200 200 cm /X1 Do Q", "/XObject << /X1 7 0 R >>", vec![x, b"10".to_vec()]);
assert_eq!(under(&mut doc, 0, &[[5.0, 5.0, 25.0, 25.0]]), 1, "{bbox:?}: verification counts it");
}
}
#[test]
fn vectors_are_removed_or_clipped_and_inline_images_go() {
let src = b"0 g 10 10 20 20 re f 0 0 300 300 re f q 10 0 0 10 50 50 cm BI /W 1 /H 1 /CS /G /BPC 8 ID \x80 EI Q 1 0 0 RG 150 150 m 160 160 l S";
+27 -7
View File
@@ -431,15 +431,35 @@ pub(crate) fn matrix_of(doc: &Document, dict: &Dict) -> Result<Matrix, ()> {
}
/// A form's `/BBox` as `[x0 y0 x1 y1]`: absent is `None`, present but malformed is an error.
pub(super) fn bbox_of(doc: &Document, dict: &Dict) -> Result<Option<[f64; 4]>, ()> {
pub(crate) fn bbox_of(doc: &Document, dict: &Dict) -> Result<Option<[f64; 4]>, ()> {
let Some(b) = dict.get(b"BBox") else { return Ok(None) };
let b = doc.resolve(b);
let v: Vec<f64> =
b.as_array().ok_or(())?.iter().map(|x| doc.resolve(x).as_f64().filter(|v| v.is_finite()).ok_or(())).collect::<Result<_, _>>()?;
match v[..] {
[a, b, c, d] => Ok(Some([a.min(c), b.min(d), a.max(c), b.max(d)])),
_ => Err(()),
// Only a reference is resolved; a direct value is read in place, since `resolve` copies it.
// The walk reads the box at every `Do`, and a direct array of any length (or an entry that is
// itself a large array) would be copied each time.
let held;
let b = match b {
Object::Ref(_) => {
held = doc.resolve(b);
&*held
}
direct => direct,
};
// A null value is no entry (ISO 32000-2 §7.3.9), whether given directly or by reference.
if matches!(b, Object::Null) {
return Ok(None);
}
// The length is checked before any entry is read.
let items = b.as_array().filter(|a| a.len() == 4).ok_or(())?;
let mut v = [0.0; 4];
for (value, item) in v.iter_mut().zip(items) {
let n = match item {
Object::Ref(_) => doc.resolve(item).as_f64(),
direct => direct.as_f64(),
};
*value = n.filter(|n| n.is_finite()).ok_or(())?;
}
let [a, b, c, d] = v;
Ok(Some([a.min(c), b.min(d), a.max(c), b.max(d)]))
}
/// The content streams of a page as `(raw object, decoded)`, a missing or unreadable entry an
+1 -1
View File
@@ -70,7 +70,7 @@ mod snapshot;
mod sweep;
mod util;
pub(crate) use extract::matrix_of;
pub(crate) use extract::{bbox_of, matrix_of};
pub use snapshot::Snapshot;
/// What the proof established for one region.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]