From 50f60249baf201da2fa45773424e2e9c0221a3e4 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Tue, 3 Jun 2025 05:21:11 +0200 Subject: [PATCH] Merge commit from fork --- .../ContentSettingsExtensions.cs | 4 +- .../ContentSettingsExtensionsTests.cs | 76 +++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 tests/Umbraco.Tests.UnitTests/Umbraco.Core/Configuration/ContentSettingsExtensionsTests.cs diff --git a/src/Umbraco.Core/Configuration/ContentSettingsExtensions.cs b/src/Umbraco.Core/Configuration/ContentSettingsExtensions.cs index 9c1f5faef9..94e2968056 100644 --- a/src/Umbraco.Core/Configuration/ContentSettingsExtensions.cs +++ b/src/Umbraco.Core/Configuration/ContentSettingsExtensions.cs @@ -14,8 +14,8 @@ public static class ContentSettingsExtensions /// true if the file extension is allowed for upload; otherwise, false. /// public static bool IsFileAllowedForUpload(this ContentSettings contentSettings, string extension) - => contentSettings.AllowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension)) || - (contentSettings.AllowedUploadedFileExtensions.Any() == false && contentSettings.DisallowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension)) == false); + => contentSettings.AllowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension.Trim())) || + (contentSettings.AllowedUploadedFileExtensions.Any() == false && contentSettings.DisallowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension.Trim())) == false); /// /// Gets the auto-fill configuration for a specified property alias. diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Configuration/ContentSettingsExtensionsTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Configuration/ContentSettingsExtensionsTests.cs new file mode 100644 index 0000000000..3558a0bd5b --- /dev/null +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Configuration/ContentSettingsExtensionsTests.cs @@ -0,0 +1,76 @@ +// Copyright (c) Umbraco. +// See LICENSE for more details. + +using NUnit.Framework; +using Umbraco.Cms.Core.Configuration.Models; +using Umbraco.Extensions; + +namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Configuration; + +[TestFixture] +public class ContentSettingsExtensionsTests +{ + [TestCase("jpg")] + [TestCase("JPG")] + [TestCase("jpg ")] + public void IsFileAllowedForUpload_Allows_File_In_Allow_List(string extension) + { + var contentSettings = new ContentSettings + { + AllowedUploadedFileExtensions = new[] { "jpg", "png" }.ToHashSet(), + }; + + Assert.IsTrue(contentSettings.IsFileAllowedForUpload(extension)); + } + + [TestCase("gif")] + [TestCase("GIF")] + [TestCase("gif ")] + public void IsFileAllowedForUpload_Rejects_File_Not_In_Allow_List(string extension) + { + var contentSettings = new ContentSettings + { + AllowedUploadedFileExtensions = new[] { "jpg", "png" }.ToHashSet(), + }; + + Assert.IsFalse(contentSettings.IsFileAllowedForUpload(extension)); + } + + [TestCase("jpg")] + [TestCase("JPG")] + [TestCase("jpg ")] + public void IsFileAllowedForUpload_Allows_File_Not_In_Disallow_List(string extension) + { + var contentSettings = new ContentSettings + { + DisallowedUploadedFileExtensions = new[] { "gif", "png" }.ToHashSet(), + }; + + Assert.IsTrue(contentSettings.IsFileAllowedForUpload(extension)); + } + + [TestCase("gif")] + [TestCase("GIF")] + [TestCase("gif ")] + public void IsFileAllowedForUpload_Rejects_File_In_Disallow_List(string extension) + { + var contentSettings = new ContentSettings + { + DisallowedUploadedFileExtensions = new[] { "gif", "png" }.ToHashSet(), + }; + + Assert.IsFalse(contentSettings.IsFileAllowedForUpload(extension)); + } + + [Test] + public void IsFileAllowedForUpload_Allows_File_In_Allow_List_Even_If_Also_In_Disallow_List() + { + var contentSettings = new ContentSettings + { + AllowedUploadedFileExtensions = new[] { "jpg", "png" }.ToHashSet(), + DisallowedUploadedFileExtensions = new[] { "jpg", }.ToHashSet(), + }; + + Assert.IsTrue(contentSettings.IsFileAllowedForUpload("jpg")); + } +}