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: Limit the length of sanitized file names.

Aman Agrawal5f52a29e8044
diff --git a/zerver/lib/upload/__init__.py b/zerver/lib/upload/__init__.py
index 239e78c2a01..61c38b684be 100644
--- a/zerver/lib/upload/__init__.py
+++ b/zerver/lib/upload/__init__.py
@@ -225,6 +225,31 @@ def store_message_attachment(
)
+# Most Linux filesystems limit each component of a path to 255 bytes,
+# and we store uploaded files with their sanitized name as the last
+# component of the path.
+MAX_FILE_NAME_LENGTH = 255
+
+
+def truncate_file_name(file_name: str, max_length: int) -> str:
+ """Truncates a file name to at most max_length bytes, preserving its
+ extension if that leaves room for the rest of the name.
+ """
+ if len(file_name.encode()) <= max_length:
+ return file_name
+
+ stem, extension = os.path.splitext(file_name)
+ if len(extension.encode()) >= max_length:
+ # The extension alone is too long to preserve, so truncate the
+ # name as a whole instead.
+ stem, extension = file_name, ""
+
+ # Slicing the encoded name can cut a multi-byte character in half;
+ # errors="ignore" discards such a partial character.
+ stem_length = max_length - len(extension.encode())
+ return stem.encode()[:stem_length].decode(errors="ignore") + extension
+
+
def sanitize_name(value: str, *, strict: bool = False) -> str:
"""Sanitizes a value to be safe to store in a Linux filesystem, in
S3, and in a URL. So Unicode is allowed, but not special
@@ -239,6 +264,7 @@ def sanitize_name(value: str, *, strict: bool = False) -> str:
* adding '.' to the list of allowed characters.
* preserving the case of the value.
* not stripping trailing dashes and underscores.
+ * limiting the length of the result.
"""
if strict:
@@ -248,6 +274,11 @@ def sanitize_name(value: str, *, strict: bool = False) -> str:
value = re.sub(r"[^\w\s.-]", "", value).strip()
value = re.sub(r"[-\s]+", "-", value)
+ # NFKC normalization can make the value considerably longer -- a
+ # single character can normalize to dozens of them -- so this has
+ # to happen after normalizing, not to the caller's value.
+ value = truncate_file_name(value, MAX_FILE_NAME_LENGTH)
+
# Django's MultiPartParser never returns files named this, but we
# could get them after removing spaces; change the name to a safer
# value.
diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py
index 5f3d9e597ca..8f7fb599924 100644
--- a/zerver/tests/test_upload.py
+++ b/zerver/tests/test_upload.py
@@ -45,7 +45,7 @@ from zerver.lib.test_helpers import (
get_test_image_file,
ratelimit_rule,
)
-from zerver.lib.upload import sanitize_name, upload_message_attachment
+from zerver.lib.upload import MAX_FILE_NAME_LENGTH, sanitize_name, upload_message_attachment
from zerver.lib.upload.base import ZulipUploadBackend
from zerver.lib.upload.local import LocalUploadBackend
from zerver.lib.upload.s3 import S3UploadBackend
@@ -2314,6 +2314,15 @@ class SanitizeNameTests(ZulipTestCase):
sanitize_name('~/."\\`\\?*"u0`000ssh/test.t**{}ar.gz'), ".u0000sshtest.tar.gz"
)
+ def test_long_file_name(self) -> None:
+ self.assertEqual(sanitize_name("a" * 300 + ".txt"), "a" * 251 + ".txt")
+ self.assertEqual(sanitize_name("test." + "a" * 300), "test." + "a" * 250)
+ # Truncation counts bytes, not characters.
+ self.assertEqual(sanitize_name("테" * 100 + ".txt"), "테" * 83 + ".txt")
+ # A single character can normalize to many more, so the result
+ # has to be measured after normalizing rather than before.
+ self.assertEqual(len(sanitize_name("ﷺ" * 79).encode()), MAX_FILE_NAME_LENGTH)
+
class UploadSpaceTests(UploadSerializeMixin, ZulipTestCase):
@override