Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
uploads: Do mime-type set comparisons with bare mime-types.
Alex Vandiver1a28aa4d1a3b
diff --git a/zerver/actions/realm_emoji.py b/zerver/actions/realm_emoji.pyindex 33f7aa08768..026020a2365 100644--- a/zerver/actions/realm_emoji.py+++ b/zerver/actions/realm_emoji.py@@ -8,7 +8,7 @@ from django.utils.translation import gettext as _from zerver.lib.emoji import get_emoji_file_namefrom zerver.lib.exceptions import JsonableError-from zerver.lib.mime_types import INLINE_MIME_TYPES+from zerver.lib.mime_types import INLINE_MIME_TYPES, bare_content_typefrom zerver.lib.thumbnail import THUMBNAIL_ACCEPT_IMAGE_TYPES, BadImageErrorfrom zerver.lib.upload import upload_emoji_imagefrom zerver.models import Realm, RealmAuditLog, RealmEmoji, UserProfile@@ -27,6 +27,7 @@ def notify_realm_emoji(realm: Realm, realm_emoji: dict[str, EmojiInfo]) -> None:def check_add_realm_emoji(realm: Realm, name: str, author: UserProfile, image_file: IO[bytes], content_type: str) -> RealmEmoji:+ content_type = bare_content_type(content_type)try:realm_emoji = RealmEmoji(realm=realm, name=name, author=author)realm_emoji.full_clean()diff --git a/zerver/data_import/import_util.py b/zerver/data_import/import_util.pyindex 7b0f74dd9be..622dfeb516b 100644--- a/zerver/data_import/import_util.py+++ b/zerver/data_import/import_util.py@@ -17,7 +17,7 @@ from django.utils.timezone import now as timezone_nowfrom zerver.data_import.sequencer import NEXT_IDfrom zerver.lib.avatar_hash import user_avatar_base_path_from_idsfrom zerver.lib.message import normalize_body_for_import-from zerver.lib.mime_types import INLINE_MIME_TYPES, guess_extension+from zerver.lib.mime_types import INLINE_MIME_TYPES, bare_content_type, guess_extensionfrom zerver.lib.parallel import run_parallelfrom zerver.lib.partial import partialfrom zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS as STREAM_COLORS@@ -744,14 +744,15 @@ def process_emojis(# Directly download the emoji and patch the file_name with the correct extension# based on the content-type returned by the server. This is needed because Slack# sometimes returns an emoji url with .png extension despite the file being a gif.- content_type = get_emojis(emoji_dir, emoji_url, emoji_path)- if content_type is None:+ content_type_raw = get_emojis(emoji_dir, emoji_url, emoji_path)+ if content_type_raw is None:logging.warning("Emoji %s has an unspecified content type. Using the original file extension.",emoji["name"],)continue+ content_type = bare_content_type(content_type_raw)if (content_type not in THUMBNAIL_ACCEPT_IMAGE_TYPESor content_type not in INLINE_MIME_TYPESdiff --git a/zerver/lib/thumbnail.py b/zerver/lib/thumbnail.pyindex bfd54031e3d..e4e69d3032b 100644--- a/zerver/lib/thumbnail.py+++ b/zerver/lib/thumbnail.py@@ -14,7 +14,7 @@ from django.utils.translation import gettext as _from typing_extensions import Self, overridefrom zerver.lib.exceptions import ErrorCode, JsonableError-from zerver.lib.mime_types import AUDIO_INLINE_MIME_TYPES, INLINE_MIME_TYPES+from zerver.lib.mime_types import AUDIO_INLINE_MIME_TYPES, INLINE_MIME_TYPES, bare_content_typefrom zerver.lib.queue import queue_event_on_commitfrom zerver.models import Attachment, ImageAttachment@@ -298,7 +298,8 @@ def missing_thumbnails(seen_thumbnails.add(StoredThumbnailFormat(**existing_thumbnail))potential_output_formats = list(THUMBNAIL_OUTPUT_FORMATS)- if image_attachment.content_type not in INLINE_MIME_TYPES:+ assert image_attachment.content_type+ if bare_content_type(image_attachment.content_type) not in INLINE_MIME_TYPES:if image_attachment.original_width_px >= image_attachment.original_height_px:additional_format = ThumbnailFormat(TRANSCODED_IMAGE_FORMAT.extension,@@ -344,7 +345,7 @@ def maybe_thumbnail(realm_id: int,skip_events: bool = False,) -> ImageAttachment | None:- if content_type not in THUMBNAIL_ACCEPT_IMAGE_TYPES:+ if content_type is None or bare_content_type(content_type) not in THUMBNAIL_ACCEPT_IMAGE_TYPES:# If it doesn't self-report as an image file that we might want# to thumbnail, don't parse the bytes at all.return None@@ -477,7 +478,8 @@ def manifest_and_get_user_upload_previews(audio_path_ids = {attachment.path_idfor attachment in non_image_attachments- if attachment.content_type in AUDIO_INLINE_MIME_TYPES+ if attachment.content_type+ and bare_content_type(attachment.content_type) in AUDIO_INLINE_MIME_TYPES}return AttachmentData(@@ -516,7 +518,10 @@ def get_transcoded_format(# not in INLINE_MIME_TYPES get an extra large-resolution thumbnail# added to their list of formats, this is thus either None or a# high-resolution thumbnail.- if image_attachment.content_type is None or image_attachment.content_type in INLINE_MIME_TYPES:+ if (+ image_attachment.content_type is None+ or bare_content_type(image_attachment.content_type) in INLINE_MIME_TYPES+ ):return Nonethumbs_by_size = sorted(diff --git a/zerver/lib/upload/__init__.py b/zerver/lib/upload/__init__.pyindex 7a3ada2ebd5..536dc3316fc 100644--- a/zerver/lib/upload/__init__.py+++ b/zerver/lib/upload/__init__.py@@ -18,7 +18,7 @@ from django.utils.translation import gettext as _from zerver.lib.avatar_hash import user_avatar_base_path_from_ids, user_avatar_pathfrom zerver.lib.exceptions import ErrorCode, JsonableError-from zerver.lib.mime_types import INLINE_MIME_TYPES, guess_type+from zerver.lib.mime_types import INLINE_MIME_TYPES, bare_content_type, guess_typefrom zerver.lib.outgoing_http import OutgoingSessionfrom zerver.lib.thumbnail import (MAX_EMOJI_GIF_FILE_SIZE_BYTES,@@ -427,6 +427,7 @@ def upload_emoji_image(# a format which is widespread enough that we're willing to inline# it. The latter contains non-image formats, but the former# limits to only images.+ content_type = bare_content_type(content_type)if content_type not in THUMBNAIL_ACCEPT_IMAGE_TYPES or content_type not in INLINE_MIME_TYPES:raise BadImageError(_("Invalid image format"))diff --git a/zerver/lib/upload/s3.py b/zerver/lib/upload/s3.pyindex c4d8f81fc97..b6b6bd61635 100644--- a/zerver/lib/upload/s3.py+++ b/zerver/lib/upload/s3.py@@ -14,7 +14,7 @@ from django.conf import settingsfrom django.utils.http import content_disposition_headerfrom typing_extensions import override-from zerver.lib.mime_types import INLINE_MIME_TYPES+from zerver.lib.mime_types import INLINE_MIME_TYPES, bare_content_typefrom zerver.lib.partial import partialfrom zerver.lib.thumbnail import resize_logo, resize_realm_iconfrom zerver.lib.upload.base import StreamingSourceWithSize, ZulipUploadBackend@@ -110,7 +110,7 @@ def upload_content_to_s3(extras = {}if content_type is None: # nocoveragecontent_type = ""- is_attachment = content_type not in INLINE_MIME_TYPES+ is_attachment = bare_content_type(content_type) not in INLINE_MIME_TYPESif filename is not None:extras["ContentDisposition"] = content_disposition_header(is_attachment, filename)elif is_attachment:diff --git a/zerver/views/tusd.py b/zerver/views/tusd.pyindex 0e902a1cf9b..309a7145050 100644--- a/zerver/views/tusd.py+++ b/zerver/views/tusd.py@@ -14,7 +14,7 @@ from pydantic.alias_generators import to_pascalfrom confirmation.models import Confirmation, ConfirmationKeyError, get_object_from_keyfrom zerver.decorator import get_basic_credentials, validate_api_keyfrom zerver.lib.exceptions import AccessDeniedError, JsonableError-from zerver.lib.mime_types import INLINE_MIME_TYPES, guess_type+from zerver.lib.mime_types import INLINE_MIME_TYPES, bare_content_type, guess_typefrom zerver.lib.rate_limiter import is_local_addrfrom zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpointfrom zerver.lib.upload import (@@ -165,7 +165,7 @@ def handle_upload_pre_finish_hook("realm_id": str(user_profile.realm_id),}- is_attachment = content_type not in INLINE_MIME_TYPES+ is_attachment = bare_content_type(content_type) not in INLINE_MIME_TYPEScontent_disposition = content_disposition_header(is_attachment, filename) or "inline"from zerver.lib.upload.s3 import S3UploadBackend