Skip to main content

wechat_media/
image.rs

1use anyhow::{ensure, Result};
2use image::{AnimationDecoder, ImageDecoder};
3use serde::{Deserialize, Serialize};
4use std::io::Cursor;
5
6#[derive(Clone, Serialize, Deserialize, Debug)]
7pub struct ImageInspection {
8    pub content_type: String,
9    pub width: u32,
10    pub height: u32,
11    pub frames: u32,
12    pub animation_preserved: bool,
13}
14
15// JPEG segments can contain marker-shaped bytes, and scan data escapes them.
16// Walk the framing so an EOI inside metadata cannot disguise truncation. Bytes
17// after the actual EOI are permitted; received photos can include a trailer.
18fn jpeg_has_end_marker(bytes: &[u8]) -> bool {
19    if !bytes.starts_with(&[0xff, 0xd8]) {
20        return false;
21    }
22    let mut position = 2;
23    let mut in_scan = false;
24    let mut saw_scan = false;
25    while position < bytes.len() {
26        if in_scan {
27            while bytes.get(position).is_some_and(|b| *b != 0xff) {
28                position += 1;
29            }
30        }
31        if bytes.get(position) != Some(&0xff) {
32            return false;
33        }
34        while bytes.get(position) == Some(&0xff) {
35            position += 1;
36        }
37        let Some(&marker) = bytes.get(position) else {
38            return false;
39        };
40        position += 1;
41        match marker {
42            0x00 | 0xd0..=0xd7 if in_scan => continue,
43            0xd9 => return saw_scan,
44            0x00 | 0xd0..=0xd8 => return false,
45            0x01 => continue,
46            _ => {
47                let Some(length) = bytes.get(position..position + 2) else {
48                    return false;
49                };
50                let length = u16::from_be_bytes([length[0], length[1]]) as usize;
51                if length < 2 || position + length > bytes.len() {
52                    return false;
53                }
54                position += length;
55                if marker == 0xda {
56                    saw_scan = true;
57                    in_scan = true;
58                } else if marker != 0xdc {
59                    in_scan = false;
60                }
61            }
62        }
63    }
64    false
65}
66
67fn gif_sub_blocks_end(bytes: &[u8], mut position: usize) -> Option<usize> {
68    loop {
69        let length = *bytes.get(position)? as usize;
70        position = position.checked_add(1 + length)?;
71        if position > bytes.len() {
72            return None;
73        }
74        if length == 0 {
75            return Some(position);
76        }
77    }
78}
79
80// A ';' inside a palette, extension or compressed frame is not the trailer.
81// Follow block lengths; any bytes after the actual trailer stay in the source.
82fn gif_has_trailer(bytes: &[u8]) -> bool {
83    if bytes.len() < 13 || ![b"GIF87a", b"GIF89a"].iter().any(|h| bytes.starts_with(*h)) {
84        return false;
85    }
86    let palette_size = |packed: u8| {
87        if packed & 0x80 != 0 {
88            3 * (2usize << (packed & 7))
89        } else {
90            0
91        }
92    };
93    let mut position = 13 + palette_size(bytes[10]);
94    while let Some(&block) = bytes.get(position) {
95        position += 1;
96        match block {
97            0x3b => return true,
98            0x21 => {
99                if bytes.get(position).is_none() {
100                    return false;
101                }
102                position += 1; // Extension label; its contents use sub-blocks.
103            }
104            0x2c => {
105                let Some(descriptor) = bytes.get(position..position + 9) else {
106                    return false;
107                };
108                position += 9 + palette_size(descriptor[8]);
109                if bytes.get(position).is_none() {
110                    return false;
111                }
112                position += 1; // LZW minimum code size, before data sub-blocks.
113            }
114            _ => return false,
115        }
116        let Some(end) = gif_sub_blocks_end(bytes, position) else {
117            return false;
118        };
119        position = end;
120    }
121    false
122}
123
124fn png_has_end_chunk(bytes: &[u8]) -> bool {
125    if !bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
126        return false;
127    }
128    let mut position: usize = 8;
129    while let Some(header) = bytes.get(position..position + 8) {
130        let length = u32::from_be_bytes(header[..4].try_into().unwrap()) as usize;
131        let Some(end) = position.checked_add(12).and_then(|n| n.checked_add(length)) else {
132            return false;
133        };
134        if end > bytes.len() {
135            return false;
136        }
137        if &header[4..8] == b"IEND" {
138            // The decoder can finish the pixels before consuming IEND. Require
139            // its zero-length chunk and fixed CRC without trimming the source.
140            return length == 0 && bytes[end - 4..end] == [0xae, 0x42, 0x60, 0x82];
141        }
142        position = end;
143    }
144    false
145}
146
147/// Largest JPEG accepted for inspection: one ordinary image transfer.
148const MAX_JPEG_BYTES: usize = 25 * 1024 * 1024 - 17;
149/// JPEGs above this many pixels are checked with a reduced-resolution decode.
150const FULL_DECODE_PIXELS: u64 = 16_777_216;
151
152/// Inspect a still JPEG of up to 8,192 pixels per edge and 64 million pixels,
153/// the size of an unchanged camera original. Every scan is decoded; photos
154/// above [`FULL_DECODE_PIXELS`] decode at one-eighth scale so checking one does
155/// not allocate the full pixel buffer.
156fn inspect_jpeg(bytes: &[u8]) -> Result<ImageInspection> {
157    ensure!(bytes.len() <= MAX_JPEG_BYTES, "image_size_limit");
158    ensure!(jpeg_has_end_marker(bytes), "jpeg_trailer_missing");
159    let failed = || anyhow::anyhow!("image_decode_failed");
160    let (width, height) =
161        image::ImageReader::with_format(Cursor::new(bytes), image::ImageFormat::Jpeg)
162            .into_dimensions()
163            .map_err(|_| failed())?;
164    let pixels = u64::from(width) * u64::from(height);
165    ensure!(
166        width > 0 && height > 0 && width <= 8192 && height <= 8192 && pixels <= 64_000_000,
167        "image_decode_limit"
168    );
169    if pixels <= FULL_DECODE_PIXELS {
170        let mut reader =
171            image::ImageReader::with_format(Cursor::new(bytes), image::ImageFormat::Jpeg);
172        let mut limits = image::Limits::default();
173        limits.max_image_width = Some(8192);
174        limits.max_image_height = Some(8192);
175        limits.max_alloc = Some(64 * 1024 * 1024);
176        reader.limits(limits);
177        let decoded = reader.decode().map_err(|_| failed())?;
178        ensure!(
179            (decoded.width(), decoded.height()) == (width, height),
180            "image_decode_failed"
181        );
182    } else {
183        let mut decoder = jpeg_decoder::Decoder::new(Cursor::new(bytes));
184        decoder.set_max_decoding_buffer_size(128 * 1024 * 1024);
185        decoder.read_info().map_err(|_| failed())?;
186        let info = decoder.info().ok_or_else(failed)?;
187        ensure!(
188            (u32::from(info.width), u32::from(info.height)) == (width, height),
189            "image_decode_failed"
190        );
191        ensure!(
192            matches!(
193                info.pixel_format,
194                jpeg_decoder::PixelFormat::L8 | jpeg_decoder::PixelFormat::RGB24
195            ),
196            "image_format_unsupported"
197        );
198        decoder.scale(1, 1).map_err(|_| failed())?;
199        decoder.decode().map_err(|_| failed())?;
200    }
201    Ok(ImageInspection {
202        content_type: "image/jpeg".into(),
203        width,
204        height,
205        frames: 1,
206        animation_preserved: false,
207    })
208}
209
210pub(crate) fn inspect_standard_image(bytes: &[u8]) -> Result<ImageInspection> {
211    ensure!(!bytes.is_empty(), "image_size_limit");
212    let format =
213        image::guess_format(bytes).map_err(|_| anyhow::anyhow!("image_format_unsupported"))?;
214    if format == image::ImageFormat::Jpeg {
215        return inspect_jpeg(bytes);
216    }
217    ensure!(bytes.len() <= 16 * 1024 * 1024, "image_size_limit");
218    let limits = || {
219        let mut l = image::Limits::default();
220        l.max_image_width = Some(4096);
221        l.max_image_height = Some(4096);
222        l.max_alloc = Some(64 * 1024 * 1024);
223        l
224    };
225    let mut frames = 0u32;
226    let mut total = 0u64;
227    let mut width = 0;
228    let mut height = 0;
229    let mut count = |frame: image::ImageResult<image::Frame>| -> Result<()> {
230        let f = frame.map_err(|_| anyhow::anyhow!("image_decode_failed"))?;
231        let (w, h) = f.buffer().dimensions();
232        ensure!(
233            w > 0 && h > 0 && w as u64 * h as u64 <= 4_000_000,
234            "image_decode_limit"
235        );
236        total += w as u64 * h as u64;
237        frames += 1;
238        ensure!(frames <= 500 && total <= 100_000_000, "image_decode_limit");
239        width = width.max(w);
240        height = height.max(h);
241        Ok(())
242    };
243    let mime = match format {
244        image::ImageFormat::Gif => {
245            ensure!(gif_has_trailer(bytes), "gif_trailer_missing");
246            let mut decoder = image::codecs::gif::GifDecoder::new(Cursor::new(bytes))?;
247            decoder.set_limits(limits())?;
248            for frame in decoder.into_frames() {
249                count(frame)?;
250            }
251            "image/gif"
252        }
253        image::ImageFormat::WebP => {
254            let encoded_length = bytes
255                .get(4..8)
256                .map(|b| u32::from_le_bytes(b.try_into().unwrap()) as u64 + 8)
257                .and_then(|n| usize::try_from(n).ok())
258                .filter(|n| *n >= 12 && *n <= bytes.len())
259                .ok_or_else(|| anyhow::anyhow!("webp_length_invalid"))?;
260            // The RIFF boundary limits decoding, not the retained source bytes.
261            // https://developers.google.com/speed/webp/docs/riff_container
262            ensure!(encoded_length % 2 == 0, "webp_length_invalid");
263            let mut decoder =
264                image::codecs::webp::WebPDecoder::new(Cursor::new(&bytes[..encoded_length]))?;
265            decoder.set_limits(limits())?;
266            if decoder.has_animation() {
267                for frame in decoder.into_frames() {
268                    count(frame)?;
269                }
270            } else {
271                let (w, h) = decoder.dimensions();
272                ensure!(
273                    w > 0 && h > 0 && w as u64 * h as u64 <= 4_000_000,
274                    "image_decode_limit"
275                );
276                image::DynamicImage::from_decoder(decoder)
277                    .map_err(|_| anyhow::anyhow!("image_decode_failed"))?;
278                width = w;
279                height = h;
280                frames = 1;
281            }
282            "image/webp"
283        }
284        image::ImageFormat::Png => {
285            ensure!(png_has_end_chunk(bytes), "png_trailer_missing");
286            let mut reader = image::ImageReader::with_format(Cursor::new(bytes), format);
287            reader.limits(limits());
288            let decoded = reader
289                .decode()
290                .map_err(|_| anyhow::anyhow!("image_decode_failed"))?;
291            width = decoded.width();
292            height = decoded.height();
293            ensure!(
294                width > 0 && height > 0 && width as u64 * height as u64 <= 16_777_216,
295                "image_decode_limit"
296            );
297            frames = 1;
298            "image/png"
299        }
300        _ => anyhow::bail!("image_format_unsupported"),
301    };
302    ensure!(frames > 0, "image_no_frames");
303    Ok(ImageInspection {
304        content_type: mime.into(),
305        width,
306        height,
307        frames,
308        animation_preserved: frames > 1,
309    })
310}
311
312#[cfg(test)]
313mod tests {
314    use super::*;
315    use crate::inspect_image;
316    #[test]
317    fn png_is_fully_decoded_and_truncation_fails() {
318        let mut bytes = std::io::Cursor::new(vec![]);
319        image::DynamicImage::new_rgb8(8, 8)
320            .write_to(&mut bytes, image::ImageFormat::Png)
321            .unwrap();
322        let good = bytes.into_inner();
323        assert_eq!(inspect_image(&good).unwrap().frames, 1);
324        for length in [20, good.len() - 12, good.len() - 1] {
325            assert!(
326                inspect_image(&good[..length]).is_err(),
327                "accepted PNG truncated to {length}"
328            );
329        }
330        let mut missing_end = good[..good.len() - 12].to_vec();
331        missing_end.extend_from_slice(&12u32.to_be_bytes());
332        missing_end.extend_from_slice(b"tEXt");
333        missing_end.extend_from_slice(&good[good.len() - 12..]);
334        missing_end.extend_from_slice(&[0; 4]);
335        assert_eq!(
336            inspect_image(&missing_end).unwrap_err().to_string(),
337            "png_trailer_missing"
338        );
339    }
340
341    fn still_image(format: image::ImageFormat) -> Vec<u8> {
342        let mut out = Cursor::new(Vec::new());
343        image::DynamicImage::new_rgb8(8, 8)
344            .write_to(&mut out, format)
345            .unwrap();
346        out.into_inner()
347    }
348
349    #[test]
350    fn non_jpeg_images_with_appended_data_retain_source_bytes() {
351        for (format, mime) in [
352            (image::ImageFormat::Png, "image/png"),
353            (image::ImageFormat::Gif, "image/gif"),
354            (image::ImageFormat::WebP, "image/webp"),
355        ] {
356            let mut bytes = still_image(format);
357            bytes.extend_from_slice(b"synthetic-opaque-trailer!");
358            let original = bytes.clone();
359            let inspection = inspect_image(&bytes).unwrap_or_else(|e| panic!("{format:?}: {e}"));
360            assert_eq!(inspection.content_type, mime);
361            assert_eq!((inspection.width, inspection.height), (8, 8));
362            assert_eq!(bytes, original);
363        }
364    }
365
366    #[test]
367    fn webp_declared_boundary_cannot_use_bytes_outside_riff() {
368        let good = still_image(image::ImageFormat::WebP);
369        assert_eq!(inspect_image(&good).unwrap().content_type, "image/webp");
370        assert!(inspect_image(&good[..good.len() - 1]).is_err());
371        let mut short_boundary = good;
372        short_boundary[4..8].copy_from_slice(&4u32.to_le_bytes());
373        assert!(inspect_image(&short_boundary).is_err());
374        short_boundary[4..8].copy_from_slice(&u32::MAX.to_le_bytes());
375        assert!(inspect_image(&short_boundary).is_err());
376    }
377
378    #[test]
379    fn animated_webp_keeps_all_frames_with_appended_data() {
380        let mut bytes = include_bytes!("../tests/fixtures/two-frame.webp").to_vec();
381        let plain = inspect_image(&bytes).unwrap();
382        assert_eq!(plain.frames, 2);
383        assert!(plain.animation_preserved);
384        bytes.extend_from_slice(b"extra-source-data");
385        let original = bytes.clone();
386        let inspected = inspect_image(&bytes).unwrap();
387        assert_eq!(inspected.frames, 2);
388        assert_eq!((inspected.width, inspected.height), (12, 8));
389        assert!(inspected.animation_preserved);
390        assert_eq!(bytes, original);
391    }
392
393    #[test]
394    fn gif_animation_and_real_trailer_are_checked_separately() {
395        let mut bytes = Vec::new();
396        {
397            let mut encoder = image::codecs::gif::GifEncoder::new(&mut bytes);
398            encoder
399                .encode_frames([
400                    image::Frame::new(image::RgbaImage::from_pixel(
401                        8,
402                        8,
403                        image::Rgba([0, 0, 0, 255]),
404                    )),
405                    image::Frame::new(image::RgbaImage::from_pixel(
406                        8,
407                        8,
408                        image::Rgba([255, 0, 0, 255]),
409                    )),
410                ])
411                .unwrap();
412        }
413        let complete_length = bytes.len();
414        assert_eq!(bytes.last(), Some(&b';'));
415        bytes.extend_from_slice(b"opaque-appended-data");
416        let result = inspect_image(&bytes).unwrap();
417        assert_eq!(result.frames, 2);
418        assert!(result.animation_preserved);
419
420        // A marker-shaped byte in a comment is not the stream's trailer.
421        let mut missing_trailer = bytes[..complete_length - 1].to_vec();
422        missing_trailer.extend_from_slice(&[0x21, 0xfe, 1, b';', 0]);
423        assert_eq!(
424            inspect_image(&missing_trailer).unwrap_err().to_string(),
425            "gif_trailer_missing"
426        );
427        // A truncated comment cannot be skipped just because it contains ';'.
428        missing_trailer.truncate(missing_trailer.len() - 1);
429        assert!(inspect_image(&missing_trailer).is_err());
430    }
431
432    fn jpeg() -> Vec<u8> {
433        let mut bytes = Cursor::new(vec![]);
434        image::DynamicImage::new_rgb8(8, 8)
435            .write_to(&mut bytes, image::ImageFormat::Jpeg)
436            .unwrap();
437        bytes.into_inner()
438    }
439
440    #[test]
441    fn jpeg_originals_larger_than_other_formats_are_inspected() {
442        let encode = |width, height, format| {
443            let mut bytes = Cursor::new(vec![]);
444            image::DynamicImage::new_rgb8(width, height)
445                .write_to(&mut bytes, format)
446                .unwrap();
447            bytes.into_inner()
448        };
449        // Past the 4,096-pixel edge, then past the full-decode pixel count.
450        for (width, height) in [(5000, 16), (8192, 2064)] {
451            let inspection = inspect_image(&encode(width, height, image::ImageFormat::Jpeg));
452            let inspection = inspection.unwrap();
453            assert_eq!((inspection.width, inspection.height), (width, height));
454            assert_eq!(inspection.content_type, "image/jpeg");
455        }
456        let too_wide = encode(8193, 16, image::ImageFormat::Jpeg);
457        assert!(inspect_image(&too_wide).is_err());
458        let mut truncated = encode(8192, 2064, image::ImageFormat::Jpeg);
459        truncated.truncate(truncated.len() / 2);
460        assert!(inspect_image(&truncated).is_err());
461        assert!(inspect_image(&encode(5000, 16, image::ImageFormat::Png)).is_err());
462    }
463    #[test]
464    fn jpeg_with_trailing_data_decodes_without_changing_source_bytes() {
465        let mut bytes = jpeg();
466        let checksum = <md5::Md5 as md5::Digest>::digest(&bytes);
467        bytes.extend_from_slice(&0x12345678u64.to_le_bytes());
468        bytes.extend_from_slice(&checksum);
469        let original = bytes.clone();
470        let inspected = inspect_image(&bytes).unwrap();
471        assert_eq!(inspected.content_type, "image/jpeg");
472        assert_eq!(
473            (inspected.width, inspected.height, inspected.frames),
474            (8, 8, 1)
475        );
476        assert_eq!(bytes, original);
477    }
478
479    #[test]
480    fn progressive_jpeg_multiple_scans_allow_trailing_data() {
481        let mut bytes = include_bytes!("../tests/fixtures/progressive.jpg").to_vec();
482        bytes.extend_from_slice(b"synthetic-photo-trailer!");
483        let inspected = inspect_image(&bytes).unwrap();
484        assert_eq!((inspected.width, inspected.height), (16, 12));
485        assert_eq!(inspected.content_type, "image/jpeg");
486        let end = bytes.len() - 24 - 2;
487        assert!(!jpeg_has_end_marker(&bytes[..end]));
488    }
489
490    #[test]
491    fn jpeg_end_marker_inside_metadata_does_not_hide_truncation() {
492        let image = jpeg();
493        let mut bytes = vec![0xff, 0xd8, 0xff, 0xfe, 0, 6, 0x11, 0xff, 0xd9, 0x22];
494        bytes.extend_from_slice(&image[2..]);
495        assert!(inspect_image(&bytes).is_ok());
496        bytes.truncate(bytes.len() - 2);
497        assert_eq!(
498            inspect_image(&bytes).unwrap_err().to_string(),
499            "jpeg_trailer_missing"
500        );
501        bytes[4..6].copy_from_slice(&u16::MAX.to_be_bytes());
502        bytes.extend_from_slice(&[0xff, 0xd9]);
503        assert!(!jpeg_has_end_marker(&bytes));
504    }
505
506    #[test]
507    fn jpeg_scan_stuffing_and_restart_markers_are_not_end_markers() {
508        let bytes = [
509            0xff, 0xd8, 0xff, 0xda, 0, 2, 7, 0xff, 0, 0xd9, 0xff, 0xd0, 9, 0xff,
510        ];
511        assert!(!jpeg_has_end_marker(&bytes));
512        let mut complete = bytes.to_vec();
513        complete.push(0xd9);
514        assert!(jpeg_has_end_marker(&complete));
515    }
516}