From a83bba62a429bfcf7c72eef051c2085461a2dd2b Mon Sep 17 00:00:00 2001 From: robinvandermolen Date: Thu, 19 Dec 2024 17:07:36 +0100 Subject: [PATCH] :bug: [#4795] Validate .msg file type only on backend The sdk cannot determine which content type belongs to a .msg file. This is because (at least) Linux and MacOS don't know this file type. To make sure these files can be uploaded, the type property on the FileSerializer is now optional. For .smg files a new rule has been added to the MimeTypeValidator --- src/openforms/formio/api/validators.py | 14 +++++++++++++- src/openforms/formio/components/vanilla.py | 9 +-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/openforms/formio/api/validators.py b/src/openforms/formio/api/validators.py index 34dcd5aa13..b1821a1463 100644 --- a/src/openforms/formio/api/validators.py +++ b/src/openforms/formio/api/validators.py @@ -56,7 +56,8 @@ def __init__(self, allowed_mime_types: Iterable[str] | None = None): def __call__(self, value: UploadedFile) -> None: head = value.read(2048) - ext = value.name.split(".")[-1] + file_name_parts = value.name.split(".") + ext = file_name_parts[-1] mime_type = magic.from_buffer(head, mime=True) # gh #2520 @@ -76,6 +77,12 @@ def __call__(self, value: UploadedFile) -> None: _("The provided file is not a valid file type.") ) + if len(file_name_parts) == 1: + raise serializers.ValidationError( + _("Could not determine the file type. Please make sure the file name " + "has an extension.") + ) + # Contents is allowed. Do extension or submitted content_type agree? if value.content_type == "application/octet-stream": m = magic.Magic(extension=True) @@ -111,6 +118,11 @@ def __call__(self, value: UploadedFile) -> None: "image/heif", ): return + # 4795 + # The sdk cannot determine the file type of .msg files, which result into + # content_type "". So we have to validate these for ourselves + elif mime_type == "application/vnd.ms-outlook" and ext == "msg": + return # gh #4658 # Windows use application/x-zip-compressed as a mimetype for .zip files, which diff --git a/src/openforms/formio/components/vanilla.py b/src/openforms/formio/components/vanilla.py index 109cb0560d..d5e6a7937a 100644 --- a/src/openforms/formio/components/vanilla.py +++ b/src/openforms/formio/components/vanilla.py @@ -338,14 +338,7 @@ class FileSerializer(serializers.Serializer): originalName = serializers.CharField(trim_whitespace=False) size = serializers.IntegerField(min_value=0) storage = serializers.ChoiceField(choices=["url"]) - type = serializers.CharField( - error_messages={ - "blank": _( - "Could not determine the file type. Please make sure the file name " - "has an extension." - ), - } - ) + type = serializers.CharField(required=False, allow_blank=True) url = serializers.URLField() data = FileDataSerializer() # type: ignore