diff --git a/src/openforms/formio/api/validators.py b/src/openforms/formio/api/validators.py index aecbcb06bf..34dcd5aa13 100644 --- a/src/openforms/formio/api/validators.py +++ b/src/openforms/formio/api/validators.py @@ -96,6 +96,16 @@ def __call__(self, value: UploadedFile) -> None: raise serializers.ValidationError( _("The provided file is not a {file_type}.").format(file_type=f".{ext}") ) + # gh #4886 + # We need to accept text/plain as a valid MIME type for CSV files. + # If the file does not strictly follow the conventions of CSV (e.g. non-standard delimiters), + # may not be considered as a valid CSV. + elif ( + value.content_type == "text/csv" + and mime_type == "text/plain" + and ext == "csv" + ): + return elif mime_type == "image/heic" and value.content_type in ( "image/heic", "image/heif", diff --git a/src/openforms/formio/tests/files/test-csv-file.csv b/src/openforms/formio/tests/files/test-csv-file.csv new file mode 100644 index 0000000000..3ae83b7712 --- /dev/null +++ b/src/openforms/formio/tests/files/test-csv-file.csv @@ -0,0 +1 @@ +123 diff --git a/src/openforms/formio/tests/test_validators.py b/src/openforms/formio/tests/test_validators.py index d448a24650..f0d0671d25 100644 --- a/src/openforms/formio/tests/test_validators.py +++ b/src/openforms/formio/tests/test_validators.py @@ -188,6 +188,20 @@ def test_multiple_valid_mimetypes_in_zip_files_are_transformed(self): validator(sample) + def test_allowed_mime_types_for_csv_files(self): + valid_types = ("text/csv", "text/plain") + csv_file = TEST_FILES / "test-csv-file.csv" + validator = validators.MimeTypeValidator() + + for valid_type in valid_types: + sample = SimpleUploadedFile( + "test-csv-file.csv", + csv_file.read_bytes(), + content_type=valid_type, + ) + + validator(sample) + def test_validate_files_multiple_mime_types(self): """Assert that validation of files associated with multiple mime types works