From a31327d5f1d01e0eaf7249cd8ffd7c8fc68a6edf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 17:36:53 +0000 Subject: [PATCH] webp: validate decoder canvas before frame allocation --- crates/webp/src/frames.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/crates/webp/src/frames.rs b/crates/webp/src/frames.rs index e97118482..4b5322d34 100644 --- a/crates/webp/src/frames.rs +++ b/crates/webp/src/frames.rs @@ -102,6 +102,15 @@ pub fn read_frames(bytes: &[u8], limits: &FrameLimits) -> Result limits.max_canvas_bytes { + return Err(FrameError::LimitExceeded(format!("a {width}x{height} canvas is over {} bytes", limits.max_canvas_bytes))); + } + if (width, height) != (info.width, info.height) { + return Err(FrameError::Malformed("conflicting canvas dimensions".into())); + } if !decoder.has_animation() { let mut rgba = vec![0u8; usize::try_from(canvas).map_err(|_| FrameError::LimitExceeded("canvas".into()))?]; let alpha = decoder.color_type().has_alpha(); @@ -244,6 +253,21 @@ mod tests { assert!(read_frames(&bytes, &FrameLimits { max_canvas_bytes: 24, ..FrameLimits::default() }).is_ok()); } + #[test] + fn conflicting_canvas_is_rejected_before_frame_decode() { + let mut bytes = anim(&[(1, 10), (2, 10)]); + // The decoder uses the first VP8X, while the scanner sees the last one. + // A tiny limit makes this an ordering test without allocating a huge frame. + let second = chunk(b"VP8X", &[0x12, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + bytes.splice(30..30, second); + let size = (bytes.len() - 8) as u32; + bytes[4..8].copy_from_slice(&size.to_le_bytes()); + assert!(matches!(read_frames(&bytes, &FrameLimits { max_canvas_bytes: 4, ..FrameLimits::default() }), Err(FrameError::LimitExceeded(_)))); + assert!( + matches!(read_frames(&bytes, &FrameLimits::default()), Err(FrameError::Malformed(message)) if message.contains("conflicting canvas")) + ); + } + #[test] fn damage_and_hostile_input() { let bytes = anim(&[(1, 10), (2, 10), (3, 10)]);