1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +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.
This commit is contained in:
catborise 2026-11-09 11:27:27 +03:00
parent 59a0a0ebec
commit 98846b1d4a
3 changed files with 9 additions and 1 deletions

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