changeset 8908:8993d401575b stable

files: fix raw download of repo files with names with unicode points above 256 in name Raw download had apparently only been tested with non-ascii characters that were latin1. That was apparently a (too) simple case that worked without crashing. Files with unicode code points above 256 in their name would fail to download, when Waitress failed like this, trying to get a real byte string by encoding WSGI headers to latin1: UnicodeEncodeError: 'latin-1' codec can't encode characters in position 84-85: ordinal not in range(256) HTTP headers are of course byte strings on the network, but Python3 WSGI does unfortunately neither expose it as bytes nor as unicode strings to be encoded as utf-8. Instead, it uses unicode strings with byte values encoded as code points 0-255. That is achieved by decoding the utf-8 encoded bytes as latin1. For raw downloads, the recommended download filename is provided in the Content-Disposition header. The problem is that it was provided as a real unicode string. Fixed by applying the "proper" latin1-decoding of a utf8-encoding.
author Mads Kiilerich <mads@kiilerich.com>
date Mon, 07 Feb 2022 19:07:08 +0100
parents 7ac728ed359a
children 98cbebff6afc
files kallithea/controllers/files.py
diffstat 1 files changed, 2 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/files.py	Thu May 27 21:33:45 2021 +0200
+++ b/kallithea/controllers/files.py	Mon Feb 07 19:07:08 2022 +0100
@@ -44,7 +44,7 @@
 from kallithea.lib import diffs, webutils
 from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired
 from kallithea.lib.exceptions import NonRelativePathError
-from kallithea.lib.utils2 import asbool, convert_line_endings, detect_mode, safe_str
+from kallithea.lib.utils2 import asbool, convert_line_endings, detect_mode, safe_bytes, safe_str
 from kallithea.lib.vcs.backends.base import EmptyChangeset
 from kallithea.lib.vcs.conf import settings
 from kallithea.lib.vcs.exceptions import (ChangesetDoesNotExistError, ChangesetError, EmptyRepositoryError, ImproperArchiveTypeError, NodeAlreadyExistsError,
@@ -233,7 +233,7 @@
         file_node = self.__get_filenode(cs, f_path)
 
         response.content_disposition = \
-            'attachment; filename=%s' % f_path.split(kallithea.URL_SEP)[-1]
+            'attachment; filename=%s' % safe_bytes(f_path.split(kallithea.URL_SEP)[-1]).decode('latin1')
 
         response.content_type = file_node.mimetype
         return file_node.content