@marcel91200 / caddy Mirror · read-only / commits / 1c6c7714
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

caddyhttp: Fix edgecase with auto HTTP->HTTPS logic (#4243)

Francis Lavoie committed Jul 14, 2021 at 12:49 UTC 1c6c7714a38879f78c486fcb9c7c6e39fc39c6a5
2 files changed +31
caddytest/integration/autohttps_test.go
+20
@@ -103,3 +103,23 @@ func TestAutoHTTPRedirectsInsertedBeforeUserDefinedCatchAll(t *testing.T) {
103 tester.AssertGetResponse("http://foo.localhost:9080/", 200, "Foo")
104 tester.AssertGetResponse("http://baz.localhost:9080/", 200, "Baz")
105 }
106 +
107 +func TestAutoHTTPRedirectsInsertedBeforeUserDefinedCatchAllWithNoExplicitHTTPSite(t *testing.T) {
108 + tester := caddytest.NewTester(t)
109 + tester.InitServer(`
110 + {
111 + http_port 9080
112 + https_port 9443
113 + local_certs
114 + }
115 + http://:9080 {
116 + respond "Foo"
117 + }
118 + bar.localhost {
119 + respond "Bar"
120 + }
121 + `, "caddyfile")
122 + tester.AssertRedirect("http://bar.localhost:9080/", "https://bar.localhost/", http.StatusPermanentRedirect)
123 + tester.AssertGetResponse("http://foo.localhost:9080/", 200, "Foo")
124 + tester.AssertGetResponse("http://baz.localhost:9080/", 200, "Foo")
125 +}
modules/caddyhttp/server.go
+11
@@ -379,7 +379,9 @@ func (s *Server) hasTLSClientAuth() bool {
379 // that it is after any other host matcher but before any "catch-all"
380 // route without a host matcher.
381 func (s *Server) findLastRouteWithHostMatcher() int {
382 + foundHostMatcher := false
383 lastIndex := len(s.Routes)
384 +
385 for i, route := range s.Routes {
386 // since we want to break out of an inner loop, use a closure
387 // to allow us to use 'return' when we found a host matcher
@@ -388,6 +390,7 @@ func (s *Server) findLastRouteWithHostMatcher() int {
390 for _, matcher := range sets {
391 switch matcher.(type) {
392 case *MatchHost:
393 + foundHostMatcher = true
394 return true
395 }
396 }
@@ -401,6 +404,14 @@ func (s *Server) findLastRouteWithHostMatcher() int {
404 lastIndex = i + 1
405 }
406 }
407 +
408 + // If we didn't actually find a host matcher, return 0
409 + // because that means every defined route was a "catch-all".
410 + // See https://caddy.community/t/how-to-set-priority-in-caddyfile/13002/8
411 + if !foundHostMatcher {
412 + return 0
413 + }
414 +
415 return lastIndex
416 }
417