From 762d72b0184c44e739d8d544d30aa1396a7eb8e3 Mon Sep 17 00:00:00 2001 From: NguyenThuyLan <116753400+NguyenThuyLan@users.noreply.github.com> Date: Wed, 25 Sep 2024 18:19:09 +0700 Subject: [PATCH] update ImageSharpMiddlewareOption for fixing invalid width and height (#17126) Co-authored-by: Lan Nguyen Thuy (cherry picked from commit 9b19d63a6a4ea44bd4b56bbb0874284a1bc5ba52) --- .../ConfigureImageSharpMiddlewareOptions.cs | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Umbraco.Cms.Imaging.ImageSharp/ConfigureImageSharpMiddlewareOptions.cs b/src/Umbraco.Cms.Imaging.ImageSharp/ConfigureImageSharpMiddlewareOptions.cs index 9a1ecead89b2..1ef672270ef8 100644 --- a/src/Umbraco.Cms.Imaging.ImageSharp/ConfigureImageSharpMiddlewareOptions.cs +++ b/src/Umbraco.Cms.Imaging.ImageSharp/ConfigureImageSharpMiddlewareOptions.cs @@ -1,3 +1,4 @@ +using System.Globalization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Headers; using Microsoft.Extensions.Options; @@ -48,16 +49,26 @@ public void Configure(ImageSharpMiddlewareOptions options) return Task.CompletedTask; } - int width = context.Parser.ParseValue(context.Commands.GetValueOrDefault(ResizeWebProcessor.Width), context.Culture); - if (width <= 0 || width > _imagingSettings.Resize.MaxWidth) + if (context.Commands.Contains(ResizeWebProcessor.Width)) { - context.Commands.Remove(ResizeWebProcessor.Width); + if (!int.TryParse(context.Commands.GetValueOrDefault(ResizeWebProcessor.Width), NumberStyles.Integer, + CultureInfo.InvariantCulture, out var width) + || width < 0 + || width >= _imagingSettings.Resize.MaxWidth) + { + context.Commands.Remove(ResizeWebProcessor.Width); + } } - int height = context.Parser.ParseValue(context.Commands.GetValueOrDefault(ResizeWebProcessor.Height), context.Culture); - if (height <= 0 || height > _imagingSettings.Resize.MaxHeight) + if (context.Commands.Contains(ResizeWebProcessor.Height)) { - context.Commands.Remove(ResizeWebProcessor.Height); + if (!int.TryParse(context.Commands.GetValueOrDefault(ResizeWebProcessor.Height), NumberStyles.Integer, + CultureInfo.InvariantCulture, out var height) + || height < 0 + || height >= _imagingSettings.Resize.MaxHeight) + { + context.Commands.Remove(ResizeWebProcessor.Height); + } } return Task.CompletedTask;