uploads: Revert "Url encoded name of the file should be an ascii."

This reverts commit fd9dd51d16 (#1815).

The issue described does not exist in Python 3, where urllib.parse now
_only_ accepts (Unicode) str and does the right thing with it.  The
workaround was not being triggered and would have failed if it were.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2019-04-22 22:28:39 -07:00 committed by Tim Abbott
parent fd2f45ce5a
commit 61982d9d47
3 changed files with 0 additions and 67 deletions

View File

@ -284,8 +284,6 @@ def check_upload_within_quota(realm: Realm, uploaded_file_size: int) -> None:
def get_file_info(request: HttpRequest, user_file: File) -> Tuple[str, int, Optional[str]]: def get_file_info(request: HttpRequest, user_file: File) -> Tuple[str, int, Optional[str]]:
uploaded_file_name = user_file.name uploaded_file_name = user_file.name
assert isinstance(uploaded_file_name, str)
content_type = request.GET.get('mimetype') content_type = request.GET.get('mimetype')
if content_type is None: if content_type is None:
guessed_type = guess_type(uploaded_file_name)[0] guessed_type = guess_type(uploaded_file_name)[0]

View File

@ -39,7 +39,6 @@ from zerver.lib.actions import (
from zerver.lib.cache import get_realm_used_upload_space_cache_key, cache_get from zerver.lib.cache import get_realm_used_upload_space_cache_key, cache_get
from zerver.lib.create_user import copy_user_settings from zerver.lib.create_user import copy_user_settings
from zerver.lib.users import get_api_key from zerver.lib.users import get_api_key
from zerver.views.upload import upload_file_backend
import urllib import urllib
import ujson import ujson
@ -131,43 +130,6 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
uri = result.json()["uri"] uri = result.json()["uri"]
self.assertTrue(uri.endswith("pasted_file.png")) self.assertTrue(uri.endswith("pasted_file.png"))
def test_filename_encoding(self) -> None:
"""
In Python 2, we need to encode unicode filenames (which converts them to
str) before they can be rendered correctly. However, in Python 3, the
separate unicode type does not exist, and we don't need to perform this
encoding. This test ensures that we handle filename encodings properly,
and does so in a way that preserves 100% test coverage for Python 3.
"""
user_profile = self.example_user('hamlet')
mock_file = mock.Mock()
mock_file._get_size = mock.Mock(return_value=1024)
mock_files = mock.Mock()
mock_files.__len__ = mock.Mock(return_value=1)
mock_files.values = mock.Mock(return_value=[mock_file])
mock_request = mock.Mock()
mock_request.FILES = mock_files
# str filenames should not be encoded.
mock_filename = mock.Mock(spec=str)
mock_file.name = mock_filename
with mock.patch('zerver.views.upload.upload_message_image_from_request'):
result = upload_file_backend(mock_request, user_profile)
self.assert_json_success(result)
mock_filename.encode.assert_not_called()
# Non-str filenames should be encoded.
mock_filename = mock.Mock(spec=None) # None is not str
mock_file.name = mock_filename
with mock.patch('zerver.views.upload.upload_message_image_from_request'):
result = upload_file_backend(mock_request, user_profile)
self.assert_json_success(result)
mock_filename.encode.assert_called_once_with('ascii')
def test_file_too_big_failure(self) -> None: def test_file_too_big_failure(self) -> None:
""" """
Attempting to upload big files should fail. Attempting to upload big files should fail.

View File

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
from django.http import HttpRequest, HttpResponse, HttpResponseForbidden, \ from django.http import HttpRequest, HttpResponse, HttpResponseForbidden, \
HttpResponseNotFound HttpResponseNotFound
from django.shortcuts import redirect from django.shortcuts import redirect
@ -73,30 +71,5 @@ def upload_file_backend(request: HttpRequest, user_profile: UserProfile) -> Http
settings.MAX_FILE_UPLOAD_SIZE)) settings.MAX_FILE_UPLOAD_SIZE))
check_upload_within_quota(user_profile.realm, file_size) check_upload_within_quota(user_profile.realm, file_size)
if not isinstance(user_file.name, str):
# It seems that in Python 2 unicode strings containing bytes are
# rendered differently than ascii strings containing same bytes.
#
# Example:
# >>> print('\xd3\x92')
# Ӓ
# >>> print(u'\xd3\x92')
# Ó
#
# This is the cause of the problem as user_file.name variable
# is received as a unicode which is converted into unicode
# strings containing bytes and is rendered incorrectly.
#
# Example:
# >>> import urllib.parse
# >>> name = u'%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D0%B5%D0%B8%CC%86%D1%82%D0%B5.txt'
# >>> print(urllib.parse.unquote(name))
# Здравейте # This is wrong
#
# >>> name = '%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D0%B5%D0%B8%CC%86%D1%82%D0%B5.txt'
# >>> print(urllib.parse.unquote(name))
# Здравейте.txt # This is correct
user_file.name = user_file.name.encode('ascii')
uri = upload_message_image_from_request(request, user_file, user_profile) uri = upload_message_image_from_request(request, user_file, user_profile)
return json_success({'uri': uri}) return json_success({'uri': uri})