1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-09 11:27:27 +03:00
parent 9a00239a86
commit 28fc394b9e
3 changed files with 9 additions and 1 deletions

View file

@ -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) |

View file

@ -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"):

View file

@ -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):