Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V8: Validate uploaded files based on the configuration of the FileUpload property configuration #10987

Merged
merged 3 commits into from Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -92,14 +92,15 @@ public override object FromEditor(ContentPropertyData editorValue, object curren
if (editorFile == null) return null;
return filepath == null ? string.Empty : _mediaFileSystem.GetUrl(filepath);


}

private string ProcessFile(ContentPropertyData editorValue, ContentPropertyFile file, string currentPath, Guid cuid, Guid puid)
{
// process the file
// no file, invalid file, reject change
if (UploadFileTypeValidator.IsValidFileExtension(file.FileName) == false)
if (UploadFileTypeValidator.IsValidFileExtension(file.FileName) is false ||
UploadFileTypeValidator.IsAllowedInDataTypeConfiguration(file.FileName, editorValue.DataTypeConfiguration) is false)
return null;

// get the filepath
Expand Down
33 changes: 29 additions & 4 deletions src/Umbraco.Web/PropertyEditors/UploadFileTypeValidator.cs
Expand Up @@ -36,20 +36,45 @@ public IEnumerable<ValidationResult> Validate(object value, string valueType, ob

foreach (string filename in fileNames)
{
if (IsValidFileExtension(filename) == false)
if (IsValidFileExtension(filename) is false || IsAllowedInDataTypeConfiguration(filename, dataTypeConfiguration) is false)
{
//we only store a single value for this editor so the 'member' or 'field'
// we'll associate this error with will simply be called 'value'
yield return new ValidationResult(Current.Services.TextService.Localize("errors", "dissallowedMediaType"), new[] { "value" });
}
}


}

internal static bool IsValidFileExtension(string fileName)
{
if (fileName.IndexOf('.') <= 0) return false;
var extension = fileName.GetFileExtension().TrimStart(".");
if (TryGetFileExtension(fileName, out var extension) is false) return false;
return Current.Configs.Settings().Content.IsFileAllowedForUpload(extension);
}

internal static bool IsAllowedInDataTypeConfiguration(string filename, object dataTypeConfiguration)
{
if (TryGetFileExtension(filename, out var extension) is false) return false;

if (dataTypeConfiguration is FileUploadConfiguration fileUploadConfiguration)
{
// If FileExtensions is empty no allowed extensions has been specified, so we allow everything.
// If there is any extensions specified, we need to check that the uploaded extension is one of them.
return fileUploadConfiguration.FileExtensions.IsCollectionEmpty() ||
nikolajlauridsen marked this conversation as resolved.
Show resolved Hide resolved
fileUploadConfiguration.FileExtensions.Any(x => x.Value.InvariantEquals(extension));
}

return false;
}

internal static bool TryGetFileExtension(string fileName, out string extension)
{
extension = null;
if (fileName.IndexOf('.') <= 0) return false;

extension = fileName.GetFileExtension().TrimStart(".");
return true;
}
}
}