message: Check wildcard mention restrictions while editing message.

This commit adds code to check whether a user is allowed to use
wildcard mention in a large stream or not while editing a message
based on the realm settings.

Previously this was only checked while sending message, thus user
was easily able to use wildcard mention by first sending a normal
message and then using a wildcard mention by editing it.
This commit is contained in:
Sahil Batra 2021-12-06 23:10:30 +05:30 committed by Tim Abbott
parent fb2d05f9e3
commit b68ebf5a22
2 changed files with 58 additions and 0 deletions

View File

@ -3068,6 +3068,13 @@ def check_update_message(
) )
links_for_embed |= rendering_result.links_for_preview links_for_embed |= rendering_result.links_for_preview
if message.is_stream_message() and rendering_result.mentions_wildcard:
stream = access_stream_by_id(user_profile, message.recipient.type_id)[0]
if not wildcard_mention_allowed(message.sender, stream):
raise JsonableError(
_("You do not have permission to use wildcard mentions in this stream.")
)
new_stream = None new_stream = None
number_changed = 0 number_changed = 0

View File

@ -1170,6 +1170,57 @@ class EditMessageTest(EditMessageTestCase):
called = True called = True
self.assertTrue(called) self.assertTrue(called)
def test_wildcard_mention_restrictions_when_editing(self) -> None:
cordelia = self.example_user("cordelia")
shiva = self.example_user("shiva")
self.login("cordelia")
stream_name = "Macbeth"
self.make_stream(stream_name, history_public_to_subscribers=True)
self.subscribe(cordelia, stream_name)
message_id = self.send_stream_message(cordelia, stream_name, "Hello everyone")
realm = cordelia.realm
do_set_realm_property(
realm,
"wildcard_mention_policy",
Realm.WILDCARD_MENTION_POLICY_MODERATORS,
acting_user=None,
)
with mock.patch("zerver.lib.message.num_subscribers_for_stream_id", return_value=17):
result = self.client_patch(
"/json/messages/" + str(message_id),
{
"message_id": message_id,
"content": "Hello @**everyone**",
},
)
self.assert_json_error(
result, "You do not have permission to use wildcard mentions in this stream."
)
with mock.patch("zerver.lib.message.num_subscribers_for_stream_id", return_value=14):
result = self.client_patch(
"/json/messages/" + str(message_id),
{
"message_id": message_id,
"content": "Hello @**everyone**",
},
)
self.assert_json_success(result)
self.login("shiva")
message_id = self.send_stream_message(shiva, stream_name, "Hi everyone")
with mock.patch("zerver.lib.message.num_subscribers_for_stream_id", return_value=17):
result = self.client_patch(
"/json/messages/" + str(message_id),
{
"message_id": message_id,
"content": "Hello @**everyone**",
},
)
self.assert_json_success(result)
def test_topic_edit_history_saved_in_all_message(self) -> None: def test_topic_edit_history_saved_in_all_message(self) -> None:
self.login("hamlet") self.login("hamlet")
id1 = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="topic1") id1 = self.send_stream_message(self.example_user("hamlet"), "Scotland", topic_name="topic1")