diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs index 15346ea56..1b92ea9d9 100644 --- a/crates/image-codecs/src/codecs/jpeg.rs +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -389,6 +389,15 @@ fn decode_craft_jpeg(bytes: &[u8], limits: &Limits, adobe_transform: Option, /// truncated or not, are far above this. const MAX_PIXELS_PER_BYTE: u64 = 2048; +/// The stored size and EXIF block [`decode`] would find, from the marker segments before the +/// first scan: the first frame header's size, checked against `limits`, and the APP1 EXIF block. +pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32, Option>), CodecError> { + let meta = scan_metadata(bytes); + let (w, h, _) = meta.frame.ok_or_else(|| err("no frame header before the image data"))?; + limits.check_size(w, h)?; + Ok((w, h, meta.exif)) +} + pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { let (end, scan_bytes) = data_end(bytes); if end == DataEnd::Empty { diff --git a/crates/image-codecs/src/codecs/png.rs b/crates/image-codecs/src/codecs/png.rs index 9965dfc30..a244a1aae 100644 --- a/crates/image-codecs/src/codecs/png.rs +++ b/crates/image-codecs/src/codecs/png.rs @@ -27,6 +27,26 @@ pub(crate) fn dpi_of_ppm(ppm: u32) -> f32 { } /// Decodes a whole PNG, by driving a [`PngDecodeRows`] to its end. +/// The stored size and EXIF block [`decode`] would find, from the chunks before the image data: +/// the same header checks (limits, a file too small for its image) and the same frame (an fcTL +/// before the IDAT overrides the header's size), without inflating any pixels or text. +pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32, Option>), CodecError> { + let mut decoder = png::Decoder::new_with_limits(Cursor::new(bytes), png::Limits { bytes: limits.alloc_usize() }); + decoder.set_ignore_text_chunk(true); + { + let info = decoder.read_header_info().map_err(map_png_err)?; + let (w, h) = info.size(); + limits.check_size(w, h)?; + let stored = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(info.bits_per_pixel() as u64).div_ceil(8); + Limits::check_plausible(stored, bytes.len(), 1100)?; + } + let reader = decoder.read_info().map_err(map_png_err)?; + let info = reader.info(); + let (w, h) = info.frame_control.map_or_else(|| info.size(), |f| (f.width, f.height)); + limits.check_size(w, h)?; + Ok((w, h, info.exif_metadata.as_ref().map(|c| c.to_vec()))) +} + pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { PngDecodeRows::new(bytes, limits)?.step(&mut || true)?.ok_or_else(|| err("the decoder stopped before the image ended")) } diff --git a/crates/image-codecs/src/codecs/tiff.rs b/crates/image-codecs/src/codecs/tiff.rs index c74ab4436..fa279d9f0 100644 --- a/crates/image-codecs/src/codecs/tiff.rs +++ b/crates/image-codecs/src/codecs/tiff.rs @@ -171,7 +171,7 @@ mod tags; mod ycbcr; pub use decode::TiffBands; -pub(crate) use decode::{decode, decode_page, has_unretained_samples, orientation}; +pub(crate) use decode::{decode, decode_page, has_unretained_samples, orientation, probe}; pub(crate) use encode::encode; #[cfg(test)] use encode::{Extras, try_with_capacity}; diff --git a/crates/image-codecs/src/codecs/tiff/decode.rs b/crates/image-codecs/src/codecs/tiff/decode.rs index 97efdd979..a9caa98ee 100644 --- a/crates/image-codecs/src/codecs/tiff/decode.rs +++ b/crates/image-codecs/src/codecs/tiff/decode.rs @@ -13,6 +13,17 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result decode_page(bytes, None, limits) } +/// The stored size and Orientation (1–8) of the page [`decode`] opens, from its directory alone: +/// the same page choice and refusals (camera raw, no directory), checked against `limits`, +/// without reading any strips or tiles. +pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32, u16), CodecError> { + let (file, dir, _) = open_page(bytes, None)?; + let side = |tag: u16| file.tag_uint(&dir, tag).and_then(|v| u32::try_from(v).ok()).unwrap_or(0); + let (w, h) = (side(ifd::TAG_WIDTH), side(ifd::TAG_HEIGHT)); + limits.check_size(w, h)?; + Ok((w, h, ifd::orientation_of(&file, &dir))) +} + /// Decodes page `page` of [`ifd::tiff_info`]'s list (`None`: the default page). The image /// carries the page's metadata, a [`DecodeWarning::MorePages`] when the file holds more /// full-resolution pages, and a [`DecodeWarning::Truncated`] when image data is cut off. diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index ab6d791ad..99f1edb47 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -225,11 +225,12 @@ pub fn decode_png_rows>( } /// The upright pixel size [`decode`] would return, read from the file's header without decoding -/// pixels, for WebP, OpenEXR (flat), AVIF (with `avif-decode`) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same -/// header reads and size checks as the decoder (default [`Limits`]) and swaps the size for an -/// EXIF orientation of 5–8 (AVIF: its container's `clap` and `irot`), so wherever [`decode`] -/// succeeds the two agree. Other formats (and deep EXR) return [`CodecError::Unsupported`]: decode -/// those to learn their size. +/// pixels, for PNG, JPEG, TIFF (the page [`decode`] opens), WebP, OpenEXR (flat), AVIF (with +/// `avif-decode`) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same header reads and size +/// checks as the decoder (default [`Limits`]) and swaps the size for an orientation of 5–8 (EXIF, +/// the TIFF page's own tag, or AVIF's container `clap` and `irot`), so wherever [`decode`] +/// succeeds the two agree. Other formats (and deep EXR) return [`CodecError::Unsupported`]: +/// decode those to learn their size. pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { let format = detect(bytes).ok_or(CodecError::UnknownFormat)?; if !caps(format).read { @@ -237,6 +238,12 @@ pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { } let l = &Limits::default(); let (w, h, exif) = match format { + Format::Png => png::probe(bytes, l)?, + Format::Jpeg => jpeg::probe(bytes, l)?, + Format::Tiff => { + let (w, h, o) = tiff::probe(bytes, l)?; + return Ok(if (5..=8).contains(&o) { (h, w) } else { (w, h) }); + } Format::WebP => webp::probe(bytes, l)?, Format::OpenExr => exr::probe(bytes, l).map(|(w, h)| (w, h, None))?, #[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))] diff --git a/crates/image-codecs/tests/it/limits.rs b/crates/image-codecs/tests/it/limits.rs index e590aa568..49561e7a8 100644 --- a/crates/image-codecs/tests/it/limits.rs +++ b/crates/image-codecs/tests/it/limits.rs @@ -671,3 +671,32 @@ fn highly_compressible_jpeg_and_gif_still_decode() { assert_eq!(decode(&b).unwrap().width(), 3000, "{f:?}"); } } + +/// `probe_size` refuses the headers `decode` refuses, from the header alone: a size past the +/// limits, and one at the limit that a tiny file cannot hold. +#[test] +fn probe_size_refuses_png_bomb_headers() { + for side in [100_000u32, 32768] { + let mut ihdr = Vec::new(); + ihdr.extend_from_slice(&side.to_be_bytes()); + ihdr.extend_from_slice(&side.to_be_bytes()); + ihdr.extend_from_slice(&[16, 6, 0, 0, 0]); + let mut b = b"\x89PNG\r\n\x1a\n".to_vec(); + b.extend(png_chunk(b"IHDR", &ihdr)); + b.extend(png_chunk(b"IDAT", &[0x78, 0x9C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01])); + b.extend(png_chunk(b"IEND", &[])); + assert!(matches!(probe_size(&b), Err(CodecError::LimitExceeded(_))), "{side}"); + } +} + +/// A JPEG header declaring a size past the default limits is refused by `probe_size` as by +/// `decode`, without reading the scan. +#[test] +fn probe_size_refuses_an_oversized_jpeg_header() { + let mut jpeg = encoded(Format::Jpeg); + assert_eq!(probe_size(&jpeg).unwrap(), (64, 48)); + let sof = jpeg.windows(2).position(|w| matches!(w, [0xFF, 0xC0..=0xC2])).unwrap(); + jpeg[sof + 5..sof + 9].copy_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]); + assert!(decode(&jpeg).is_err()); + assert!(matches!(probe_size(&jpeg), Err(CodecError::LimitExceeded(_))), "{:?}", probe_size(&jpeg)); +} diff --git a/crates/image-codecs/tests/it/orientation.rs b/crates/image-codecs/tests/it/orientation.rs index d5d3ad90e..4eaf188a8 100644 --- a/crates/image-codecs/tests/it/orientation.rs +++ b/crates/image-codecs/tests/it/orientation.rs @@ -202,6 +202,7 @@ fn png_opens_upright_for_every_orientation() { let file = png_with_exif(&png, &exif(u32::from(o), o % 2 == 0, false)); let img = decode(&file).unwrap(); assert_eq!(img.dimensions(), (w as u32, h as u32), "o={o}"); + assert_eq!(probe_size(&file).unwrap(), img.dimensions(), "the header probe turns upright too, o={o}"); assert_eq!(img.data(), &up[..], "o={o}"); assert_eq!(exif_orientation(img.meta.exif.as_deref().unwrap()), 1, "o={o}: metadata says upright"); // Decoded in steps and finished by `finish_png_decode`, it is the same image. @@ -309,8 +310,10 @@ fn tiff_opens_upright_for_every_orientation() { let up = exact_scene(w, h); for o in 1..=8u16 { let (s, sw, sh) = stored(&up, w, h, o); - let img = decode(&tiff_with_orientation(&s, sw as u32, sh as u32, o)).unwrap(); + let file = tiff_with_orientation(&s, sw as u32, sh as u32, o); + let img = decode(&file).unwrap(); assert_eq!(img.dimensions(), (w as u32, h as u32), "o={o}"); + assert_eq!(probe_size(&file).unwrap(), img.dimensions(), "the header probe turns upright too, o={o}"); assert_eq!(img.data(), &up[..], "o={o}"); } } @@ -358,6 +361,7 @@ fn jpeg_opens_upright_for_every_orientation() { let file = jpeg_with_exif(&jpeg, &exif(u32::from(o), o > 4, o == 7)); let img = decode(&file).unwrap(); assert_blocks(&img, &up, w, h, &format!("o={o}")); + assert_eq!(probe_size(&file).unwrap(), img.dimensions(), "the header probe turns upright too, o={o}"); assert_eq!(exif_orientation(img.meta.exif.as_deref().unwrap()), 1); } } @@ -659,7 +663,19 @@ fn long_orientation_reads_and_rewrites_in_both_byte_orders() { #[test] fn probe_size_matches_decode() { let img = synth(7, 5, ChannelLayout::Rgb, SampleType::U8, 3, 0.0); - let probed = [Format::WebP, Format::Gif, Format::Bmp, Format::Tga, Format::Qoi, Format::Hdr, Format::Ico, Format::OpenExr]; + let probed = [ + Format::Png, + Format::Jpeg, + Format::Tiff, + Format::WebP, + Format::Gif, + Format::Bmp, + Format::Tga, + Format::Qoi, + Format::Hdr, + Format::Ico, + Format::OpenExr, + ]; assert!(probed.iter().all(|f| rw_formats().contains(f))); for format in rw_formats() { let bytes = encode(&img, format, &EncodeOptions::default()).unwrap(); diff --git a/crates/image-codecs/tests/it/tiff_pages.rs b/crates/image-codecs/tests/it/tiff_pages.rs index b94509598..7501be936 100644 --- a/crates/image-codecs/tests/it/tiff_pages.rs +++ b/crates/image-codecs/tests/it/tiff_pages.rs @@ -539,6 +539,7 @@ fn every_ifd_and_subifd_is_listed_and_decodable() { // Like Photoshop: the first full-resolution page opens; the thumbnail is skipped. let img = decode_ok(&b.bytes); assert_eq!((img.dimensions(), img.data()[0]), ((9, 7), 2)); + assert_eq!(probe_size(&b.bytes).unwrap(), (9, 7), "the header probe sizes the page decode opens"); assert_eq!(img.warnings, [DecodeWarning::MorePages { total: Some(2) }]); // Every directory decodes on request. for (i, (w, level)) in [(4, 1u8), (9, 2), (3, 5), (2, 6), (9, 3), (5, 4)].into_iter().enumerate() { @@ -554,6 +555,7 @@ fn a_file_of_only_thumbnails_still_opens() { let b = build(true, false, &[gray_page(4, 3, 7, Some(1))], &[0]); assert_eq!(tiff_info(&b.bytes).unwrap().default_page(), Some(0)); assert_eq!(decode_ok(&b.bytes).data()[0], 7); + assert_eq!(probe_size(&b.bytes).unwrap(), (4, 3)); } #[test] @@ -698,6 +700,9 @@ fn hostile_sizes_and_counts_are_refused_without_allocating() { patch_value(&mut b, little, big, 0, 256, 0, wv); patch_value(&mut b, little, big, 0, 257, 0, hv); assert!(decode(&b.bytes).is_err(), "{wv}x{hv}"); + if wv > 300_000 || hv > 300_000 { + assert!(matches!(probe_size(&b.bytes), Err(CodecError::LimitExceeded(_))), "{wv}x{hv}: probe"); + } assert!(decode_with(&b.bytes, &DecodeOptions { limits: Limits::none(), ..Default::default() }).is_err(), "{wv}x{hv} without limits"); } // A strip-offset count far past the end of the file. @@ -805,6 +810,7 @@ fn poke(bytes: &[u8]) { let _ = decode_with(bytes, &DecodeOptions { limits: Limits { max_alloc: 16 << 20, ..Limits::default() }, ..Default::default() }); let _ = exif_orientation(bytes); let _ = tiff_orientation(bytes); + let _ = probe_size(bytes); if let Ok(info) = tiff_info(bytes) { for i in 0..info.pages.len().min(8) { let _ = decode_tiff_page(bytes, i, &DecodeOptions::default());