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.

linkifiers: Validate alternative URL template uniqueness in backend.

Shubham Padiae820339143be
diff --git a/zerver/models/linkifiers.py b/zerver/models/linkifiers.py
index d4237d7e4c1..3d15aec0b74 100644
--- a/zerver/models/linkifiers.py
+++ b/zerver/models/linkifiers.py
@@ -167,6 +167,14 @@ class RealmFilter(models.Model):
params={"name": name},
)
+ if self.url_template in self.alternative_url_templates:
+ raise ValidationError(
+ _("An alternative URL template cannot be the same as the URL template.")
+ )
+
+ if len(self.alternative_url_templates) != len(set(self.alternative_url_templates)):
+ raise ValidationError(_("Alternative URL templates must be unique."))
+
for alternative_template in self.alternative_url_templates:
url_template_validator(alternative_template)
diff --git a/zerver/tests/test_realm_linkifiers.py b/zerver/tests/test_realm_linkifiers.py
index d1ca7c5cf0c..175b29b7d9e 100644
--- a/zerver/tests/test_realm_linkifiers.py
+++ b/zerver/tests/test_realm_linkifiers.py
@@ -427,6 +427,31 @@ class RealmFilterTest(ZulipTestCase):
"Group 'org' in linkifier pattern is not present in alternative URL template.",
)
+ # Alternative matching the main URL template should be rejected.
+ data["pattern"] = r"#(?P<id>[0-9]+)"
+ data["url_template"] = "https://github.com/zulip/zulip/issues/{id}"
+ data["alternative_url_templates"] = orjson.dumps(
+ ["https://github.com/zulip/zulip/issues/{id}"]
+ ).decode()
+ result = self.client_post("/json/realm/filters", info=data)
+ self.assert_json_error(
+ result,
+ "An alternative URL template cannot be the same as the URL template.",
+ )
+
+ # Duplicate alternative URL templates should be rejected.
+ data["alternative_url_templates"] = orjson.dumps(
+ [
+ "https://github.com/zulip/zulip/pull/{id}",
+ "https://github.com/zulip/zulip/pull/{id}",
+ ]
+ ).decode()
+ result = self.client_post("/json/realm/filters", info=data)
+ self.assert_json_error(
+ result,
+ "Alternative URL templates must be unique.",
+ )
+
def test_valid_urls(self) -> None:
valid_urls = [
"http://example.com/",