Skip to content

armgit / ente

Public Imported from GitHub
0

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

[rust] Restrict bounded decoding to supported formats

laurensprieme241ce9f1ffa
diff --git a/rust/crates/image/README.md b/rust/crates/image/README.md
index 6ec76ec148..52e94022cd 100644
--- a/rust/crates/image/README.md
+++ b/rust/crates/image/README.md
@@ -12,13 +12,16 @@ let decoded = decode_bounded(ImageInput::Bytes(&encoded_bytes), 6000)?;
The result contains `image`, an RGB8 `DecodedImage`, and `original_dimensions`.
Both sets of dimensions include EXIF orientation. Output preserves aspect ratio
with integer rounding, never upscales, and fits within the requested maximum side.
-Existing decode functions and callers are unchanged.
+This API is opt-in and is not wired into ML, the viewer, or other production
+callers. Existing decode entry points remain in use.
PNG is detected from its signature and decoded row by row through the `png`
crate. Pixels are converted using the existing ICC-to-sRGB handling and reduced
with an area average before applying orientation. Palette, transparency,
grayscale, 16-bit samples, and Adam7 interlacing are supported. Alpha is discarded,
matching the existing RGB decoder. APNG returns its default image.
+Trailing chunks are validated too, so damaged files tolerated by the existing
+decoder may be rejected here.
Non-interlaced PNG needs the reduced RGB output and a few source/target rows.
Adam7 additionally needs a floating-point accumulator at the reduced dimensions
@@ -31,9 +34,10 @@ its side length is permitted. These are allocation guards, not a process-wide
RSS limit; caller-owned encoded bytes and color-management allocations also use
memory. Decode time still depends on the source image size.
-Other formats use the existing decoder followed by a resize. Their output is
-bounded, but their full-resolution decode memory is not bounded by `max_side`.
-PNG failures are returned directly without retrying a full-image decode.
+Only PNG is currently supported. Other formats return an unsupported-format
+decode error before pixel decoding. JPEG and HEIC can be added explicitly in
+later changes. There is no fallback to the existing full-image decoder, including
+when PNG decoding fails.
Run the optional large-input regression with:
diff --git a/rust/crates/image/src/bounded.rs b/rust/crates/image/src/bounded.rs
index 4e2f216a3c..02492305cb 100644
--- a/rust/crates/image/src/bounded.rs
+++ b/rust/crates/image/src/bounded.rs
@@ -3,12 +3,7 @@ use std::{
io::{BufRead, BufReader, Cursor},
};
-use image::{DynamicImage, imageops::FilterType};
-
-use crate::{
- DecodedImage, Dimensions, ImageError, ImageResult,
- decode::{decode_image_from_bytes, decode_image_from_path},
-};
+use crate::{DecodedImage, Dimensions, ImageError, ImageResult};
pub enum ImageInput<'a> {
Path(&'a str),
@@ -27,7 +22,7 @@ pub fn decode_bounded(input: ImageInput<'_>, max_side: u32) -> ImageResult<Bound
"maximum side must be greater than zero".into(),
));
}
- let decoded = match input {
+ match input {
ImageInput::Path(path) => {
let file = File::open(path).map_err(|error| {
ImageError::Decode(format!("failed to open image file '{path}': {error}"))
@@ -39,39 +34,16 @@ pub fn decode_bounded(input: ImageInput<'_>, max_side: u32) -> ImageResult<Bound
if header.starts_with(crate::png::SIGNATURE) {
return crate::png::decode(reader, max_side);
}
- decode_image_from_path(path)?
}
ImageInput::Bytes(bytes) => {
if bytes.starts_with(crate::png::SIGNATURE) {
return crate::png::decode(Cursor::new(bytes), max_side);
}
- decode_image_from_bytes(bytes)?
}
- };
- let original_dimensions = decoded.dimensions.clone();
- let dimensions = target_dimensions(&original_dimensions, max_side);
- if dimensions == original_dimensions {
- return Ok(BoundedDecodedImage {
- image: decoded,
- original_dimensions,
- });
}
- let source = image::RgbImage::from_raw(
- original_dimensions.width,
- original_dimensions.height,
- decoded.rgb,
- )
- .ok_or_else(|| ImageError::Postprocess("invalid decoded RGB buffer".into()))?;
- let resized = DynamicImage::ImageRgb8(source)
- .resize_exact(dimensions.width, dimensions.height, FilterType::Triangle)
- .into_rgb8();
- Ok(BoundedDecodedImage {
- image: DecodedImage {
- dimensions,
- rgb: resized.into_raw(),
- },
- original_dimensions,
- })
+ Err(ImageError::Decode(
+ "unsupported format for bounded decoding: only PNG is currently supported".into(),
+ ))
}
pub(crate) fn target_dimensions(source: &Dimensions, max_side: u32) -> Dimensions {
@@ -85,3 +57,49 @@ pub(crate) fn target_dimensions(source: &Dimensions, max_side: u32) -> Dimension
height: scale(source.height),
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::{
+ decode::decode_image_from_bytes,
+ image_compression::{EncodedImageFormat, encode_rgb},
+ };
+
+ #[test]
+ fn rejects_non_png_from_bytes_and_paths_without_affecting_existing_decode() {
+ let jpeg = encode_rgb(
+ &[100; 12 * 8 * 3],
+ 12,
+ 8,
+ EncodedImageFormat::Jpeg { quality: 90 },
+ )
+ .unwrap();
+ assert_eq!(
+ decode_image_from_bytes(&jpeg).unwrap().dimensions,
+ Dimensions {
+ width: 12,
+ height: 8
+ }
+ );
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("image.png");
+ for bytes in [
+ jpeg.as_slice(),
+ b"\0\0\0\x18ftypheic\0\0\0\0",
+ b"not an image",
+ b"",
+ ] {
+ std::fs::write(&path, bytes).unwrap();
+ for input in [
+ ImageInput::Bytes(bytes),
+ ImageInput::Path(path.to_str().unwrap()),
+ ] {
+ assert!(matches!(
+ decode_bounded(input, 6000),
+ Err(ImageError::Decode(message)) if message.contains("unsupported format")
+ ));
+ }
+ }
+ }
+}
diff --git a/rust/crates/image/src/color_management.rs b/rust/crates/image/src/color_management.rs
index 217fdb2eb0..2015ff7246 100644
--- a/rust/crates/image/src/color_management.rs
+++ b/rust/crates/image/src/color_management.rs
@@ -55,6 +55,7 @@ impl<'a> IccConverter<'a> {
Ok(image) => image,
Err((image, err)) => {
eprintln!("[ml][decode] failed to convert embedded ICC profile to sRGB: {err}");
+ self.source_profile = None;
image
}
}
diff --git a/rust/crates/image/src/png.rs b/rust/crates/image/src/png.rs
index 0632b714a3..b0cca7a44a 100644
--- a/rust/crates/image/src/png.rs
+++ b/rust/crates/image/src/png.rs
@@ -593,6 +593,11 @@ mod tests {
None,
Some(b"invalid ICC profile".to_vec()),
Some(moxcms::ColorProfile::new_srgb().encode().unwrap()),
+ Some(
+ moxcms::ColorProfile::new_gray_with_gamma(1.8)
+ .encode()
+ .unwrap(),
+ ),
Some(pq.encode().unwrap()),
Some(hlg.encode().unwrap()),
] {
@@ -716,32 +721,6 @@ mod tests {
));
}
- #[test]
- fn jpeg_fallback_caps_output_and_keeps_original_dimensions() {
- let bytes = crate::image_compression::encode_rgb(
- &[100; 12 * 8 * 3],
- 12,
- 8,
- crate::image_compression::EncodedImageFormat::Jpeg { quality: 90 },
- )
- .unwrap();
- let output = decode_bounded(ImageInput::Bytes(&bytes), 6).unwrap();
- assert_eq!(
- output.original_dimensions,
- Dimensions {
- width: 12,
- height: 8
- }
- );
- assert_eq!(
- output.image.dimensions,
- Dimensions {
- width: 6,
- height: 4
- }
- );
- }
-
#[test]
#[ignore = "ICC streaming performance regression; run in release mode"]
fn profiled_png_decode_avoids_per_row_setup_cost() {