From 965cebb47273b9d99c797d9d73078bb17215843a Mon Sep 17 00:00:00 2001 From: Jon Fox Date: Thu, 12 Feb 2026 13:26:24 -0800 Subject: [PATCH] Fix port matching false positive in server discovery Use strings.HasSuffix instead of strings.Contains to prevent ports like :8080 from being incorrectly matched as the :80 server. Co-Authored-By: Claude Opus 4.6 --- caddy/routes.go | 2 +- caddy/routes_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/caddy/routes.go b/caddy/routes.go index 59336bc..76919a7 100644 --- a/caddy/routes.go +++ b/caddy/routes.go @@ -88,7 +88,7 @@ func (c *CaddyClient) discoverServerName() { continue } for _, addr := range srv.Listen { - if strings.Contains(addr, ":80") { + if strings.HasSuffix(addr, ":80") { c.serverName = name return } diff --git a/caddy/routes_test.go b/caddy/routes_test.go index c062697..609930c 100644 --- a/caddy/routes_test.go +++ b/caddy/routes_test.go @@ -209,6 +209,22 @@ func TestDiscoverServerName(t *testing.T) { } }) + t.Run("does not match :8080 as :80", func(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(map[string]any{ + "wrong": map[string]any{"listen": []string{":8080"}}, + "right": map[string]any{"listen": []string{":80"}}, + }) + })) + defer ts.Close() + + c := newTestClient(ts, "placeholder") + c.discoverServerName() + if c.serverName != "right" { + t.Errorf("expected right, got %s", c.serverName) + } + }) + t.Run("falls back to srv1 when no :80 server", func(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _ = json.NewEncoder(w).Encode(map[string]any{