Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
string_validation: Loosen to allow some `Cn` unicode characters.
Alex Vandiverdf50280c5423
diff --git a/zerver/lib/string_validation.py b/zerver/lib/string_validation.pyindex df0f34bd5f8..e8a74d90c53 100644--- a/zerver/lib/string_validation.py+++ b/zerver/lib/string_validation.py@@ -6,6 +6,25 @@ from django.utils.translation import gettext as _from zerver.lib.exceptions import JsonableErrorfrom zerver.models import Stream+# There are 66 Unicode non-characters; see+# https://www.unicode.org/faq/private_use.html#nonchar4+unicode_non_chars = [+ chr(x)+ for x in list(range(0xFDD0, 0xFDF0)) # FDD0 through FDEF, inclusive+ + list(range(0xFFFE, 0x110000, 0x10000)) # 0xFFFE, 0x1FFFE, ... 0x10FFFE inclusive+ + list(range(0xFFFF, 0x110000, 0x10000)) # 0xFFFF, 0x1FFFF, ... 0x10FFFF inclusive+]+++def check_string_is_printable(var: str) -> Optional[int]:+ # Return position (1-indexed!) of the character which is not+ # printable, None if no such character is present.+ for i in range(len(var)):+ unicode_character = unicodedata.category(var[i])+ if (unicode_character in ["Cc", "Cs"]) or var[i] in unicode_non_chars:+ return i + 1+ return None+def check_stream_name(stream_name: str) -> None:if stream_name.strip() == "":@@ -26,7 +45,8 @@ def check_stream_topic(topic: str) -> None:if topic.strip() == "":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!"))+ invalid_character_pos = check_string_is_printable(topic)+ if invalid_character_pos is not None:+ raise JsonableError(+ _("Invalid character in topic, at position {}!").format(invalid_character_pos)+ )diff --git a/zerver/migrations/0371_invalid_characters_in_topics.py b/zerver/migrations/0371_invalid_characters_in_topics.pyindex 64e94bfd055..fae4155d2d5 100644--- a/zerver/migrations/0371_invalid_characters_in_topics.py+++ b/zerver/migrations/0371_invalid_characters_in_topics.py@@ -4,6 +4,19 @@ from django.db import connection, migrations, modelsfrom django.db.backends.postgresql.schema import DatabaseSchemaEditorfrom django.db.migrations.state import StateApps+# There are 66 Unicode non-characters; see+# https://www.unicode.org/faq/private_use.html#nonchar4+unicode_non_chars = [+ chr(x)+ for x in list(range(0xFDD0, 0xFDF0)) # FDD0 through FDEF, inclusive+ + list(range(0xFFFE, 0x110000, 0x10000)) # 0xFFFE, 0x1FFFE, ... 0x10FFFE inclusive+ + list(range(0xFFFF, 0x110000, 0x10000)) # 0xFFFF, 0x1FFFF, ... 0x10FFFF inclusive+]+++def character_is_printable(character: str) -> bool:+ return unicodedata.category(character) in ["Cc", "Cs"] or character in unicode_non_chars+def fix_topics(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:Message = apps.get_model("zerver", "Message")@@ -30,11 +43,7 @@ def fix_topics(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:topics = [r[0] for r in results]for topic in topics:fixed_topic = "".join(- [- character- for character in topic- if unicodedata.category(character) not in ["Cc", "Cs", "Cn"]- ]+ [character for character in topic if character_is_printable(character)])if fixed_topic == topic:continuediff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.pyindex 95a389df3c3..b264aa194bf 100644--- a/zerver/tests/test_message_edit.py+++ b/zerver/tests/test_message_edit.py@@ -206,7 +206,7 @@ class EditMessagePayloadTest(EditMessageTestCase):"topic": "editing\nfun",},)- self.assert_json_error(result, "Invalid characters in topic!")+ self.assert_json_error(result, "Invalid character in topic, at position 8!")def test_move_message_to_stream_with_content(self) -> None:(user_profile, old_stream, new_stream, msg_id, msg_id_later) = self.prepare_move_topics(diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.pyindex 4da2c61ea83..6ea1ae9f786 100644--- a/zerver/tests/test_message_send.py+++ b/zerver/tests/test_message_send.py@@ -752,7 +752,7 @@ class MessagePOSTTest(ZulipTestCase):"content": "Test message",},)- self.assert_json_error(result, "Invalid characters in topic!")+ self.assert_json_error(result, "Invalid character in topic, at position 5!")# For 'Cn' categoryresult = self.client_post(@@ -765,7 +765,7 @@ class MessagePOSTTest(ZulipTestCase):"content": "Test message",},)- self.assert_json_error(result, "Invalid characters in topic!")+ self.assert_json_error(result, "Invalid character in topic, at position 5!")def test_invalid_message_type(self) -> None:"""