Piotr Król
02/21/2025, 2:12 PMTackleMcClean 🏅
02/22/2025, 12:33 PMDean Leigh
02/22/2025, 5:39 PMJason
02/22/2025, 11:34 PMIFileStreamSecurityValidator
that forms will automatically use to scan files on upload.
Have a look at:
https://docs.umbraco.com/umbraco-forms/13.latest/editor/creating-a-form/fieldtypes/fileupload#server-side-file-validation
And: https://docs.umbraco.com/umbraco-cms/10.latest/reference/security/serverside-file-validation
Otherwise, what I do in v8 (not sure if it works in v10, but worth a try) is to create a custom upload field that replaces the built-in one (create a class that inherits from Umbraco.Forms.Core.Providers.FieldTypes.FileUpload
) and then override the ValidateField
method (ensuring you call the base method) and then add any additional file checking logic.Jason
02/22/2025, 11:34 PMcsharp
```csharp
public class FileCheckingUploadField : Umbraco.Forms.Core.Providers.FieldTypes.FileUpload
{
public FileCheckingUploadField(
IOptions<SecuritySettings> config,
IHostEnvironment hostEnvironment,
MediaFileManager mediaFileManager,
IDataProtectionProvider dataProtectionProvider) : base(config, hostEnvironment, mediaFileManager, dataProtectionProvider)
{
}
public override IEnumerable<string> ValidateField(
Form form,
Field field,
IEnumerable<object> postedValues,
HttpContext context,
IPlaceholderParsingService placeholderParsingService,
IFieldTypeStorage fieldTypeStorage)
{
var errors = base.ValidateField(form, field, postedValues, context, placeholderParsingService, fieldTypeStorage).ToList();
foreach(var file in context.Request.Form.Files)
{
// TODO: Make sure you only check the files for this actual field
// TODO: validate the file
// I recommend copying the stream to a new memory stream with file.CopyTo() rather than using file.OpenReadStream()
// TODO: Add an error message to the errors list if the file is invalid
}
return errors;
}
}
Jonathon Cove
02/24/2025, 8:46 AMPiotr Król
02/24/2025, 3:00 PMPiotr Król
02/24/2025, 4:00 PMJason
02/24/2025, 4:29 PMPiotr Król
02/25/2025, 8:48 AM