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.

checks: Perform more validation of hostnames.

Alex Vandiverf2f7e48a6e05
diff --git a/zerver/checks.py b/zerver/checks.py
index fdae5e78283..b7681911e04 100644
--- a/zerver/checks.py
+++ b/zerver/checks.py
@@ -1,4 +1,5 @@
import os
+import re
from collections.abc import Iterable, Sequence
from typing import Any
@@ -51,14 +52,50 @@ def check_external_host_setting(
if not hasattr(settings, "EXTERNAL_HOST"): # nocoverage
return []
- if "." not in settings.EXTERNAL_HOST and os.environ.get("ZULIP_TEST_SUITE") != "true":
- suggest = ".localdomain" if settings.EXTERNAL_HOST == "localhost" else ".local"
- return [
+ errors = []
+ hostname = settings.EXTERNAL_HOST
+ if "." not in hostname and os.environ.get("ZULIP_TEST_SUITE") != "true":
+ suggest = ".localdomain" if hostname == "localhost" else ".local"
+ errors.append(
checks.Error(
- f"EXTERNAL_HOST ({settings.EXTERNAL_HOST}) does not contain a domain part",
+ f"EXTERNAL_HOST ({hostname}) does not contain a domain part",
obj="settings.EXTERNAL_HOST",
hint=f"Add {suggest} to the end",
id="zulip.E002",
)
- ]
- return []
+ )
+
+ if ":" in hostname:
+ hostname = hostname.split(":")[0]
+
+ if len(hostname) > 255:
+ errors.append(
+ checks.Error(
+ f"EXTERNAL_HOST ({hostname}) is too long to be a valid hostname",
+ obj="settings.EXTERNAL_HOST",
+ id="zulip.E002",
+ )
+ )
+ domain_part = re.compile(r"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
+ if not hostname.isascii():
+ suggestion = ".".join(
+ "xn--" + part.encode("punycode").decode() if not part.isascii() else part
+ for part in hostname.split(".")
+ )
+ errors.append(
+ checks.Error(
+ f"EXTERNAL_HOST ({hostname}) contains non-ASCII characters",
+ hint=f"Switch to punycode: {suggestion}",
+ obj="settings.EXTERNAL_HOST",
+ id="zulip.E002",
+ )
+ )
+ elif not all(domain_part.match(x) for x in hostname.split(".")):
+ errors.append(
+ checks.Error(
+ f"EXTERNAL_HOST ({hostname}) does not validate as a hostname",
+ obj="settings.EXTERNAL_HOST",
+ id="zulip.E002",
+ )
+ )
+ return errors
diff --git a/zerver/tests/test_checks.py b/zerver/tests/test_checks.py
index 1b163633d44..2dbfe8d7e29 100644
--- a/zerver/tests/test_checks.py
+++ b/zerver/tests/test_checks.py
@@ -57,3 +57,25 @@ class TestChecks(ZulipTestCase):
finally:
os.environ["ZULIP_TEST_SUITE"] = "true"
+
+ def test_checks_external_host_value(self) -> None:
+ self.assert_check_with_error(None, EXTERNAL_HOST="testserver.local")
+ self.assert_check_with_error(None, EXTERNAL_HOST="testserver.local:443")
+ self.assert_check_with_error(None, EXTERNAL_HOST="testserver.local:https")
+
+ self.assert_check_with_error(
+ re.compile(r"EXTERNAL_HOST \(\S+\) is too long"),
+ EXTERNAL_HOST=("a234567890." * 25 + "local"),
+ )
+
+ self.assert_check_with_error(
+ re.compile(
+ r"\(zulip\.E002\) EXTERNAL_HOST \(\S+\) contains non-ASCII characters\n.*xn--wgv71a119e\.example\.com"
+ ),
+ EXTERNAL_HOST="日本語.example.com",
+ )
+
+ self.assert_check_with_error(
+ "EXTERNAL_HOST (-bogus-.example.com) does not validate as a hostname",
+ EXTERNAL_HOST="-bogus-.example.com:443",
+ )