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.pyindex 60817ea94c1..b4efd7bb9e9 100644--- a/zerver/lib/addressee.py+++ b/zerver/lib/addressee.py@@ -1,3 +1,4 @@+import unicodedatafrom typing import Iterable, List, Optional, Sequence, Union, castfrom 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 topicdiff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.pyindex 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