From ae6c916f0f2b2b8386764461c111f2fa705c2f7b Mon Sep 17 00:00:00 2001 From: Brendan Creane Date: Thu, 5 Nov 2026 08:32:05 -0800 Subject: [PATCH] ipn/ipnlocal: normalize the serve hostname before the config lookup (#22141) Serve looked up its config with the name exactly as the client sent it. Config keys are lowercase with no trailing dot, so a plain HTTP request for "svelte.example.ts.net." or "Svelte.Example.TS.net" got a 404. HTTPS had the same gap for case. TLS rejects a trailing dot in SNI but does not lowercase it, so a client that sent mixed-case SNI failed the handshake with "no webserver configured". Browsers and curl lowercase the hostname before connecting, which hid that path. Lowercase the name and trim the trailing dot in both branches of getServeHandler, and lowercase the SNI in the cert callback. Fixes #21538 Fixes #21986 Signed-off-by: Brendan Creane --- ipn/ipnlocal/serve.go | 16 +++++- ipn/ipnlocal/serve_test.go | 113 +++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 3 deletions(-) diff --git a/ipn/ipnlocal/serve.go b/ipn/ipnlocal/serve.go index da0e14928..5a0413632 100644 --- a/ipn/ipnlocal/serve.go +++ b/ipn/ipnlocal/serve.go @@ -842,11 +842,16 @@ func (b *LocalBackend) getServeHandler(r *http.Request) (_ ipn.HTTPHandlerView, if host, _, err := net.SplitHostPort(hostname); err == nil { hostname = host } + // DNS names are case-insensitive, and a trailing dot marks a name + // as fully qualified. Serve config keys are lowercase with no + // trailing dot, so normalize the Host header to match. + hostname = strings.ToLower(strings.TrimSuffix(hostname, ".")) if !strings.HasSuffix(hostname, tcd) { hostname += tcd } } else { - hostname = r.TLS.ServerName + // TLS rejects a trailing dot in SNI but does not lowercase it. + hostname = strings.ToLower(r.TLS.ServerName) } sctx, ok := serveHTTPContextKey.ValueOk(r.Context()) @@ -1446,14 +1451,19 @@ func (b *LocalBackend) getTLSServeCertForPort(port uint16, forVIPService tailcfg if cert, ok := b.getACMETLSALPNCert(hi); ok { return cert, nil } - _, ok := b.webServerConfig(hi.ServerName, forVIPService, port) + // Serve config keys and cert domains are lowercase; SNI is not + // guaranteed to be. The ACME challenge lookup above keeps the + // name as sent, since it is matched against the name the ACME + // server dials. + name := strings.ToLower(hi.ServerName) + _, ok := b.webServerConfig(name, forVIPService, port) if !ok { return nil, errors.New("no webserver configured for name/port") } ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() - pair, err := b.GetCertPEM(ctx, hi.ServerName) + pair, err := b.GetCertPEM(ctx, name) if err != nil { return nil, err } diff --git a/ipn/ipnlocal/serve_test.go b/ipn/ipnlocal/serve_test.go index b65ebb56d..f1c1d720a 100644 --- a/ipn/ipnlocal/serve_test.go +++ b/ipn/ipnlocal/serve_test.go @@ -23,6 +23,7 @@ import ( "os" "path/filepath" "reflect" + "slices" "strconv" "strings" "sync" @@ -41,6 +42,7 @@ import ( "tailscale.com/tailcfg/peercap" "tailscale.com/tsd" "tailscale.com/tstest" + "tailscale.com/tstest/tlstest" "tailscale.com/types/logger" "tailscale.com/types/logid" "tailscale.com/types/netmap" @@ -2455,3 +2457,114 @@ func TestReverseProxyRechecksTailscaledSocketPerDial(t *testing.T) { t.Fatalf("dial after the symlink swap = %v; want ErrProxyToTailscaledSocket", err) } } + +// TestServeWebHandlerHostNormalization checks that a request is matched +// to its serve config regardless of how the client spells the name. DNS +// names are case-insensitive and may carry a trailing dot, while serve +// config keys are always lowercase with no trailing dot. For plain HTTP +// the name comes from the Host header. For HTTPS it comes from SNI, which +// cannot carry a trailing dot but is not lowercased by TLS. (#21538, #21986) +func TestServeWebHandlerHostNormalization(t *testing.T) { + b := newTestBackend(t) + b.pm.currentProfile = (&ipn.LoginProfile{ + ID: "id0", + NetworkProfile: ipn.NetworkProfile{MagicDNSName: "example.ts.net"}, + }).View() + conf := &ipn.ServeConfig{ + Web: map[ipn.HostPort]*ipn.WebServerConfig{ + "svelte.example.ts.net:12075": { + Handlers: map[string]*ipn.HTTPHandler{"/": {Text: "hello"}}, + }, + }, + } + if err := b.SetServeConfig(conf, ""); err != nil { + t.Fatal(err) + } + tests := []struct { + host string // Host header for plain HTTP; SNI for HTTPS + tls bool + wantCode int + }{ + {host: "svelte.example.ts.net:12075", wantCode: http.StatusOK}, + {host: "svelte:12075", wantCode: http.StatusOK}, + {host: "svelte.example.ts.net.:12075", wantCode: http.StatusOK}, + {host: "svelte.example.ts.net.", wantCode: http.StatusOK}, + {host: "svelte.:12075", wantCode: http.StatusOK}, + {host: "Svelte.Example.TS.net:12075", wantCode: http.StatusOK}, + {host: "SVELTE:12075", wantCode: http.StatusOK}, + {host: "other.example.ts.net:12075", wantCode: http.StatusNotFound}, + {host: "svelte.example.ts.net..:12075", wantCode: http.StatusNotFound}, + {host: "svelte.example.ts.net", tls: true, wantCode: http.StatusOK}, + {host: "Svelte.Example.TS.net", tls: true, wantCode: http.StatusOK}, + {host: "other.example.ts.net", tls: true, wantCode: http.StatusNotFound}, + } + for _, tt := range tests { + name := tt.host + if tt.tls { + name = "tls/" + name + } + t.Run(name, func(t *testing.T) { + req := &http.Request{Host: tt.host, URL: &url.URL{Path: "/"}, RequestURI: "/"} + if tt.tls { + req.TLS = &tls.ConnectionState{ServerName: tt.host} + } + req = req.WithContext(serveHTTPContextKey.WithValue(req.Context(), &serveHTTPContext{ + DestPort: 12075, + SrcAddr: netip.MustParseAddrPort("100.64.0.2:1234"), + })) + w := httptest.NewRecorder() + b.serveWebHandler(w, req) + if w.Code != tt.wantCode { + t.Errorf("%s: got %d, want %d", name, w.Code, tt.wantCode) + } + }) + } +} + +// TestGetTLSServeCertForPortLowercasesSNI checks that the serve TLS cert +// callback lowercases the SNI before both the serve config lookup and the +// cert lookup. Cert domains are lowercase and matched exactly, so passing +// the SNI through as sent made a mixed-case ClientHello fail the handshake. +func TestGetTLSServeCertForPortLowercasesSNI(t *testing.T) { + b := newTestBackend(t) + conf := &ipn.ServeConfig{ + Web: map[ipn.HostPort]*ipn.WebServerConfig{ + "svelte.example.ts.net:443": { + Handlers: map[string]*ipn.HTTPHandler{"/": {Text: "hello"}}, + }, + }, + } + if err := b.SetServeConfig(conf, ""); err != nil { + t.Fatal(err) + } + + var gotDomain string + restore := HookGetCertPEM.SetForTest(func(_ context.Context, _ *LocalBackend, domain string, _ time.Duration) (*TLSCertKeyPair, error) { + gotDomain = domain + if domain != "svelte.example.ts.net" { + return nil, fmt.Errorf("invalid domain %q", domain) + } + d := tlstest.Domain(domain) + return &TLSCertKeyPair{CertPEM: d.CertPEM(), KeyPEM: d.KeyPEM()}, nil + }) + defer restore() + + getCert := b.getTLSServeCertForPort(443, "") + for _, sni := range []string{"svelte.example.ts.net", "Svelte.Example.TS.net"} { + gotDomain = "" + cert, err := getCert(&tls.ClientHelloInfo{ServerName: sni}) + if err != nil { + t.Errorf("SNI %q: %v", sni, err) + continue + } + if gotDomain != "svelte.example.ts.net" { + t.Errorf("SNI %q: cert looked up for %q", sni, gotDomain) + } + if cert == nil || cert.Leaf == nil || !slices.Contains(cert.Leaf.DNSNames, "svelte.example.ts.net") { + t.Errorf("SNI %q: unexpected cert %+v", sni, cert) + } + } + if _, err := getCert(&tls.ClientHelloInfo{ServerName: "Other.Example.TS.net"}); err == nil { + t.Error("unconfigured name: got cert, want error") + } +}