From 57fabe8f94a636823ca90d696dd5ed0a02d6ddc7 Mon Sep 17 00:00:00 2001 From: vasileios Date: Tue, 17 Dec 2024 16:35:41 +0100 Subject: [PATCH] [#4886] Added check for csv uploads to the MimeTypeValidator There is a problem defining that a file is text/plain or text/csv. We decided to make some extra checks in order to be closer to that conclusion because a lot of csv files are not created properly (according to the proper structure, delimiters etc.) and are treated as text/plain from magic library. --- src/openforms/formio/api/validators.py | 10 ++++++++++ src/openforms/formio/tests/files/test-csv-file.csv | 1 + src/openforms/formio/tests/test_validators.py | 14 ++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 src/openforms/formio/tests/files/test-csv-file.csv 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