Add support for redirect https to https when from-to-www-redirect is defined

This commit is contained in:
Manuel Alejandro de Brito Fontes 2019-01-09 00:33:16 -03:00
parent 35f5a6ce1f
commit a3bcbeb3d2
No known key found for this signature in database
GPG key ID: 786136016A8BA02A
6 changed files with 196 additions and 31 deletions

View file

@ -30,6 +30,7 @@ import (
"math/big"
"net"
"strconv"
"strings"
"time"
"github.com/zakjan/cert-chain-resolver/certUtil"
@ -508,3 +509,21 @@ func FullChainCert(in string, fs file.Filesystem) ([]byte, error) {
return certUtil.EncodeCertificates(certs), nil
}
// IsValidHostname checks if a hostname is valid in a list of common names
func IsValidHostname(hostname string, commonNames []string) bool {
for _, cn := range commonNames {
if strings.EqualFold(hostname, cn) {
return true
}
labels := strings.Split(hostname, ".")
labels[0] = "*"
candidate := strings.Join(labels, ".")
if strings.EqualFold(candidate, cn) {
return true
}
}
return false
}

View file

@ -205,3 +205,39 @@ func newCA(name string) (*keyPair, error) {
Cert: cert,
}, nil
}
func TestIsValidHostname(t *testing.T) {
cases := map[string]struct {
Hostname string
CN []string
Valid bool
}{
"when there is no common names": {
"foo.bar",
[]string{},
false,
},
"when there is a match for foo.bar": {
"foo.bar",
[]string{"foo.bar"},
true,
},
"when there is a wildcard match for foo.bar": {
"foo.bar",
[]string{"*.bar"},
true,
},
"when there is a wrong wildcard for *.bar": {
"invalid.foo.bar",
[]string{"*.bar"},
false,
},
}
for k, tc := range cases {
valid := IsValidHostname(tc.Hostname, tc.CN)
if valid != tc.Valid {
t.Errorf("%s: expected '%v' but returned %v", k, tc.Valid, valid)
}
}
}