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_delete: Send event to acting_user only if they have access.

Sahil Batra985950f71fe6
diff --git a/api_docs/changelog.md b/api_docs/changelog.md
index 6326d4ce4ce..a96bfe15d2e 100644
--- a/api_docs/changelog.md
+++ b/api_docs/changelog.md
@@ -20,6 +20,13 @@ format used by the Zulip server that they are interacting with.
## Changes in Zulip 12.0
+**Feature level 457**
+
+[`GET /events`](/api/get-events): `delete_message` events are now
+ sent to the user who deletes the message only if they have content
+ access to the messages' recipient, and the `message_ids` list only
+ includes IDs of the messages that they can access.
+
**Feature level 456**
* `PATCH /realm`, [`POST /register`](/api/register-queue),
diff --git a/version.py b/version.py
index 0393e0375f7..9d96c198a30 100644
--- a/version.py
+++ b/version.py
@@ -35,7 +35,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"
# new level means in api_docs/changelog.md, as well as "**Changes**"
# entries in the endpoint's documentation in `zulip.yaml`.
-API_FEATURE_LEVEL = 456
+API_FEATURE_LEVEL = 457
# Bump the minor PROVISION_VERSION to indicate that folks should provision
# only when going from an old version of the code to a newer version. Bump
diff --git a/zerver/lib/retention.py b/zerver/lib/retention.py
index e6108505842..24eb15a0038 100644
--- a/zerver/lib/retention.py
+++ b/zerver/lib/retention.py
@@ -26,6 +26,7 @@
# same system for routine deletions via the Zulip UI (deleting a
# message or group of messages) as we use for message retention policy
# deletions.
+import copy
import logging
import time
from collections.abc import Iterable, Mapping
@@ -42,7 +43,7 @@ from psycopg2.sql import SQL, Composable, Identifier, Literal
from zerver.actions.message_flags import do_clear_mobile_push_notifications_for_ids
from zerver.lib.logging_util import log_to_file
-from zerver.lib.message import event_recipient_ids_for_action_on_messages
+from zerver.lib.message import bulk_access_messages, event_recipient_ids_for_action_on_messages
from zerver.lib.request import RequestVariableConversionError
from zerver.lib.topic import DB_TOPIC_NAME
from zerver.lib.utils import assert_is_not_none
@@ -366,9 +367,35 @@ def _process_grouped_messages_deletion(
is_channel_message=message_type == "stream",
channel=stream if message_type == "stream" else None,
)
- if acting_user is not None:
- # Always send event to the user who deleted the message.
- users_to_notify.add(acting_user.id)
+
+ acting_user_event: DeleteMessagesEvent | None = None
+ if acting_user is not None and message_type == "stream":
+ # Send event to the user who deleted the messages only if the
+ # user has access to the messages, and the event should only
+ # include message IDs which the user can access.
+ #
+ # For DMs, acting_user will already be included in users_to_notify
+ # if they can access the messages, so this logic applies only to
+ # channel messages.
+ #
+ # We need to check access here for cases where messages
+ # are deleted in bulk by an admin, like when deactivating
+ # a spam user, when we do not check access to messages.
+ messages = Message.objects.filter(id__in=message_ids)
+ assert stream is not None
+ accessible_messages = bulk_access_messages(
+ acting_user, messages, stream=stream, is_modifying_message=False
+ )
+
+ if len(accessible_messages) == len(message_ids):
+ if acting_user.id not in users_to_notify:
+ users_to_notify.add(acting_user.id)
+ elif len(accessible_messages) != 0:
+ if acting_user.id in users_to_notify:
+ users_to_notify.remove(acting_user.id)
+ accessible_message_ids = [msg.id for msg in accessible_messages]
+ acting_user_event = copy.deepcopy(event)
+ acting_user_event["message_ids"] = sorted(accessible_message_ids)
# Uses index: zerver_message_pkey
Message.objects.filter(id__in=message_ids).delete()
@@ -379,6 +406,10 @@ def _process_grouped_messages_deletion(
send_event_on_commit(realm, event, users_to_notify)
+ if acting_user_event is not None:
+ assert acting_user is not None
+ send_event_on_commit(realm, acting_user_event, [acting_user.id])
+
def delete_messages(
msg_ids: list[int],
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index 61a5039df9d..bb5117fe4d8 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -2142,23 +2142,35 @@ paths:
description: |
Event sent when a message has been deleted.
- Sent to all users who currently are subscribed to the
- messages' recipient. May also be sent to additional users
- who had access to it, including, in particular, an
- administrator user deleting messages in a stream that they
- are not subscribed to.
-
- This means that clients can assume that they will always
- receive an event of this type for deletions that the
- client itself initiated.
+ Sent to all users who currently are subscribed to the messages'
+ recipient. May also be sent to additional users who had access to
+ the deleted message, including, in particular, an administrator user
+ deleting messages in a channel that they are not subscribed to, but
+ have content access to. Clients can thus reliably remove the
+ messages from whatever view the administrator was using to delete
+ them.
+
+ Clients will receive an event of this type for message deletions
+ that the client itself initiated if and only if the user previously
+ had access to the deleted messages. (Some moderation actions, such
+ as deleting all messages sent by a user, allow deleting DMs or
+ private channel messages that the acting user cannot themselves
+ access. The user will not receive deletion events for such
+ inaccessible messages).
This event is also sent when the user loses access to a message,
such as when it is [moved to a channel][message-move-channel] that
the user does not [have permission to access][channel-access].
- **Changes**: Prior to Zulip 12.0 (feature level 452) messages
- deleted via a message retention policy incorrectly failed to
- generate `delete_message` events.
+ **Changes**: Before Zulip 12.0 (feature level 457), an
+ administrator who initiated the deletion of messages that they did
+ not have permission to access could theoretically receive this event
+ for inaccessible messages. However, no Zulip feature actually
+ generated events of that type.
+
+ Prior to Zulip 12.0 (feature level 452) messages deleted via a
+ message retention policy incorrectly failed to generate
+ `delete_message` events.
Before Zulip 9.0 (feature level 274), this event was only sent to
subscribers of the message's recipient.
diff --git a/zerver/tests/test_message_delete.py b/zerver/tests/test_message_delete.py
index ae18f51ea06..81a4771cf69 100644
--- a/zerver/tests/test_message_delete.py
+++ b/zerver/tests/test_message_delete.py
@@ -46,7 +46,20 @@ class DeleteMessageTest(ZulipTestCase):
self.assertIn("type", event)
self.assertEqual(event["type"], "delete_message")
self.assertIn(msg_id, event["message_ids"])
- self.assertIn(acting_user.id, events[0]["users"])
+ self.assertCountEqual([hamlet.id, cordelia.id], events[0]["users"])
+
+ msg_id = self.send_group_direct_message(cordelia, [hamlet, acting_user], "Hello!")
+ message = Message.objects.get(id=msg_id)
+
+ with self.capture_send_event_calls(expected_num_events=1) as events:
+ do_delete_messages(realm, [message], acting_user=acting_user)
+
+ self.assert_length(events, 1)
+ event = events[0]["event"]
+ self.assertIn("type", event)
+ self.assertEqual(event["type"], "delete_message")
+ self.assertIn(msg_id, event["message_ids"])
+ self.assertCountEqual([acting_user.id, hamlet.id, cordelia.id], events[0]["users"])
def test_do_delete_stream_messages_without_acting_user(self) -> None:
realm = get_realm("zulip")
@@ -67,6 +80,166 @@ class DeleteMessageTest(ZulipTestCase):
self.assertEqual(events[1]["event"]["type"], "delete_message")
self.assertIn(msg_id, events[1]["event"]["message_ids"])
+ def test_do_delete_stream_messages_with_acting_user(self) -> None:
+ realm = get_realm("zulip")
+ cordelia = self.example_user("cordelia")
+ iago = self.example_user("iago")
+ hamlet = self.example_user("hamlet")
+
+ # For public stream, acting_user will receive the event even if
+ # they are not subscribed and do not have UserMessage row.
+ stream_name = "public_stream"
+ stream = self.make_stream(stream_name)
+ self.subscribe(cordelia, stream_name)
+ msg_id = self.send_stream_message(cordelia, stream_name, "Hello!")
+ message = Message.objects.get(id=msg_id)
+
+ with self.capture_send_event_calls(expected_num_events=2) as events:
+ do_delete_messages(realm, [message], acting_user=hamlet)
+
+ self.assertEqual(events[1]["event"]["type"], "delete_message")
+ self.assertIn(msg_id, events[1]["event"]["message_ids"])
+ self.assertCountEqual([cordelia.id, hamlet.id], events[1]["users"])
+
+ # For a private stream with public history, acting_user will receive
+ # the event if they have content access to the stream even when they
+ # were not subscribed to the stream when message was sent and do not
+ # have a UserMessage row for the message.
+ stream_name = "private_stream"
+ stream = self.make_stream(stream_name, invite_only=True, history_public_to_subscribers=True)
+ self.subscribe(cordelia, stream_name)
+ msg_id = self.send_stream_message(cordelia, stream_name, "Hello!")
+ message = Message.objects.get(id=msg_id)
+
+ # Acting user does not receive the event since they do not have
+ # content access to the stream.
+ with self.capture_send_event_calls(expected_num_events=2) as events:
+ do_delete_messages(realm, [message], acting_user=iago)
+
+ self.assertEqual(events[1]["event"]["type"], "delete_message")
+ self.assertIn(msg_id, events[1]["event"]["message_ids"])
+ self.assertCountEqual([cordelia.id], events[1]["users"])
+
+ admins_group = NamedUserGroup.objects.get(
+ name=SystemGroups.ADMINISTRATORS, realm_for_sharding=realm, is_system_group=True
+ )
+ do_change_stream_group_based_setting(
+ stream, "can_subscribe_group", admins_group, acting_user=iago
+ )
+
+ msg_id = self.send_stream_message(cordelia, stream_name, "Hello!")
+ message = Message.objects.get(id=msg_id)
+
+ # Acting user receives the event since they have content access
+ # to the stream.
+ with self.capture_send_event_calls(expected_num_events=2) as events:
+ do_delete_messages(realm, [message], acting_user=iago)
+
+ self.assertEqual(events[1]["event"]["type"], "delete_message")
+ self.assertIn(msg_id, events[1]["event"]["message_ids"])
+ self.assertCountEqual([cordelia.id, iago.id], events[1]["users"])
+
+ # For a private stream with protected history, acting_user will receive
+ # the event only if they have UserMessage rows for at least one of the
+ # messages being deleted and have content access to the stream.
+ stream_name = "protected_history_stream"
+ stream = self.make_stream(stream_name, invite_only=True)
+ self.subscribe(cordelia, stream_name)
+
+ msg_id = self.send_stream_message(cordelia, stream_name, "Hello!")
+ message = Message.objects.get(id=msg_id)
+
+ # Acting user does not receive the event as they do not have UserMessage
+ # row for any of the messages being deleted and they do not have content
+ # access to the stream.
+ with self.capture_send_event_calls(expected_num_events=2) as events:
+ do_delete_messages(realm, [message], acting_user=iago)
+
+ self.assertEqual(events[1]["event"]["type"], "delete_message")
+ self.assertIn(msg_id, events[1]["event"]["message_ids"])
+ self.assertCountEqual([cordelia.id], events[1]["users"])
+
+ first_msg_id = self.send_stream_message(cordelia, stream_name, "Hello!")
+ self.subscribe(iago, stream_name)
+ second_msg_id = self.send_stream_message(cordelia, stream_name, "Hello again!")
+ self.unsubscribe(iago, stream_name)
+ messages = Message.objects.filter(id__in=[first_msg_id, second_msg_id])
+
+ # Acting user does not receive the event as they do not have conte access
+ # to the stream even if they have UserMessage row for at least one of the
+ # messages being deleted.
+ with self.capture_send_event_calls(expected_num_events=2) as events:
+ do_delete_messages(realm, messages, acting_user=iago)
+ self.assertEqual(events[1]["event"]["type"], "delete_message")
+ self.assertCountEqual([first_msg_id, second_msg_id], events[1]["event"]["message_ids"])
+ self.assertCountEqual([cordelia.id], events[1]["users"])
+
+ do_change_stream_group_based_setting(
+ stream, "can_subscribe_group", admins_group, acting_user=iago
+ )
+
+ first_msg_id = self.send_stream_message(cordelia, stream_name, "Hello!")
+ second_msg_id = self.send_stream_message(cordelia, stream_name, "Hello again!")
+ messages = Message.objects.filter(id__in=[first_msg_id, second_msg_id])
+
+ # Acting user does not receive the event as they do not have
+ # UserMessage row for any of the messages being deleted even
+ # when they have content access to the stream.
+ with self.capture_send_event_calls(expected_num_events=2) as events:
+ do_delete_messages(realm, messages, acting_user=iago)
+ self.assertEqual(events[1]["event"]["type"], "delete_message")
+ self.assertCountEqual([first_msg_id, second_msg_id], events[1]["event"]["message_ids"])
+ self.assertCountEqual([cordelia.id], events[1]["users"])
+
+ first_msg_id = self.send_stream_message(cordelia, stream_name, "Hello!")
+ self.subscribe(iago, stream_name)
+ second_msg_id = self.send_stream_message(cordelia, stream_name, "Hello again!")
+ self.unsubscribe(iago, stream_name)
+ messages = Message.objects.filter(id__in=[first_msg_id, second_msg_id])
+
+ # Acting user receives the event as they are subscribed to the stream
+ # and also have UserMessage row for at least one of the messages being
+ # deleted and the event includes messages with only UserMessage rows.
+ with self.capture_send_event_calls(expected_num_events=3) as events:
+ do_delete_messages(realm, messages, acting_user=iago)
+
+ self.assertEqual(events[1]["event"]["type"], "delete_message")
+ self.assertCountEqual([first_msg_id, second_msg_id], events[1]["event"]["message_ids"])
+ self.assertCountEqual([cordelia.id], events[1]["users"])
+ self.assertEqual(events[2]["event"]["type"], "delete_message")
+ self.assertCountEqual([second_msg_id], events[2]["event"]["message_ids"])
+ self.assertCountEqual([iago.id], events[2]["users"])
+
+ first_msg_id = self.send_stream_message(cordelia, stream_name, "Hello!")
+ self.subscribe(iago, stream_name)
+ second_msg_id = self.send_stream_message(cordelia, stream_name, "Hello again!")
+ messages = Message.objects.filter(id__in=[first_msg_id, second_msg_id])
+
+ with self.capture_send_event_calls(expected_num_events=3) as events:
+ do_delete_messages(realm, messages, acting_user=iago)
+
+ self.assertEqual(events[1]["event"]["type"], "delete_message")
+ self.assertCountEqual([first_msg_id, second_msg_id], events[1]["event"]["message_ids"])
+ self.assertCountEqual([cordelia.id], events[1]["users"])
+ self.assertEqual(events[2]["event"]["type"], "delete_message")
+ self.assertCountEqual([second_msg_id], events[2]["event"]["message_ids"])
+ self.assertCountEqual([iago.id], events[2]["users"])
+
+ # If the acting_user has UserMessage row for all the messages being
+ # deleted in a protected history stream, then we send the same event
+ # to all the users with access.
+ self.subscribe(iago, stream_name)
+ first_msg_id = self.send_stream_message(cordelia, stream_name, "Hello!")
+ second_msg_id = self.send_stream_message(cordelia, stream_name, "Hello again!")
+ self.unsubscribe(iago, stream_name)
+ messages = Message.objects.filter(id__in=[first_msg_id, second_msg_id])
+ with self.capture_send_event_calls(expected_num_events=2) as events:
+ do_delete_messages(realm, messages, acting_user=iago)
+
+ self.assertEqual(events[1]["event"]["type"], "delete_message")
+ self.assertCountEqual([first_msg_id, second_msg_id], events[1]["event"]["message_ids"])
+ self.assertCountEqual([cordelia.id, iago.id], events[1]["users"])
+
def test_do_delete_messages_grouping_logic(self) -> None:
realm = get_realm("zulip")
cordelia = self.example_user("cordelia")