From 28fc394b9ede73386b051fb4f5ee4c08d3348c21 Mon Sep 17 00:00:00 2001 From: catborise Date: Mon, 9 Nov 2026 11:27:27 +0300 Subject: [PATCH] fix(settings): a network in ALLOWED_HOSTS stops the start Django matches the request's host with each ALLOWED_HOSTS entry as a name: 192.0.2.0/24 was accepted and matched nothing, so requests to the addresses in it got 400 with no sign why. Such an entry now raises ImproperlyConfigured, naming it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- README.md | 2 +- webvirtcloud/settings.py.template | 4 ++++ webvirtcloud/tests/test_settings_env.py | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 724c8057..872dde63 100644 --- a/README.md +++ b/README.md @@ -337,7 +337,7 @@ If libvirt warns `Host SMBIOS information is not available`, install `dmidecode` | Variable | Effect | | --- | --- | -| `ALLOWED_HOSTS` | extra host names/IPs the panel answers to (comma-separated, `*` for any); other hosts get 400 | +| `ALLOWED_HOSTS` | extra host names/IPs the panel answers to (comma-separated, `*` for any; a network such as `192.0.2.0/24` is refused at start); other hosts get 400 | | `CSRF_TRUSTED_ORIGINS` | extra trusted origins, e.g. `https://wvc.example.com` | | `WEBVIRTCLOUD_HTTPS=1` | HTTPS profile, see [HTTPS and reverse proxy](#https-and-reverse-proxy) | | `WEBVIRTCLOUD_SERIAL_CONSOLE=1` | serial console, see [Serial console](#serial-console) | diff --git a/webvirtcloud/settings.py.template b/webvirtcloud/settings.py.template index 6504aac8..99bb9f5e 100644 --- a/webvirtcloud/settings.py.template +++ b/webvirtcloud/settings.py.template @@ -47,6 +47,10 @@ MAC_OUI = '52:54:10' ALLOWED_HOSTS = ["localhost", "127.0.0.1", "[::1]"] if extra_hosts := os.getenv("ALLOWED_HOSTS"): ALLOWED_HOSTS.extend([host.strip() for host in extra_hosts.split(",") if host.strip()]) +# Django compares the request's host with each entry as a name: a network +# (192.0.2.0/24) matches no request and would leave its addresses refused +if networks := [host for host in ALLOWED_HOSTS if "/" in host]: + raise ImproperlyConfigured(f"ALLOWED_HOSTS takes host names and addresses, not networks: {', '.join(networks)}") CSRF_TRUSTED_ORIGINS = ["http://localhost", "http://127.0.0.1", "http://localhost:8000", "http://127.0.0.1:8000"] if extra_origins := os.getenv("CSRF_TRUSTED_ORIGINS"): diff --git a/webvirtcloud/tests/test_settings_env.py b/webvirtcloud/tests/test_settings_env.py index 7005a36e..6533a076 100644 --- a/webvirtcloud/tests/test_settings_env.py +++ b/webvirtcloud/tests/test_settings_env.py @@ -91,6 +91,10 @@ class AllowedHostsTestCase(SimpleTestCase): def test_allowed_hosts_star_escape(self): self.assertIn("*", self.hosts({"ALLOWED_HOSTS": "*"})) + def test_a_network_stops_the_start(self): + with self.assertRaisesRegex(AssertionError, "not networks: 192.0.2.0/24"): + self.hosts({"ALLOWED_HOSTS": "wvc.example.com,192.0.2.0/24"}) + class EmailEnvTestCase(SimpleTestCase): def test_email_from_env(self):