diff --git a/dojo/api_v2/serializers.py b/dojo/api_v2/serializers.py index c8ab20cc33..74ffe721c3 100644 --- a/dojo/api_v2/serializers.py +++ b/dojo/api_v2/serializers.py @@ -1,5 +1,6 @@ import json import logging +import os import re from datetime import datetime from typing import List @@ -797,6 +798,24 @@ class Meta: model = FileUpload fields = "__all__" + def validate(self, data): + if file := data.get("file"): + ext = os.path.splitext(file.name)[1] # [0] returns path+filename + valid_extensions = settings.FILE_UPLOAD_TYPES + if ext.lower() not in valid_extensions: + if accepted_extensions := f"{', '.join(valid_extensions)}": + msg = ( + "Unsupported extension. Supported extensions are as " + f"follows: {accepted_extensions}" + ) + else: + msg = ( + "File uploads are prohibited due to the list of acceptable " + "file extensions being empty" + ) + raise ValidationError(msg) + return data + class RawFileSerializer(serializers.ModelSerializer): file = serializers.FileField(required=True) diff --git a/dojo/forms.py b/dojo/forms.py index 0bf5429f71..09b8c33949 100644 --- a/dojo/forms.py +++ b/dojo/forms.py @@ -850,13 +850,22 @@ def clean(self): # Don't bother validating the formset unless each form is valid on its own return for form in self.forms: - print(dir(form)) file = form.cleaned_data.get('file', None) if file: ext = os.path.splitext(file.name)[1] # [0] returns path+filename valid_extensions = settings.FILE_UPLOAD_TYPES if ext.lower() not in valid_extensions: - form.add_error('file', 'Unsupported file extension.') + if accepted_extensions := f"{', '.join(valid_extensions)}": + msg = ( + "Unsupported extension. Supported extensions are as " + f"follows: {accepted_extensions}" + ) + else: + msg = ( + "File uploads are prohibited due to the list of acceptable " + "file extensions being empty" + ) + form.add_error('file', msg) ManageFileFormSet = modelformset_factory(FileUpload, extra=3, max_num=10, fields=['title', 'file'], can_delete=True, formset=BaseManageFileFormSet) diff --git a/dojo/templates/dojo/view_eng.html b/dojo/templates/dojo/view_eng.html index 35f27e434d..7189c7326d 100644 --- a/dojo/templates/dojo/view_eng.html +++ b/dojo/templates/dojo/view_eng.html @@ -691,7 +691,7 @@

Files