128 lines
4.6 KiB
Rust
128 lines
4.6 KiB
Rust
//! Synthetic images for the sandbox tests and the seccomp derivation (`test-hooks` only):
|
||
//! nothing binary lives in the repository.
|
||
|
||
use std::io::Cursor;
|
||
|
||
use image::{DynamicImage, ImageBuffer, ImageFormat, Rgba};
|
||
|
||
fn gradient(width: u32, height: u32) -> DynamicImage {
|
||
let buffer = ImageBuffer::from_fn(width, height, |x, y| {
|
||
let r = u8::try_from(x * 255 / width.max(1)).unwrap_or(255);
|
||
let g = u8::try_from(y * 255 / height.max(1)).unwrap_or(255);
|
||
let b = u8::try_from((x / 8 + y / 8) % 256).unwrap_or(0);
|
||
Rgba([r, g, b, 255])
|
||
});
|
||
DynamicImage::ImageRgba8(buffer)
|
||
}
|
||
|
||
fn encode(image: &DynamicImage, format: ImageFormat) -> Vec<u8> {
|
||
let mut out = Cursor::new(Vec::new());
|
||
if image.write_to(&mut out, format).is_err() {
|
||
return Vec::new();
|
||
}
|
||
out.into_inner()
|
||
}
|
||
|
||
/// A PNG gradient.
|
||
pub fn png(width: u32, height: u32) -> Vec<u8> {
|
||
encode(&gradient(width, height), ImageFormat::Png)
|
||
}
|
||
|
||
/// A baseline JPEG gradient (no alpha channel).
|
||
pub fn jpeg(width: u32, height: u32) -> Vec<u8> {
|
||
let rgb = DynamicImage::ImageRgb8(gradient(width, height).into_rgb8());
|
||
encode(&rgb, ImageFormat::Jpeg)
|
||
}
|
||
|
||
/// A lossless WebP gradient (the only WebP encoder of the `image` crate).
|
||
pub fn webp(width: u32, height: u32) -> Vec<u8> {
|
||
encode(&gradient(width, height), ImageFormat::WebP)
|
||
}
|
||
|
||
/// A PNG whose header declares `width` × `height` with no pixel data: the decoder must
|
||
/// refuse it on dimensions before allocating anything.
|
||
pub fn png_header_only(width: u32, height: u32) -> Vec<u8> {
|
||
let mut out = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a];
|
||
let mut ihdr = Vec::new();
|
||
ihdr.extend_from_slice(&width.to_be_bytes());
|
||
ihdr.extend_from_slice(&height.to_be_bytes());
|
||
ihdr.extend_from_slice(&[8, 6, 0, 0, 0]);
|
||
push_chunk(&mut out, b"IHDR", &ihdr);
|
||
push_chunk(&mut out, b"IEND", &[]);
|
||
out
|
||
}
|
||
|
||
fn push_chunk(out: &mut Vec<u8>, kind: &[u8; 4], payload: &[u8]) {
|
||
out.extend_from_slice(&(payload.len() as u32).to_be_bytes());
|
||
let mut hasher = crc32fast::Hasher::new();
|
||
hasher.update(kind);
|
||
hasher.update(payload);
|
||
out.extend_from_slice(kind);
|
||
out.extend_from_slice(payload);
|
||
out.extend_from_slice(&hasher.finalize().to_be_bytes());
|
||
}
|
||
|
||
/// A small PNG header whose `iCCP` profile inflates to `inflated_bytes` (hundreds of
|
||
/// kilobytes of compressed zeros) and that carries no image data: the decoder refuses it as
|
||
/// content (`decode_failed` or `resource_limit`, depending on which check fires first),
|
||
/// never as a broken environment. R1 adds a variant with image data so the budget itself
|
||
/// is seen firing.
|
||
pub fn png_iccp_bomb(inflated_bytes: usize) -> Vec<u8> {
|
||
use std::io::Write;
|
||
let mut out = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a];
|
||
let mut ihdr = Vec::new();
|
||
ihdr.extend_from_slice(&64u32.to_be_bytes());
|
||
ihdr.extend_from_slice(&64u32.to_be_bytes());
|
||
ihdr.extend_from_slice(&[8, 6, 0, 0, 0]);
|
||
push_chunk(&mut out, b"IHDR", &ihdr);
|
||
let mut iccp = b"p\0\0".to_vec();
|
||
let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best());
|
||
let zeros = vec![0u8; 1 << 20];
|
||
let mut written = 0;
|
||
while written < inflated_bytes {
|
||
let chunk = (inflated_bytes - written).min(zeros.len());
|
||
if encoder.write_all(&zeros[..chunk]).is_err() {
|
||
break;
|
||
}
|
||
written += chunk;
|
||
}
|
||
iccp.extend_from_slice(&encoder.finish().unwrap_or_default());
|
||
push_chunk(&mut out, b"iCCP", &iccp);
|
||
push_chunk(&mut out, b"IEND", &[]);
|
||
out
|
||
}
|
||
|
||
/// The first bytes of a GIF: a format the avatar pipeline never accepts.
|
||
pub fn gif() -> Vec<u8> {
|
||
let mut out = b"GIF89a".to_vec();
|
||
out.extend_from_slice(&[64, 0, 64, 0, 0xf7, 0, 0]);
|
||
out.extend(std::iter::repeat_n(0u8, 800));
|
||
out
|
||
}
|
||
|
||
/// A valid PNG cut in half.
|
||
pub fn truncated_png() -> Vec<u8> {
|
||
let mut bytes = png(200, 200);
|
||
bytes.truncate(bytes.len() / 2);
|
||
bytes
|
||
}
|
||
|
||
/// Writes one named fixture on stdout (`faunosteo-worker fixture <name>`).
|
||
pub fn by_name(name: &str) -> Option<Vec<u8>> {
|
||
Some(match name {
|
||
"png" => png(800, 600),
|
||
"jpeg" => jpeg(640, 480),
|
||
"webp" => webp(300, 700),
|
||
"big-jpeg" => jpeg(4096, 4096),
|
||
// Same pixel budget as the JPEG but compressible (4096 × 2048 of this gradient
|
||
// weighed more than the 5 MiB input cap).
|
||
"big-png" => png(3000, 3000),
|
||
"tiny" => png(32, 32),
|
||
"huge-header" => png_header_only(20_000, 20_000),
|
||
"iccp-bomb" => png_iccp_bomb(300 << 20),
|
||
"gif" => gif(),
|
||
"truncated" => truncated_png(),
|
||
_ => return None,
|
||
})
|
||
}
|