@marcel91200 / caddy Mirror · read-only / commits / 97e61c16
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

httpcaddyfile: Sort site blocks with wildcards last (fix #3410)

Matthew Holt committed Jun 3, 2020 at 09:35 UTC 97e61c16a3a0e74be0810b4714de1dab345fba8b
1 file changed +13
caddyconfig/httpcaddyfile/httptype.go
+13
@@ -377,7 +377,11 @@ func (st *ServerType) serversFromPairings(
377 // but I don't expect many blocks will have THAT many keys...
378 var iLongestPath, jLongestPath string
379 var iLongestHost, jLongestHost string
380 + var iWildcardHost, jWildcardHost bool
381 for _, addr := range p.serverBlocks[i].keys {
382 + if strings.Contains(addr.Host, "*.") {
383 + iWildcardHost = true
384 + }
385 if specificity(addr.Host) > specificity(iLongestHost) {
386 iLongestHost = addr.Host
387 }
@@ -386,6 +390,9 @@ func (st *ServerType) serversFromPairings(
390 }
391 }
392 for _, addr := range p.serverBlocks[j].keys {
393 + if strings.Contains(addr.Host, "*.") {
394 + jWildcardHost = true
395 + }
396 if specificity(addr.Host) > specificity(jLongestHost) {
397 jLongestHost = addr.Host
398 }
@@ -393,6 +400,12 @@ func (st *ServerType) serversFromPairings(
400 jLongestPath = addr.Path
401 }
402 }
403 + if iWildcardHost != jWildcardHost {
404 + // site blocks that have a key with a wildcard in the hostname
405 + // must always be less specific than blocks without one; see
406 + // https://github.com/caddyserver/caddy/issues/3410
407 + return jWildcardHost && !iWildcardHost
408 + }
409 if specificity(iLongestHost) == specificity(jLongestHost) {
410 return len(iLongestPath) > len(jLongestPath)
411 }