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.

message_edit: Send content-edit events only to users with message access.

Sahil Batraf81f38f01d24
diff --git a/zerver/actions/message_edit.py b/zerver/actions/message_edit.py
index 37b0fea5157..f123784e6f4 100644
--- a/zerver/actions/message_edit.py
+++ b/zerver/actions/message_edit.py
@@ -1212,7 +1212,26 @@ def do_update_message(
# in the organization (too expansive, and also not what we do for
# newly sent messages anyway) and having magical live-updates
# where possible.
- users_to_be_notified = list(map(user_info, unmodified_user_messages))
+ #
+ # Only notify users who can currently access the (possibly moved)
+ # messages. A UserMessage row by itself does not grant access, so
+ # notifying every UserMessage-row holder would leak the edited
+ # content to users who have lost access.
+ #
+ # TODO: We can improve some unnecessary computation that is being
+ # done currently as event_recipient_ids_for_action_on_messages
+ # also computes UserMessage rows and the target stream subscribers.
+ message_access_user_ids = event_recipient_ids_for_action_on_messages(
+ changed_message_ids,
+ is_channel_message=True,
+ channel=message_edit_request.target_stream,
+ exclude_long_term_idle_users=False,
+ )
+ users_to_be_notified = [
+ user_info(um)
+ for um in unmodified_user_messages
+ if um.user_profile_id in message_access_user_ids
+ ]
if stream_being_edited.is_history_public_to_subscribers():
subscriptions = get_active_subscriptions_for_stream_id(
message_edit_request.target_stream.id, include_deactivated_users=False
@@ -1224,13 +1243,10 @@ def do_update_message(
# We exclude long-term idle users, since they by
# definition have no active clients.
subs = subs.exclude(user_profile__long_term_idle=True)
- # Remove duplicates by excluding the id of users already
- # in users_to_be_notified list. This is the case where a
- # user both has a UserMessage row and is a current
- # Subscriber
- subs = subs.exclude(
- user_profile_id__in=[um.user_profile_id for um in unmodified_user_messages]
- )
+ # Remove duplicates by excluding users already in
+ # users_to_be_notified. This is the case where a user both
+ # has a UserMessage row and is a current Subscriber.
+ subs = subs.exclude(user_profile_id__in=[user["id"] for user in users_to_be_notified])
if message_edit_request.is_stream_edited:
subs = subs.exclude(user_profile__in=users_losing_access)
diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.py
index 3447756f6dd..8f41072b8aa 100644
--- a/zerver/tests/test_message_edit.py
+++ b/zerver/tests/test_message_edit.py
@@ -1666,6 +1666,43 @@ class EditMessageTest(ZulipTestCase):
id_, "M", "You don't have permission to edit this message", "shiva"
)
+ def test_content_edit_not_notified_without_message_access(self) -> None:
+ # Editing a message's content must not notify users who can no
+ # longer access it.
+ hamlet = self.example_user("hamlet")
+ cordelia = self.example_user("cordelia")
+ stream_name = "protected history private"
+ self.make_stream(stream_name, invite_only=True, history_public_to_subscribers=False)
+ self.subscribe(hamlet, stream_name)
+ self.subscribe(cordelia, stream_name)
+ message_id = self.send_stream_message(hamlet, stream_name, "Hello", topic_name="test")
+
+ # Cordelia received the message, then unsubscribes. So
+ # the UserMessage row remains, but can no longer access
+ # the message in a protected-history channel.
+ self.unsubscribe(cordelia, stream_name)
+ self.assertTrue(
+ UserMessage.objects.filter(user_profile=cordelia, message_id=message_id).exists()
+ )
+
+ self.login("hamlet")
+ with self.capture_send_event_calls(expected_num_events=1) as events:
+ result = self.client_patch(
+ f"/json/messages/{message_id}",
+ {"content": "Hello, edited"},
+ )
+ self.assert_json_success(result)
+
+ self.assertTrue(
+ UserMessage.objects.filter(user_profile=cordelia, message_id=message_id).exists()
+ )
+
+ self.assertEqual(events[0]["event"]["type"], "update_message")
+ notified_user_ids = {user["id"] for user in events[0]["users"]}
+ # Hamlet (still subscribed) is notified but Cordelia is not.
+ self.assertIn(hamlet.id, notified_user_ids)
+ self.assertNotIn(cordelia.id, notified_user_ids)
+
@mock.patch("zerver.actions.message_edit.send_event_on_commit")
def test_topic_wildcard_mention_in_followed_topic(
self, mock_send_event: mock.MagicMock
diff --git a/zerver/tests/test_message_move_stream.py b/zerver/tests/test_message_move_stream.py
index 7dbaff66d91..12ef989bda1 100644
--- a/zerver/tests/test_message_move_stream.py
+++ b/zerver/tests/test_message_move_stream.py
@@ -1621,7 +1621,7 @@ class MessageMoveStreamTest(ZulipTestCase):
"iago", "test move stream", "new stream", "test"
)
- with self.assert_database_query_count(57), self.assert_memcached_count(19):
+ with self.assert_database_query_count(59), self.assert_memcached_count(19):
result = self.client_patch(
f"/json/messages/{msg_id}",
{
diff --git a/zerver/tests/test_message_move_topic.py b/zerver/tests/test_message_move_topic.py
index 5d350932a16..68fee5c5427 100644
--- a/zerver/tests/test_message_move_topic.py
+++ b/zerver/tests/test_message_move_topic.py
@@ -579,7 +579,7 @@ class MessageMoveTopicTest(ZulipTestCase):
# state + 1/user with a UserTopic row for the events data)
# beyond what is typical were there not UserTopic records to
# update. Ideally, we'd eliminate the per-user component.
- with self.assert_database_query_count(24):
+ with self.assert_database_query_count(26):
check_update_message(
user_profile=hamlet,
message_id=message_id,
@@ -676,7 +676,7 @@ class MessageMoveTopicTest(ZulipTestCase):
set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)
set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
- with self.assert_database_query_count(25):
+ with self.assert_database_query_count(27):
check_update_message(
user_profile=desdemona,
message_id=message_id,
@@ -706,7 +706,7 @@ class MessageMoveTopicTest(ZulipTestCase):
]
set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)
set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
- with self.assert_database_query_count(32):
+ with self.assert_database_query_count(34):
check_update_message(
user_profile=desdemona,
message_id=message_id,
@@ -739,7 +739,7 @@ class MessageMoveTopicTest(ZulipTestCase):
set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)
set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
- with self.assert_database_query_count(28):
+ with self.assert_database_query_count(30):
check_update_message(
user_profile=desdemona,
message_id=message_id,
@@ -762,7 +762,7 @@ class MessageMoveTopicTest(ZulipTestCase):
second_message_id = self.send_stream_message(
hamlet, stream_name, topic_name="changed topic name", content="Second message"
)
- with self.assert_database_query_count(22):
+ with self.assert_database_query_count(24):
check_update_message(
user_profile=desdemona,
message_id=second_message_id,
@@ -920,7 +920,7 @@ class MessageMoveTopicTest(ZulipTestCase):
users_to_be_notified_via_muted_topics_event.append(user_topic.user_profile_id)
change_all_topic_name = "Topic 1 edited"
- with self.assert_database_query_count(29):
+ with self.assert_database_query_count(31):
check_update_message(
user_profile=hamlet,
message_id=message_id,