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.

tusd: Remove non-ASCII characters from path-ids.

Alex Vandiver287850d08d77
diff --git a/zerver/lib/upload/__init__.py b/zerver/lib/upload/__init__.py
index 670ac6483fe..1a2eb527768 100644
--- a/zerver/lib/upload/__init__.py
+++ b/zerver/lib/upload/__init__.py
@@ -114,20 +114,27 @@ def get_public_upload_root_url() -> str:
return upload_backend.get_public_upload_root_url()
-def sanitize_name(value: str) -> str:
- """
- Sanitizes a value to be safe to store in a Linux filesystem, in
+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
characters other than ".", "-", and "_".
+ In "strict" mode, it does not allow Unicode, allowing only ASCII
+ [A-Za-z0-9_] as word characters. This is for the benefit of tusd,
+ which is not Unicode-aware.
+
This implementation is based on django.utils.text.slugify; it is
modified by:
* adding '.' to the list of allowed characters.
* preserving the case of the value.
* not stripping trailing dashes and underscores.
+
"""
- value = unicodedata.normalize("NFKC", value)
- value = re.sub(r"[^\w\s.-]", "", value).strip()
+ if strict:
+ value = re.sub(r"[^A-Za-z0-9_ .-]", "", value).strip()
+ else:
+ value = unicodedata.normalize("NFKC", value)
+ value = re.sub(r"[^\w\s.-]", "", value).strip()
value = re.sub(r"[-\s]+", "-", value)
# Django's MultiPartParser never returns files named this, but we
diff --git a/zerver/tests/test_tusd.py b/zerver/tests/test_tusd.py
index 5bd2424a689..f9c9ea758b2 100644
--- a/zerver/tests/test_tusd.py
+++ b/zerver/tests/test_tusd.py
@@ -182,7 +182,7 @@ class TusdPreCreateTest(ZulipTestCase):
def test_sanitize_filename(self) -> None:
self.login("hamlet")
request = self.request()
- request.event.upload.meta_data["filename"] = "some thing! ... like this?"
+ request.event.upload.meta_data["filename"] = "some 例 thing! ... like this?"
result = self.client_post(
"/api/internal/tusd",
request.model_dump(),
diff --git a/zerver/views/tusd.py b/zerver/views/tusd.py
index 1f6aaae8827..d1ca31e6bd0 100644
--- a/zerver/views/tusd.py
+++ b/zerver/views/tusd.py
@@ -111,7 +111,7 @@ def handle_upload_pre_create_hook(
return reject_upload(str(e), 413)
# Determine the path_id to store it at
- file_name = sanitize_name(data.meta_data.get("filename", ""))
+ file_name = sanitize_name(data.meta_data.get("filename", ""), strict=True)
path_id = upload_backend.generate_message_upload_path(str(user_profile.realm_id), file_name)
return tusd_json_response({"ChangeFileInfo": {"ID": path_id}})