Skip to content

Commit

Permalink
[#4886] Added check for csv uploads to the MimeTypeValidator
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vaszig committed Dec 17, 2024
1 parent 486d9cb commit 57fabe8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/openforms/formio/api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/openforms/formio/tests/files/test-csv-file.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123
14 changes: 14 additions & 0 deletions src/openforms/formio/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 57fabe8

Please sign in to comment.