Skip to content

Commit 7ffb640

Browse files
authored
httpcaddyfile: Fix missing TLS connection policies when auto_https is default (#7325) (#7507)
1 parent d7b21c6 commit 7ffb640

File tree

2 files changed

+53
-1
lines changed

2 files changed

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

0 commit comments

Comments
 (0)