PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(content): restrict '>EI' terminator detection to ASCIIHex filter
Ozt-y494567771767
diff --git a/crates/content/src/lib.rs b/crates/content/src/lib.rsindex 143387c59..be6e7c36c 100644--- a/crates/content/src/lib.rs+++ b/crates/content/src/lib.rs@@ -219,19 +219,34 @@ fn inline_image(data: &[u8], pos: usize) -> Result<(Dict, Vec<u8>, usize), usizereturn Ok((dict, Vec::new(), data.len()));}// Without an `EI` after it, the image runs to the end of the stream.- return Err(end_of_image(data, at).map_or(data.len(), |(_, end)| end));+ return Err(end_of_image(data, at, false).map_or(data.len(), |(_, end)| end));};dict.set(k, v);}// One whitespace byte separates ID from the data.let begin = (lx.pos + 1).min(data.len());- let end = unfiltered_image_end(data, begin, &dict).or_else(|| end_of_image(data, begin));+ let end = unfiltered_image_end(data, begin, &dict).or_else(|| end_of_image(data, begin, is_ascii_hex_filter(&dict)));Ok(match end {Some((end_data, end)) => (dict, data[begin..end_data].to_vec(), end),None => (dict, data[begin..].to_vec(), data.len()),})}+fn is_ascii_hex_filter(dict: &Dict) -> bool {+ for key in [b"Filter".as_slice(), b"F"] {+ if let Some(filter) = dict.get(key) {+ match filter {+ Object::Name(name) => return matches!(name.as_slice(), b"ASCIIHexDecode" | b"AHx"),+ Object::Array(filters) => {+ return matches!(filters.first().and_then(Object::as_name), Some(b"ASCIIHexDecode" | b"AHx"));+ }+ _ => return false,+ }+ }+ }+ false+}+/// An unfiltered image's samples determine its length, including byte padding per row./// Binary samples may themselves contain whitespace + `EI` + whitespace, so use the known/// length when the actual terminator follows it. Unknown or damaged layouts keep the heuristic.@@ -303,13 +318,13 @@ fn unfiltered_image_end(data: &[u8], begin: usize, dict: &Dict) -> Option<(usize}/// Where inline image data starting at `begin` ends: (end of the data, position after `EI`). The-/// data ends at whitespace or `>` + `EI` + (whitespace, a delimiter or the end).-fn end_of_image(data: &[u8], begin: usize) -> Option<(usize, usize)> {+/// data ends at whitespace (or `>` for ASCIIHex) + `EI` + (whitespace, a delimiter or the end).+fn end_of_image(data: &[u8], begin: usize, allow_gt_ei: bool) -> Option<(usize, usize)> {let mut i = begin;while i + 2 <= data.len() {if data[i] == b'E'&& data.get(i + 1) == Some(&b'I')- && (i == begin || is_ws(data[i - 1]) || data[i - 1] == b'>')+ && (i == begin || is_ws(data[i - 1]) || (allow_gt_ei && data[i - 1] == b'>'))&& data.get(i + 2).is_none_or(|b| is_ws(*b) || is_delim(*b)){let end_data = if i > begin && is_ws(data[i - 1]) { i - 1 } else { i };diff --git a/crates/content/src/tests.rs b/crates/content/src/tests.rsindex 2ce806fba..e450d5a5f 100644--- a/crates/content/src/tests.rs+++ b/crates/content/src/tests.rs@@ -413,3 +413,14 @@ fn inline_image_terminator_without_preceding_whitespace() {assert_eq!(img, expected_data, "inline image data must match: {}", String::from_utf8_lossy(src));}}++#[test]+fn ascii85_inline_image_does_not_terminate_at_gt_ei_inside_payload() {+ let src = b"BI /W 4 /H 1 /BPC 8 /CS /G /F /A85 ID !!!!>EI !!~>\nEI Q BT /F1 18 Tf (After) Tj ET";+ let p = parse(src);+ assert_eq!(p.skipped, 0);+ let names: Vec<&[u8]> = p.ops.iter().map(|o| o.op.as_slice()).collect();+ assert_eq!(names, [&b"BI"[..], b"Q", b"BT", b"Tf", b"Tj", b"ET"]);+ let (_, img) = p.ops[0].inline.as_ref().unwrap();+ assert_eq!(img, b"!!!!>EI !!~>");+}