Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
subdomains: Extend "static" to include resources hosted on S3.
Alex Vandiver721546dfc0b1
diff --git a/zerver/lib/subdomains.py b/zerver/lib/subdomains.pyindex 2c3085753ff..ab00b40c563 100644--- a/zerver/lib/subdomains.py+++ b/zerver/lib/subdomains.py@@ -5,6 +5,7 @@ from typing import Optionalfrom django.conf import settingsfrom django.http import HttpRequest+from zerver.lib.upload import get_public_upload_root_urlfrom zerver.models import Realm, UserProfile@@ -76,4 +77,13 @@ def is_static_or_current_realm_url(url: str, realm: Realm) -> bool:if split_url.netloc == "" and split_url.scheme == "":return True+ # S3 storage we control, if used, is also static and thus exempt+ if settings.LOCAL_UPLOADS_DIR is None:+ # The startswith check is correct here because the public+ # upload base URL is guaranteed to end with /.+ public_upload_root_url = get_public_upload_root_url()+ assert public_upload_root_url.endswith("/")+ if url.startswith(public_upload_root_url):+ return True+return Falsediff --git a/zerver/lib/upload.py b/zerver/lib/upload.pyindex 66469a57ce5..0d1d9ff8ce9 100644--- a/zerver/lib/upload.py+++ b/zerver/lib/upload.py@@ -216,6 +216,9 @@ def resize_emoji(image_data: bytes, size: int = DEFAULT_EMOJI_SIZE) -> bytes:class ZulipUploadBackend:+ def get_public_upload_root_url(self) -> str:+ raise NotImplementedError()+def upload_message_file(self,uploaded_file_name: str,@@ -425,6 +428,14 @@ class S3UploadBackend(ZulipUploadBackend):key.delete()return True+ def get_public_upload_root_url(self) -> str:+ # boto requires a key name, so we can't just pass "" here;+ # trim the offending character back off from the URL we get+ # back.+ u = urllib.parse.urlsplit(self.get_public_upload_url("a"))+ assert u.path.endswith("/a")+ return urllib.parse.urlunsplit((u.scheme, u.netloc, u.path[:-1], "", ""))+def upload_message_file(self,uploaded_file_name: str,@@ -749,6 +760,9 @@ def get_local_file_path_id_from_token(token: str) -> Optional[str]:class LocalUploadBackend(ZulipUploadBackend):+ def get_public_upload_root_url(self) -> str:+ return "/user_avatars/"+def upload_message_file(self,uploaded_file_name: str,@@ -930,6 +944,10 @@ else:upload_backend = S3UploadBackend() # nocoverage+def get_public_upload_root_url() -> str:+ return upload_backend.get_public_upload_root_url()++def delete_message_image(path_id: str) -> bool:return upload_backend.delete_message_image(path_id)diff --git a/zerver/tests/test_subdomains.py b/zerver/tests/test_subdomains.pyindex 6f1b2c14aec..33549a1167a 100644--- a/zerver/tests/test_subdomains.py+++ b/zerver/tests/test_subdomains.py@@ -3,8 +3,10 @@ from unittest import mockfrom django.conf import settings+import zerver.lib.uploadfrom zerver.lib.subdomains import get_subdomain, is_static_or_current_realm_urlfrom zerver.lib.test_classes import ZulipTestCase+from zerver.lib.test_helpers import create_s3_buckets, use_s3_backendfrom zerver.models import Realm@@ -82,3 +84,18 @@ class SubdomainsTest(ZulipTestCase):self.assertTrue(test(f"{settings.STATIC_URL}/x"))self.assertFalse(test(evil_url))self.assertFalse(test(f"{evil_url}/x"))++ @use_s3_backend+ def test_is_static_or_current_realm_url_with_s3(self) -> None:+ create_s3_buckets(settings.S3_AVATAR_BUCKET)[0]++ realm = self.example_user("hamlet").realm++ def test(url: str) -> bool:+ return is_static_or_current_realm_url(url, realm)++ upload_backend = zerver.lib.upload.upload_backend+ self.assertTrue(test(upload_backend.get_realm_icon_url(realm.id, version=1)))+ self.assertTrue(test(upload_backend.get_realm_logo_url(realm.id, version=1, night=False)))+ self.assertTrue(test(upload_backend.get_avatar_url("deadbeefcafe")))+ self.assertTrue(test(upload_backend.get_emoji_url("emoji.gif", realm.id)))diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.pyindex cf4103673d1..dddc95df0bd 100644--- a/zerver/tests/test_upload.py+++ b/zerver/tests/test_upload.py@@ -912,6 +912,7 @@ class AvatarTest(UploadSerializeMixin, ZulipTestCase):def test_avatar_url(self) -> None:"""Verifies URL schemes for avatars and realm icons."""backend: ZulipUploadBackend = LocalUploadBackend()+ self.assertEqual(backend.get_public_upload_root_url(), "/user_avatars/")self.assertEqual(backend.get_avatar_url("hash", False), "/user_avatars/hash.png?x=x")self.assertEqual(backend.get_avatar_url("hash", True), "/user_avatars/hash-medium.png?x=x")self.assertEqual(