Skip to content

Commit 95689d6

Browse files
committed
httpcaddyfile: Fix missing TLS connection policies when auto_https is default (#7325)
1 parent 6610e2f commit 95689d6

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

caddyconfig/httpcaddyfile/httptype.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ func (st *ServerType) serversFromPairings(
822822
// https://caddy.community/t/making-sense-of-auto-https-and-why-disabling-it-still-serves-https-instead-of-http/9761
823823
createdTLSConnPolicies, ok := sblock.pile["tls.connection_policy"]
824824
hasTLSEnabled := (ok && len(createdTLSConnPolicies) > 0) ||
825-
(addr.Host != "" && srv.AutoHTTPS != nil && !slices.Contains(srv.AutoHTTPS.Skip, addr.Host))
825+
(addr.Host != "" && (srv.AutoHTTPS == nil || !slices.Contains(srv.AutoHTTPS.Skip, addr.Host)))
826826

827827
// we'll need to remember if the address qualifies for auto-HTTPS, so we
828828
// can add a TLS conn policy if necessary

caddyconfig/httpcaddyfile/httptype_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package httpcaddyfile
22

33
import (
4+
"encoding/json"
45
"testing"
56

67
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
8+
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
79
)
810

911
func TestMatcherSyntax(t *testing.T) {
@@ -209,3 +211,54 @@ func TestGlobalOptions(t *testing.T) {
209211
}
210212
}
211213
}
214+
215+
func TestDefaultSNIWithoutHTTPS(t *testing.T) {
216+
caddyfileStr := `{
217+
default_sni my-sni.com
218+
}
219+
example.com {
220+
file_server
221+
}`
222+
223+
adapter := caddyfile.Adapter{
224+
ServerType: ServerType{},
225+
}
226+
227+
result, _, err := adapter.Adapt([]byte(caddyfileStr), nil)
228+
if err != nil {
229+
t.Fatalf("Failed to adapt Caddyfile: %v", err)
230+
}
231+
232+
var config struct {
233+
Apps struct {
234+
HTTP struct {
235+
Servers map[string]*caddyhttp.Server `json:"servers"`
236+
} `json:"http"`
237+
} `json:"apps"`
238+
}
239+
240+
if err := json.Unmarshal(result, &config); err != nil {
241+
t.Fatalf("Failed to unmarshal JSON config: %v", err)
242+
}
243+
244+
server, ok := config.Apps.HTTP.Servers["srv0"]
245+
if !ok {
246+
t.Fatalf("Expected server 'srv0' to be created")
247+
}
248+
249+
if len(server.TLSConnPolicies) == 0 {
250+
t.Fatalf("Expected TLS connection policies to be generated, got none")
251+
}
252+
253+
found := false
254+
for _, policy := range server.TLSConnPolicies {
255+
if policy.DefaultSNI == "my-sni.com" {
256+
found = true
257+
break
258+
}
259+
}
260+
261+
if !found {
262+
t.Errorf("Expected default_sni 'my-sni.com' in TLS connection policies, but it was missing. Generated JSON: %s", string(result))
263+
}
264+
}

0 commit comments

Comments
 (0)