Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
linkifiers: Support URL percent-encoded bytes.
Alex Vandiver9381a3bd4596
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex 6d3244d954c..bc7d6efce46 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -1795,7 +1795,22 @@ class LinkifierPattern(CompiledInlineProcessor):options.log_errors = Falsecompiled_re2 = re2.compile(prepare_linkifier_pattern(source_pattern), options=options)- self.format_string = format_string++ # Find percent-encoded bytes and escape them from the python+ # interpolation. That is:+ # %(foo)s -> %(foo)s+ # %% -> %%+ # %ab -> %%ab+ # %%ab -> %%ab+ # %%%ab -> %%%%ab+ #+ # We do this here, rather than before storing, to make edits+ # to the underlying linkifier more straightforward, and+ # because JS does not have a real formatter.+ self.format_string = re.sub(+ r"(?<!%)(%%)*%([a-fA-F0-9][a-fA-F0-9])", r"\1%%\2", format_string+ )+super().__init__(compiled_re2, md)def handleMatch( # type: ignore[override] # supertype incompatible with supersupertypediff --git a/zerver/models.py b/zerver/models.pyindex 9221546da2c..adafae941a6 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -1138,6 +1138,9 @@ def filter_format_validator(value: str) -> None:\( [a-zA-Z0-9_-]+ \) s # Interpolation group| # OR% # %%, which is an escaped %+ | # OR+ [0-9a-fA-F][0-9a-fA-F] # URL percent-encoded bytes, which we+ # special-case in markdown translation))+ # Those happen one or more times$diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.pyindex 702aab6d763..7f8dd6ef617 100644--- a/zerver/tests/test_markdown.py+++ b/zerver/tests/test_markdown.py@@ -1401,6 +1401,20 @@ class MarkdownTest(ZulipTestCase):],)+ # Test URI escaping+ RealmFilter(+ realm=realm,+ pattern=r"url-(?P<id>[0-9]+)",+ url_format_string="https://example.com/%%%ba/%(id)s",+ ).save()+ msg = Message(sender=self.example_user("hamlet"))+ content = "url-123 is well-escaped"+ converted = markdown_convert(content, message_realm=realm, message=msg)+ self.assertEqual(+ converted.rendered_content,+ '<p><a href="https://example.com/%%ba/123">url-123</a> is well-escaped</p>',+ )+def test_multiple_matching_realm_patterns(self) -> None:realm = get_realm("zulip")url_format_string = r"https://trac.example.com/ticket/%(id)s"diff --git a/zerver/tests/test_realm_linkifiers.py b/zerver/tests/test_realm_linkifiers.pyindex 98f41166905..534163d8508 100644--- a/zerver/tests/test_realm_linkifiers.py+++ b/zerver/tests/test_realm_linkifiers.py@@ -126,6 +126,11 @@ class RealmFilterTest(ZulipTestCase):result = self.client_post("/json/realm/filters", info=data)self.assert_json_success(result)+ data["pattern"] = r"ZUL-URI-(?P<id>\d+)"+ data["url_format_string"] = "https://example.com/%ba/%(id)s"+ result = self.client_post("/json/realm/filters", info=data)+ self.assert_json_success(result)+data["pattern"] = r"(?P<org>[a-zA-Z0-9_-]+)/(?P<repo>[a-zA-Z0-9_-]+)#(?P<id>[0-9]+)"data["url_format_string"] = "https://github.com/%(org)s/%(repo)s/issue/%(id)s"result = self.client_post("/json/realm/filters", info=data)@@ -215,6 +220,13 @@ class RealmFilterTest(ZulipTestCase):"https://example.com/%(foo)s%(bars)s","https://example.com/%(foo)s/and/%(bar)s","https://example.com/?foo=%(foo)s",+ "https://example.com/%ab",+ "https://example.com/%ba",+ "https://example.com/%21",+ "https://example.com/words%20with%20spaces",+ "https://example.com/back%20to%20%(back)s",+ "https://example.com/encoded%2fwith%2fletters",+ "https://example.com/encoded%2Fwith%2Fupper%2Fcase%2Fletters","https://example.com/%%","https://example.com/%%(","https://example.com/%%()",@@ -228,13 +240,6 @@ class RealmFilterTest(ZulipTestCase):invalid_urls = ["file:///etc/passwd","data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==",- "https://example.com/%ab",- "https://example.com/%ba",- "https://example.com/%21",- "https://example.com/words%20with%20spaces",- "https://example.com/back%20to%20%(back)s",- "https://example.com/encoded%2fwith%2fletters",- "https://example.com/encoded%2Fwith%2Fupper%2Fcase%2Fletters","https://example.com/%(foo)","https://example.com/%()s","https://example.com/%4!",