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.

api: Prevent special characters in topics.

Shlok Patelb3c58f454f0d
diff --git a/zerver/lib/addressee.py b/zerver/lib/addressee.py
index 60817ea94c1..b4efd7bb9e9 100644
--- a/zerver/lib/addressee.py
+++ b/zerver/lib/addressee.py
@@ -1,3 +1,4 @@
+import unicodedata
from typing import Iterable, List, Optional, Sequence, Union, cast
from django.utils.translation import gettext as _
@@ -40,6 +41,10 @@ def validate_topic(topic: str) -> str:
if topic == "":
raise JsonableError(_("Topic can't be empty"))
+ for character in topic:
+ unicodeCategory = unicodedata.category(character)
+ if unicodeCategory in ["Cc", "Cs", "Cn"]:
+ raise JsonableError(_("Invalid characters in topic!"))
return topic
diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py
index 120c2ad4308..820c8468b01 100644
--- a/zerver/tests/test_message_send.py
+++ b/zerver/tests/test_message_send.py
@@ -730,6 +730,37 @@ class MessagePOSTTest(ZulipTestCase):
)
self.assert_json_error(result, "Missing topic")
+ def test_invalid_topic(self) -> None:
+ """
+ Sending a message with invalid 'Cc', 'Cs' and 'Cn' category of unicode characters
+ """
+ # For 'Cc' category
+ self.login("hamlet")
+ result = self.client_post(
+ "/json/messages",
+ {
+ "type": "stream",
+ "to": "Verona",
+ "client": "test suite",
+ "topic": "Test\n\rTopic",
+ "content": "Test message",
+ },
+ )
+ self.assert_json_error(result, "Invalid characters in topic!")
+
+ # For 'Cn' category
+ result = self.client_post(
+ "/json/messages",
+ {
+ "type": "stream",
+ "to": "Verona",
+ "client": "test suite",
+ "topic": "Test\uFFFETopic",
+ "content": "Test message",
+ },
+ )
+ self.assert_json_error(result, "Invalid characters in topic!")
+
def test_invalid_message_type(self) -> None:
"""
Messages other than the type of "private" or "stream" are considered as invalid