Browse Source

Limit the requested width and height when resizing assets (#1387)

Co-authored-by: claude[bot] <claude[bot]@users.noreply.github.com>
pull/1389/head
github-actions[bot] 5 days ago
committed by GitHub
parent
commit
2d2362d83a
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 13
      backend/src/Squidex.Domain.Apps.Entities/Assets/AssetOptions.cs
  2. 7
      backend/src/Squidex/Areas/Api/Controllers/Assets/AssetContentController.cs
  3. 5
      backend/src/Squidex/appsettings.json
  4. 43
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetOptionsTests.cs

13
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;
}
}

7
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)

5
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",

43
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));
}
}
Loading…
Cancel
Save