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.

users: Restrict read access to users in access_user_by_id.

Sahil Batraa23eff20fe97
diff --git a/zerver/lib/users.py b/zerver/lib/users.py
index 023b4ffda1c..8fc97eb6729 100644
--- a/zerver/lib/users.py
+++ b/zerver/lib/users.py
@@ -34,6 +34,7 @@ from zerver.models import (
Recipient,
Service,
Subscription,
+ SystemGroups,
UserMessage,
UserProfile,
get_fake_email_domain,
@@ -249,7 +250,11 @@ def access_user_common(
if not target.is_active and not allow_deactivated:
raise JsonableError(_("User is deactivated"))
if not for_admin:
- # Administrative access is not required just to read a user.
+ # Administrative access is not required just to read a user
+ # but we need to check can_access_all_users_group setting.
+ if not check_can_access_user(target, user_profile):
+ raise JsonableError(_("Insufficient permission"))
+
return target
if not user_profile.can_admin_user(target):
raise JsonableError(_("Insufficient permission"))
@@ -510,6 +515,17 @@ def format_user_row(
return result
+def user_access_restricted_in_realm(target_user: UserProfile) -> bool:
+ if target_user.is_bot:
+ return False
+
+ realm = target_user.realm
+ if realm.can_access_all_users_group.name == SystemGroups.EVERYONE:
+ return False
+
+ return True
+
+
def check_user_can_access_all_users(acting_user: Optional[UserProfile]) -> bool:
if acting_user is None:
# We allow spectators to access all users since they
@@ -526,6 +542,51 @@ def check_user_can_access_all_users(acting_user: Optional[UserProfile]) -> bool:
return False
+def check_can_access_user(
+ target_user: UserProfile, user_profile: Optional[UserProfile] = None
+) -> bool:
+ if not user_access_restricted_in_realm(target_user):
+ return True
+
+ if check_user_can_access_all_users(user_profile):
+ return True
+
+ assert user_profile is not None
+
+ if target_user.id == user_profile.id:
+ return True
+
+ # These include Subscription objects for streams as well as group DMs.
+ subscribed_recipient_ids = Subscription.objects.filter(
+ user_profile=user_profile,
+ active=True,
+ recipient__type__in=[Recipient.STREAM, Recipient.HUDDLE],
+ ).values_list("recipient_id", flat=True)
+
+ if Subscription.objects.filter(
+ recipient_id__in=subscribed_recipient_ids,
+ user_profile=target_user,
+ active=True,
+ is_user_active=True,
+ ).exists():
+ return True
+
+ assert user_profile.recipient_id is not None
+ assert target_user.recipient_id is not None
+
+ # Querying the "Message" table is expensive so we do this last.
+ direct_message_query = Message.objects.filter(
+ recipient__type=Recipient.PERSONAL, realm=target_user.realm
+ )
+ if direct_message_query.filter(
+ Q(sender_id=target_user.id, recipient_id=user_profile.recipient_id)
+ | Q(recipient_id=target_user.recipient_id, sender_id=user_profile.id)
+ ).exists():
+ return True
+
+ return False
+
+
def get_subscribers_of_target_user_subscriptions(
target_users: List[UserProfile],
) -> Dict[int, Set[int]]:
diff --git a/zerver/tests/test_muted_users.py b/zerver/tests/test_muted_users.py
index 8d41c5ff62d..717bafc5f71 100644
--- a/zerver/tests/test_muted_users.py
+++ b/zerver/tests/test_muted_users.py
@@ -364,3 +364,34 @@ class MutedUsersTests(ZulipTestCase):
# `maybe_enqueue_notifications` wasn't called for Hamlet after message edit which mentioned him,
# because the sender (Cordelia) was muted.
m.assert_not_called()
+
+ def test_muting_and_unmuting_restricted_users(self) -> None:
+ # NOTE: It is a largely unintended side effect of how we use
+ # the access_user_by_id API that limited guests can't mute
+ # inaccessible users. These tests verify the expected
+ # behavior.
+
+ self.set_up_db_for_testing_user_access()
+ polonius = self.example_user("polonius")
+ cordelia = self.example_user("cordelia")
+ iago = self.example_user("iago")
+
+ url = f"/api/v1/users/me/muted_users/{cordelia.id}"
+ result = self.api_post(polonius, url)
+ self.assert_json_error(result, "Insufficient permission")
+
+ url = f"/api/v1/users/me/muted_users/{iago.id}"
+ result = self.api_post(polonius, url)
+ self.assert_json_success(result)
+ muted_users = get_user_mutes(polonius)
+ self.assert_length(muted_users, 1)
+
+ url = f"/api/v1/users/me/muted_users/{cordelia.id}"
+ result = self.api_delete(polonius, url)
+ self.assert_json_error(result, "Insufficient permission")
+
+ url = f"/api/v1/users/me/muted_users/{iago.id}"
+ result = self.api_delete(polonius, url)
+ self.assert_json_success(result)
+ muted_users = get_user_mutes(polonius)
+ self.assert_length(muted_users, 0)
diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py
index da41b43e62f..60fd9ccbddc 100644
--- a/zerver/tests/test_users.py
+++ b/zerver/tests/test_users.py
@@ -1437,6 +1437,17 @@ class UserProfileTest(ZulipTestCase):
)
self.assertTrue(result["is_subscribed"])
+ # Test case when guest cannot access all users in the realm.
+ self.set_up_db_for_testing_user_access()
+ cordelia = self.example_user("cordelia")
+ result = self.client_get(f"/json/users/{cordelia.id}/subscriptions/{stream.id}")
+ self.assert_json_error(result, "Insufficient permission")
+
+ result = orjson.loads(
+ self.client_get(f"/json/users/{iago.id}/subscriptions/{stream.id}").content
+ )
+ self.assertTrue(result["is_subscribed"])
+
self.login("iago")
stream = self.make_stream("private_stream", invite_only=True)
# Unsubscribed admin can check subscription status in a private stream.
@@ -2581,6 +2592,45 @@ class GetProfileTest(ZulipTestCase):
inaccessible_user_ids, [cordelia.id, desdemona.id, othello.id, hamlet.id]
)
+ def test_get_user_with_restricted_access(self) -> None:
+ polonius = self.example_user("polonius")
+ hamlet = self.example_user("hamlet")
+ othello = self.example_user("othello")
+ iago = self.example_user("iago")
+ prospero = self.example_user("prospero")
+ aaron = self.example_user("aaron")
+ zoe = self.example_user("ZOE")
+ shiva = self.example_user("shiva")
+ cordelia = self.example_user("cordelia")
+ desdemona = self.example_user("desdemona")
+ reset_email_visibility_to_everyone_in_zulip_realm()
+
+ self.set_up_db_for_testing_user_access()
+
+ self.login("polonius")
+
+ accessible_users = [polonius, iago, shiva, hamlet, aaron, zoe, shiva, prospero]
+ for user in accessible_users:
+ result = self.client_get(f"/json/users/{user.id}")
+ self.assert_json_success(result)
+ user_dict = orjson.loads(result.content)["user"]
+ self.assertEqual(user_dict["user_id"], user.id)
+ self.assertEqual(user_dict["full_name"], user.full_name)
+ self.assertEqual(user_dict["delivery_email"], user.delivery_email)
+
+ # Guest user cannot access -
+ # 1. othello, even though he sent a message to test_stream1
+ # because he is not subscribed to the stream now.
+ # 2. cordelia, because the guest user is not subscribed
+ # to Verona anymore to which cordelia is subscribed.
+ # 3. desdemona, because she is not subscribed to any
+ # streams that the guest is subscribed to and is not
+ # involved in any DMs with guest.
+ inaccessible_users = [othello, cordelia, desdemona]
+ for user in inaccessible_users:
+ result = self.client_get(f"/json/users/{user.id}")
+ self.assert_json_error(result, "Insufficient permission")
+
class DeleteUserTest(ZulipTestCase):
def test_do_delete_user(self) -> None:
diff --git a/zerver/views/muted_users.py b/zerver/views/muted_users.py
index c0847f9956f..42e7b97ffde 100644
--- a/zerver/views/muted_users.py
+++ b/zerver/views/muted_users.py
@@ -15,6 +15,14 @@ def mute_user(request: HttpRequest, user_profile: UserProfile, muted_user_id: in
if user_profile.id == muted_user_id:
raise JsonableError(_("Cannot mute self"))
+ # Arguably, access_used_by_id is not quite the right check; in the
+ # corner case of a limited guest trying to mute a deactivated user
+ # who they no longer have access to because the user was
+ # deactivated... it might from a policy perspective be OK to allow
+ # that operation even though this API will reject it.
+ #
+ # But it's quite possibly something nobody will try to do, so we
+ # just reuse the existing shared code path.
muted_user = access_user_by_id(
user_profile, muted_user_id, allow_bots=True, allow_deactivated=True, for_admin=False
)