Skip to content

Commit d920e93

Browse files
authored
Merge commit from fork
1 parent 853c1ac commit d920e93

File tree

2 files changed

+86
-7
lines changed

2 files changed

+86
-7
lines changed

src/Umbraco.Core/Configuration/ContentSettingsExtensions.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ namespace Umbraco.Extensions;
55
public static class ContentSettingsExtensions
66
{
77
/// <summary>
8-
/// Determines if file extension is allowed for upload based on (optional) white list and black list
9-
/// held in settings.
10-
/// Allow upload if extension is whitelisted OR if there is no whitelist and extension is NOT blacklisted.
8+
/// Determines if file extension is allowed for upload based on (optional) allow list and deny list held in settings.
9+
/// Disallowed file extensions are only considered if there are no allowed file extensions.
1110
/// </summary>
12-
public static bool IsFileAllowedForUpload(this ContentSettings contentSettings, string extension) =>
13-
contentSettings.AllowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension)) ||
14-
(contentSettings.AllowedUploadedFileExtensions.Any() == false &&
15-
contentSettings.DisallowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension)) == false);
11+
/// <param name="contentSettings">The content settings.</param>
12+
/// <param name="extension">The file extension.</param>
13+
/// <returns>
14+
/// <c>true</c> if the file extension is allowed for upload; otherwise, <c>false</c>.
15+
/// </returns>
16+
public static bool IsFileAllowedForUpload(this ContentSettings contentSettings, string extension)
17+
=> contentSettings.AllowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension.Trim())) ||
18+
(contentSettings.AllowedUploadedFileExtensions.Any() == false && contentSettings.DisallowedUploadedFileExtensions.Any(x => x.InvariantEquals(extension.Trim())) == false);
1619

1720
/// <summary>
1821
/// Gets the auto-fill configuration for a specified property alias.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Umbraco.
2+
// See LICENSE for more details.
3+
4+
using NUnit.Framework;
5+
using Umbraco.Cms.Core.Configuration.Models;
6+
using Umbraco.Extensions;
7+
8+
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Configuration;
9+
10+
[TestFixture]
11+
public class ContentSettingsExtensionsTests
12+
{
13+
[TestCase("jpg")]
14+
[TestCase("JPG")]
15+
[TestCase("jpg ")]
16+
public void IsFileAllowedForUpload_Allows_File_In_Allow_List(string extension)
17+
{
18+
var contentSettings = new ContentSettings
19+
{
20+
AllowedUploadedFileExtensions = ["jpg", "png"],
21+
};
22+
23+
Assert.IsTrue(contentSettings.IsFileAllowedForUpload(extension));
24+
}
25+
26+
[TestCase("gif")]
27+
[TestCase("GIF")]
28+
[TestCase("gif ")]
29+
public void IsFileAllowedForUpload_Rejects_File_Not_In_Allow_List(string extension)
30+
{
31+
var contentSettings = new ContentSettings
32+
{
33+
AllowedUploadedFileExtensions = ["jpg", "png"],
34+
};
35+
36+
Assert.IsFalse(contentSettings.IsFileAllowedForUpload(extension));
37+
}
38+
39+
[TestCase("jpg")]
40+
[TestCase("JPG")]
41+
[TestCase("jpg ")]
42+
public void IsFileAllowedForUpload_Allows_File_Not_In_Disallow_List(string extension)
43+
{
44+
var contentSettings = new ContentSettings
45+
{
46+
DisallowedUploadedFileExtensions = ["gif", "png"],
47+
};
48+
49+
Assert.IsTrue(contentSettings.IsFileAllowedForUpload(extension));
50+
}
51+
52+
[TestCase("gif")]
53+
[TestCase("GIF")]
54+
[TestCase("gif ")]
55+
public void IsFileAllowedForUpload_Rejects_File_In_Disallow_List(string extension)
56+
{
57+
var contentSettings = new ContentSettings
58+
{
59+
DisallowedUploadedFileExtensions = ["gif", "png"],
60+
};
61+
62+
Assert.IsFalse(contentSettings.IsFileAllowedForUpload(extension));
63+
}
64+
65+
[Test]
66+
public void IsFileAllowedForUpload_Allows_File_In_Allow_List_Even_If_Also_In_Disallow_List()
67+
{
68+
var contentSettings = new ContentSettings
69+
{
70+
AllowedUploadedFileExtensions = ["jpg", "png"],
71+
DisallowedUploadedFileExtensions = ["jpg"],
72+
};
73+
74+
Assert.IsTrue(contentSettings.IsFileAllowedForUpload("jpg"));
75+
}
76+
}

0 commit comments

Comments
 (0)