From dff3d8a739b770836482d186dcd56b2a159f41ab Mon Sep 17 00:00:00 2001 From: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> Date: Thu, 17 Nov 2022 09:43:57 +0100 Subject: [PATCH] V10: AllowedUploadFiles appsetting not working (#13408) * Add new Settings * Use new settings instead of old ones * Implement AllowedUploadedFiles value to be copied to AllowedUplayedFileExtensions * Obsolete old settings * Rename DisallowedUploadFileExtensions * Implement same fix for DisallowedUploadFiles * Use new settings for backoffice server variables * Update the correct setting Co-authored-by: Zeegaan --- .../ContentSettingsExtensions.cs | 6 ++--- .../Configuration/Models/ContentSettings.cs | 13 ++++++++++ .../UmbracoBuilder.Configuration.cs | 26 +++++++++++++++++++ .../Controllers/BackOfficeServerVariables.cs | 4 +-- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Core/Configuration/ContentSettingsExtensions.cs b/src/Umbraco.Core/Configuration/ContentSettingsExtensions.cs index 315cee4627..23a67f3267 100644 --- a/src/Umbraco.Core/Configuration/ContentSettingsExtensions.cs +++ b/src/Umbraco.Core/Configuration/ContentSettingsExtensions.cs @@ -10,9 +10,9 @@ public static class ContentSettingsExtensions /// Allow upload if extension is whitelisted OR if there is no whitelist and extension is NOT blacklisted. /// public static bool IsFileAllowedForUpload(this ContentSettings contentSettings, string extension) => - contentSettings.AllowedUploadFiles.Any(x => x.InvariantEquals(extension)) || - (contentSettings.AllowedUploadFiles.Any() == false && - contentSettings.DisallowedUploadFiles.Any(x => x.InvariantEquals(extension)) == false); + contentSettings.AllowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension)) || + (contentSettings.AllowedUploadedFileExtensions.Any() == false && + contentSettings.DisallowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension)) == false); /// /// Gets the auto-fill configuration for a specified property alias. diff --git a/src/Umbraco.Core/Configuration/Models/ContentSettings.cs b/src/Umbraco.Core/Configuration/Models/ContentSettings.cs index f4f3040b79..4014930a5c 100644 --- a/src/Umbraco.Core/Configuration/Models/ContentSettings.cs +++ b/src/Umbraco.Core/Configuration/Models/ContentSettings.cs @@ -196,11 +196,13 @@ public class ContentSettings /// Gets or sets a value for the collection of file extensions that are disallowed for upload. /// [DefaultValue(StaticDisallowedUploadFiles)] + [Obsolete("Please use DisAllowedUploadedFileExtensions instead, scheduled for removal in V13")] public IEnumerable DisallowedUploadFiles { get; set; } = StaticDisallowedUploadFiles.Split(','); /// /// Gets or sets a value for the collection of file extensions that are allowed for upload. /// + [Obsolete("Please use AllowedUploadedFileExtensions instead, scheduled for removal in V13")] public IEnumerable AllowedUploadFiles { get; set; } = Array.Empty(); /// @@ -249,4 +251,15 @@ public class ContentSettings /// [DefaultValue(StaticAllowEditInvariantFromNonDefault)] public bool AllowEditInvariantFromNonDefault { get; set; } = StaticAllowEditInvariantFromNonDefault; + + /// + /// Gets or sets a value for the collection of file extensions that are allowed for upload. + /// + public string[] AllowedUploadedFileExtensions { get; set; } = Array.Empty(); + + /// + /// Gets or sets a value for the collection of file extensions that are disallowed for upload. + /// + [DefaultValue(StaticDisallowedUploadFiles)] + public string[] DisallowedUploadedFileExtensions { get; set; } = StaticDisallowedUploadFiles.Split(','); } diff --git a/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Configuration.cs b/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Configuration.cs index 6efd096c68..e65bc3c47b 100644 --- a/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Configuration.cs +++ b/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Configuration.cs @@ -119,6 +119,32 @@ public static partial class UmbracoBuilderExtensions } }); + // TODO: Remove this in V13 + // This is to avoid a breaking change in ContentSettings, if the old AllowedFileUploads has a value, and the new + // AllowedFileUploadExtensions does not, copy the value over, if the new has a value, use that instead. + builder.Services.Configure(settings => + { + // We have to use Config.GetSection().Get, as the GetSection.GetValue simply cannot retrieve a string array + var allowedUploadedFileExtensionsValue = builder.Config.GetSection($"{Constants.Configuration.ConfigContent}:{nameof(ContentSettings.AllowedUploadedFileExtensions)}").Get(); + var allowedUploadFilesValue = builder.Config.GetSection($"{Constants.Configuration.ConfigContent}:{nameof(ContentSettings.AllowedUploadFiles)}").Get(); + + if (allowedUploadedFileExtensionsValue is null && allowedUploadFilesValue is not null) + { + settings.AllowedUploadedFileExtensions = allowedUploadFilesValue; + } + }); + + // TODO: Remove this in V13 + builder.Services.Configure(settings => + { + var disallowedUploadedFileExtensionsValue = builder.Config.GetSection($"{Constants.Configuration.ConfigContent}:{nameof(ContentSettings.DisallowedUploadedFileExtensions)}").Get(); + var disallowedUploadFilesValue = builder.Config.GetSection($"{Constants.Configuration.ConfigContent}:{nameof(ContentSettings.DisallowedUploadFiles)}").Get(); + + if (disallowedUploadedFileExtensionsValue is null && disallowedUploadFilesValue is not null) + { + settings.DisallowedUploadedFileExtensions = disallowedUploadFilesValue; + } + }); return builder; } } diff --git a/src/Umbraco.Web.BackOffice/Controllers/BackOfficeServerVariables.cs b/src/Umbraco.Web.BackOffice/Controllers/BackOfficeServerVariables.cs index 6471b5b2ae..9243eb49a5 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/BackOfficeServerVariables.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/BackOfficeServerVariables.cs @@ -543,11 +543,11 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers }, { "disallowedUploadFiles", - string.Join(",", _contentSettings.DisallowedUploadFiles) + string.Join(",", _contentSettings.DisallowedUploadedFileExtensions) }, { "allowedUploadFiles", - string.Join(",", _contentSettings.AllowedUploadFiles) + string.Join(",", _contentSettings.AllowedUploadedFileExtensions) }, { "maxFileSize",