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.

CVE-2026-25742: Anonymous file access after disabling spectator access.

Sahil Batrafd85f45c2d9a
diff --git a/zerver/actions/realm_settings.py b/zerver/actions/realm_settings.py
index 4fcd83db382..acdad67aece 100644
--- a/zerver/actions/realm_settings.py
+++ b/zerver/actions/realm_settings.py
@@ -96,6 +96,12 @@ def do_set_realm_property(
else:
realm.save(update_fields=[name])
+ if name == "enable_spectator_access":
+ Attachment.objects.filter(realm=realm).update(is_web_public=None)
+ # We need to do the same for ArchivedAttachment to avoid
+ # bugs if deleted attachments are later restored.
+ ArchivedAttachment.objects.filter(realm=realm).update(is_web_public=None)
+
event = dict(
type="realm",
op="update",
diff --git a/zerver/actions/uploads.py b/zerver/actions/uploads.py
index f95a5d0ee67..59a9cfab90d 100644
--- a/zerver/actions/uploads.py
+++ b/zerver/actions/uploads.py
@@ -54,7 +54,9 @@ def do_claim_attachments(
if is_channel_message:
stream = Stream.objects.get(id=message.recipient.type_id)
is_message_realm_public = stream.is_public()
- is_message_web_public = stream.is_web_public
+ is_message_web_public = (
+ user_profile.realm.web_public_streams_enabled() and stream.is_web_public
+ )
if not validate_attachment_request(user_profile, path_id)[0]:
# Technically, there are 2 cases here:
diff --git a/zerver/lib/attachments.py b/zerver/lib/attachments.py
index da9a0c295fc..c86ab2bd479 100644
--- a/zerver/lib/attachments.py
+++ b/zerver/lib/attachments.py
@@ -58,6 +58,9 @@ def validate_attachment_request_for_spectator_access(realm: Realm, attachment: A
if attachment.realm != realm:
return False
+ if not realm.web_public_streams_enabled():
+ return False
+
# Update cached is_web_public property, if necessary.
if attachment.is_web_public is None:
# Fill the cache in a single query. This is important to avoid
diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py
index cffa44b41cc..4ab2f348bf9 100644
--- a/zerver/tests/test_realm.py
+++ b/zerver/tests/test_realm.py
@@ -6,6 +6,7 @@ import re
import string
from datetime import datetime, timedelta
from enum import Enum
+from io import StringIO
from pathlib import Path
from typing import Any
from unittest import mock, skipUnless
@@ -3059,6 +3060,32 @@ class RealmAPITest(ZulipTestCase):
result = self.client_patch("/json/realm", req)
self.assert_json_error(result, "Available on Zulip Cloud Standard. Upgrade to access.")
+ def test_attachment_when_updating_enable_spectator_access(self) -> None:
+ self.login("desdemona")
+ fp = StringIO("zulip!")
+ fp.name = "zulip.txt"
+
+ result = self.client_post("/json/user_uploads", {"file": fp})
+ url = self.assert_json_success(result)["url"]
+
+ owner = self.example_user("desdemona")
+ realm = owner.realm
+ self.make_stream("test_stream", realm=realm, is_web_public=True)
+ self.subscribe(owner, "test_stream")
+ body = f"First message ...[zulip.txt](http://{realm.host}" + url + ")"
+ msg_id = self.send_stream_message(owner, "test_stream", body, "test")
+ attachment = Attachment.objects.get(messages__id=msg_id)
+
+ self.assertTrue(realm.enable_spectator_access)
+ self.assertTrue(attachment.is_web_public)
+
+ result = self.client_patch(
+ "/json/realm", {"enable_spectator_access": orjson.dumps(False).decode()}
+ )
+ self.assert_json_success(result)
+ attachment.refresh_from_db()
+ self.assertIsNone(attachment.is_web_public)
+
def test_can_create_groups_limited_plan_realms(self) -> None:
self.login("iago")
realm = get_realm("zulip")
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index 5f6133d97b1..9fc4fb3d9f9 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -1087,6 +1087,46 @@ class StreamAdminTest(ZulipTestCase):
self.assertEqual(stream.history_public_to_subscribers, False)
self.assertEqual(stream.can_create_topic_group.id, everyone_system_group.id)
+ def test_attachment_in_web_public_stream(self) -> None:
+ self.login("desdemona")
+ fp = StringIO("zulip!")
+ fp.name = "zulip.txt"
+
+ result = self.client_post("/json/user_uploads", {"file": fp})
+ url = self.assert_json_success(result)["url"]
+
+ owner = self.example_user("desdemona")
+ realm = owner.realm
+ self.make_stream("test_stream", realm=realm, is_web_public=True)
+ self.subscribe(owner, "test_stream")
+ body = f"First message ...[zulip.txt](http://{realm.host}" + url + ")"
+ msg_id = self.send_stream_message(owner, "test_stream", body, "test")
+ attachment = Attachment.objects.get(messages__id=msg_id)
+ self.assertTrue(attachment.is_web_public)
+
+ self.assertTrue(validate_attachment_request_for_spectator_access(realm, attachment))
+
+ do_set_realm_property(realm, "enable_spectator_access", False, acting_user=None)
+ attachment = Attachment.objects.get(messages__id=msg_id)
+ self.assertIsNone(attachment.is_web_public)
+
+ self.assertFalse(validate_attachment_request_for_spectator_access(realm, attachment))
+ attachment = Attachment.objects.get(messages__id=msg_id)
+ self.assertIsNone(attachment.is_web_public)
+
+ # Check that is_web_public is set as False when uploading a file in
+ # web-public stream with spectator access disabled for realm.
+ fp = StringIO("zulip!")
+ fp.name = "zulip1.txt"
+
+ result = self.client_post("/json/user_uploads", {"file": fp})
+ url = self.assert_json_success(result)["url"]
+
+ body = f"Second message ...[zulip1.txt](http://{realm.host}" + url + ")"
+ msg_id = self.send_stream_message(owner, "test_stream", body, "test")
+ attachment = Attachment.objects.get(messages__id=msg_id)
+ self.assertEqual(attachment.is_web_public, False)
+
def test_stream_permission_changes_updates_updates_attachments(self) -> None:
self.login("desdemona")
fp = StringIO("zulip!")