1package awsrulesfn
2
3import (
4 "net"
5 "strings"
6
7 smithyhttp "github.com/aws/smithy-go/transport/http"
8)
9
10// IsVirtualHostableS3Bucket returns if the input is a DNS compatible bucket
11// name and can be used with Amazon S3 virtual hosted style addressing. Similar
12// to [rulesfn.IsValidHostLabel] with the added restriction that the length of label
13// must be [3:63] characters long, all lowercase, and not formatted as an IP
14// address.
15func IsVirtualHostableS3Bucket(input string, allowSubDomains bool) bool {
16 // input should not be formatted as an IP address
17 // NOTE: this will technically trip up on IPv6 hosts with zone IDs, but
18 // validation further down will catch that anyway (it's guaranteed to have
19 // unfriendly characters % and : if that's the case)
20 if net.ParseIP(input) != nil {
21 return false
22 }
23
24 var labels []string
25 if allowSubDomains {
26 labels = strings.Split(input, ".")
27 } else {
28 labels = []string{input}
29 }
30
31 for _, label := range labels {
32 // validate special length constraints
33 if l := len(label); l < 3 || l > 63 {
34 return false
35 }
36
37 // Validate no capital letters
38 for _, r := range label {
39 if r >= 'A' && r <= 'Z' {
40 return false
41 }
42 }
43
44 // Validate valid host label
45 if !smithyhttp.ValidHostLabel(label) {
46 return false
47 }
48 }
49
50 return true
51}