forked from spiffe/spire
/
dns.go
51 lines (43 loc) · 1.22 KB
/
dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package x509util
import (
"errors"
"fmt"
"regexp"
"strings"
)
var isDNSLabel = regexp.MustCompile(`^[a-zA-Z0-9]([-]*[a-zA-Z0-9])+$`).MatchString
// ValidateDNS validates that provided string is a valid DNS name
func ValidateDNS(dns string) error {
// follow https://tools.ietf.org/html/rfc5280#section-4.2.1.6
// do not allow empty or the technically valid DNS " "
dns = strings.TrimSpace(dns)
if len(dns) == 0 {
return errors.New("empty or only whitespace")
}
// handle up to 255 characters
if len(dns) > 255 {
return errors.New("length exceeded")
}
// a DNS is split into labels by "."
splitDNS := strings.Split(dns, ".")
for _, label := range splitDNS {
if err := validateDNSLabel(label); err != nil {
return err
}
}
return nil
}
func validateDNSLabel(label string) error {
// follow https://tools.ietf.org/html/rfc5280#section-4.2.1.6 guidance
// <label> ::= <let-dig> [ [ <ldh-str> ] <let-dig> ]
// <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
switch {
case len(label) == 0:
return errors.New("label is empty")
case len(label) > 63:
return fmt.Errorf("label length exceeded: %v", label)
case !isDNSLabel(label):
return fmt.Errorf("label does not match regex: %v", label)
}
return nil
}