From ee44af6a45c2d12c4e63058db08a9f417002636d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:14:00 +0000 Subject: [PATCH 1/5] Share first-directory TIFF inspection --- crates/image-codecs/src/lib.rs | 1 + crates/image-codecs/src/tiff_probe.rs | 68 +++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 crates/image-codecs/src/tiff_probe.rs diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 6516b9ff2..b94eb6606 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -30,6 +30,7 @@ mod options; pub mod orientation; pub mod resolution; pub mod source_settings; +pub mod tiff_probe; pub mod web; pub use crate::allocation::probe_allocation_size; pub use crate::cicp::Cicp; diff --git a/crates/image-codecs/src/tiff_probe.rs b/crates/image-codecs/src/tiff_probe.rs new file mode 100644 index 000000000..1a542e412 --- /dev/null +++ b/crates/image-codecs/src/tiff_probe.rs @@ -0,0 +1,68 @@ +//! First-directory TIFF inspection without decoding pixels. + +/// A header inspection failure, distinguishing caller limits from unsupported decoder layouts. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// Dimensions exceed the caller's pixel limit. + #[error("TIFF dimensions {width}x{height} exceed the pixel limit")] + TooLarge { + /// Stored width. + width: u32, + /// Stored height. + height: u32, + }, + /// The shared TIFF decoder does not support this layout. + #[error("unsupported TIFF: {0}")] + Unsupported(&'static str), + /// The file's header, directory or image-data bounds are malformed. + #[error("malformed TIFF: {0}")] + Malformed(String), +} + +/// Stored dimensions and orientation of the first directory, after checking the layouts and +/// image-data bounds required by the TIFF decoder. Compressed strips are not decompressed. +pub fn header(bytes: &[u8], max_pixels: u64) -> Result<(u32, u32, u16), Error> { + let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(|e| Error::Malformed(e.to_string()))?; + if !complete { + return Err(Error::Malformed("truncated TIFF".into())); + } + let ifd = tiff.ifds.first().ok_or_else(|| Error::Malformed("no IFD".into()))?; + let info = ifd.image().map_err(|e| Error::Malformed(e.to_string()))?; + if info.width == 0 || info.height == 0 { + return Err(Error::Malformed("zero dimension".into())); + } + if u64::from(info.width) * u64::from(info.height) > max_pixels { + return Err(Error::TooLarge { width: info.width, height: info.height }); + } + let bits = info.bits_per_sample.first().copied().unwrap_or(1); + if bits < 8 && info.planar == 2 && info.samples_per_pixel > 1 { + return Err(Error::Unsupported("planar TIFF with samples narrower than a byte")); + } + if !(matches!(info.photometric, 0 | 1 | 2 | 3 | 5) || info.photometric == 6 && info.compression == 7) { + return Err(Error::Unsupported("colour type")); + } + if !matches!(info.compression, 1 | 4 | 5 | 7 | 8 | 32773 | 32946) { + return Err(Error::Unsupported("compression method")); + } + let len = bytes.len() as u64; + if info.offsets.is_empty() + || info.offsets.iter().zip(&info.byte_counts).any(|(&o, &c)| c > 0 && (o >= len || (info.compression == 1 && o.saturating_add(c) > len))) + { + return Err(Error::Malformed("truncated: image data past the end of the file".into())); + } + let orientation = ifd.u16(craft_tiff::tags::ORIENTATION).filter(|o| (1..=8).contains(o)).unwrap_or(1); + Ok((info.width, info.height, orientation)) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn reads_first_directory_and_enforces_limits_and_bounds() { + let image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap(); + let bytes = crate::encode(&image, crate::Format::Tiff, &crate::EncodeOptions::default()).unwrap(); + assert_eq!(header(&bytes, 6).unwrap(), (2, 3, 1)); + assert!(matches!(header(&bytes, 5), Err(Error::TooLarge { width: 2, height: 3 }))); + assert!(header(&bytes[..8], 6).is_err()); + } +} From fb6cb780061c6744ed6886e7dd1a80ccf449e655 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:15:21 +0000 Subject: [PATCH 2/5] Depend on shared TIFF directory parser --- Cargo.lock | 1 + crates/image-codecs/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 9fb46057c..23cdde361 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1927,6 +1927,7 @@ dependencies = [ "craft-heif", "craft-jpeg", "craft-metadata", + "craft-tiff", "craft-video", "crc32fast", "exr", diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index df71d9860..f697610a6 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -32,6 +32,7 @@ zune-core = "0.5" jpeg-encoder = "0.6" craft-jpeg = { workspace = true } craft-metadata = { workspace = true } +craft-tiff = { workspace = true } zune-jpegxl = { version = "=0.5.2", default-features = false, features = ["std"] } craft-heif = { workspace = true, optional = true } ravif = { version = "0.13", default-features = false, optional = true } From c339a9e4715bb0856603de7af897efcb43430f59 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:16:52 +0000 Subject: [PATCH 3/5] Share TIFF decode preflight fields --- crates/image-codecs/src/tiff_probe.rs | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/crates/image-codecs/src/tiff_probe.rs b/crates/image-codecs/src/tiff_probe.rs index 1a542e412..05bd1e4db 100644 --- a/crates/image-codecs/src/tiff_probe.rs +++ b/crates/image-codecs/src/tiff_probe.rs @@ -19,6 +19,42 @@ pub enum Error { Malformed(String), } +/// First-directory values needed to preflight decoding. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct DecodeInfo { + /// Stored width. + pub width: u32, + /// Stored height. + pub height: u32, + /// First sample's bit depth, defaulting to eight. + pub bit_depth: u8, + /// Raw orientation tag, if present. + pub orientation: Option, +} + +/// Parse the first directory and enforce the pixel limit before decoding. +/// Unlike [`header`], this does not impose layout or strip checks ahead of the decoder. +pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result { + let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(|e| Error::Malformed(e.to_string()))?; + if !complete { + return Err(Error::Malformed("truncated TIFF".into())); + } + let ifd = tiff.ifds.first().ok_or_else(|| Error::Malformed("no IFD".into()))?; + let info = ifd.image().map_err(|e| Error::Malformed(e.to_string()))?; + if info.width == 0 || info.height == 0 { + return Err(Error::Malformed("zero dimension".into())); + } + if u64::from(info.width) * u64::from(info.height) > max_pixels { + return Err(Error::TooLarge { width: info.width, height: info.height }); + } + Ok(DecodeInfo { + width: info.width, + height: info.height, + bit_depth: info.bits_per_sample.first().copied().unwrap_or(8) as u8, + orientation: ifd.u16(craft_tiff::tags::ORIENTATION), + }) +} + /// Stored dimensions and orientation of the first directory, after checking the layouts and /// image-data bounds required by the TIFF decoder. Compressed strips are not decompressed. pub fn header(bytes: &[u8], max_pixels: u64) -> Result<(u32, u32, u16), Error> { From b16c5908ae777f81fa33a9e9ad0e22b09b37a383 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:17:27 +0000 Subject: [PATCH 4/5] Cover TIFF decode preflight limits --- crates/image-codecs/src/tiff_probe.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/image-codecs/src/tiff_probe.rs b/crates/image-codecs/src/tiff_probe.rs index 05bd1e4db..6aaf5c21a 100644 --- a/crates/image-codecs/src/tiff_probe.rs +++ b/crates/image-codecs/src/tiff_probe.rs @@ -98,6 +98,9 @@ mod tests { let image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap(); let bytes = crate::encode(&image, crate::Format::Tiff, &crate::EncodeOptions::default()).unwrap(); assert_eq!(header(&bytes, 6).unwrap(), (2, 3, 1)); + let info = decode_info(&bytes, 6).unwrap(); + assert_eq!((info.width, info.height, info.bit_depth), (2, 3, 8)); + assert!(matches!(decode_info(&bytes, 5), Err(Error::TooLarge { width: 2, height: 3 }))); assert!(matches!(header(&bytes, 5), Err(Error::TooLarge { width: 2, height: 3 }))); assert!(header(&bytes[..8], 6).is_err()); } From 4ed217f1bc84e0bb438b9bb8358f64ee5b4fe44f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 18:43:11 +0000 Subject: [PATCH 5/5] Share TIFF first-directory preflight between inspection APIs --- crates/image-codecs/src/tiff_probe.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/crates/image-codecs/src/tiff_probe.rs b/crates/image-codecs/src/tiff_probe.rs index 6aaf5c21a..1c6889c21 100644 --- a/crates/image-codecs/src/tiff_probe.rs +++ b/crates/image-codecs/src/tiff_probe.rs @@ -32,14 +32,12 @@ pub struct DecodeInfo { pub orientation: Option, } -/// Parse the first directory and enforce the pixel limit before decoding. -/// Unlike [`header`], this does not impose layout or strip checks ahead of the decoder. -pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result { +fn first_directory(bytes: &[u8], max_pixels: u64) -> Result<(craft_tiff::Ifd, craft_tiff::image::ImageInfo), Error> { let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(|e| Error::Malformed(e.to_string()))?; if !complete { return Err(Error::Malformed("truncated TIFF".into())); } - let ifd = tiff.ifds.first().ok_or_else(|| Error::Malformed("no IFD".into()))?; + let ifd = tiff.ifds.into_iter().next().ok_or_else(|| Error::Malformed("no IFD".into()))?; let info = ifd.image().map_err(|e| Error::Malformed(e.to_string()))?; if info.width == 0 || info.height == 0 { return Err(Error::Malformed("zero dimension".into())); @@ -47,6 +45,13 @@ pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result { if u64::from(info.width) * u64::from(info.height) > max_pixels { return Err(Error::TooLarge { width: info.width, height: info.height }); } + Ok((ifd, info)) +} + +/// Parse the first directory and enforce the pixel limit before decoding. +/// Unlike [`header`], this does not impose layout or strip checks ahead of the decoder. +pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result { + let (ifd, info) = first_directory(bytes, max_pixels)?; Ok(DecodeInfo { width: info.width, height: info.height, @@ -58,18 +63,7 @@ pub fn decode_info(bytes: &[u8], max_pixels: u64) -> Result { /// Stored dimensions and orientation of the first directory, after checking the layouts and /// image-data bounds required by the TIFF decoder. Compressed strips are not decompressed. pub fn header(bytes: &[u8], max_pixels: u64) -> Result<(u32, u32, u16), Error> { - let (tiff, complete) = craft_tiff::Tiff::parse_with_status(bytes, &Default::default()).map_err(|e| Error::Malformed(e.to_string()))?; - if !complete { - return Err(Error::Malformed("truncated TIFF".into())); - } - let ifd = tiff.ifds.first().ok_or_else(|| Error::Malformed("no IFD".into()))?; - let info = ifd.image().map_err(|e| Error::Malformed(e.to_string()))?; - if info.width == 0 || info.height == 0 { - return Err(Error::Malformed("zero dimension".into())); - } - if u64::from(info.width) * u64::from(info.height) > max_pixels { - return Err(Error::TooLarge { width: info.width, height: info.height }); - } + let (ifd, info) = first_directory(bytes, max_pixels)?; let bits = info.bits_per_sample.first().copied().unwrap_or(1); if bits < 8 && info.planar == 2 && info.samples_per_pixel > 1 { return Err(Error::Unsupported("planar TIFF with samples narrower than a byte"));