dns.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2025 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package content
  14. import (
  15. "regexp"
  16. )
  17. const dns1123LabelFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
  18. const dns1123LabelErrMsg string = "a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character"
  19. // DNS1123LabelMaxLength is a label's max length in DNS (RFC 1123)
  20. const DNS1123LabelMaxLength int = 63
  21. var dns1123LabelRegexp = regexp.MustCompile("^" + dns1123LabelFmt + "$")
  22. // IsDNS1123Label tests for a string that conforms to the definition of a label in
  23. // DNS (RFC 1123).
  24. func IsDNS1123Label(value string) []string {
  25. var errs []string
  26. if len(value) > DNS1123LabelMaxLength {
  27. errs = append(errs, MaxLenError(DNS1123LabelMaxLength))
  28. }
  29. if !dns1123LabelRegexp.MatchString(value) {
  30. if dns1123SubdomainRegexp.MatchString(value) {
  31. // It was a valid subdomain and not a valid label. Since we
  32. // already checked length, it must be dots.
  33. errs = append(errs, "must not contain dots")
  34. } else {
  35. errs = append(errs, RegexError(dns1123LabelErrMsg, dns1123LabelFmt, "my-name", "123-abc"))
  36. }
  37. }
  38. return errs
  39. }
  40. const dns1123SubdomainFmt string = dns1123LabelFmt + "(\\." + dns1123LabelFmt + ")*"
  41. const dns1123SubdomainFmtCaseless string = "(?i)" + dns1123SubdomainFmt
  42. const dns1123SubdomainErrorMsg string = "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character"
  43. const dns1123SubdomainCaselessErrorMsg string = "an RFC 1123 subdomain must consist of alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character"
  44. // DNS1123SubdomainMaxLength is a subdomain's max length in DNS (RFC 1123)
  45. const DNS1123SubdomainMaxLength int = 253
  46. var dns1123SubdomainRegexp = regexp.MustCompile("^" + dns1123SubdomainFmt + "$")
  47. var dns1123SubdomainCaselessRegexp = regexp.MustCompile("^" + dns1123SubdomainFmtCaseless + "$")
  48. // IsDNS1123Subdomain tests for a string that conforms to the definition of a
  49. // subdomain in DNS (RFC 1123) lowercase.
  50. func IsDNS1123Subdomain(value string) []string {
  51. return isDNS1123Subdomain(value, false)
  52. }
  53. // IsDNS1123SubdomainCaseless tests for a string that conforms to the definition of a
  54. // subdomain in DNS (RFC 1123).
  55. //
  56. // Deprecated: API validation should never be caseless. Caseless validation is a vector
  57. // for bugs and failed uniqueness assumptions. For example, names like "foo.com" and
  58. // "FOO.COM" are both accepted as valid, but they are typically not treated as equal by
  59. // consumers (e.g. CSI and DRA driver names). This fails the "least surprise" principle and
  60. // can cause inconsistent behaviors.
  61. //
  62. // Note: This allows uppercase names but is not caseless — uppercase and lowercase are
  63. // treated as different values. Use IsDNS1123Subdomain for strict, lowercase validation
  64. // instead.
  65. func IsDNS1123SubdomainCaseless(value string) []string {
  66. return isDNS1123Subdomain(value, true)
  67. }
  68. func isDNS1123Subdomain(value string, caseless bool) []string {
  69. var errs []string
  70. if len(value) > DNS1123SubdomainMaxLength {
  71. errs = append(errs, MaxLenError(DNS1123SubdomainMaxLength))
  72. }
  73. errorMsg := dns1123SubdomainErrorMsg
  74. example := "example.com"
  75. regexp := dns1123SubdomainRegexp
  76. if caseless {
  77. errorMsg = dns1123SubdomainCaselessErrorMsg
  78. example = "Example.com"
  79. regexp = dns1123SubdomainCaselessRegexp
  80. }
  81. if !regexp.MatchString(value) {
  82. errs = append(errs, RegexError(errorMsg, dns1123SubdomainFmt, example))
  83. }
  84. return errs
  85. }