//! Hostile inputs: bombs, truncation, garbage and mutation. Nothing may panic or allocate from a header. mod common; use std::time::{Duration, Instant}; use common::*; use pictor_raster::{Format, Limits, RasterError, decode, probe}; fn limits() -> Limits { Limits::default() } #[test] fn png_bomb_is_refused_by_the_pixel_cap_quickly() { let bomb = png_with_size(100_000, 100_000); assert!(bomb.len() < 100); let start = Instant::now(); let err = decode(&bomb, &limits()).unwrap_err(); assert!(start.elapsed() < Duration::from_secs(1), "took {:?}", start.elapsed()); match &err { RasterError::TooManyPixels { width, height } => assert_eq!((*width, *height), (100_000, 100_000)), other => panic!("expected TooManyPixels, got {other:?}"), } assert_eq!(err.to_string(), "This image is too large (100000 × 100000 pixels)."); let p = probe(&bomb, &limits()).unwrap(); assert_eq!((p.width, p.height, p.format), (100_000, 100_000, Format::Png)); } #[test] fn jpeg_with_huge_sof_is_refused() { let bomb = patch_jpeg_sof(jpeg_fixture(32, 32), 65_000, 65_000); let start = Instant::now(); let err = decode(&bomb, &limits()).unwrap_err(); assert!(start.elapsed() < Duration::from_secs(1)); assert!(matches!(err, RasterError::TooManyPixels { width: 65_000, height: 65_000 }), "{err:?}"); } #[test] fn gif_bomb_by_header_is_refused() { let bomb = gif_with_size(65_535, 65_535); let start = Instant::now(); let err = decode(&bomb, &limits()).unwrap_err(); assert!(start.elapsed() < Duration::from_secs(1)); assert!(matches!(err, RasterError::TooManyPixels { .. } | RasterError::Corrupt { .. }), "{err:?}"); } #[test] fn tiff_bomb_by_header_is_refused() { let bomb = tiff_with_size(1_000_000, 1_000_000); let start = Instant::now(); let err = decode(&bomb, &limits()).unwrap_err(); assert!(start.elapsed() < Duration::from_secs(1)); assert!(matches!(err, RasterError::TooManyPixels { width: 1_000_000, height: 1_000_000 }), "{err:?}"); } #[test] fn a_tiny_cap_refuses_an_ordinary_image() { let png = encode_with_image(&sample_rgb(64, 64), image::ImageFormat::Png); let small = Limits { max_pixels: 1000, ..Limits::default() }; assert!(matches!(decode(&png, &small), Err(RasterError::TooManyPixels { .. }))); let tiny_input = Limits { max_input_bytes: 10, ..Limits::default() }; assert!(matches!(decode(&png, &tiny_input), Err(RasterError::InputTooLarge { .. }))); let tiny_alloc = Limits { max_alloc: 1000, ..Limits::default() }; assert!(matches!(decode(&png, &tiny_alloc), Err(RasterError::TooMuchMemory { .. }))); } #[test] fn truncated_png_errors() { let png = encode_with_image(&sample_rgb(200, 150), image::ImageFormat::Png); let cut = &png[..png.len() * 6 / 10]; let err = decode(cut, &limits()).unwrap_err(); assert!(matches!(err, RasterError::Corrupt { format: Format::Png, .. }), "{err:?}"); } #[test] fn truncated_jpeg_does_not_panic() { let jpeg = jpeg_fixture(200, 150); let cut = &jpeg[..jpeg.len() * 6 / 10]; // zune-jpeg may fill the missing part; both outcomes are acceptable as long as nothing panics. match decode(cut, &limits()) { Ok(img) => assert_eq!((img.width(), img.height()), (200, 150)), Err(e) => assert!(matches!(e, RasterError::Corrupt { format: Format::Jpeg, .. }), "{e:?}"), } } #[test] fn empty_and_random_inputs_are_typed_errors() { assert!(matches!(decode(&[], &limits()), Err(RasterError::Empty))); assert!(matches!(probe(&[], &limits()), Err(RasterError::Empty))); let mut rng = XorShift::new(7); for len in [1usize, 3, 16, 100, 4096] { let junk: Vec = (0..len).map(|_| rng.byte()).collect(); let err = decode(&junk, &limits()).unwrap_err(); assert!(matches!(err, RasterError::UnknownFormat | RasterError::Corrupt { .. }), "{err:?}"); } } #[test] fn bogus_ico_directory_is_a_typed_error() { let ico = bogus_ico(); assert_eq!(Format::sniff(&ico), Some(Format::Ico)); let err = decode(&ico, &limits()).unwrap_err(); assert!(matches!(err, RasterError::Corrupt { .. } | RasterError::TooManyPixels { .. }), "{err:?}"); let _ = probe(&ico, &limits()); } #[test] fn every_error_message_is_plain_english() { let samples: Vec = vec![ decode(&[], &limits()).unwrap_err(), decode(b"hello world, not an image", &limits()).unwrap_err(), decode(&png_with_size(100_000, 100_000), &limits()).unwrap_err(), decode(&bogus_ico(), &limits()).unwrap_err(), ]; for e in samples { let msg = e.to_string(); assert!(msg.ends_with('.'), "{msg}"); assert!(!msg.contains("Error") && !msg.contains("{"), "{msg}"); } } fn valid_fixtures() -> Vec<(&'static str, Vec)> { let rgb = sample_rgb(24, 16); let rgba = sample_rgba(24, 16); vec![ ("png", encode_with_image(&rgba, image::ImageFormat::Png)), ("jpeg", encode_with_image(&rgb, image::ImageFormat::Jpeg)), ("jpeg-exif", jpeg_with_orientation(&encode_with_image(&rgb, image::ImageFormat::Jpeg), 6)), ("gif", encode_with_image(&rgba, image::ImageFormat::Gif)), ("bmp", encode_with_image(&rgb, image::ImageFormat::Bmp)), ("tiff", encode_with_image(&rgba, image::ImageFormat::Tiff)), ("webp", encode_with_image(&rgba, image::ImageFormat::WebP)), ("ico", encode_with_image(&rgba, image::ImageFormat::Ico)), ("heic", minimal_heic(64, 64)), ] } fn mutate(rng: &mut XorShift, src: &[u8]) -> Vec { let mut m = src.to_vec(); match rng.below(5) { 0 => { for _ in 0..1 + rng.below(8) { let i = rng.below(m.len()); m[i] = rng.byte(); } } 1 => { let i = rng.below(m.len()); m[i] ^= 1 << rng.below(8); } 2 => m.truncate(rng.below(m.len())), 3 => { let i = rng.below(m.len()); let extra: Vec = (0..1 + rng.below(16)).map(|_| rng.byte()).collect(); m.splice(i..i, extra); } _ => { // Header fields are where the interesting bugs live: hammer the first 64 bytes with 0xFF / 0x00. let i = rng.below(m.len().min(64)); m[i] = if rng.below(2) == 0 { 0xFF } else { 0x00 }; } } m } #[test] fn mutation_loop_never_panics() { let limits = Limits { max_pixels: 4_000_000, max_input_bytes: 1 << 20, max_alloc: 64 << 20 }; let mut rng = XorShift::new(0x5EED_1234_ABCD); let start = Instant::now(); for (name, fixture) in valid_fixtures() { if name != "heic" { assert!(decode(&fixture, &limits).is_ok(), "fixture {name} must decode"); } // Each HEIC attempt activates the Store codec (~0.3 s), so it gets fewer rounds. let rounds = if name == "heic" { 20 } else { 300 }; let t = Instant::now(); for round in 0..rounds { let m = mutate(&mut rng, &fixture); let caught = std::panic::catch_unwind(|| { let _ = probe(&m, &limits); let _ = decode(&m, &limits); }); assert!(caught.is_ok(), "panic on {name} mutation {round}"); } eprintln!("{name}: {rounds} mutations in {:?}", t.elapsed()); } eprintln!("mutation loop took {:?}", start.elapsed()); }