Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
checks: Add check for EXTERNAL_HOST being qualified.
Alex Vandiver3900c8a4f89d
diff --git a/tools/run-dev b/tools/run-devindex 934fd777919..857891b7975 100755--- a/tools/run-dev+++ b/tools/run-dev@@ -447,7 +447,7 @@ async def serve() -> None:# the default/zulip subdomain.default_hostname = "zulip." + os.uname()[1].lower()else:- default_hostname = "localhost"+ default_hostname = "localhost.localdomain"external_host = os.getenv("EXTERNAL_HOST", f"{default_hostname}:{proxy_port}")http_protocol = "https" if options.behind_https_proxy else "http"diff --git a/zerver/apps.py b/zerver/apps.pyindex da64f0c63c3..1ae9ea6d764 100644--- a/zerver/apps.py+++ b/zerver/apps.py@@ -9,7 +9,7 @@ from django.core.checks import registerfrom django.db.models.signals import post_migratefrom typing_extensions import override-from zerver.checks import check_required_settings+from zerver.checks import check_external_host_setting, check_required_settingsdef flush_cache(sender: AppConfig | None, **kwargs: Any) -> None:@@ -23,6 +23,7 @@ class ZerverConfig(AppConfig):@overridedef ready(self) -> None:register(check_required_settings)+ register(check_external_host_setting)if settings.SENTRY_DSN: # nocoveragefrom zproject.config import get_configdiff --git a/zerver/checks.py b/zerver/checks.pyindex 2e7c702c86b..fdae5e78283 100644--- a/zerver/checks.py+++ b/zerver/checks.py@@ -1,3 +1,4 @@+import osfrom collections.abc import Iterable, Sequencefrom typing import Any@@ -40,3 +41,24 @@ def check_required_settings())return errors+++def check_external_host_setting(+ app_configs: Sequence[AppConfig] | None,+ databases: Sequence[str] | None,+ **kwargs: Any,+) -> Iterable[checks.CheckMessage]:+ 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 [+ checks.Error(+ f"EXTERNAL_HOST ({settings.EXTERNAL_HOST}) does not contain a domain part",+ obj="settings.EXTERNAL_HOST",+ hint=f"Add {suggest} to the end",+ id="zulip.E002",+ )+ ]+ return []diff --git a/zerver/tests/test_checks.py b/zerver/tests/test_checks.pyindex 812dbaac29b..1b163633d44 100644--- a/zerver/tests/test_checks.py+++ b/zerver/tests/test_checks.py@@ -1,4 +1,5 @@import os+import refrom contextlib import ExitStackfrom typing import Any@@ -10,10 +11,12 @@ from zerver.lib.test_classes import ZulipTestCaseclass TestChecks(ZulipTestCase):- def assert_check_with_error(self, message: str | None, **kwargs: Any) -> None:+ def assert_check_with_error(self, test: re.Pattern[str] | str | None, **kwargs: Any) -> None:with open(os.devnull, "w") as DEVNULL, override_settings(**kwargs), ExitStack() as stack:- if message is not None:- stack.enter_context(self.assertRaisesMessage(SystemCheckError, message))+ if isinstance(test, str):+ stack.enter_context(self.assertRaisesMessage(SystemCheckError, test))+ elif isinstance(test, re.Pattern):+ stack.enter_context(self.assertRaisesRegex(SystemCheckError, test))call_command("check", stdout=DEVNULL)def test_checks_required_setting(self) -> None:@@ -32,7 +35,25 @@ class TestChecks(ZulipTestCase):ZULIP_ADMINISTRATOR=None,)- self.assert_check_with_error(- None,- ZULIP_ADMINISTRATOR="[email protected]",- )+ def test_checks_external_host_domain(self) -> None:+ message_re = r"\(zulip\.E002\) EXTERNAL_HOST \(\S+\) does not contain a domain part"+ try:+ # We default to skippping this check in CI, because+ # "testserver" is part of so many tests. We temporarily+ # strip out the environment variable we use to detect+ # that, so we can trigger the check.+ del os.environ["ZULIP_TEST_SUITE"]++ self.assert_check_with_error(None, EXTERNAL_HOST="server-1.local")++ self.assert_check_with_error(+ re.compile(rf"{message_re}\s*HINT: Add .local to the end"), EXTERNAL_HOST="server-1"+ )++ self.assert_check_with_error(+ re.compile(rf"{message_re}\s*HINT: Add .localdomain to the end"),+ EXTERNAL_HOST="localhost",+ )++ finally:+ os.environ["ZULIP_TEST_SUITE"] = "true"