From cdfff73f73d0ba67cd716ca7e8a86f792753f1c1 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Sun, 8 Nov 2026 15:53:00 -0700 Subject: [PATCH] --proxy snippets: nginx answers 421 for any other name on its TLS port (#48) A browser reuses an HTTP/2 connection for every name its certificate covers. With one certificate for the console's, the webmail's and the mail server's names, a request for the mail server can arrive over the webmail's connection, past the router, and nginx answered it as the webmail or the console. A default server on 127.0.0.1:8444 now answers any name but those two with 421 Misdirected Request, so the browser opens a connection of its own for it, and the router passes that one through. It needs no certificate: ssl_reject_handshake refuses a handshake for any other name, and the router sends none. The reporter of #48 pointed this out. Checked against nginx 1.26 with one certificate for all three names: their own names reach the console and the webmail, anything else gets 421; the e2e case checks it too. --- e2e/cases/proxy-snippets.sh | 4 ++++ internal/compose/compose_test.go | 6 +++++- .../compose/templates/nginx-inbuxa.conf.tmpl | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/e2e/cases/proxy-snippets.sh b/e2e/cases/proxy-snippets.sh index ce33ef4..0348993 100644 --- a/e2e/cases/proxy-snippets.sh +++ b/e2e/cases/proxy-snippets.sh @@ -58,6 +58,10 @@ CODE=$(via mail.$D -o /dev/null -w '%{http_code}' https://mail.$D/.well-known/jm case "$CODE" in 30*|200|401) ok "the mail server answers on https://mail.$D ($CODE)" ;; *) bad "https://mail.$D/.well-known/jmap answered $CODE" ;; esac via admin.$D https://admin.$D/ | grep -q "content=\"https://mail.$D\"" && ok "the console, pointed at https://mail.$D" || bad "the console is not served through nginx" via webmail.$D https://webmail.$D/api/health | grep -q '"ok":true' && ok "the webmail is healthy through nginx" || bad "the webmail is not healthy through nginx" +# A browser reuses the webmail's connection for any name its certificate +# covers: a request for the mail server that arrives that way is sent back. +CODE=$(via webmail.$D -o /dev/null -w '%{http_code}' -H "Host: mail.$D" https://webmail.$D/.well-known/jmap) +[ "$CODE" = 421 ] && ok "a request for mail.$D on the webmail's connection gets 421" || bad "a request for mail.$D on the webmail's connection answered $CODE" ADMIN_FP=$(openssl x509 -in /etc/ssl/inbuxa/admin.$D.fullchain.pem -noout -fingerprint -sha256) WEBMAIL_FP=$(openssl x509 -in /etc/ssl/inbuxa/webmail.$D.fullchain.pem -noout -fingerprint -sha256) [ "$(fp admin.$D)" = "$ADMIN_FP" ] && ok "the console is served with nginx's certificate" || bad "the console's certificate is not nginx's" diff --git a/internal/compose/compose_test.go b/internal/compose/compose_test.go index ece4cbe..eed1051 100644 --- a/internal/compose/compose_test.go +++ b/internal/compose/compose_test.go @@ -236,6 +236,7 @@ func TestProxySnippetsRouteByName(t *testing.T) { "listen 127.0.0.1:8444 ssl proxy_protocol;", "real_ip_header proxy_protocol;", "~^(autoconfig|autodiscover|mta-sts|ua-auto-config)\\.;", "location /.well-known/acme-challenge/", + "listen 127.0.0.1:8444 ssl proxy_protocol default_server;", "ssl_reject_handshake on;", "return 421;", } { if !strings.Contains(string(nginx), want) { t.Errorf("nginx-inbuxa.conf lacks %q", want) @@ -246,7 +247,7 @@ func TestProxySnippetsRouteByName(t *testing.T) { t.Errorf("nginx-inbuxa.conf still has %q: the router holds 443 and the server's names are its own", bad) } } - if strings.Count(string(nginx), "listen 127.0.0.1:8444") != 2 { + if strings.Count(string(nginx), "listen 127.0.0.1:8444 ssl proxy_protocol;") != 2 { t.Errorf("the console and the webmail are not both behind the router:\n%s", nginx) } @@ -277,6 +278,9 @@ func TestProxySnippetsWithoutRoutingStayAsTheyWere(t *testing.T) { t.Errorf("nginx-inbuxa.conf lacks %q", want) } } + if strings.Contains(string(nginx), "return 421;") { + t.Error("the fallback for the router's port is written without the router") + } c, _ := render(t, s) if strings.Contains(c, ":443\"") { t.Errorf("the server's HTTPS is published without the router:\n%s", c) diff --git a/internal/compose/templates/nginx-inbuxa.conf.tmpl b/internal/compose/templates/nginx-inbuxa.conf.tmpl index ef0d2b7..a3f9fa3 100644 --- a/internal/compose/templates/nginx-inbuxa.conf.tmpl +++ b/internal/compose/templates/nginx-inbuxa.conf.tmpl @@ -173,3 +173,19 @@ server { } } {{- end}} +{{- if .RoutesByName}} + +# Any other name asked of {{.FrontTLSBind}}: 421, so the browser opens a new +# connection for it. A browser reuses a connection for every name its +# certificate covers, so with one certificate for the console's, the +# webmail's and the mail server's names, a request for the mail server can +# arrive here over the webmail's connection, without the router seeing its +# name. Answered from here, it would get the console or the webmail. No +# certificate: the router sends this port only the two names above, and a +# handshake for any other is refused. +server { + listen {{.FrontTLSBind}} ssl proxy_protocol default_server; + ssl_reject_handshake on; + return 421; +} +{{- end}} -- 2.54.0