Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Rewrite all external images to use Camo.
Wesley Aptekar-Cassels6b7a3fb74acb
diff --git a/docs/production/security-model.md b/docs/production/security-model.mdindex 8ddeb4432df..14db85feaad 100644--- a/docs/production/security-model.md+++ b/docs/production/security-model.md@@ -235,9 +235,10 @@ strength allowed is controlled by two settings inbrowser is logged into a Zulip account that has received theuploaded file in question).-* Zulip supports using the Camo image proxy to proxy content, like- inline image previews, that can be inserted into the Zulip message- feed by other users over HTTPS.+* Zulip supports using the Camo image proxy to proxy content like+ inline image previews, that can be inserted into the Zulip message feed by+ other users. This ensures that clients do not make requests to external+ servers to fetch images, improving privacy.* By default, Zulip will provide image previews inline in the body ofmessages when a message contains a link to an image. You candiff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex 9d3760894b8..b94e71587ea 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -59,6 +59,7 @@ from zerver.lib.exceptions import MarkdownRenderingExceptionfrom zerver.lib.markdown import fenced_codefrom zerver.lib.markdown.fenced_code import FENCE_REfrom zerver.lib.mention import extract_user_group, possible_mentions, possible_user_group_mentions+from zerver.lib.subdomains import is_static_or_current_realm_urlfrom zerver.lib.tex import render_texfrom zerver.lib.thumbnail import user_uploads_or_externalfrom zerver.lib.timeout import TimeoutExpired, timeout@@ -574,15 +575,27 @@ def get_tweet_id(url: str) -> Optional[str]:return tweet_id_match.group("tweetid")-class InlineHttpsProcessor(markdown.treeprocessors.Treeprocessor):+class InlineImageProcessor(markdown.treeprocessors.Treeprocessor):+ """+ Rewrite inline img tags to serve external content via Camo.++ This rewrites all images, except ones that are served from the current+ realm or global STATIC_URL. This is to ensure that each realm only loads+ images that are hosted on that realm or by the global installation,+ avoiding information leakage to external domains or between realms. We need+ to disable proxying of images hosted on the same realm, because otherwise+ we will break images in /user_uploads/, which require authorization to+ view.+ """+def run(self, root: Element) -> None:# Get all URLs from the blobfound_imgs = walk_tree(root, lambda e: e if e.tag == "img" else None)for img in found_imgs:url = img.get("src")assert url is not None- if urllib.parse.urlsplit(url).scheme != "http":- # Don't rewrite images on our own site (e.g. emoji).+ if is_static_or_current_realm_url(url, self.md.zulip_realm):+ # Don't rewrite images on our own site (e.g. emoji, user uploads).continueimg.set("src", get_camo_url(url))@@ -2268,7 +2281,7 @@ class Markdown(markdown.Markdown):InlineInterestingLinkProcessor(self), "inline_interesting_links", 15)if settings.CAMO_URI:- treeprocessors.register(InlineHttpsProcessor(self), "rewrite_to_https", 10)+ treeprocessors.register(InlineImageProcessor(self), "rewrite_images_proxy", 10)return treeprocessorsdef build_postprocessors(self) -> markdown.util.Registry:@@ -2289,7 +2302,7 @@ class Markdown(markdown.Markdown):# not removedself.inlinePatterns = get_sub_registry(self.inlinePatterns, ["autolink"])self.treeprocessors = get_sub_registry(- self.treeprocessors, ["inline_interesting_links", "rewrite_to_https"]+ self.treeprocessors, ["inline_interesting_links", "rewrite_images_proxy"])# insert new 'inline' processor because we have changed self.inlinePatterns# but InlineProcessor copies md as self.md in __init__.diff --git a/zerver/lib/subdomains.py b/zerver/lib/subdomains.pyindex e8a42f76391..2c3085753ff 100644--- a/zerver/lib/subdomains.py+++ b/zerver/lib/subdomains.py@@ -1,4 +1,5 @@import re+import urllibfrom typing import Optionalfrom django.conf import settings@@ -54,3 +55,25 @@ def is_root_domain_available() -> bool:if settings.ROOT_DOMAIN_LANDING_PAGE:return Falsereturn not Realm.objects.filter(string_id=Realm.SUBDOMAIN_FOR_ROOT_DOMAIN).exists()+++def is_static_or_current_realm_url(url: str, realm: Realm) -> bool:+ split_url = urllib.parse.urlsplit(url)+ split_static_url = urllib.parse.urlsplit(settings.STATIC_URL)++ # The netloc check here is important to correctness if STATIC_URL+ # does not contain a `/`; see the tests for why.+ if split_url.netloc == split_static_url.netloc and url.startswith(settings.STATIC_URL):+ return True++ # HTTPS access to this Zulip organization's domain; our existing+ # HTTPS protects this request, and there's no privacy benefit to+ # using camo in front of the Zulip server itself.+ if split_url.netloc == realm.host and f"{split_url.scheme}://" == settings.EXTERNAL_URI_SCHEME:+ return True++ # Relative URLs will be processed by the browser the same way as the above.+ if split_url.netloc == "" and split_url.scheme == "":+ return True++ return Falsediff --git a/zerver/tests/test_link_embed.py b/zerver/tests/test_link_embed.pyindex c1159346e4c..d520fae3d84 100644--- a/zerver/tests/test_link_embed.py+++ b/zerver/tests/test_link_embed.py@@ -8,6 +8,7 @@ from requests.exceptions import ConnectionErrorfrom zerver.lib.actions import queue_json_publishfrom zerver.lib.cache import NotFoundInCache, cache_set, preview_url_cache_key+from zerver.lib.camo import get_camo_urlfrom zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.test_helpers import MockPythonResponse, mock_queue_publishfrom zerver.lib.url_preview.oembed import get_oembed_data, strip_cdata@@ -855,7 +856,7 @@ class PreviewTestCase(ZulipTestCase):)msg.refresh_from_db()- expected_content = '<p><a href="https://www.youtube.com/watch?v=eSJTXC7Ixgg">YouTube - Clearer Code at Scale - Static Types at Zulip and Dropbox</a></p>\n<div class="youtube-video message_inline_image"><a data-id="eSJTXC7Ixgg" href="https://www.youtube.com/watch?v=eSJTXC7Ixgg"><img src="https://i.ytimg.com/vi/eSJTXC7Ixgg/default.jpg"></a></div>'+ expected_content = f"""<p><a href="https://www.youtube.com/watch?v=eSJTXC7Ixgg">YouTube - Clearer Code at Scale - Static Types at Zulip and Dropbox</a></p>\n<div class="youtube-video message_inline_image"><a data-id="eSJTXC7Ixgg" href="https://www.youtube.com/watch?v=eSJTXC7Ixgg"><img src="{get_camo_url("https://i.ytimg.com/vi/eSJTXC7Ixgg/default.jpg")}"></a></div>"""self.assertEqual(expected_content, msg.rendered_content)@override_settings(INLINE_URL_EMBED_PREVIEW=True)@@ -892,5 +893,5 @@ class PreviewTestCase(ZulipTestCase):)msg.refresh_from_db()- expected_content = '<p><a href="https://www.youtube.com/watch?v=eSJTXC7Ixgg">YouTube link</a></p>\n<div class="youtube-video message_inline_image"><a data-id="eSJTXC7Ixgg" href="https://www.youtube.com/watch?v=eSJTXC7Ixgg"><img src="https://i.ytimg.com/vi/eSJTXC7Ixgg/default.jpg"></a></div>'+ expected_content = f"""<p><a href="https://www.youtube.com/watch?v=eSJTXC7Ixgg">YouTube link</a></p>\n<div class="youtube-video message_inline_image"><a data-id="eSJTXC7Ixgg" href="https://www.youtube.com/watch?v=eSJTXC7Ixgg"><img src="{get_camo_url("https://i.ytimg.com/vi/eSJTXC7Ixgg/default.jpg")}"></a></div>"""self.assertEqual(expected_content, msg.rendered_content)diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.pyindex 2940e1d4e22..bc1517c137b 100644--- a/zerver/tests/test_markdown.py+++ b/zerver/tests/test_markdown.py@@ -19,6 +19,7 @@ from zerver.lib.actions import (do_set_user_display_setting,)from zerver.lib.alert_words import get_alert_word_automaton+from zerver.lib.camo import get_camo_urlfrom zerver.lib.create_user import create_userfrom zerver.lib.emoji import get_emoji_urlfrom zerver.lib.exceptions import MarkdownRenderingException@@ -492,7 +493,7 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(converted,- '<p>Check out the debate: <a href="http://www.youtube.com/watch?v=hx1mjT73xYE">http://www.youtube.com/watch?v=hx1mjT73xYE</a></p>\n<div class="youtube-video message_inline_image"><a data-id="hx1mjT73xYE" href="http://www.youtube.com/watch?v=hx1mjT73xYE"><img src="https://i.ytimg.com/vi/hx1mjT73xYE/default.jpg"></a></div>',+ f"""<p>Check out the debate: <a href="http://www.youtube.com/watch?v=hx1mjT73xYE">http://www.youtube.com/watch?v=hx1mjT73xYE</a></p>\n<div class="youtube-video message_inline_image"><a data-id="hx1mjT73xYE" href="http://www.youtube.com/watch?v=hx1mjT73xYE"><img src="{get_camo_url("https://i.ytimg.com/vi/hx1mjT73xYE/default.jpg")}"></a></div>""",)msg = "http://www.youtube.com/watch?v=hx1mjT73xYE"@@ -500,7 +501,7 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(converted,- '<p><a href="http://www.youtube.com/watch?v=hx1mjT73xYE">http://www.youtube.com/watch?v=hx1mjT73xYE</a></p>\n<div class="youtube-video message_inline_image"><a data-id="hx1mjT73xYE" href="http://www.youtube.com/watch?v=hx1mjT73xYE"><img src="https://i.ytimg.com/vi/hx1mjT73xYE/default.jpg"></a></div>',+ f"""<p><a href="http://www.youtube.com/watch?v=hx1mjT73xYE">http://www.youtube.com/watch?v=hx1mjT73xYE</a></p>\n<div class="youtube-video message_inline_image"><a data-id="hx1mjT73xYE" href="http://www.youtube.com/watch?v=hx1mjT73xYE"><img src="{get_camo_url("https://i.ytimg.com/vi/hx1mjT73xYE/default.jpg")}"></a></div>""",)msg = "https://youtu.be/hx1mjT73xYE"@@ -508,7 +509,7 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(converted,- '<p><a href="https://youtu.be/hx1mjT73xYE">https://youtu.be/hx1mjT73xYE</a></p>\n<div class="youtube-video message_inline_image"><a data-id="hx1mjT73xYE" href="https://youtu.be/hx1mjT73xYE"><img src="https://i.ytimg.com/vi/hx1mjT73xYE/default.jpg"></a></div>',+ f"""<p><a href="https://youtu.be/hx1mjT73xYE">https://youtu.be/hx1mjT73xYE</a></p>\n<div class="youtube-video message_inline_image"><a data-id="hx1mjT73xYE" href="https://youtu.be/hx1mjT73xYE"><img src="{get_camo_url("https://i.ytimg.com/vi/hx1mjT73xYE/default.jpg")}"></a></div>""",)msg = "https://www.youtube.com/playlist?list=PL8dPuuaLjXtNlUrzyH5r6jN9ulIgZBpdo"@@ -526,7 +527,7 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(converted,- '<p><a href="https://www.youtube.com/playlist?v=O5nskjZ_GoI&list=PL8dPuuaLjXtNlUrzyH5r6jN9ulIgZBpdo">https://www.youtube.com/playlist?v=O5nskjZ_GoI&list=PL8dPuuaLjXtNlUrzyH5r6jN9ulIgZBpdo</a></p>\n<div class="youtube-video message_inline_image"><a data-id="O5nskjZ_GoI" href="https://www.youtube.com/playlist?v=O5nskjZ_GoI&list=PL8dPuuaLjXtNlUrzyH5r6jN9ulIgZBpdo"><img src="https://i.ytimg.com/vi/O5nskjZ_GoI/default.jpg"></a></div>',+ f"""<p><a href="https://www.youtube.com/playlist?v=O5nskjZ_GoI&list=PL8dPuuaLjXtNlUrzyH5r6jN9ulIgZBpdo">https://www.youtube.com/playlist?v=O5nskjZ_GoI&list=PL8dPuuaLjXtNlUrzyH5r6jN9ulIgZBpdo</a></p>\n<div class="youtube-video message_inline_image"><a data-id="O5nskjZ_GoI" href="https://www.youtube.com/playlist?v=O5nskjZ_GoI&list=PL8dPuuaLjXtNlUrzyH5r6jN9ulIgZBpdo"><img src="{get_camo_url("https://i.ytimg.com/vi/O5nskjZ_GoI/default.jpg")}"></a></div>""",)msg = "http://www.youtube.com/watch_videos?video_ids=nOJgD4fcZhI,i96UO8-GFvw"@@ -534,7 +535,7 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(converted,- '<p><a href="http://www.youtube.com/watch_videos?video_ids=nOJgD4fcZhI,i96UO8-GFvw">http://www.youtube.com/watch_videos?video_ids=nOJgD4fcZhI,i96UO8-GFvw</a></p>\n<div class="youtube-video message_inline_image"><a data-id="nOJgD4fcZhI" href="http://www.youtube.com/watch_videos?video_ids=nOJgD4fcZhI,i96UO8-GFvw"><img src="https://i.ytimg.com/vi/nOJgD4fcZhI/default.jpg"></a></div>',+ f"""<p><a href="http://www.youtube.com/watch_videos?video_ids=nOJgD4fcZhI,i96UO8-GFvw">http://www.youtube.com/watch_videos?video_ids=nOJgD4fcZhI,i96UO8-GFvw</a></p>\n<div class="youtube-video message_inline_image"><a data-id="nOJgD4fcZhI" href="http://www.youtube.com/watch_videos?video_ids=nOJgD4fcZhI,i96UO8-GFvw"><img src="{get_camo_url("https://i.ytimg.com/vi/nOJgD4fcZhI/default.jpg")}"></a></div>""",)@override_settings(INLINE_URL_EMBED_PREVIEW=False)@@ -576,7 +577,7 @@ class MarkdownTest(ZulipTestCase):self.assertIn(thumbnail_img, converted)msg = "https://www.google.com/images/srpr/logo4w.png"- thumbnail_img = '<div class="message_inline_image"><a href="https://www.google.com/images/srpr/logo4w.png"><img src="https://www.google.com/images/srpr/logo4w.png"></a></div>'+ thumbnail_img = f"""<div class="message_inline_image"><a href="https://www.google.com/images/srpr/logo4w.png"><img src="{get_camo_url("https://www.google.com/images/srpr/logo4w.png")}"></a></div>"""with self.settings(THUMBNAIL_IMAGES=False):converted = markdown_convert_wrapper(msg)self.assertIn(thumbnail_img, converted)@@ -615,6 +616,36 @@ class MarkdownTest(ZulipTestCase):converted = render_markdown(msg, content)self.assertEqual(converted, without_preview)+ @override_settings(THUMBNAIL_IMAGES=False, EXTERNAL_URI_SCHEME="https://")+ def test_external_image_preview_use_camo(self) -> None:+ content = "https://example.com/thing.jpeg"++ thumbnail_img = f"""<div class="message_inline_image"><a href="{content}"><img src="{get_camo_url(content)}"></a></div>"""+ converted = markdown_convert_wrapper(content)+ self.assertIn(converted, thumbnail_img)++ @override_settings(THUMBNAIL_IMAGES=False, EXTERNAL_URI_SCHEME="https://")+ def test_static_image_preview_skip_camo(self) -> None:+ content = f"{ settings.STATIC_URL }/thing.jpeg"++ thumbnail_img = f"""<div class="message_inline_image"><a href="{content}"><img src="{content}"></a></div>"""+ converted = markdown_convert_wrapper(content)+ self.assertIn(converted, thumbnail_img)++ @override_settings(THUMBNAIL_IMAGES=False, EXTERNAL_URI_SCHEME="https://")+ def test_realm_image_preview_skip_camo(self) -> None:+ content = f"https://zulip.{ settings.EXTERNAL_HOST }/thing.jpeg"+ converted = markdown_convert_wrapper(content)+ self.assertNotIn(converted, get_camo_url(content))++ @override_settings(THUMBNAIL_IMAGES=False, EXTERNAL_URI_SCHEME="https://")+ def test_cross_realm_image_preview_use_camo(self) -> None:+ content = f"https://otherrealm.{ settings.EXTERNAL_HOST }/thing.jpeg"++ thumbnail_img = f"""<div class="message_inline_image"><a href="{ content }"><img src="{ get_camo_url(content) }"></a></div>"""+ converted = markdown_convert_wrapper(content)+ self.assertIn(converted, thumbnail_img)+@override_settings(INLINE_IMAGE_PREVIEW=True)def test_inline_image_quoted_blocks(self) -> None:content = "http://cdn.wallpapersafari.com/13/6/16eVjx.jpeg"@@ -747,7 +778,7 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(converted,- '<p>Look at how hilarious our old office was: <a href="https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG">https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG</a></p>\n<div class="message_inline_image"><a href="https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG" title="IMG_0923.JPG"><img src="https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG?dl=1"></a></div>',+ f"""<p>Look at how hilarious our old office was: <a href="https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG">https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG</a></p>\n<div class="message_inline_image"><a href="https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG" title="IMG_0923.JPG"><img src="{get_camo_url("https://www.dropbox.com/s/ymdijjcg67hv2ta/IMG_0923.JPG?dl=1")}"></a></div>""",)msg = "Look at my hilarious drawing folder: https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl="@@ -761,7 +792,7 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(converted,- '<p>Look at my hilarious drawing folder: <a href="https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl=">https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl=</a></p>\n<div class="message_inline_ref"><a href="https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl=" title="Saves"><img src="https://cf.dropboxstatic.com/static/images/icons128/folder_dropbox.png"></a><div><div class="message_inline_image_title">Saves</div><desc class="message_inline_image_desc"></desc></div></div>',+ f"""<p>Look at my hilarious drawing folder: <a href="https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl=">https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl=</a></p>\n<div class="message_inline_ref"><a href="https://www.dropbox.com/sh/cm39k9e04z7fhim/AAAII5NK-9daee3FcF41anEua?dl=" title="Saves"><img src="{get_camo_url("https://cf.dropboxstatic.com/static/images/icons128/folder_dropbox.png")}"></a><div><div class="message_inline_image_title">Saves</div><desc class="message_inline_image_desc"></desc></div></div>""",)def test_inline_dropbox_preview(self) -> None:@@ -777,7 +808,7 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(converted,- '<p><a href="https://www.dropbox.com/sc/tditp9nitko60n5/03rEiZldy5">https://www.dropbox.com/sc/tditp9nitko60n5/03rEiZldy5</a></p>\n<div class="message_inline_image"><a href="https://www.dropbox.com/sc/tditp9nitko60n5/03rEiZldy5" title="1 photo"><img src="https://photos-6.dropbox.com/t/2/AAAlawaeD61TyNewO5vVi-DGf2ZeuayfyHFdNTNzpGq-QA/12/271544745/jpeg/1024x1024/2/_/0/5/baby-piglet.jpg/CKnjvYEBIAIgBygCKAc/tditp9nitko60n5/AADX03VAIrQlTl28CtujDcMla/0"></a></div>',+ f"""<p><a href="https://www.dropbox.com/sc/tditp9nitko60n5/03rEiZldy5">https://www.dropbox.com/sc/tditp9nitko60n5/03rEiZldy5</a></p>\n<div class="message_inline_image"><a href="https://www.dropbox.com/sc/tditp9nitko60n5/03rEiZldy5" title="1 photo"><img src="{get_camo_url("https://photos-6.dropbox.com/t/2/AAAlawaeD61TyNewO5vVi-DGf2ZeuayfyHFdNTNzpGq-QA/12/271544745/jpeg/1024x1024/2/_/0/5/baby-piglet.jpg/CKnjvYEBIAIgBygCKAc/tditp9nitko60n5/AADX03VAIrQlTl28CtujDcMla/0")}"></a></div>""",)def test_inline_dropbox_negative(self) -> None:@@ -826,7 +857,7 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(converted,- '<div class="spoiler-block"><div class="spoiler-header">\n<p>Check out this PyCon video</p>\n</div><div class="spoiler-content" aria-hidden="true">\n<p><a href="https://www.youtube.com/watch?v=0c46YHS3RY8">https://www.youtube.com/watch?v=0c46YHS3RY8</a></p>\n<div class="youtube-video message_inline_image"><a data-id="0c46YHS3RY8" href="https://www.youtube.com/watch?v=0c46YHS3RY8"><img src="https://i.ytimg.com/vi/0c46YHS3RY8/default.jpg"></a></div></div></div>',+ f"""<div class="spoiler-block"><div class="spoiler-header">\n<p>Check out this PyCon video</p>\n</div><div class="spoiler-content" aria-hidden="true">\n<p><a href="https://www.youtube.com/watch?v=0c46YHS3RY8">https://www.youtube.com/watch?v=0c46YHS3RY8</a></p>\n<div class="youtube-video message_inline_image"><a data-id="0c46YHS3RY8" href="https://www.youtube.com/watch?v=0c46YHS3RY8"><img src="{get_camo_url("https://i.ytimg.com/vi/0c46YHS3RY8/default.jpg")}"></a></div></div></div>""",)# Test YouTube URLs in normal messages.@@ -835,7 +866,7 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(converted,- '<p><a href="https://www.youtube.com/watch?v=0c46YHS3RY8">YouTube link</a></p>\n<div class="youtube-video message_inline_image"><a data-id="0c46YHS3RY8" href="https://www.youtube.com/watch?v=0c46YHS3RY8"><img src="https://i.ytimg.com/vi/0c46YHS3RY8/default.jpg"></a></div>',+ f"""<p><a href="https://www.youtube.com/watch?v=0c46YHS3RY8">YouTube link</a></p>\n<div class="youtube-video message_inline_image"><a data-id="0c46YHS3RY8" href="https://www.youtube.com/watch?v=0c46YHS3RY8"><img src="{get_camo_url("https://i.ytimg.com/vi/0c46YHS3RY8/default.jpg")}"></a></div>""",)msg = "https://www.youtube.com/watch?v=0c46YHS3RY8\n\nSample text\n\nhttps://www.youtube.com/watch?v=lXFO2ULktEI"@@ -843,7 +874,7 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(converted,- '<p><a href="https://www.youtube.com/watch?v=0c46YHS3RY8">https://www.youtube.com/watch?v=0c46YHS3RY8</a></p>\n<div class="youtube-video message_inline_image"><a data-id="0c46YHS3RY8" href="https://www.youtube.com/watch?v=0c46YHS3RY8"><img src="https://i.ytimg.com/vi/0c46YHS3RY8/default.jpg"></a></div><p>Sample text</p>\n<p><a href="https://www.youtube.com/watch?v=lXFO2ULktEI">https://www.youtube.com/watch?v=lXFO2ULktEI</a></p>\n<div class="youtube-video message_inline_image"><a data-id="lXFO2ULktEI" href="https://www.youtube.com/watch?v=lXFO2ULktEI"><img src="https://i.ytimg.com/vi/lXFO2ULktEI/default.jpg"></a></div>',+ f"""<p><a href="https://www.youtube.com/watch?v=0c46YHS3RY8">https://www.youtube.com/watch?v=0c46YHS3RY8</a></p>\n<div class="youtube-video message_inline_image"><a data-id="0c46YHS3RY8" href="https://www.youtube.com/watch?v=0c46YHS3RY8"><img src="{get_camo_url("https://i.ytimg.com/vi/0c46YHS3RY8/default.jpg")}"></a></div><p>Sample text</p>\n<p><a href="https://www.youtube.com/watch?v=lXFO2ULktEI">https://www.youtube.com/watch?v=lXFO2ULktEI</a></p>\n<div class="youtube-video message_inline_image"><a data-id="lXFO2ULktEI" href="https://www.youtube.com/watch?v=lXFO2ULktEI"><img src="{get_camo_url("https://i.ytimg.com/vi/lXFO2ULktEI/default.jpg")}"></a></div>""",)def test_twitter_id_extraction(self) -> None:@@ -1086,7 +1117,7 @@ class MarkdownTest(ZulipTestCase):('<div class="twitter-image">''<a href="http://t.co/xo7pAhK6n3">'- '<img src="https://pbs.twimg.com/media/BdoEjD4IEAIq86Z.jpg:small">'+ f"""<img src="{get_camo_url("https://pbs.twimg.com/media/BdoEjD4IEAIq86Z.jpg:small")}">""""</a>""</div>"),diff --git a/zerver/tests/test_subdomains.py b/zerver/tests/test_subdomains.pyindex 2410cf2e3f6..6f1b2c14aec 100644--- a/zerver/tests/test_subdomains.py+++ b/zerver/tests/test_subdomains.py@@ -1,7 +1,9 @@from typing import Any, Mapping, Sequencefrom unittest import mock-from zerver.lib.subdomains import get_subdomain+from django.conf import settings++from zerver.lib.subdomains import get_subdomain, is_static_or_current_realm_urlfrom zerver.lib.test_classes import ZulipTestCasefrom zerver.models import Realm@@ -57,3 +59,26 @@ class SubdomainsTest(ZulipTestCase):test(ROOT, "foo.example.org", external_host="example.org:12345")test(ROOT, "foo.example.org", external_host="example.org:443", plusport=False)test("foo", "foo.example.org:443", external_host="example.org:443")++ def test_is_static_or_current_realm_url(self) -> None:+ realm = self.example_user("hamlet").realm++ def test(url: str) -> bool:+ return is_static_or_current_realm_url(url, realm)++ self.assertTrue(test("/static/images/logo/zulip-org-logo.svg"))+ self.assertTrue(test("/anything"))+ self.assertFalse(test("https://zulip.com"))+ self.assertFalse(test("http://zulip.com"))+ self.assertTrue(test(f"{realm.uri}"))++ self.assertFalse(test(f"{realm.uri}@www.google.com"))++ # We don't have an existing configuration STATIC_URL with this+ # format, but it's worth testing in case that changes.+ with self.settings(STATIC_URL="https://zulipstatic.example.com"):+ evil_url = f"{settings.STATIC_URL}@evil.example.com"+ self.assertEqual(evil_url, "https://[email protected]")+ self.assertTrue(test(f"{settings.STATIC_URL}/x"))+ self.assertFalse(test(evil_url))+ self.assertFalse(test(f"{evil_url}/x"))