fix: normalize coolify host env
All checks were successful
Build and Push Docker Image / build (push) Successful in 26s

This commit is contained in:
2026-04-21 12:26:14 +02:00
parent 78f3e0f184
commit bba8fdf2a2
2 changed files with 32 additions and 15 deletions

28
home/tests_settings.py Normal file
View File

@@ -0,0 +1,28 @@
import os
from unittest.mock import patch
from django.test import SimpleTestCase
from santehray.settings import collect_hosts, collect_origins, normalize_host, normalize_origin
class SettingsNormalizationTests(SimpleTestCase):
def test_normalize_host_strips_scheme_and_port(self):
self.assertEqual(normalize_host("https://preview-1.azimkin.dev:8000"), "preview-1.azimkin.dev")
def test_collect_hosts_normalizes_urls_and_bare_hosts(self):
with patch.dict(os.environ, {"ALLOWED_HOSTS": "https://preview-1.azimkin.dev:8000, localhost"}):
self.assertEqual(
collect_hosts("ALLOWED_HOSTS"),
["preview-1.azimkin.dev", "localhost"],
)
def test_collect_origins_normalizes_bare_hosts_and_urls(self):
with patch.dict(os.environ, {"CSRF_TRUSTED_ORIGINS": "preview-1.azimkin.dev,https://localhost:8443"}):
self.assertEqual(
collect_origins("CSRF_TRUSTED_ORIGINS"),
["https://preview-1.azimkin.dev", "https://localhost:8443"],
)
def test_normalize_origin_preserves_explicit_scheme_and_port(self):
self.assertEqual(normalize_origin("https://preview-1.azimkin.dev:8443"), "https://preview-1.azimkin.dev:8443")

View File

@@ -39,13 +39,6 @@ def env_bool(name: str, default: bool = False) -> bool:
return os.getenv(name, str(default)).lower() in {"1", "true", "yes", "on"} return os.getenv(name, str(default)).lower() in {"1", "true", "yes", "on"}
def env_list(name: str, default: list[str]) -> list[str]:
value = os.getenv(name)
if value is None or not value.strip():
return default
return [item.strip() for item in value.split(",") if item.strip()]
def env_pair(name: str, default: tuple[str, str]) -> tuple[str, str]: def env_pair(name: str, default: tuple[str, str]) -> tuple[str, str]:
value = os.getenv(name) value = os.getenv(name)
if not value: if not value:
@@ -159,16 +152,12 @@ SECRET_KEY = os.getenv("SECRET_KEY", "django-insecure-*%u*i415=%@ny05mbd-j3802bc
DEBUG = env_bool("DEBUG", True) DEBUG = env_bool("DEBUG", True)
ALLOWED_HOSTS = merge_unique( ALLOWED_HOSTS = merge_unique(
collect_hosts("COOLIFY_FQDN", "COOLIFY_URL"), collect_hosts("COOLIFY_FQDN", "COOLIFY_URL", "ALLOWED_HOSTS"),
env_list("ALLOWED_HOSTS", ["127.0.0.1", "localhost", "preview-1.azimkin.dev"]), ["127.0.0.1", "localhost", "preview-1.azimkin.dev"],
)
CSRF_TRUSTED_ORIGINS = env_list(
"CSRF_TRUSTED_ORIGINS",
["https://preview-1.azimkin.dev", "http://127.0.0.1", "http://localhost"],
) )
CSRF_TRUSTED_ORIGINS = merge_unique( CSRF_TRUSTED_ORIGINS = merge_unique(
collect_origins("COOLIFY_URL", "COOLIFY_FQDN"), collect_origins("COOLIFY_URL", "COOLIFY_FQDN", "CSRF_TRUSTED_ORIGINS"),
CSRF_TRUSTED_ORIGINS, ["https://preview-1.azimkin.dev", "http://127.0.0.1", "http://localhost"],
) )
USE_X_FORWARDED_HOST = env_bool("USE_X_FORWARDED_HOST", True) USE_X_FORWARDED_HOST = env_bool("USE_X_FORWARDED_HOST", True)
SECURE_PROXY_SSL_HEADER = env_pair( SECURE_PROXY_SSL_HEADER = env_pair(