From 2d5b96929b2f56c0c67e9b68bf7cd82859c80d9f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 10:34:27 -0800 Subject: [PATCH 1/2] image-codecs: read a file or stream into bytes with a size cap The decoders take a byte slice, and Limits bound only the decoded image, so a caller had to cap and read the input itself. read_bounded(reader, max_bytes) reads at most one byte past max_bytes and fails with ErrorKind::FileTooLarge for longer input, including a file that grows after its size was checked and a stream with no end. --- crates/image-codecs/README.md | 3 ++- crates/image-codecs/src/lib.rs | 16 ++++++++++++++++ crates/image-codecs/tests/it/limits.rs | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index 7d588336f1..381d6f5226 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -4,7 +4,8 @@ Codecs for flat raster image formats, written in pure Rust. This crate defines i type and shares its baseline and extended JPEG decoders through `craft-jpeg`, its optional HEIF decoder through `craft-heif`, and its optional AVIF decoder through `craft-video`'s AV1 decoder. It builds for `wasm32-unknown-unknown` and does no filesystem I/O: decoders take `&[u8]` and -encoders return `Vec`. +encoders return `Vec`. `read_bounded(reader, max_bytes)` reads a caller's file or stream into +bytes for them and fails with `ErrorKind::FileTooLarge` when the input is longer than `max_bytes`. It follows the "Avoiding GIMP's hole" rules in `docs/architecture.md` ยง1.1: diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 2a91dc6ea8..df882b1bbc 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -92,6 +92,22 @@ pub fn decode_with(bytes: &[u8], opts: &DecodeOptions) -> Result std::io::Result> { + use std::io::Read as _; + let mut bytes = Vec::new(); + reader.take(max_bytes.saturating_add(1)).read_to_end(&mut bytes)?; + if bytes.len() as u64 > max_bytes { + return Err(std::io::Error::new(std::io::ErrorKind::FileTooLarge, format!("the image input is longer than {max_bytes} bytes"))); + } + Ok(bytes) +} + /// Decode as a specific format with default [`Limits`]. pub fn decode_as(format: Format, bytes: &[u8]) -> Result { decode_as_with(format, bytes, &DecodeOptions::default()) diff --git a/crates/image-codecs/tests/it/limits.rs b/crates/image-codecs/tests/it/limits.rs index 49561e7a81..b6ef3a14e3 100644 --- a/crates/image-codecs/tests/it/limits.rs +++ b/crates/image-codecs/tests/it/limits.rs @@ -17,6 +17,20 @@ fn encoded(f: Format) -> Vec { encode(&img, f, &EncodeOptions::default()).unwrap() } +#[test] +fn read_bounded_takes_input_up_to_its_cap() { + let png = encoded(Format::Png); + let len = png.len() as u64; + let bytes = read_bounded(png.as_slice(), len).unwrap(); + assert!(decode_with(&bytes, &DecodeOptions::default()).is_ok()); + assert_eq!(read_bounded(png.as_slice(), len - 1).err().map(|e| e.kind()), Some(std::io::ErrorKind::FileTooLarge)); +} + +#[test] +fn read_bounded_stops_a_stream_with_no_end() { + assert_eq!(read_bounded(std::io::repeat(0), 1 << 16).err().map(|e| e.kind()), Some(std::io::ErrorKind::FileTooLarge)); +} + #[test] fn max_pixels_enforced_for_every_format() { for f in rw_formats() { From 14a5e7ed05988edb128f1de070ccf67101260de3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 10:56:48 -0800 Subject: [PATCH 2/2] image-codecs: test that read_bounded reads one byte past its cap A finite input longer than the cap fails with FileTooLarge and leaves the reader at the cap plus one byte. --- crates/image-codecs/tests/it/limits.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/image-codecs/tests/it/limits.rs b/crates/image-codecs/tests/it/limits.rs index b6ef3a14e3..bab7950c6a 100644 --- a/crates/image-codecs/tests/it/limits.rs +++ b/crates/image-codecs/tests/it/limits.rs @@ -31,6 +31,13 @@ fn read_bounded_stops_a_stream_with_no_end() { assert_eq!(read_bounded(std::io::repeat(0), 1 << 16).err().map(|e| e.kind()), Some(std::io::ErrorKind::FileTooLarge)); } +#[test] +fn read_bounded_reads_one_byte_past_its_cap() { + let mut input = std::io::Cursor::new(vec![0u8; 1 << 16]); + assert_eq!(read_bounded(&mut input, 100).err().map(|e| e.kind()), Some(std::io::ErrorKind::FileTooLarge)); + assert_eq!(input.position(), 101); +} + #[test] fn max_pixels_enforced_for_every_format() { for f in rw_formats() {