host.go 1.3 KB

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