Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
linkifiers: Explicitly only check format strings after verifying as a URL.
Alex Vandiverd6768814a1b5
diff --git a/zerver/migrations/0368_alter_realmfilter_url_format_string.py b/zerver/migrations/0368_alter_realmfilter_url_format_string.pynew file mode 100644index 00000000000..0ceed9c1e57--- /dev/null+++ b/zerver/migrations/0368_alter_realmfilter_url_format_string.py@@ -0,0 +1,20 @@+# Generated by Django 3.2.8 on 2021-10-20 23:42++from django.db import migrations, models++from zerver.models import filter_format_validator+++class Migration(migrations.Migration):++ dependencies = [+ ("zerver", "0367_scimclient"),+ ]++ operations = [+ migrations.AlterField(+ model_name="realmfilter",+ name="url_format_string",+ field=models.TextField(validators=[filter_format_validator]),+ ),+ ]diff --git a/zerver/models.py b/zerver/models.pyindex ca84a60b6ed..9221546da2c 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -1120,12 +1120,14 @@ def filter_pattern_validator(value: str) -> Pattern[str]:def filter_format_validator(value: str) -> None:- """Does not attempt to verify URL-ness, but rather %(foo)s.+ """Verifies URL-ness, and then %(foo)s.- URLValidator is assumed to have caught anything which is malformed- as a URL.+ URLValidator is assumed to catch anything which is malformed as a+ URL; the regex then verifies the format-string pieces."""+ URLValidator()(value)+regex = re.compile(r"""^@@ -1144,7 +1146,7 @@ def filter_format_validator(value: str) -> None:)if not regex.match(value):- raise ValidationError(_("Invalid URL format string."))+ raise ValidationError(_("Invalid format string in URL."))class RealmFilter(models.Model):@@ -1155,7 +1157,7 @@ class RealmFilter(models.Model):id: int = models.AutoField(auto_created=True, primary_key=True, verbose_name="ID")realm: Realm = models.ForeignKey(Realm, on_delete=CASCADE)pattern: str = models.TextField()- url_format_string: str = models.TextField(validators=[URLValidator(), filter_format_validator])+ url_format_string: str = models.TextField(validators=[filter_format_validator])class Meta:unique_together = ("realm", "pattern")diff --git a/zerver/tests/test_realm_linkifiers.py b/zerver/tests/test_realm_linkifiers.pyindex eca9b8f7a18..98f41166905 100644--- a/zerver/tests/test_realm_linkifiers.py+++ b/zerver/tests/test_realm_linkifiers.py@@ -204,6 +204,7 @@ class RealmFilterTest(ZulipTestCase):def test_valid_urls(self) -> None:valid_urls = [+ "http://example.com/","https://example.com/","https://user:[email protected]/","https://example.com/@user/thing",@@ -225,6 +226,8 @@ class RealmFilterTest(ZulipTestCase):filter_format_validator(url)invalid_urls = [+ "file:///etc/passwd",+ "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==","https://example.com/%ab","https://example.com/%ba","https://example.com/%21",