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.

typing_notifications: Check that the sender can access recipients.

Mateusz Mandera0cdfe767edca
diff --git a/zerver/actions/typing.py b/zerver/actions/typing.py
index 1556bdee701..cf1b0da2cd9 100644
--- a/zerver/actions/typing.py
+++ b/zerver/actions/typing.py
@@ -14,6 +14,7 @@ from zerver.lib.event_types import (
TypingStopEvent,
)
from zerver.lib.exceptions import JsonableError
+from zerver.lib.recipient_users import check_sender_can_access_recipients
from zerver.lib.stream_subscription import get_active_subscriptions_for_stream_id
from zerver.models import Realm, Stream, UserProfile
from zerver.models.users import get_user_by_id_in_realm_including_cross_realm
@@ -82,6 +83,8 @@ def check_send_typing_notification(
raise JsonableError(_("Invalid user ID {user_id}").format(user_id=user_id))
user_profiles.append(user_profile)
+ check_sender_can_access_recipients(sender, user_profiles)
+
do_send_typing_notification(
realm=realm,
sender=sender,
diff --git a/zerver/tests/test_typing.py b/zerver/tests/test_typing.py
index 214ef4f3222..054ed6e528b 100644
--- a/zerver/tests/test_typing.py
+++ b/zerver/tests/test_typing.py
@@ -444,6 +444,54 @@ class TypingHappyPathTestDirectMessages(ZulipTestCase):
self.assertEqual(event["op"], "stop")
+class TypingRestrictedUserAccessTest(ZulipTestCase):
+ def test_direct_typing_notification_to_inaccessible_user(self) -> None:
+ """
+ Verifies that a user in a realm with restricted user access
+ cannot send direct-message typing notifications to users they cannot
+ access.
+ """
+ self.set_up_db_for_testing_user_access()
+ polonius = self.example_user("polonius")
+ # Polonius shares "test_stream1" with hamlet, but not with othello,
+ # so hamlet is accessible and othello is not.
+ hamlet = self.example_user("hamlet")
+ othello = self.example_user("othello")
+
+ # Sending to an inaccessible user is rejected.
+ with self.capture_send_event_calls(expected_num_events=0):
+ result = self.api_post(
+ polonius,
+ "/api/v1/typing",
+ {"to": orjson.dumps([othello.id]).decode(), "op": "start"},
+ )
+ self.assert_json_error(
+ result, "You do not have permission to access some of the recipients."
+ )
+
+ # A group with any inaccessible recipient is rejected too.
+ with self.capture_send_event_calls(expected_num_events=0):
+ result = self.api_post(
+ polonius,
+ "/api/v1/typing",
+ {"to": orjson.dumps([hamlet.id, othello.id]).decode(), "op": "start"},
+ )
+ self.assert_json_error(
+ result, "You do not have permission to access some of the recipients."
+ )
+
+ # Sending to an accessible user still works.
+ with self.capture_send_event_calls(expected_num_events=1) as events:
+ result = self.api_post(
+ polonius,
+ "/api/v1/typing",
+ {"to": orjson.dumps([hamlet.id]).decode(), "op": "start"},
+ )
+ self.assert_json_success(result)
+ event_recipient_ids = {user["user_id"] for user in events[0]["event"]["recipients"]}
+ self.assertEqual(event_recipient_ids, {polonius.id, hamlet.id})
+
+
class TypingHappyPathTestStreams(ZulipTestCase):
def test_valid_type_and_op_parameters(self) -> None:
recipient_type_name = ["channel", "stream"]