From a804602b6d99270e80bbc1f86e5774e830d85e8d Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 30 May 2025 10:40:15 +1000 Subject: [PATCH] Do not encode WEBP images exceeding max dimensions --- src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs | 2 +- src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs | 3 --- src/ImageSharp/Formats/Webp/WebpEncoderCore.cs | 5 +++++ src/ImageSharp/Formats/Webp/WebpThrowHelper.cs | 3 +++ 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs index 34028c2f8..c9f4258fe 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs @@ -58,7 +58,7 @@ internal sealed unsafe partial class JpegEncoderCore Guard.NotNull(image, nameof(image)); Guard.NotNull(stream, nameof(stream)); - if (image.Width >= JpegConstants.MaxLength || image.Height >= JpegConstants.MaxLength) + if (image.Width > JpegConstants.MaxLength || image.Height > JpegConstants.MaxLength) { JpegThrowHelper.ThrowDimensionsTooLarge(image.Width, image.Height); } diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs index f08844839..1864e539c 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs @@ -371,9 +371,6 @@ internal class Vp8LEncoder : IDisposable /// The input image height. private void WriteImageSize(int inputImgWidth, int inputImgHeight) { - Guard.MustBeLessThan(inputImgWidth, WebpConstants.MaxDimension, nameof(inputImgWidth)); - Guard.MustBeLessThan(inputImgHeight, WebpConstants.MaxDimension, nameof(inputImgHeight)); - uint width = (uint)inputImgWidth - 1; uint height = (uint)inputImgHeight - 1; diff --git a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs index b3270786d..788a90a82 100644 --- a/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpEncoderCore.cs @@ -132,6 +132,11 @@ internal sealed class WebpEncoderCore Guard.NotNull(image, nameof(image)); Guard.NotNull(stream, nameof(stream)); + if (image.Width > WebpConstants.MaxDimension || image.Height > WebpConstants.MaxDimension) + { + WebpThrowHelper.ThrowDimensionsTooLarge(image.Width, image.Height); + } + bool lossless; if (this.fileFormat is not null) { diff --git a/src/ImageSharp/Formats/Webp/WebpThrowHelper.cs b/src/ImageSharp/Formats/Webp/WebpThrowHelper.cs index c633c5273..d73095382 100644 --- a/src/ImageSharp/Formats/Webp/WebpThrowHelper.cs +++ b/src/ImageSharp/Formats/Webp/WebpThrowHelper.cs @@ -18,4 +18,7 @@ internal static class WebpThrowHelper [DoesNotReturn] public static void ThrowInvalidImageDimensions(string errorMessage) => throw new InvalidImageContentException(errorMessage); + + [DoesNotReturn] + public static void ThrowDimensionsTooLarge(int width, int height) => throw new ImageFormatException($"Image is too large to encode at {width}x{height} for WEBP format."); }