From f8197e29ca61037cfa83751104de1902b07fd003 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 21:59:15 -0500 Subject: [PATCH] fix(fonts): a CMap's codespace ranges are at most four bytes and listed once (#6075) --- crates/fonts/src/pdf.rs | 28 +++++++++++++++++++--------- crates/fonts/src/pdf/tests.rs | 21 ++++++++++++++++++++- crates/redact/src/interp.rs | 12 ++++++++++++ crates/redact/src/lib.rs | 7 +++++++ crates/redact/src/tests.rs | 17 +++++++++++++++++ 5 files changed, 75 insertions(+), 10 deletions(-) diff --git a/crates/fonts/src/pdf.rs b/crates/fonts/src/pdf.rs index 7ae75ddf31..4ff8feb722 100644 --- a/crates/fonts/src/pdf.rs +++ b/crates/fonts/src/pdf.rs @@ -75,6 +75,10 @@ pub struct Metrics { /// read that fails without one, such as an empty stream behind a filter nothing decodes, /// doesn't set it, with or without anything left. pub past_allowance: bool, + /// A codespace range of the embedded CMap was dropped (not one to four bytes, or past the range + /// limit): how the font's codes split into characters is then a guess, and a caller that must + /// know which text a string holds (redaction) refuses to rely on it. + pub codespaces_dropped: bool, } fn nums(doc: &Document, o: Option<&Object>) -> Vec { @@ -838,18 +842,22 @@ pub fn base_font_name(doc: &Document, font: &Dict) -> String { /// Codespace ranges and CID mappings from an embedded CMap stream, at most /// [`MAX_CMAP_RANGES`] of each (every glyph searches them). #[allow(clippy::type_complexity)] -fn parse_cmap(data: &[u8]) -> (Vec<(usize, Vec, Vec)>, Vec<(u32, u32, u32)>) { +fn parse_cmap(data: &[u8]) -> (Vec<(usize, Vec, Vec)>, Vec<(u32, u32, u32)>, bool) { let (mut spaces, mut cids) = (Vec::new(), Vec::new()); + let mut dropped = false; + let mut seen = HashSet::new(); let mut toks = Lexer::new(data); while let Some(t) = toks.next() { match t { Tok::Word(b"begincodespacerange") => { while let Some([lo, hi]) = entry(&mut toks, b"endcodespacerange") { - if let (Tok::Hex(Some(lo)), Tok::Hex(Some(hi))) = (lo, hi) - && lo.len() == hi.len() - && spaces.len() < MAX_CMAP_RANGES - { - spaces.push((lo.len(), lo, hi)); + if let (Tok::Hex(Some(lo)), Tok::Hex(Some(hi))) = (lo, hi) { + // A code is at most four bytes; a repeated range can never match first, so it only costs every glyph a comparison. + if lo.len() != hi.len() || !(1..=4).contains(&lo.len()) || spaces.len() >= MAX_CMAP_RANGES { + dropped = true; + } else if seen.insert((lo.clone(), hi.clone())) { + spaces.push((lo.len(), lo, hi)); + } } } } @@ -875,7 +883,7 @@ fn parse_cmap(data: &[u8]) -> (Vec<(usize, Vec, Vec)>, Vec<(u32, u32, u3 } } spaces.sort_by_key(|s| s.0); - (spaces, cids) + (spaces, cids, dropped) } impl Metrics { @@ -905,6 +913,7 @@ impl Metrics { explicit_reverse: (32..127u8).map(|c| (char::from(c), u32::from(c))).collect(), outlined_cids: Vec::new(), past_allowance: false, + codespaces_dropped: false, } } @@ -935,7 +944,7 @@ impl Metrics { m.code_len = match &m.codes { Codes::One => 1, Codes::Two => 2, - Codes::Ranges(r) => r.last().map_or(2, |x| x.0), + Codes::Ranges(r) => r.last().map_or(2, |x| x.0.clamp(1, 4)), }; m.unicode = unicode_map(doc, font, m.composite, &mut reads); if m.std14.is_some() { @@ -977,7 +986,8 @@ impl Metrics { && let Object::Stream(s) = &*e && let Some(data) = reads.decode(s, MAX_CMAP_BYTES) { - let (spaces, cids) = parse_cmap(&data); + let (spaces, cids, dropped) = parse_cmap(&data); + m.codespaces_dropped = dropped; if !spaces.is_empty() { m.codes = Codes::Ranges(spaces); } diff --git a/crates/fonts/src/pdf/tests.rs b/crates/fonts/src/pdf/tests.rs index 74880c94a4..e85870d4a7 100644 --- a/crates/fonts/src/pdf/tests.rs +++ b/crates/fonts/src/pdf/tests.rs @@ -1557,7 +1557,7 @@ fn cmap_tokens_need_no_space_between_them() { let mut got: Vec<_> = map.iter().map(|(c, u)| (*c, u.as_str())).collect(); got.sort_unstable(); assert_eq!(got, [(3, " "), (0x10, "é"), (0x24, "A"), (0x25, "B"), (0x26, "C"), (0x30, "H"), (0x31, "i")]); - let (spaces, cids) = parse_cmap(b"1 begincodespacerange<00>endcodespacerange 1 begincidrange<20><7E>1 endcidrange"); + let (spaces, cids, _) = parse_cmap(b"1 begincodespacerange<00>endcodespacerange 1 begincidrange<20><7E>1 endcidrange"); assert_eq!(spaces, [(1, vec![0], vec![0xff])]); assert_eq!(cids, [(0x20, 0x7e, 1)]); } @@ -1823,6 +1823,16 @@ fn low_codes_resolve_to_the_covering_range() { assert_eq!((u.covering(0xFFFF).is_some(), u.covering(0x1_0001).is_some(), u.covering(0x1_0002).is_none()), (true, true, true)); } +#[test] +fn codespaces_are_deduplicated_and_at_most_four_bytes() { + let cmap = "3 begincodespacerange <00> <7F> <00> <7F> <0000000000> endcodespacerange"; + let (spaces, ..) = parse_cmap(cmap.as_bytes()); + assert_eq!(spaces, vec![(1, vec![0x00], vec![0x7F])]); + let many: String = (0..2000).map(|_| "<0000> \n").collect(); + let (spaces, ..) = parse_cmap(format!("2000 begincodespacerange\n{many}endcodespacerange").as_bytes()); + assert_eq!(spaces.len(), 1); +} + #[test] fn base_font_name_matches_the_metrics_name() { let doc = Document::new_empty(); @@ -2146,3 +2156,12 @@ fn a_full_cid_map_rebuilds_a_composite_subsets_cmap_behind_two_filters() { let editor = embedded_sfnt(&doc, &source).expect("the map behind two filters is read"); assert_eq!(skrifa::FontRef::new(&editor).unwrap().charmap().map('Ω'), Some(expected)); } + +/// A codespace range of five bytes, or one whose ends differ in length, can't be kept and is said +/// to be dropped; a repeated range is not a loss. +#[test] +fn a_dropped_codespace_range_is_reported() { + assert!(parse_cmap(b"1 begincodespacerange <0000000000> endcodespacerange").2); + assert!(parse_cmap(b"1 begincodespacerange <00> endcodespacerange").2); + assert!(!parse_cmap(b"2 begincodespacerange <0000> <0000> endcodespacerange").2); +} diff --git a/crates/redact/src/interp.rs b/crates/redact/src/interp.rs index 12636dc88f..6c9e675269 100644 --- a/crates/redact/src/interp.rs +++ b/crates/redact/src/interp.rs @@ -145,6 +145,9 @@ pub(crate) struct Output { /// Apply mode met vertical-writing text (which it cannot remove) while a redaction region was /// in force: the operation must fail closed rather than leave that text under the mark. pub vertical: bool, + /// Apply mode met text in a font whose CMap dropped codespace ranges while a redaction region + /// was in force: which text its strings hold is a guess, so the operation must fail closed. + pub dropped_codes: bool, /// The content holds an inline image the renderer reads no image for /// ([`pdfcraft_content::Parsed::ambiguous_images`]): what this scope placed after it may not /// be what the page shows, so a redaction must not trust it. @@ -845,6 +848,14 @@ impl Walk { { self.out.vertical = true; } + if self.gs.font.codespaces_dropped + && scope.mode == Mode::Apply + && scope.hidden_text.is_none() + && !scope.rects.is_empty() + && items.iter().any(|o| matches!(o, Object::String(s) if !s.bytes.is_empty())) + { + self.out.dropped_codes = true; + } let (new_items, removed) = show(scope, &self.gs, &mut self.tm, &items); if removed == 0 { return Some(op); @@ -1256,6 +1267,7 @@ fn xobject( scope.fonts = saved_fonts; out.residue += inner.residue; out.vertical |= inner.vertical; + out.dropped_codes |= inner.dropped_codes; out.ambiguous |= inner.ambiguous; out.unparsed |= inner.unparsed; out.doomed.extend_from_slice(&inner.doomed); diff --git a/crates/redact/src/lib.rs b/crates/redact/src/lib.rs index 819cae99cd..adc5ec527d 100644 --- a/crates/redact/src/lib.rs +++ b/crates/redact/src/lib.rs @@ -81,6 +81,10 @@ pub enum RedactError { "page {0} has vertical-writing text while a redaction mark is pending, and PdfCraft cannot tell whether that text lies under the mark yet, so nothing was changed" )] VerticalText(usize), + #[error( + "page {0} shows text in a font whose CMap has codespace ranges PdfCraft could not read, so it cannot tell which text a string holds while a redaction mark is pending; nothing was changed" + )] + DroppedCodespace(usize), #[error( "redaction appearance has invalid geometry or exceeds the 8 MiB budget; reduce the marked area, turn off repeated text or apply fewer pages at once (nothing was redacted)" )] @@ -855,6 +859,9 @@ fn redact_page_content( if out.vertical { return Err(RedactError::VerticalText(pi + 1)); } + if out.dropped_codes { + return Err(RedactError::DroppedCodespace(pi + 1)); + } // An inline image the renderer reads no image for (no `EI` it accepts, or parameters it // can't read): where the page's operators resume after it is a guess, so neither removal // nor the verify pass can be trusted on this page. Likewise bytes the parser can't read in diff --git a/crates/redact/src/tests.rs b/crates/redact/src/tests.rs index 43b6caaadc..96a4f1439a 100644 --- a/crates/redact/src/tests.rs +++ b/crates/redact/src/tests.rs @@ -94,6 +94,23 @@ fn redacted_paint_loses_the_actual_text_that_names_it() { assert!(!c.contains("CLIPS"), "{c}"); } +/// A CMap with a codespace range the font parser can't keep (five bytes) makes the page's text a +/// guess: apply refuses while a mark is pending. +#[test] +fn a_cmap_with_a_dropped_codespace_range_fails_closed() { + let program = "/CIDInit /ProcSet findresource begin 12 dict begin begincmap 2 begincodespacerange <0000> <0000000000> endcodespacerange 1 begincidrange <0000> 0 endcidrange endcmap CMapName currentdict /CMap defineresource pop end end"; + 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 /Resources << /Font << /F2 5 0 R >> >> >>".to_vec(), + stream("", b"BT /F2 10 Tf 100 50 Td <0001000200030004> Tj ET"), + b"<< /Type /Font /Subtype /Type0 /BaseFont /X /Encoding 6 0 R /DescendantFonts [<< /Type /Font /Subtype /CIDFontType2 /BaseFont /X /DW 1000 >>] >>".to_vec(), + stream("/Type /CMap /CMapName /Custom", program.as_bytes()), + ]); + mark(&mut doc, 0, &[[95.0, 40.0, 140.0, 70.0]], ""); + assert_eq!(apply(&mut doc, None), Err(RedactError::DroppedCodespace(1))); +} + /// Nested names around redacted paint all go, the innermost too, whichever scope was cut first. #[test] fn redacted_paint_loses_every_name_around_it() {