From 832f91b5d4e8f36816bd82d71f900df6e258b887 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Wed, 25 Nov 2026 17:42:14 -0300 Subject: [PATCH] Keep original Host on HTTPS upstreams, add use_upstream_host option --- app_gateway/caddy_config_export.py | 1 + app_gateway/forms.py | 4 ++- .../0015_application_use_upstream_host.py | 18 ++++++++++ app_gateway/models.py | 1 + .../fixtures/config_files/applications.json | 18 ++++++---- containers/caddy/process_config.py | 35 ++++++++++++------- 6 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 app_gateway/migrations/0015_application_use_upstream_host.py diff --git a/app_gateway/caddy_config_export.py b/app_gateway/caddy_config_export.py index 8f788f6..fc85335 100644 --- a/app_gateway/caddy_config_export.py +++ b/app_gateway/caddy_config_export.py @@ -64,6 +64,7 @@ def build_applications_data(worker=None): 'hosts': list(app.hosts.values_list('hostname', flat=True)), 'upstream': app.upstream, 'allow_invalid_cert': app.allow_invalid_cert, + 'use_upstream_host': app.use_upstream_host, } entries.append(entry) return {'entries': entries} diff --git a/app_gateway/forms.py b/app_gateway/forms.py index 7efde9a..e87b510 100644 --- a/app_gateway/forms.py +++ b/app_gateway/forms.py @@ -21,11 +21,12 @@ DOMAIN_PATTERN = re.compile(r'^(?=.{1,253}$)([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\ class ApplicationForm(forms.ModelForm): class Meta: model = Application - fields = ['display_name', 'upstream', 'allow_invalid_cert', 'serve_on_primary', 'all_workers', 'workers'] + fields = ['display_name', 'upstream', 'allow_invalid_cert', 'use_upstream_host', 'serve_on_primary', 'all_workers', 'workers'] labels = { 'display_name': _('Name'), 'upstream': _('Upstream'), 'allow_invalid_cert': _('Allow invalid/self-signed certificate'), + 'use_upstream_host': _('Send the upstream address as Host'), 'serve_on_primary': _('Serve on the primary server'), 'all_workers': _('Serve on all cluster workers'), 'workers': _('Cluster workers'), @@ -70,6 +71,7 @@ class ApplicationForm(forms.ModelForm): Div( Div('upstream', css_class='col-md-12'), Div('allow_invalid_cert', css_class='col-md-12'), + Div('use_upstream_host', css_class='col-md-12'), css_class='row' ), *cluster_rows, diff --git a/app_gateway/migrations/0015_application_use_upstream_host.py b/app_gateway/migrations/0015_application_use_upstream_host.py new file mode 100644 index 0000000..4731444 --- /dev/null +++ b/app_gateway/migrations/0015_application_use_upstream_host.py @@ -0,0 +1,18 @@ +# Generated by Django 5.2.19 on 2026-11-25 18:06 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('app_gateway', '0014_dnschallengecredential_delegation_domain'), + ] + + operations = [ + migrations.AddField( + model_name='application', + name='use_upstream_host', + field=models.BooleanField(default=False, help_text='Enable when the upstream selects the site by name, such as shared hosting, cloud services or CDNs. By default, the original Host of the request is sent.'), + ), + ] diff --git a/app_gateway/models.py b/app_gateway/models.py index 946b183..74bae58 100644 --- a/app_gateway/models.py +++ b/app_gateway/models.py @@ -67,6 +67,7 @@ class Application(models.Model): display_name = models.CharField(max_length=128, blank=True) upstream = models.CharField(max_length=255, help_text=_("Upstream address, e.g.: http://10.188.18.27:3000")) allow_invalid_cert = models.BooleanField(default=False, help_text=_("Allow invalid or self-signed TLS certificates from the upstream")) + use_upstream_host = models.BooleanField(default=False, help_text=_("Enable when the upstream selects the site by name, such as shared hosting, cloud services or CDNs. By default, the original Host of the request is sent.")) serve_on_primary = models.BooleanField(default=True) all_workers = models.BooleanField(default=False) workers = models.ManyToManyField('cluster.Worker', blank=True, related_name='applications') diff --git a/containers/auth-gateway/tests/fixtures/config_files/applications.json b/containers/auth-gateway/tests/fixtures/config_files/applications.json index 10535e9..28d7434 100644 --- a/containers/auth-gateway/tests/fixtures/config_files/applications.json +++ b/containers/auth-gateway/tests/fixtures/config_files/applications.json @@ -5,7 +5,8 @@ "name": "Caf\u00e9", "hosts": [], "upstream": "http://10.0.0.14:8000", - "allow_invalid_cert": false + "allow_invalid_cert": false, + "use_upstream_host": false }, { "id": "dashboard", @@ -14,14 +15,16 @@ "dashboard.example.test" ], "upstream": "http://10.0.0.10:3000", - "allow_invalid_cert": false + "allow_invalid_cert": false, + "use_upstream_host": false }, { "id": "files", "name": "Files", "hosts": [], "upstream": "http://10.0.0.13:8080", - "allow_invalid_cert": false + "allow_invalid_cert": false, + "use_upstream_host": false }, { "id": "hypervisor", @@ -30,7 +33,8 @@ "hypervisor.example.test" ], "upstream": "https://10.0.0.12:8006", - "allow_invalid_cert": true + "allow_invalid_cert": true, + "use_upstream_host": false }, { "id": "media", @@ -40,7 +44,8 @@ "media2.example.test" ], "upstream": "http://10.0.0.11:32400", - "allow_invalid_cert": false + "allow_invalid_cert": false, + "use_upstream_host": false }, { "id": "sso", @@ -49,7 +54,8 @@ "sso.example.test" ], "upstream": "http://10.0.0.15:8000", - "allow_invalid_cert": false + "allow_invalid_cert": false, + "use_upstream_host": false } ] } diff --git a/containers/caddy/process_config.py b/containers/caddy/process_config.py index 37c583e..b03687f 100644 --- a/containers/caddy/process_config.py +++ b/containers/caddy/process_config.py @@ -257,25 +257,28 @@ def build_caddyfile(apps, auth_policies, routes, tls_data=None): return " handle {" return f" handle {matcher} {{" - def emit_reverse_proxy(base, upstream_path, indent=" ", allow_invalid_cert=False): + def emit_reverse_proxy(base, upstream_path, indent=" ", allow_invalid_cert=False, host_header=None): if upstream_path: lines.append(f"{indent}rewrite * {upstream_path}{{uri}}") + if not allow_invalid_cert and not host_header: + lines.append(f"{indent}reverse_proxy {base}") + return + lines.append(f"{indent}reverse_proxy {base} {{") + if host_header: + lines.append(f"{indent} header_up Host {host_header}") if allow_invalid_cert: - lines.append(f"{indent}reverse_proxy {base} {{") lines.append(f"{indent} transport http {{") lines.append(f"{indent} tls_insecure_skip_verify") lines.append(f"{indent} }}") - lines.append(f"{indent}}}") - else: - lines.append(f"{indent}reverse_proxy {base}") + lines.append(f"{indent}}}") - def emit_protected_handle(path_matcher, base, upstream_path, allow_invalid_cert=False): + def emit_protected_handle(path_matcher, base, upstream_path, allow_invalid_cert=False, host_header=None): lines.append(handle_open(path_matcher)) lines.append(f" forward_auth {AUTH_GATEWAY_INTERNAL_URL} {{") lines.append(f" uri {AUTH_GATEWAY_CHECK_URI}") lines.append(" copy_headers X-Auth-User X-Auth-Email X-Auth-Groups X-Auth-Factors X-Auth-Policy") lines.append(" }") - emit_reverse_proxy(base, upstream_path, allow_invalid_cert=allow_invalid_cert) + emit_reverse_proxy(base, upstream_path, allow_invalid_cert=allow_invalid_cert, host_header=host_header) lines.append(" }") lines.append("") @@ -283,6 +286,14 @@ def build_caddyfile(apps, auth_policies, routes, tls_data=None): upstream = app.get("upstream", "") # tls_insecure_skip_verify is only valid for https upstreams allow_invalid_cert = app.get("allow_invalid_cert", False) and upstream.startswith("https://") + # Caddy sends the upstream address as Host to https upstreams; the original Host is kept unless the + # application asks for the upstream address, which upstreams that select the site by name may need + if app.get("use_upstream_host", False): + host_header = "{upstream_hostport}" + elif upstream.startswith("https://"): + host_header = "{hostport}" + else: + host_header = None static_routes = app.get("static_routes", []) app_id = app.get("id", "") @@ -313,7 +324,7 @@ def build_caddyfile(apps, auth_policies, routes, tls_data=None): app_route_data = route_entries.get(app_id) if app_route_data is None: - emit_reverse_proxy(base, upstream_path, indent=" ", allow_invalid_cert=allow_invalid_cert) + emit_reverse_proxy(base, upstream_path, indent=" ", allow_invalid_cert=allow_invalid_cert, host_header=host_header) lines.append("}") lines.append("") return @@ -325,7 +336,7 @@ def build_caddyfile(apps, auth_policies, routes, tls_data=None): matcher = emit_route_matcher(f"route_{app_id}_{route_index}", path_prefix) if policy_type == "bypass": lines.append(handle_open(matcher)) - emit_reverse_proxy(base, upstream_path, allow_invalid_cert=allow_invalid_cert) + emit_reverse_proxy(base, upstream_path, allow_invalid_cert=allow_invalid_cert, host_header=host_header) lines.append(" }") lines.append("") elif policy_type == "deny": @@ -334,15 +345,15 @@ def build_caddyfile(apps, auth_policies, routes, tls_data=None): lines.append(" }") lines.append("") else: - emit_protected_handle(matcher, base, upstream_path, allow_invalid_cert=allow_invalid_cert) + emit_protected_handle(matcher, base, upstream_path, allow_invalid_cert=allow_invalid_cert, host_header=host_header) default_policy_type = get_policy_type(app_route_data.get("default_policy")) if default_policy_type == "bypass": - emit_reverse_proxy(base, upstream_path, indent=" ", allow_invalid_cert=allow_invalid_cert) + emit_reverse_proxy(base, upstream_path, indent=" ", allow_invalid_cert=allow_invalid_cert, host_header=host_header) elif default_policy_type == "deny": lines.append(" respond 403") else: - emit_protected_handle(None, base, upstream_path, allow_invalid_cert=allow_invalid_cert) + emit_protected_handle(None, base, upstream_path, allow_invalid_cert=allow_invalid_cert, host_header=host_header) lines.append("}") lines.append("")