From 373d36ea3f66be15b826c861681147e32314b875 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:18:47 +0000 Subject: [PATCH 1/4] Expose bounded stored image dimensions --- crates/image-codecs/src/allocation.rs | 49 +++++++++++++++++++++++++++ crates/image-codecs/src/lib.rs | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/allocation.rs b/crates/image-codecs/src/allocation.rs index fe98122e7..40d55dfc8 100644 --- a/crates/image-codecs/src/allocation.rs +++ b/crates/image-codecs/src/allocation.rs @@ -247,3 +247,52 @@ fn read_le24(bytes: &[u8], at: usize) -> Option { let b = bytes.get(at..at.checked_add(3)?)?; Some(u32::from_le_bytes([b[0], b[1], b[2], 0])) } + +/// Probe the stored dimensions of the first PNG, JPEG, BMP or TIFF image, without decoding +/// pixels or applying orientation. TIFF uses the first directory even if it is a thumbnail. +/// The caller's limits bound both the header decoder and the declared output dimensions. +pub fn probe_stored_size(bytes: &[u8], limits: &crate::Limits) -> Result<(u32, u32), CodecError> { + use std::io::Cursor; + let format = crate::detect(bytes).ok_or(CodecError::UnknownFormat)?; + 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))?; + 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); + image_limits.max_alloc = Some(limits.max_alloc); + reader.limits(image_limits); + let (w, h) = reader.into_dimensions().map_err(|e| CodecError::malformed(format, e))?; + limits.check_size(w, h)?; + Ok((w, h)) +} + +#[cfg(test)] +mod stored_size_tests { + #![allow(clippy::unwrap_used)] + use super::*; + use std::io::Cursor; + + #[test] + fn stored_probe_uses_caller_limits_without_decoding() { + let mut bmp = Cursor::new(Vec::new()); + image::write_buffer_with_format(&mut bmp, &vec![0; 300_000 * 3], 1, 300_000, image::ExtendedColorType::Rgb8, image::ImageFormat::Bmp) + .unwrap(); + let bytes = bmp.into_inner(); + let limits = crate::Limits::new(u32::MAX, u32::MAX, 128_000_000, 2_048_000_000); + assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (1, 300_000)); + assert!(probe_stored_size(&bytes, &crate::Limits::default()).is_err()); + assert!(probe_stored_size(&bytes[..20], &limits).is_err()); + } + + #[test] + fn common_stored_headers_are_probed() { + let limits = crate::Limits::new(100, 100, 10_000, 1 << 20); + for format in [image::ImageFormat::Png, image::ImageFormat::Jpeg, image::ImageFormat::Bmp, image::ImageFormat::Tiff] { + let mut out = Cursor::new(Vec::new()); + image::write_buffer_with_format(&mut out, &[10; 18], 3, 2, image::ExtendedColorType::Rgb8, format).unwrap(); + assert_eq!(probe_stored_size(&out.into_inner(), &limits).unwrap(), (3, 2), "{format:?}"); + } + } +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 6516b9ff2..5cd1b9c60 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -31,7 +31,7 @@ pub mod orientation; pub mod resolution; pub mod source_settings; pub mod web; -pub use crate::allocation::probe_allocation_size; +pub use crate::allocation::{probe_allocation_size, probe_stored_size}; pub use crate::cicp::Cicp; pub use crate::cicp::ContentLightLevel; pub use crate::codecs::avif::{HDR_REFERENCE_WHITE_NITS, HdrTransfer, encode_avif_hdr, encode_avif_with_speed}; From ae5020c1ed922cf7976a3a5545f05b07f466be12 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:23:57 +0000 Subject: [PATCH 2/4] Use a BMP size supported by the baseline decoder --- crates/image-codecs/src/allocation.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/image-codecs/src/allocation.rs b/crates/image-codecs/src/allocation.rs index 40d55dfc8..a31684a33 100644 --- a/crates/image-codecs/src/allocation.rs +++ b/crates/image-codecs/src/allocation.rs @@ -277,12 +277,11 @@ mod stored_size_tests { #[test] fn stored_probe_uses_caller_limits_without_decoding() { let mut bmp = Cursor::new(Vec::new()); - image::write_buffer_with_format(&mut bmp, &vec![0; 300_000 * 3], 1, 300_000, image::ExtendedColorType::Rgb8, image::ImageFormat::Bmp) - .unwrap(); + image::write_buffer_with_format(&mut bmp, &vec![0; 60_000 * 3], 1, 60_000, image::ExtendedColorType::Rgb8, image::ImageFormat::Bmp).unwrap(); let bytes = bmp.into_inner(); let limits = crate::Limits::new(u32::MAX, u32::MAX, 128_000_000, 2_048_000_000); - assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (1, 300_000)); - assert!(probe_stored_size(&bytes, &crate::Limits::default()).is_err()); + assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (1, 60_000)); + assert!(probe_stored_size(&bytes, &crate::Limits::new(u32::MAX, 50_000, u64::MAX, 2_048_000_000)).is_err()); assert!(probe_stored_size(&bytes[..20], &limits).is_err()); } From 50c3053bbc67b43439b8f2ca578d5e0af18266e1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:12:12 +0000 Subject: [PATCH 3/4] 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); From b3b8521ab4029033a9ffc4ebad53f4901cbdf639 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:24:59 +0000 Subject: [PATCH 4/4] Initialize JPEG budget regression limits directly --- crates/image-codecs/src/allocation.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/image-codecs/src/allocation.rs b/crates/image-codecs/src/allocation.rs index 6ba09f569..df9afd93f 100644 --- a/crates/image-codecs/src/allocation.rs +++ b/crates/image-codecs/src/allocation.rs @@ -311,8 +311,7 @@ mod stored_size_tests { 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; + let mut limits = crate::Limits { max_alloc: bytes.len() as u64, ..Default::default() }; assert_eq!(probe_stored_size(&bytes, &limits).unwrap(), (3, 2)); limits.max_alloc -= 1; assert!(probe_stored_size(&bytes, &limits).is_err());