Skip to content

Commit cf29bf2

Browse files
author
chaodu-agent
committed
fix(gateway): add GIF size guard (5MB) in resize_and_compress
Prevents oversized GIFs from being base64-encoded and exceeding downstream LLM API payload limits. Returns an error if the raw GIF exceeds 5MB, consistent with the existing IMAGE_MAX_DOWNLOAD limit for other formats. Addresses review NIT from wangyuyan-agent.
1 parent c4b0f27 commit cf29bf2

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

gateway/src/media.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ pub const IMAGE_JPEG_QUALITY: u8 = 75;
66
pub const IMAGE_MAX_DOWNLOAD: u64 = 10 * 1024 * 1024; // 10 MB
77
pub const FILE_MAX_DOWNLOAD: u64 = 512 * 1024; // 512 KB
88
pub const AUDIO_MAX_DOWNLOAD: u64 = 20 * 1024 * 1024; // 20 MB
9+
pub const GIF_MAX_SIZE: usize = 5 * 1024 * 1024; // 5 MB — prevents base64 bloat exceeding LLM payload limits
910

1011
/// Resize image so longest side <= 1200px, then encode as JPEG.
1112
/// GIFs are passed through unchanged to preserve animation.
1213
pub fn resize_and_compress(raw: &[u8]) -> Result<(Vec<u8>, String), image::ImageError> {
1314
let reader = ImageReader::new(Cursor::new(raw)).with_guessed_format()?;
1415
let format = reader.format();
1516
if format == Some(image::ImageFormat::Gif) {
17+
if raw.len() > GIF_MAX_SIZE {
18+
return Err(image::ImageError::Limits(
19+
image::error::LimitError::from_kind(image::error::LimitErrorKind::InsufficientMemory),
20+
));
21+
}
1622
return Ok((raw.to_vec(), "image/gif".to_string()));
1723
}
1824
let img = reader.decode()?;

0 commit comments

Comments
 (0)