Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message: Do not allow guest to mention inaccessible users.
Sahil Batra72aa4b256d0e
diff --git a/zerver/actions/message_edit.py b/zerver/actions/message_edit.pyindex 0f7a4920cdf..d542de77bfd 100644--- a/zerver/actions/message_edit.py+++ b/zerver/actions/message_edit.py@@ -1265,6 +1265,7 @@ def check_update_message(mention_data = MentionData(mention_backend=mention_backend,content=content,+ message_sender=message.sender,)prior_mention_user_ids = get_mentions_for_message_updates(message.id)diff --git a/zerver/actions/message_send.py b/zerver/actions/message_send.pyindex 5ba43d0480b..079121555b8 100644--- a/zerver/actions/message_send.py+++ b/zerver/actions/message_send.py@@ -595,6 +595,7 @@ def build_message_send_dict(mention_data = MentionData(mention_backend=mention_backend,content=message.content,+ message_sender=message.sender,)if message.is_stream_message():diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex f43d63bed05..ff15043ecd6 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -2637,7 +2637,10 @@ def do_convert(if mention_data is None:mention_backend = MentionBackend(message_realm.id)- mention_data = MentionData(mention_backend, content)+ message_sender = None+ if message is not None:+ message_sender = message.sender+ mention_data = MentionData(mention_backend, content, message_sender)stream_names = possible_linked_stream_names(content)stream_name_info = mention_data.get_stream_name_map(stream_names)diff --git a/zerver/lib/mention.py b/zerver/lib/mention.pyindex 48768079ed9..a67f696f99a 100644--- a/zerver/lib/mention.py+++ b/zerver/lib/mention.py@@ -6,6 +6,7 @@ from typing import Dict, List, Match, Optional, Set, Tuplefrom django.conf import settingsfrom django.db.models import Q+from zerver.lib.users import get_inaccessible_user_idsfrom zerver.models import UserGroup, UserProfile, get_linkable_streamsBEFORE_MENTION_ALLOWED_REGEX = r"(?<![^\s\'\"\(\{\[\/<])"@@ -61,12 +62,21 @@ class PossibleMentions:class MentionBackend:+ # Be careful about reuse: MentionBackend contains caches which are+ # designed to only have the lifespan of a sender user (typically a+ # single request).+ #+ # In particular, user_cache is not robust to message_sender+ # within the lifetime of a single MentionBackend lifetime.+def __init__(self, realm_id: int) -> None:self.realm_id = realm_idself.user_cache: Dict[Tuple[int, str], FullNameInfo] = {}self.stream_cache: Dict[str, int] = {}- def get_full_name_info_list(self, user_filters: List[UserFilter]) -> List[FullNameInfo]:+ def get_full_name_info_list(+ self, user_filters: List[UserFilter], message_sender: Optional[UserProfile]+ ) -> List[FullNameInfo]:result: List[FullNameInfo] = []unseen_user_filters: List[UserFilter] = []@@ -105,9 +115,15 @@ class MentionBackend:))+ possible_mention_user_ids = [row.id for row in rows]+ inaccessible_user_ids = get_inaccessible_user_ids(+ possible_mention_user_ids, message_sender+ )+user_list = [FullNameInfo(id=row.id, full_name=row.full_name, is_active=row.is_active)for row in rows+ if row.id not in inaccessible_user_ids]# We expect callers who take advantage of our cache to supply both@@ -198,7 +214,7 @@ def possible_user_group_mentions(content: str) -> Set[str]:def get_possible_mentions_info(- mention_backend: MentionBackend, mention_texts: Set[str]+ mention_backend: MentionBackend, mention_texts: Set[str], message_sender: Optional[UserProfile]) -> List[FullNameInfo]:if not mention_texts:return []@@ -222,15 +238,19 @@ def get_possible_mentions_info(# For **name** syntax.user_filters.append(UserFilter(full_name=mention_text, id=None))- return mention_backend.get_full_name_info_list(user_filters)+ return mention_backend.get_full_name_info_list(user_filters, message_sender)class MentionData:- def __init__(self, mention_backend: MentionBackend, content: str) -> None:+ def __init__(+ self, mention_backend: MentionBackend, content: str, message_sender: Optional[UserProfile]+ ) -> None:self.mention_backend = mention_backendrealm_id = mention_backend.realm_idmentions = possible_mentions(content)- possible_mentions_info = get_possible_mentions_info(mention_backend, mentions.mention_texts)+ possible_mentions_info = get_possible_mentions_info(+ mention_backend, mentions.mention_texts, message_sender+ )self.full_name_info = {row.full_name.lower(): row for row in possible_mentions_info}self.user_id_info = {row.id: row for row in possible_mentions_info}self.init_user_group_data(realm_id=realm_id, content=content)diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 2c49e68ff64..7c03dd75584 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -506,6 +506,7 @@ class NormalActionsTest(BaseAction):mention_data = MentionData(mention_backend=mention_backend,content=content,+ message_sender=self.example_user("cordelia"),)events = self.verify_action(@@ -813,6 +814,7 @@ class NormalActionsTest(BaseAction):mention_data = MentionData(mention_backend=mention_backend,content=content,+ message_sender=iago,)events = self.verify_action(diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.pyindex 33c4eb93e14..e1002ac7c76 100644--- a/zerver/tests/test_markdown.py+++ b/zerver/tests/test_markdown.py@@ -242,7 +242,9 @@ class MarkdownMiscTest(ZulipTestCase):mention_backend = MentionBackend(realm.id)lst = get_possible_mentions_info(- mention_backend, {"Fred Flintstone", "Cordelia, LEAR's daughter", "Not A User"}+ mention_backend,+ {"Fred Flintstone", "Cordelia, LEAR's daughter", "Not A User"},+ message_sender=None,)set_of_names = {x.full_name.lower() for x in lst}self.assertEqual(set_of_names, {"fred flintstone", "cordelia, lear's daughter"})@@ -271,7 +273,7 @@ class MarkdownMiscTest(ZulipTestCase):cordelia = self.example_user("cordelia")content = "@**King Hamlet** @**Cordelia, lear's daughter**"mention_backend = MentionBackend(realm.id)- mention_data = MentionData(mention_backend, content)+ mention_data = MentionData(mention_backend, content, message_sender=None)self.assertEqual(mention_data.get_user_ids(), {hamlet.id, cordelia.id})self.assertEqual(mention_data.get_user_by_id(hamlet.id),@@ -288,12 +290,12 @@ class MarkdownMiscTest(ZulipTestCase):self.assertFalse(mention_data.message_has_stream_wildcards())content = "@**King Hamlet** @**Cordelia, lear's daughter** @**all**"- mention_data = MentionData(mention_backend, content)+ mention_data = MentionData(mention_backend, content, message_sender=None)self.assertTrue(mention_data.message_has_stream_wildcards())self.assertFalse(mention_data.message_has_topic_wildcards())content = "@**King Hamlet** @**Cordelia, lear's daughter** @**topic**"- mention_data = MentionData(mention_backend, content)+ mention_data = MentionData(mention_backend, content, message_sender=None)self.assertTrue(mention_data.message_has_topic_wildcards())def test_invalid_katex_path(self) -> None:@@ -2175,6 +2177,35 @@ class MarkdownTest(ZulipTestCase):)self.assertEqual(rendering_result.mentions_user_ids, set())+ def test_mention_inaccessible_users(self) -> None:+ self.set_up_db_for_testing_user_access()+ polonius = self.example_user("polonius")+ hamlet = self.example_user("hamlet")+ msg = Message(+ sender=polonius,+ sending_client=get_client("test"),+ realm=polonius.realm,+ )+ content = "@**Othello, the Moor of Venice** @**King Hamlet** test message"+ rendering_result = render_markdown(msg, content)+ self.assertEqual(+ rendering_result.rendered_content,+ '<p>@<strong>Othello, the Moor of Venice</strong> <span class="user-mention" '+ f'data-user-id="{hamlet.id}">'+ "@King Hamlet</span> test message</p>",+ )+ self.assertEqual(rendering_result.mentions_user_ids, {hamlet.id})++ content = "@_**Othello, the Moor of Venice** @_**King Hamlet** test message"+ rendering_result = render_markdown(msg, content)+ self.assertEqual(+ rendering_result.rendered_content,+ '<p>@_<strong>Othello, the Moor of Venice</strong> <span class="user-mention silent" '+ f'data-user-id="{hamlet.id}">'+ "King Hamlet</span> test message</p>",+ )+ self.assertEqual(rendering_result.mentions_user_ids, set())+def test_silent_stream_wildcard_mention(self) -> None:user_profile = self.example_user("othello")msg = Message(diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.pyindex 3179a2aebcb..a4391638c0d 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -4301,7 +4301,7 @@ class MessageHasKeywordsTest(ZulipTestCase):realm_id = hamlet.realm.idrendering_result = render_markdown(msg, content)mention_backend = MentionBackend(realm_id)- mention_data = MentionData(mention_backend, content)+ mention_data = MentionData(mention_backend, content, msg.sender)do_update_message(hamlet,msg,diff --git a/zerver/tests/test_notification_data.py b/zerver/tests/test_notification_data.pyindex 02bfd645a7d..c6ce2320bd5 100644--- a/zerver/tests/test_notification_data.py+++ b/zerver/tests/test_notification_data.py@@ -384,7 +384,7 @@ class TestNotificationData(ZulipTestCase):result = get_user_group_mentions_data(mentioned_user_ids=set(),mentioned_user_group_ids=[],- mention_data=MentionData(mention_backend, "no group mentioned"),+ mention_data=MentionData(mention_backend, "no group mentioned", message_sender=None),)self.assertDictEqual(result, {})@@ -392,7 +392,9 @@ class TestNotificationData(ZulipTestCase):result = get_user_group_mentions_data(mentioned_user_ids=set(),mentioned_user_group_ids=[hamlet_and_cordelia.id],- mention_data=MentionData(mention_backend, "hey @*hamlet_and_cordelia*!"),+ mention_data=MentionData(+ mention_backend, "hey @*hamlet_and_cordelia*!", message_sender=None+ ),)self.assertDictEqual(result,@@ -408,7 +410,9 @@ class TestNotificationData(ZulipTestCase):mentioned_user_ids=set(),mentioned_user_group_ids=[hamlet_and_cordelia.id, hamlet_only.id],mention_data=MentionData(- mention_backend, "hey @*hamlet_and_cordelia* and @*hamlet_only*"+ mention_backend,+ "hey @*hamlet_and_cordelia* and @*hamlet_only*",+ message_sender=None,),)self.assertDictEqual(@@ -425,7 +429,9 @@ class TestNotificationData(ZulipTestCase):mentioned_user_ids=set(),mentioned_user_group_ids=[hamlet_only.id, hamlet_and_cordelia.id],mention_data=MentionData(- mention_backend, "hey @*hamlet_only* and @*hamlet_and_cordelia*"+ mention_backend,+ "hey @*hamlet_only* and @*hamlet_and_cordelia*",+ message_sender=None,),)self.assertDictEqual(@@ -441,7 +447,9 @@ class TestNotificationData(ZulipTestCase):result = get_user_group_mentions_data(mentioned_user_ids={hamlet.id},mentioned_user_group_ids=[hamlet_and_cordelia.id],- mention_data=MentionData(mention_backend, "hey @*hamlet_and_cordelia*!"),+ mention_data=MentionData(+ mention_backend, "hey @*hamlet_and_cordelia*!", message_sender=None+ ),)self.assertDictEqual(result,