Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Codecs: bound thumbnail fallback decodes and WebP metadata allocations

radialmonsteree9a989834a9
diff --git a/crates/codecs/README.md b/crates/codecs/README.md
index 4b815a6cd..d826b5ec1 100644
--- a/crates/codecs/README.md
+++ b/crates/codecs/README.md
@@ -24,6 +24,12 @@ encode_webp_lossless(&img, &meta)?;
encode_avif(&img, 70, 6, &meta)?; // native + feature `avif`
```
+Thumbnail fallback decoding limits the source to 64 million pixels by default.
+Non-JPEG decoders allocate at source resolution before resizing, even for a tiny
+thumbnail. Use `decode_thumbnail_with` and `ThumbnailOptions::max_pixels` to set
+a different source budget. Oversized sources return `Error::TooLarge`; embedded
+JPEG previews can still be used without decoding the full source image.
+
`Format::RawTiffLike` (DNG, CR2, NEF, ARW, PEF, ORF, RW2, SRW, …) and `Format::RawOther` (CR3, RAF,
CRW, MRW, X3F) are detected so the engine can route them to `lightcraft-raw`; `decode` returns
`Error::Unsupported` for them.
diff --git a/crates/codecs/src/lib.rs b/crates/codecs/src/lib.rs
index 3e8929e14..708aa9b79 100644
--- a/crates/codecs/src/lib.rs
+++ b/crates/codecs/src/lib.rs
@@ -217,11 +217,19 @@ pub struct ThumbnailOptions {
/// smaller size). Defaults to `max_edge`, i.e. only previews that need no upscaling. Lower it
/// (e.g. 160 for typical EXIF thumbnails) for an instant placeholder.
pub min_embedded_edge: u32,
+ /// Maximum source pixels for fallback decoding (default 64 MP). Non-JPEG formats
+ /// decode at source resolution before resizing, so output size does not bound memory.
+ #[serde(default = "thumbnail_max_pixels")]
+ pub max_pixels: u64,
+}
+
+fn thumbnail_max_pixels() -> u64 {
+ 64_000_000
}
impl ThumbnailOptions {
pub fn new(max_edge: u32) -> Self {
- ThumbnailOptions { max_edge, min_embedded_edge: max_edge }
+ ThumbnailOptions { max_edge, min_embedded_edge: max_edge, max_pixels: thumbnail_max_pixels() }
}
}
@@ -240,7 +248,7 @@ pub fn decode_thumbnail_with(bytes: &[u8], opts: &ThumbnailOptions) -> Result<Th
{
return Ok(t);
}
- let d = decode(bytes, DecodeOptions::fit(max_edge, max_edge))?;
+ let d = decode(bytes, DecodeOptions { max_size: Some((max_edge, max_edge)), max_pixels: opts.max_pixels })?;
Ok(Thumbnail {
image: d.to_srgb8(),
orientation: d.orientation,
diff --git a/crates/codecs/src/webp.rs b/crates/codecs/src/webp.rs
index ba4cd0c7d..8da561017 100644
--- a/crates/codecs/src/webp.rs
+++ b/crates/codecs/src/webp.rs
@@ -12,6 +12,9 @@ fn err(e: impl std::fmt::Display) -> Error {
pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
let mut d = image_webp::WebPDecoder::new(std::io::Cursor::new(bytes)).map_err(err)?;
+ // RIFF chunk lengths are untrusted. Metadata cannot exceed the input;
+ // otherwise a tiny truncated file can request gigabytes before read_exact fails.
+ d.set_memory_limit(bytes.len());
let (w, h) = d.dimensions();
check_size(F, w as u64, h as u64, opts)?;
let alpha = d.has_alpha();
diff --git a/crates/codecs/tests/fuzz.rs b/crates/codecs/tests/fuzz.rs
index 51f8096a4..cf858d6b5 100644
--- a/crates/codecs/tests/fuzz.rs
+++ b/crates/codecs/tests/fuzz.rs
@@ -32,10 +32,46 @@ fn try_all(bytes: &[u8]) {
let _ = decode_unguarded(bytes, f, &DecodeOptions { max_size: Some((8, 8)), max_pixels: 1 << 22 });
}
let _ = decode(bytes, small_opts());
- let _ = decode_thumbnail(bytes, 16);
+ let _ = decode_thumbnail_with(bytes, &ThumbnailOptions { max_pixels: small_opts().max_pixels, ..ThumbnailOptions::new(16) });
let _ = lightcraft_codecs::icc::parse(bytes);
}
+#[test]
+fn webp_truncated_metadata_cannot_allocate_declared_chunk_size() {
+ let mut bytes = seeds()[5].clone();
+ let tag = bytes.windows(4).rposition(|w| w == b"XMP ").unwrap();
+ bytes[tag + 4..tag + 8].copy_from_slice(&1_946_157_060u32.to_le_bytes());
+ let decoded = decode(&bytes, small_opts()).unwrap();
+ assert_eq!((decoded.image.width, decoded.image.height), (24, 16));
+ assert!(decoded.xmp.is_none(), "invalid optional metadata is ignored");
+}
+
+#[test]
+fn thumbnail_rejects_huge_gif_before_allocating_source_pixels() {
+ // A logical screen header without a global palette; previously this reached a
+ // multi-gigabyte output allocation before discovering that no frame follows.
+ let bytes = [
+ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x8f, 0xa4, 0xb0, 0x3f, 0x57, 0x14, 0xe8, 0x8a, 0xf8, 0xe0, 0xc7, 0x41, 0x9d, 0x0c, 0x10, 0xde, 0x54,
+ 0x24, 0xcb, 0xed, 0xff, 0x4a, 0xe7, 0xa0, 0x82, 0x10, 0xa0, 0xbc, 0xac, 0xe5, 0x33, 0x3f, 0xc9, 0x8d, 0x69, 0xf4, 0x24, 0xce, 0x99, 0x4f,
+ 0x0d, 0x47, 0xa0, 0xb0, 0xf4, 0xa8, 0x59, 0xad, 0x29, 0xbd, 0x09, 0x48, 0x63, 0xea, 0x59, 0xb7, 0xb7, 0xf2, 0xa1, 0x90, 0xeb, 0x92, 0xc5,
+ 0x9c, 0xde, 0xcc, 0x96, 0x67, 0xc4, 0xe5, 0x8c, 0x03, 0x50, 0xb2, 0x30, 0xf8, 0xf4, 0x69, 0x1d, 0xeb, 0xb9, 0xbd, 0x4e, 0xfb, 0xbd, 0xb6,
+ 0x02,
+ ];
+ let result = decode_thumbnail(&bytes, 16);
+ assert!(matches!(result, Err(Error::TooLarge(42_127, 16_304))), "{result:?}");
+}
+
+#[test]
+fn thumbnail_respects_configured_source_pixel_limit() {
+ let image = Rgba8::from_fn(3, 2, |_, _| [40, 80, 120, 255]);
+ let bytes = encode_png(&EncodeImage::rgba8(&image), &EncodeMeta::default()).unwrap();
+ let mut opts = ThumbnailOptions::new(1);
+ opts.max_pixels = 5;
+ assert!(matches!(decode_thumbnail_with(&bytes, &opts), Err(Error::TooLarge(3, 2))));
+ opts.max_pixels = 6;
+ assert_eq!(decode_thumbnail_with(&bytes, &opts).unwrap().image.width, 1);
+}
+
fn seeds() -> &'static Vec<Vec<u8>> {
static S: OnceLock<Vec<Vec<u8>>> = OnceLock::new();
S.get_or_init(|| {
diff --git a/crates/codecs/tests/roundtrip.rs b/crates/codecs/tests/roundtrip.rs
index 259ee835b..4bce59de8 100644
--- a/crates/codecs/tests/roundtrip.rs
+++ b/crates/codecs/tests/roundtrip.rs
@@ -461,7 +461,7 @@ fn thumbnail_accepts_smaller_preview_when_asked() {
encode_jpeg(&EncodeImage::rgba8(&main), 80, ChromaSubsampling::S420, &EncodeMeta { exif: Some(&exif), ..Default::default() }).unwrap();
let t = decode_thumbnail(&bytes, 256).unwrap();
assert_eq!(t.source, ThumbnailSource::Scaled);
- let t = decode_thumbnail_with(&bytes, &ThumbnailOptions { max_edge: 256, min_embedded_edge: 160 }).unwrap();
+ let t = decode_thumbnail_with(&bytes, &ThumbnailOptions { min_embedded_edge: 160, ..ThumbnailOptions::new(256) }).unwrap();
assert_eq!(t.source, ThumbnailSource::ExifThumbnail);
assert_eq!((t.image.width, t.image.height), (160, 120));
}