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 accessing avatar for inaccessible users.

Sahil Batra58461660c32c
diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py
index 9e57fd42d27..9ed3310a01f 100644
--- a/zerver/tests/test_upload.py
+++ b/zerver/tests/test_upload.py
@@ -1112,6 +1112,19 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):
status_code=401,
)
+ self.set_up_db_for_testing_user_access()
+ self.login("polonius")
+
+ response = self.client_get(f"/avatar/{cordelia.id}", {"foo": "bar"})
+ self.assertEqual(302, response.status_code)
+ redirect_url = response["Location"]
+ self.assertTrue(redirect_url.endswith("images/unknown-user-avatar.png?foo=bar"))
+
+ response = self.client_get("/avatar/[email protected]", {"foo": "bar"})
+ self.assertEqual(302, response.status_code)
+ redirect_url = response["Location"]
+ self.assertTrue(redirect_url.endswith("images/unknown-user-avatar.png?foo=bar"))
+
def test_get_user_avatar_medium(self) -> None:
hamlet = self.example_user("hamlet")
self.login_user(hamlet)
diff --git a/zerver/views/users.py b/zerver/views/users.py
index 33066c6ad77..4ef656222c1 100644
--- a/zerver/views/users.py
+++ b/zerver/views/users.py
@@ -34,7 +34,7 @@ from zerver.actions.users import (
from zerver.context_processors import get_valid_realm_from_request
from zerver.decorator import require_member_or_admin, require_realm_admin
from zerver.forms import PASSWORD_TOO_WEAK_ERROR, CreateUserForm
-from zerver.lib.avatar import avatar_url, get_gravatar_url
+from zerver.lib.avatar import avatar_url, get_avatar_for_inaccessible_user, get_gravatar_url
from zerver.lib.bot_config import set_bot_config
from zerver.lib.email_validation import email_allowed_for_realm
from zerver.lib.exceptions import (
@@ -61,6 +61,7 @@ from zerver.lib.users import (
add_service,
check_bot_creation_policy,
check_bot_name_available,
+ check_can_access_user,
check_full_name,
check_short_name,
check_valid_bot_config,
@@ -307,8 +308,16 @@ def avatar(
avatar_user_profile = get_user_by_id_in_realm_including_cross_realm(
int(email_or_id), realm
)
- # If there is a valid user account passed in, use its avatar
- url = avatar_url(avatar_user_profile, medium=medium)
+
+ url: Optional[str] = None
+ if maybe_user_profile.is_authenticated and not check_can_access_user(
+ avatar_user_profile, maybe_user_profile
+ ):
+ url = get_avatar_for_inaccessible_user()
+ else:
+ # If there is a valid user account passed in, use its avatar
+ url = avatar_url(avatar_user_profile, medium=medium)
+ assert url is not None
except UserProfile.DoesNotExist:
# If there is no such user, treat it as a new gravatar
email = email_or_id