Skip to content

Commit 2d29b9b

Browse files
committed
[#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. Backport-of: #4940
1 parent 613514c commit 2d29b9b

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/openforms/formio/api/validators.py

+10
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ def __call__(self, value: UploadedFile) -> None:
9696
raise serializers.ValidationError(
9797
_("The provided file is not a {file_type}.").format(file_type=f".{ext}")
9898
)
99+
# gh #4886
100+
# We need to accept text/plain as a valid MIME type for CSV files.
101+
# If the file does not strictly follow the conventions of CSV (e.g. non-standard delimiters),
102+
# may not be considered as a valid CSV.
103+
elif (
104+
value.content_type == "text/csv"
105+
and mime_type == "text/plain"
106+
and ext == "csv"
107+
):
108+
return
99109
elif mime_type == "image/heic" and value.content_type in (
100110
"image/heic",
101111
"image/heif",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
123

src/openforms/formio/tests/test_validators.py

+14
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@ def test_multiple_valid_mimetypes_in_zip_files_are_transformed(self):
188188

189189
validator(sample)
190190

191+
def test_allowed_mime_types_for_csv_files(self):
192+
valid_types = ("text/csv", "text/plain")
193+
csv_file = TEST_FILES / "test-csv-file.csv"
194+
validator = validators.MimeTypeValidator()
195+
196+
for valid_type in valid_types:
197+
sample = SimpleUploadedFile(
198+
"test-csv-file.csv",
199+
csv_file.read_bytes(),
200+
content_type=valid_type,
201+
)
202+
203+
validator(sample)
204+
191205
def test_validate_files_multiple_mime_types(self):
192206
"""Assert that validation of files associated with multiple mime types works
193207

0 commit comments

Comments
 (0)