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: Provide a default upload file name, rather than 500.

Alex Vandivere2847790b6f5
diff --git a/zerver/lib/upload/base.py b/zerver/lib/upload/base.py
index 8ff8f5d969f..df77870dbb4 100644
--- a/zerver/lib/upload/base.py
+++ b/zerver/lib/upload/base.py
@@ -50,7 +50,8 @@ def sanitize_name(value: str) -> str:
value = unicodedata.normalize("NFKC", value)
value = re.sub(r"[^\w\s.-]", "", value).strip()
value = re.sub(r"[-\s]+", "-", value)
- assert value not in {"", ".", ".."}
+ if value in {"", ".", ".."}:
+ return "uploaded-file"
return value
diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py
index a780cc0a5cd..fa2ea41aca5 100644
--- a/zerver/tests/test_upload.py
+++ b/zerver/tests/test_upload.py
@@ -658,6 +658,25 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):
response_dict = self.assert_json_success(result)
assert sanitize_name(expected) in response_dict["uri"]
+ def test_sanitize_file_name(self) -> None:
+ self.login("hamlet")
+ for uploaded_filename, expected in [
+ ("../foo", "foo"),
+ (".. ", "uploaded-file"),
+ ("/", "f1"),
+ ("./", "f1"),
+ ("././", "f1"),
+ (".!", "uploaded-file"),
+ ("**", "uploaded-file"),
+ ]:
+ fp = StringIO("bah!")
+ fp.name = urllib.parse.quote(uploaded_filename)
+
+ result = self.client_post("/json/user_uploads", {"f1": fp})
+ response_dict = self.assert_json_success(result)
+ self.assertNotIn(response_dict["uri"], uploaded_filename)
+ self.assertTrue(response_dict["uri"].endswith("/" + expected))
+
def test_realm_quota(self) -> None:
"""
Realm quota for uploading should not be exceeded.