From 50c3053bbc67b43439b8f2ca578d5e0af18266e1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:12:12 +0000 Subject: [PATCH] Preserve raster probe format and bound JPEG header input --- crates/image-codecs/src/allocation.rs | 35 ++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/allocation.rs b/crates/image-codecs/src/allocation.rs index a31684a33..6ba09f569 100644 --- a/crates/image-codecs/src/allocation.rs +++ b/crates/image-codecs/src/allocation.rs @@ -257,7 +257,19 @@ pub fn probe_stored_size(bytes: &[u8], limits: &crate::Limits) -> Result<(u32, u if !matches!(format, Format::Png | Format::Jpeg | Format::Bmp | Format::Tiff) { return Err(CodecError::unsupported(format, "stored-size probe is not available for this format")); } - let mut reader = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().map_err(|e| CodecError::malformed(format, e))?; + // The JPEG header reader owns a copy of its input before applying image limits. + if format == Format::Jpeg && u64::try_from(bytes.len()).map_or(true, |len| len > limits.max_alloc) { + return Err(CodecError::malformed(format, "JPEG input exceeds the allocation budget")); + } + let image_format = match format { + Format::Png => image::ImageFormat::Png, + Format::Jpeg => image::ImageFormat::Jpeg, + Format::Bmp => image::ImageFormat::Bmp, + Format::Tiff => image::ImageFormat::Tiff, + _ => return Err(CodecError::unsupported(format, "stored-size probe is not available for this format")), + }; + // Preserve detect's BigTIFF selection; image's signature guesser only knows classic TIFF. + let mut reader = image::ImageReader::with_format(Cursor::new(bytes), image_format); let mut image_limits = image::Limits::default(); image_limits.max_image_width = Some(limits.max_width); image_limits.max_image_height = Some(limits.max_height); @@ -285,6 +297,27 @@ mod stored_size_tests { assert!(probe_stored_size(&bytes[..20], &limits).is_err()); } + #[test] + fn bigtiff_stored_dimensions_preserve_detected_format() { + let mut out = Cursor::new(Vec::new()); + tiff::encoder::TiffEncoder::new_big(&mut out).unwrap().write_image::(3, 2, &[10; 18]).unwrap(); + let bytes = out.into_inner(); + assert_eq!(crate::detect(&bytes), Some(Format::Tiff)); + assert_eq!(probe_stored_size(&bytes, &crate::Limits::default()).unwrap(), (3, 2)); + } + + #[test] + fn jpeg_input_copy_is_bounded_by_allocation_budget() { + let mut out = Cursor::new(Vec::new()); + image::write_buffer_with_format(&mut out, &[10; 18], 3, 2, image::ExtendedColorType::Rgb8, image::ImageFormat::Jpeg).unwrap(); + let bytes = out.into_inner(); + let mut limits = crate::Limits::default(); + limits.max_alloc = bytes.len() as u64; + assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (3, 2)); + limits.max_alloc -= 1; + assert!(probe_stored_size(&bytes, &limits).is_err()); + } + #[test] fn common_stored_headers_are_probed() { let limits = crate::Limits::new(100, 100, 10_000, 1 << 20);