Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

upload: Add assertions before accessing uploaded files.

Zixuan James Li0ec561ab5726
diff --git a/zerver/views/realm_emoji.py b/zerver/views/realm_emoji.py
index d8e6df49653..4187adb3744 100644
--- a/zerver/views/realm_emoji.py
+++ b/zerver/views/realm_emoji.py
@@ -1,4 +1,5 @@
from django.conf import settings
+from django.core.files.uploadedfile import UploadedFile
from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _
@@ -40,6 +41,8 @@ def upload_emoji(
if not user_profile.is_realm_admin:
raise JsonableError(_("Only administrators can override built-in emoji."))
emoji_file = list(request.FILES.values())[0]
+ assert isinstance(emoji_file, UploadedFile)
+ assert emoji_file.size is not None
if (settings.MAX_EMOJI_FILE_SIZE_MIB * 1024 * 1024) < emoji_file.size:
raise JsonableError(
_("Uploaded file is larger than the allowed limit of {} MiB").format(
diff --git a/zerver/views/realm_icon.py b/zerver/views/realm_icon.py
index 9cb77772ce3..22d3d7e926a 100644
--- a/zerver/views/realm_icon.py
+++ b/zerver/views/realm_icon.py
@@ -1,4 +1,5 @@
from django.conf import settings
+from django.core.files.uploadedfile import UploadedFile
from django.http import HttpRequest, HttpResponse
from django.shortcuts import redirect
from django.utils.translation import gettext as _
@@ -20,6 +21,8 @@ def upload_icon(request: HttpRequest, user_profile: UserProfile) -> HttpResponse
raise JsonableError(_("You must upload exactly one icon."))
icon_file = list(request.FILES.values())[0]
+ assert isinstance(icon_file, UploadedFile)
+ assert icon_file.size is not None
if (settings.MAX_ICON_FILE_SIZE_MIB * 1024 * 1024) < icon_file.size:
raise JsonableError(
_("Uploaded file is larger than the allowed limit of {} MiB").format(
diff --git a/zerver/views/realm_logo.py b/zerver/views/realm_logo.py
index d17e414af5d..63390620242 100644
--- a/zerver/views/realm_logo.py
+++ b/zerver/views/realm_logo.py
@@ -1,4 +1,5 @@
from django.conf import settings
+from django.core.files.uploadedfile import UploadedFile
from django.http import HttpRequest, HttpResponse
from django.shortcuts import redirect
from django.utils.translation import gettext as _
@@ -25,6 +26,8 @@ def upload_logo(
if len(request.FILES) != 1:
raise JsonableError(_("You must upload exactly one logo."))
logo_file = list(request.FILES.values())[0]
+ assert isinstance(logo_file, UploadedFile)
+ assert logo_file.size is not None
if (settings.MAX_LOGO_FILE_SIZE_MIB * 1024 * 1024) < logo_file.size:
raise JsonableError(
_("Uploaded file is larger than the allowed limit of {} MiB").format(
diff --git a/zerver/views/upload.py b/zerver/views/upload.py
index 5dff4666f59..6caa6439fed 100644
--- a/zerver/views/upload.py
+++ b/zerver/views/upload.py
@@ -3,6 +3,7 @@ from typing import Union
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
+from django.core.files.uploadedfile import UploadedFile
from django.http import HttpRequest, HttpResponse, HttpResponseForbidden, HttpResponseNotFound
from django.shortcuts import redirect
from django.utils.cache import patch_cache_control
@@ -137,7 +138,9 @@ def upload_file_backend(request: HttpRequest, user_profile: UserProfile) -> Http
raise JsonableError(_("You may only upload one file at a time"))
user_file = list(request.FILES.values())[0]
+ assert isinstance(user_file, UploadedFile)
file_size = user_file.size
+ assert file_size is not None
if settings.MAX_FILE_UPLOAD_SIZE * 1024 * 1024 < file_size:
raise JsonableError(
_("Uploaded file is larger than the allowed limit of {} MiB").format(
diff --git a/zerver/views/users.py b/zerver/views/users.py
index 62c2c2871bc..d0ae17fccb3 100644
--- a/zerver/views/users.py
+++ b/zerver/views/users.py
@@ -2,6 +2,7 @@ from typing import Any, Dict, List, Optional, Union
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
+from django.core.files.uploadedfile import UploadedFile
from django.http import HttpRequest, HttpResponse
from django.shortcuts import redirect
from django.utils.translation import gettext as _
@@ -375,6 +376,8 @@ def patch_bot_backend(
pass
elif len(request.FILES) == 1:
user_file = list(request.FILES.values())[0]
+ assert isinstance(user_file, UploadedFile)
+ assert user_file.size is not None
upload_avatar_image(user_file, user_profile, bot)
avatar_source = UserProfile.AVATAR_FROM_USER
do_change_avatar_fields(bot, avatar_source, acting_user=user_profile)
@@ -511,6 +514,8 @@ def add_bot_backend(
)
if len(request.FILES) == 1:
user_file = list(request.FILES.values())[0]
+ assert isinstance(user_file, UploadedFile)
+ assert user_file.size is not None
upload_avatar_image(user_file, user_profile, bot_profile)
if bot_type in (UserProfile.OUTGOING_WEBHOOK_BOT, UserProfile.EMBEDDED_BOT):