Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[photos] Bound ente-image decode memory with 64 MP / 384 MiB caps

laurenspriem64502a4deaa4
diff --git a/rust/crates/image/src/decode.rs b/rust/crates/image/src/decode.rs
index 02a58b29ef..3ffe47a164 100644
--- a/rust/crates/image/src/decode.rs
+++ b/rust/crates/image/src/decode.rs
@@ -33,6 +33,27 @@ use crate::{
static IMAGE_DECODER_HOOKS_INIT: Once = Once::new();
+const MAX_DECODE_PIXELS: u64 = 64_000_000;
+const MAX_DECODE_ALLOC_BYTES: u64 = 384 * 1024 * 1024;
+
+fn ensure_within_decode_pixel_cap(width: u32, height: u32) -> image::ImageResult<()> {
+ if u64::from(width) * u64::from(height) > MAX_DECODE_PIXELS {
+ return Err(image::ImageError::Limits(
+ image::error::LimitError::from_kind(image::error::LimitErrorKind::InsufficientMemory),
+ ));
+ }
+ Ok(())
+}
+
+fn ensure_within_decode_alloc_cap(total_bytes: u64) -> image::ImageResult<()> {
+ if total_bytes > MAX_DECODE_ALLOC_BYTES {
+ return Err(image::ImageError::Limits(
+ image::error::LimitError::from_kind(image::error::LimitErrorKind::InsufficientMemory),
+ ));
+ }
+ Ok(())
+}
+
struct DecodedDynamicImage {
image: DynamicImage,
icc_profile: Option<Vec<u8>>,
@@ -51,7 +72,12 @@ impl DecodedDynamicImage {
pub fn decode_image_from_path(image_path: &str) -> ImageResult<DecodedImage> {
let decoded_dynamic = decode_with_image_crate(image_path)?;
let (decoded_dynamic, orientation) = decoded_dynamic.into_srgb();
- let oriented = orient_decoded_image(decoded_dynamic, image_path, orientation).into_rgb8();
+ let oriented = orient_decoded_image(
+ DynamicImage::ImageRgb8(decoded_dynamic.into_rgb8()),
+ image_path,
+ orientation,
+ )
+ .into_rgb8();
Ok(DecodedImage {
dimensions: Dimensions {
@@ -65,8 +91,12 @@ pub fn decode_image_from_path(image_path: &str) -> ImageResult<DecodedImage> {
pub fn decode_image_from_bytes(image_bytes: &[u8]) -> ImageResult<DecodedImage> {
let decoded_dynamic = decode_bytes_with_image_crate(image_bytes)?;
let (decoded_dynamic, orientation) = decoded_dynamic.into_srgb();
- let oriented =
- orient_decoded_image_from_bytes(decoded_dynamic, image_bytes, orientation).into_rgb8();
+ let oriented = orient_decoded_image_from_bytes(
+ DynamicImage::ImageRgb8(decoded_dynamic.into_rgb8()),
+ image_bytes,
+ orientation,
+ )
+ .into_rgb8();
Ok(DecodedImage {
dimensions: Dimensions {
@@ -95,10 +125,7 @@ fn decode_with_image_crate(image_path: &str) -> ImageResult<DecodedDynamicImage>
match decode_with_tiff_crate(image_path) {
Ok(decoded) => Ok(decoded),
- Err(ImageError::Decode(fallback_error)) => Err(ImageError::Decode(format!(
- "failed to decode TIFF with image crate: {primary_error}; fallback with tiff crate also failed: {fallback_error}"
- ))),
- Err(other) => Err(other),
+ Err(fallback_error) => Err(tiff_fallback_failure(primary_error, fallback_error)),
}
}
Err(other) => Err(other.into()),
@@ -118,16 +145,30 @@ fn decode_bytes_with_image_crate(image_bytes: &[u8]) -> ImageResult<DecodedDynam
Err(primary_error) if should_attempt_tiff_fallback(guessed_format) => {
match decode_tiff_from_bytes(image_bytes) {
Ok(decoded) => Ok(decoded),
- Err(ImageError::Decode(fallback_error)) => Err(ImageError::Decode(format!(
- "failed to decode TIFF with image crate: {primary_error}; fallback with tiff crate also failed: {fallback_error}"
- ))),
- Err(other) => Err(other),
+ Err(fallback_error) => Err(tiff_fallback_failure(primary_error, fallback_error)),
}
}
Err(other) => Err(other.into()),
}
}
+fn tiff_fallback_failure(
+ primary_error: image::ImageError,
+ fallback_error: ImageError,
+) -> ImageError {
+ let primary_message = primary_error.to_string();
+ let primary_error = ImageError::from(primary_error);
+ if matches!(primary_error, ImageError::TooLarge(_)) {
+ return primary_error;
+ }
+ let ImageError::Decode(fallback_message) = fallback_error else {
+ return fallback_error;
+ };
+ ImageError::Decode(format!(
+ "failed to decode TIFF with image crate: {primary_message}; fallback with tiff crate also failed: {fallback_message}"
+ ))
+}
+
fn decode_reader_with_image_crate<R>(
reader: ImageReader<R>,
) -> image::ImageResult<DecodedDynamicImage>
@@ -137,6 +178,9 @@ where
// ente_heic decodes lazily into DynamicImage's output buffer, so the
// decoder must remain intact through `from_decoder`.
let mut decoder = reader.into_decoder()?;
+ let (width, height) = decoder.dimensions();
+ ensure_within_decode_pixel_cap(width, height)?;
+ ensure_within_decode_alloc_cap(decoder.total_bytes())?;
let icc_profile = match decoder.icc_profile() {
Ok(icc_profile) => icc_profile,
Err(err) => {
@@ -183,6 +227,7 @@ fn decode_tiff<R: Read + Seek>(reader: R, source: &str) -> ImageResult<DecodedDy
let (width, height) = decoder
.dimensions()
.map_err(|e| ImageError::Decode(format!("failed to read TIFF dimensions: {e}")))?;
+ ensure_within_decode_pixel_cap(width, height)?;
let color_type = decoder
.colortype()
.map_err(|e| ImageError::Decode(format!("failed to read TIFF color type: {e}")))?;
@@ -193,9 +238,7 @@ fn decode_tiff<R: Read + Seek>(reader: R, source: &str) -> ImageResult<DecodedDy
.flatten()
.and_then(|value| u8::try_from(value).ok())
.and_then(Orientation::from_exif);
- let decoded = decoder
- .read_image()
- .map_err(|e| ImageError::Decode(format!("failed to decode TIFF image data: {e}")))?;
+ let decoded = decoder.read_image().map_err(tiff_decode_error)?;
Ok(DecodedDynamicImage {
image: dynamic_image_from_tiff(source, width, height, color_type, decoded)?,
@@ -204,6 +247,15 @@ fn decode_tiff<R: Read + Seek>(reader: R, source: &str) -> ImageResult<DecodedDy
})
}
+fn tiff_decode_error(error: tiff::TiffError) -> ImageError {
+ if matches!(error, tiff::TiffError::LimitsExceeded) {
+ return ImageError::TooLarge(image::ImageError::Limits(
+ image::error::LimitError::from_kind(image::error::LimitErrorKind::InsufficientMemory),
+ ));
+ }
+ ImageError::Decode(format!("failed to decode TIFF image data: {error}"))
+}
+
fn dynamic_image_from_tiff(
source: &str,
width: u32,
@@ -494,9 +546,10 @@ mod tests {
};
use super::{
- bytes_look_like_heif, decode_image_from_bytes, decode_tiff_from_bytes, init_image_decoders,
- should_attempt_tiff_fallback,
+ bytes_look_like_heif, decode_image_from_bytes, decode_tiff_from_bytes,
+ ensure_within_decode_pixel_cap, init_image_decoders, should_attempt_tiff_fallback,
};
+ use crate::ImageError;
#[test]
fn attempts_tiff_fallback_for_tiff_format() {
@@ -568,6 +621,206 @@ mod tests {
assert!(!bytes_look_like_heif(b"not an image"));
}
+ #[test]
+ fn admits_panoramas_and_phone_photos_within_the_pixel_cap() {
+ ensure_within_decode_pixel_cap(20_000, 1_000).unwrap();
+ ensure_within_decode_pixel_cap(8_064, 6_024).unwrap();
+ ensure_within_decode_pixel_cap(8_000, 8_000).unwrap();
+ assert!(ensure_within_decode_pixel_cap(8_000, 8_001).is_err());
+ assert!(ensure_within_decode_pixel_cap(u32::MAX, u32::MAX).is_err());
+ }
+
+ #[test]
+ fn rejects_images_exceeding_the_pixel_cap() {
+ let bytes = header_only_png(80_000, 1_000, ::png::ColorType::Rgb, ::png::BitDepth::Eight);
+
+ assert!(matches!(
+ decode_image_from_bytes(&bytes),
+ Err(ImageError::TooLarge(_))
+ ));
+ }
+
+ #[test]
+ fn rejects_decoder_output_exceeding_the_alloc_cap() {
+ let bytes = header_only_png(
+ 8_000,
+ 6_600,
+ ::png::ColorType::Rgba,
+ ::png::BitDepth::Sixteen,
+ );
+
+ assert!(matches!(
+ decode_image_from_bytes(&bytes),
+ Err(ImageError::TooLarge(_))
+ ));
+ }
+
+ #[test]
+ fn rejects_tiffs_exceeding_the_pixel_cap() {
+ let bytes = header_only_gray_tiff(80_000, 1_000);
+
+ assert!(matches!(
+ decode_tiff_from_bytes(&bytes),
+ Err(ImageError::TooLarge(_))
+ ));
+ assert!(matches!(
+ decode_image_from_bytes(&bytes),
+ Err(ImageError::TooLarge(_))
+ ));
+ }
+
+ #[test]
+ fn rejects_over_cap_tiff_output_as_too_large() {
+ let bytes = header_only_rgba16_tiff(8_000, 6_600);
+
+ assert!(matches!(
+ decode_tiff_from_bytes(&bytes),
+ Err(ImageError::TooLarge(_))
+ ));
+ assert!(matches!(
+ decode_image_from_bytes(&bytes),
+ Err(ImageError::TooLarge(_))
+ ));
+ }
+
+ #[test]
+ fn rejects_over_cap_planar_tiff_as_too_large() {
+ let bytes = planar_rgba16_header_only_tiff(8_000, 6_600);
+
+ assert!(matches!(
+ decode_image_from_bytes(&bytes),
+ Err(ImageError::TooLarge(_))
+ ));
+ }
+
+ fn header_only_png(
+ width: u32,
+ height: u32,
+ color: ::png::ColorType,
+ depth: ::png::BitDepth,
+ ) -> Vec<u8> {
+ let mut bytes = Vec::new();
+ let mut encoder = ::png::Encoder::new(&mut bytes, width, height);
+ encoder.set_color(color);
+ encoder.set_depth(depth);
+ let mut writer = encoder.write_header().unwrap();
+ writer.write_chunk(::png::chunk::IDAT, &[]).unwrap();
+ writer.finish().unwrap();
+ bytes
+ }
+
+ fn header_only_gray_tiff(width: u32, height: u32) -> Vec<u8> {
+ let ifd_offset = 8u32;
+ let ifd_bytes = 2 + 7 * 12 + 4;
+ let strip_offset = ifd_offset + ifd_bytes as u32;
+
+ let entries: [(u16, u16, u32, u32); 7] = [
+ (256, 4, 1, width),
+ (257, 4, 1, height),
+ (258, 3, 1, 8),
+ (259, 3, 1, 1),
+ (262, 3, 1, 1),
+ (273, 4, 1, strip_offset),
+ (279, 4, 1, width * height),
+ ];
+
+ let mut bytes = Vec::new();
+ bytes.extend_from_slice(b"II");
+ bytes.extend_from_slice(&42u16.to_le_bytes());
+ bytes.extend_from_slice(&ifd_offset.to_le_bytes());
+ bytes.extend_from_slice(&(entries.len() as u16).to_le_bytes());
+ for (tag, field_type, count, value) in entries {
+ bytes.extend_from_slice(&tag.to_le_bytes());
+ bytes.extend_from_slice(&field_type.to_le_bytes());
+ bytes.extend_from_slice(&count.to_le_bytes());
+ bytes.extend_from_slice(&value.to_le_bytes());
+ }
+ bytes.extend_from_slice(&0u32.to_le_bytes());
+ bytes
+ }
+
+ fn header_only_rgba16_tiff(width: u32, height: u32) -> Vec<u8> {
+ let ifd_offset = 8u32;
+ let ifd_bytes = 2 + 9 * 12 + 4;
+ let bits_offset = ifd_offset + ifd_bytes;
+ let strip_offset = bits_offset + 8;
+
+ let entries: [(u16, u16, u32, u32); 9] = [
+ (256, 4, 1, width),
+ (257, 4, 1, height),
+ (258, 3, 4, bits_offset),
+ (259, 3, 1, 1),
+ (262, 3, 1, 2),
+ (273, 4, 1, strip_offset),
+ (277, 3, 1, 4),
+ (279, 4, 1, width * height * 8),
+ (338, 3, 1, 0),
+ ];
+
+ let mut bytes = Vec::new();
+ bytes.extend_from_slice(b"II");
+ bytes.extend_from_slice(&42u16.to_le_bytes());
+ bytes.extend_from_slice(&ifd_offset.to_le_bytes());
+ bytes.extend_from_slice(&(entries.len() as u16).to_le_bytes());
+ for (tag, field_type, count, value) in entries {
+ bytes.extend_from_slice(&tag.to_le_bytes());
+ bytes.extend_from_slice(&field_type.to_le_bytes());
+ bytes.extend_from_slice(&count.to_le_bytes());
+ bytes.extend_from_slice(&value.to_le_bytes());
+ }
+ bytes.extend_from_slice(&0u32.to_le_bytes());
+ for _ in 0..4 {
+ bytes.extend_from_slice(&16u16.to_le_bytes());
+ }
+ bytes
+ }
+
+ fn planar_rgba16_header_only_tiff(width: u32, height: u32) -> Vec<u8> {
+ let ifd_offset = 8u32;
+ let ifd_bytes = 2 + 10 * 12 + 4;
+ let bits_offset = ifd_offset + ifd_bytes;
+ let strip_offsets_offset = bits_offset + 8;
+ let strip_counts_offset = strip_offsets_offset + 16;
+ let strip_offset = strip_counts_offset + 16;
+
+ let entries: [(u16, u16, u32, u32); 10] = [
+ (256, 4, 1, width),
+ (257, 4, 1, height),
+ (258, 3, 4, bits_offset),
+ (259, 3, 1, 1),
+ (262, 3, 1, 2),
+ (273, 4, 4, strip_offsets_offset),
+ (277, 3, 1, 4),
+ (279, 4, 4, strip_counts_offset),
+ (284, 3, 1, 2),
+ (338, 3, 1, 0),
+ ];
+
+ let mut bytes = Vec::new();
+ bytes.extend_from_slice(b"II");
+ bytes.extend_from_slice(&42u16.to_le_bytes());
+ bytes.extend_from_slice(&ifd_offset.to_le_bytes());
+ bytes.extend_from_slice(&(entries.len() as u16).to_le_bytes());
+ for (tag, field_type, count, value) in entries {
+ bytes.extend_from_slice(&tag.to_le_bytes());
+ bytes.extend_from_slice(&field_type.to_le_bytes());
+ bytes.extend_from_slice(&count.to_le_bytes());
+ bytes.extend_from_slice(&value.to_le_bytes());
+ }
+ bytes.extend_from_slice(&0u32.to_le_bytes());
+ for _ in 0..4 {
+ bytes.extend_from_slice(&16u16.to_le_bytes());
+ }
+ for _ in 0..4 {
+ bytes.extend_from_slice(&strip_offset.to_le_bytes());
+ }
+ let plane_bytes = width * height * 2;
+ for _ in 0..4 {
+ bytes.extend_from_slice(&plane_bytes.to_le_bytes());
+ }
+ bytes
+ }
+
#[test]
fn registers_jxl_decoder_hook() {
init_image_decoders();