PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M11: redaction's decode allowance takes ASCIIHex text broken into lines; tests for the bounds
Grzegorz Martens68c6d4b052e1
diff --git a/crates/cos/src/document/tests.rs b/crates/cos/src/document/tests.rsindex aeb518472..53941f02b 100644--- a/crates/cos/src/document/tests.rs+++ b/crates/cos/src/document/tests.rs@@ -118,6 +118,13 @@ fn fax_images_decode_with_their_parameters() {[0b0000_0111, 0b1100_0000, 0b0000_0111, 0b1100_0000, 0b0000_0111, 0b1100_0000]);assert_eq!(image(&doc, Some(Object::Dict(parms.clone())), 10, 2).unwrap(), bits[..4], "at most /Height rows");+ // Within a bound: no more rows than it holds.+ let mut bounded = Dict::new();+ bounded.set(b"Width".to_vec(), Object::Int(10));+ bounded.set(b"Height".to_vec(), Object::Int(3));+ bounded.set(b"Filter".to_vec(), Object::name("CCITTFaxDecode"));+ bounded.set(b"DecodeParms".to_vec(), Object::Dict(parms.clone()));+ assert_eq!(doc.decoded_image_within(&Stream::from_raw(bounded, coded.clone()), 5).unwrap(), bits[..4], "two rows of two bytes in five");let mut by_reference = Dict::new();by_reference.set(b"K".to_vec(), Object::Ref(doc.add(Object::Int(-1))));by_reference.set(b"Columns".to_vec(), Object::Ref(doc.add(Object::Int(10))));diff --git a/crates/redact/src/image.rs b/crates/redact/src/image.rsindex a1bf022df..297b3510c 100644--- a/crates/redact/src/image.rs+++ b/crates/redact/src/image.rs@@ -86,14 +86,15 @@ fn decode_and_clear(doc: &Document, s: &Stream, ctm: &Matrix, rects: &[[f64; 4]]let row_bits = row.checked_mul(8)?;bytes.checked_mul(8)?;// Decode no further than the raster can need: a filter stage may legitimately produce up to- // twice it (ASCIIHex text under Flate) plus a PNG predictor's byte per row. A fax image's+ // three bytes for each of it and a PNG predictor's byte per row (ASCIIHex text under Flate,+ // two digits a byte broken into lines or spaced), as the optimizer allows. A fax image's// code, which the filters before its decoder produce, takes at most about a byte per pixel// plus each row's end. Data past that can only be a decompression bomb; the image is then// removed whole.let allowance = if is_fax {w.checked_mul(h)?.checked_add(h.checked_mul(4)?)?.checked_add(4096)?} else {- bytes.checked_add(h)?.checked_mul(2)?.checked_add(4096)?+ bytes.checked_add(h)?.checked_mul(3)?.checked_add(4096)?};// Through the document, which reads `/DecodeParms` given by reference (`Stream::decoded*`// alone would skip such a predictor, and the cleared bytes would then miss the covered pixels).diff --git a/crates/redact/src/tests.rs b/crates/redact/src/tests.rsindex c6885eaf1..33dc675cd 100644--- a/crates/redact/src/tests.rs+++ b/crates/redact/src/tests.rs@@ -626,6 +626,39 @@ fn a_fax_image_has_its_covered_pixels_cleared_instead_of_being_removed() {}}+/// Images whose data is more than twice their raster before its last filter are cleared, not+/// taken for decompression bombs: grey stored as ASCIIHex text broken into lines under Flate (two+/// digits a byte and a line break every 64), and RGB behind an array of filters, which a fax+/// image's bound (a byte a pixel) would refuse.+#[test]+fn images_behind_ascii_hex_in_lines_or_a_filter_array_are_cleared() {+ let (w, h) = (1000usize, 200usize);+ let grey: Vec<u8> = (0..w * h).map(|i| (i % 251) as u8).collect();+ let digits: Vec<u8> = grey.iter().flat_map(|b| format!("{b:02X}").into_bytes()).collect();+ let mut lines: Vec<u8> = digits.chunks(64).flat_map(|line| line.iter().copied().chain(*b"\n")).collect();+ lines.push(b'>');+ let rgb: Vec<u8> = (0..100 * 100 * 3).map(|i| (i % 253) as u8).collect();+ let rgb_hex: Vec<u8> = pdfcraft_filters::encode_flate(&rgb).iter().flat_map(|b| format!("{b:02x}").into_bytes()).chain(*b">").collect();+ for (dict, data) in [+ (+ format!(+ "/Type /XObject /Subtype /Image /Width {w} /Height {h} /ColorSpace /DeviceGray /BitsPerComponent 8 /Filter [/FlateDecode /ASCIIHexDecode]"+ ),+ pdfcraft_filters::encode_flate(&lines),+ ),+ (+ "/Type /XObject /Subtype /Image /Width 100 /Height 100 /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter [/ASCIIHexDecode /FlateDecode]"+ .to_string(),+ rgb_hex,+ ),+ ] {+ let mut doc = one_page(b"q 100 0 0 20 0 0 cm /Im Do Q", "/XObject << /Im 7 0 R >>", vec![stream(&dict, data)]);+ mark(&mut doc, 0, &[[0.0, 0.0, 10.0, 10.0]], "");+ let report = apply(&mut doc, None).unwrap();+ assert_eq!((report.images_cleared, report.images_removed), (1, 0), "{dict}");+ }+}+/// A fax image that can't be decoded in full is removed whole, as any other image is: one whose/// code is cut short, and one behind a filter that inflates past what a code for its pixels can/// take (a decompression bomb, which `decoded_image` alone would undo up to a gigabyte).