Skip to content

Do not encode WEBP images exceeding max dimensions #2930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025

Conversation

JimBobSquarePants
Copy link
Member

@JimBobSquarePants JimBobSquarePants commented May 30, 2025

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

Fixes #2920

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds a centralized dimension check for WEBP encoding and aligns boundary behavior with JPEG.

  • Introduces ThrowDimensionsTooLarge in WebpThrowHelper for uniform errors.
  • Adds a guard in WebpEncoderCore to block overly large images.
  • Removes duplicate checks in Vp8LEncoder and updates JPEG’s boundary condition.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
WebpThrowHelper.cs New ThrowDimensionsTooLarge method for WEBP dimension errors
WebpEncoderCore.cs Guard against Width/Height > MaxDimension before encoding
Lossless/Vp8LEncoder.cs Removed redundant Guard.MustBeLessThan calls in WriteImageSize
JpegEncoderCore.cs Switched from >= to > when comparing dimensions to max value
Comments suppressed due to low confidence (4)

src/ImageSharp/Formats/Webp/WebpEncoderCore.cs:135

  • Add unit tests to verify that images exceeding WebpConstants.MaxDimension correctly trigger ImageFormatException via ThrowDimensionsTooLarge.
if (image.Width > WebpConstants.MaxDimension || image.Height > WebpConstants.MaxDimension)

src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs:373

  • [nitpick] Consider invoking WebpThrowHelper.ThrowDimensionsTooLarge here if inputs exceed MaxDimension, to keep error handling consistent and avoid relying solely on upstream guards.
private void WriteImageSize(int inputImgWidth, int inputImgHeight)

src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs:61

  • Update or add tests covering the changed boundary condition (> instead of >=) to ensure the new logic matches the intended maximum dimension behavior.
if (image.Width > JpegConstants.MaxLength || image.Height > JpegConstants.MaxLength)

src/ImageSharp/Formats/Webp/WebpThrowHelper.cs:22

  • Align the indentation of the new ThrowDimensionsTooLarge method with existing members (six spaces) to match the class’s formatting conventions.
[DoesNotReturn]

@JimBobSquarePants JimBobSquarePants merged commit 1226eb5 into main May 30, 2025
15 checks passed
@JimBobSquarePants JimBobSquarePants deleted the js/issue-2920 branch May 30, 2025 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[WEBP] Creating lossy webp from this png produces corrupted webp file
1 participant