diff --git a/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetOptions.cs b/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetOptions.cs index e9435a4f4..50f47846f 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetOptions.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Assets/AssetOptions.cs @@ -25,6 +25,8 @@ public sealed class AssetOptions public int MaxConcurrentResizes { get; set; } + public int MaxResizeDimension { get; set; } = 4096; + public TimeSpan MaxCacheDuration { get; set; } = TimeSpan.FromDays(365); public string? CDN { get; set; } @@ -32,4 +34,15 @@ public sealed class AssetOptions public TimeSpan TimeoutFind { get; set; } = TimeSpan.FromSeconds(1); public TimeSpan TimeoutQuery { get; set; } = TimeSpan.FromSeconds(5); + + public bool IsValidResizeSize(int? width, int? height) + { + // Zero or less disables the limit. + if (MaxResizeDimension <= 0) + { + return true; + } + + return (width ?? 0) <= MaxResizeDimension && (height ?? 0) <= MaxResizeDimension; + } } diff --git a/backend/src/Squidex/Areas/Api/Controllers/Assets/AssetContentController.cs b/backend/src/Squidex/Areas/Api/Controllers/Assets/AssetContentController.cs index 776764de4..c5169c4e2 100644 --- a/backend/src/Squidex/Areas/Api/Controllers/Assets/AssetContentController.cs +++ b/backend/src/Squidex/Areas/Api/Controllers/Assets/AssetContentController.cs @@ -17,6 +17,7 @@ using Squidex.Domain.Apps.Entities; using Squidex.Domain.Apps.Entities.Assets; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; +using Squidex.Infrastructure.Validation; using Squidex.Web; namespace Squidex.Areas.Api.Controllers.Assets; @@ -102,6 +103,12 @@ public sealed class AssetContentController( return StatusCode(403); } + // Large target sizes allocate huge image buffers and are also persisted for every combination. + if (!assetOptions.IsValidResizeSize(request.Width, request.Height)) + { + throw new ValidationException($"Width and height must not be greater than {assetOptions.MaxResizeDimension}."); + } + if (asset != null && request.Version > EtagVersion.Any && asset.Version != request.Version) { if (context.App != null) diff --git a/backend/src/Squidex/appsettings.json b/backend/src/Squidex/appsettings.json index 231feaa9b..4acab5344 100644 --- a/backend/src/Squidex/appsettings.json +++ b/backend/src/Squidex/appsettings.json @@ -405,6 +405,11 @@ // WARNING: Resizing is CPU and memory bound, therefore a high number can exhaust the node. "maxConcurrentResizes": 0, + // The maximum width and height in pixels that a caller can request when resizing images. Set to 0 to disable the limit. + // + // WARNING: The memory to resize an image grows with width * height, therefore a high number can exhaust the node. + "maxResizeDimension": 4096, + // The maximum cache duration that a caller can request with the "cache" parameter. Default: 365 days. "maxCacheDuration": "365.00:00:00", diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetOptionsTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetOptionsTests.cs new file mode 100644 index 000000000..f6e98c738 --- /dev/null +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetOptionsTests.cs @@ -0,0 +1,43 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +namespace Squidex.Domain.Apps.Entities.Assets; + +public class AssetOptionsTests +{ + [Theory] + [InlineData(null, null)] + [InlineData(100, null)] + [InlineData(null, 100)] + [InlineData(4096, 4096)] + public void Should_accept_resize_size_within_limit(int? width, int? height) + { + var sut = new AssetOptions { MaxResizeDimension = 4096 }; + + Assert.True(sut.IsValidResizeSize(width, height)); + } + + [Theory] + [InlineData(4097, null)] + [InlineData(null, 4097)] + [InlineData(100, 50000)] + [InlineData(int.MaxValue, int.MaxValue)] + public void Should_reject_resize_size_above_limit(int? width, int? height) + { + var sut = new AssetOptions { MaxResizeDimension = 4096 }; + + Assert.False(sut.IsValidResizeSize(width, height)); + } + + [Fact] + public void Should_accept_any_resize_size_if_limit_disabled() + { + var sut = new AssetOptions { MaxResizeDimension = 0 }; + + Assert.True(sut.IsValidResizeSize(int.MaxValue, int.MaxValue)); + } +}