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.

markdown: Restrict "file" scheme links based on ENABLE_FILE_LINKS.

yashmatrix57ef63b9a416
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py
index cae847166c3..490359108f3 100644
--- a/zerver/lib/markdown/__init__.py
+++ b/zerver/lib/markdown/__init__.py
@@ -1382,6 +1382,13 @@ def sanitize_url(url: str) -> str | None:
if scheme not in allowed_schemes:
return None
+ # The "file" scheme is only allowed when ENABLE_FILE_LINKS is set;
+ # otherwise, explicit Markdown links like [text](file:///etc/passwd)
+ # would bypass the restriction that already applies to bare
+ # file:// URLs via the autolinker (see file_links above).
+ if scheme == "file" and not settings.ENABLE_FILE_LINKS:
+ return None
+
# Upstream code scans path, parameters, and query for colon characters
# because
#
diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py
index a0723000f3c..3665f4bb512 100644
--- a/zerver/tests/test_markdown.py
+++ b/zerver/tests/test_markdown.py
@@ -760,6 +760,31 @@ class MarkdownLinkTest(ZulipTestCase):
finally:
clear_web_link_regex_for_testing()
+ def test_explicit_link_file(self) -> None:
+ # Regression test: explicit Markdown links like
+ # [text](file:///...) must respect ENABLE_FILE_LINKS just
+ # like bare file:// URLs do (see test_inline_file above).
+ # Previously this bypassed the setting because sanitize_url()
+ # unconditionally allowed the "file" scheme.
+ msg = "[click me](file:///etc/passwd)"
+
+ realm = do_create_realm(string_id="file_links_disabled_2", name="File links disabled")
+ self.assertEqual(
+ markdown_convert(msg, message_realm=realm).rendered_content,
+ "<p>[click me](file:///etc/passwd)</p>",
+ )
+ clear_web_link_regex_for_testing()
+
+ try:
+ with self.settings(ENABLE_FILE_LINKS=True):
+ realm = do_create_realm(string_id="file_links_enabled_2", name="File links enabled")
+ self.assertEqual(
+ markdown_convert(msg, message_realm=realm).rendered_content,
+ '<p><a href="file:///etc/passwd">click me</a></p>',
+ )
+ finally:
+ clear_web_link_regex_for_testing()
+
def test_inline_bitcoin(self) -> None:
msg = "To bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa or not to bitcoin"
converted = markdown_convert_wrapper(msg)