Fix: restrict validation to RFC1123 label name

This commit is contained in:
Tova-Rozovsky
2025-05-04 16:14:17 +03:00
parent 5f214247ca
commit 469d6fadcd
+11 -6
View File
@@ -31,6 +31,7 @@ const (
) )
var serviceIDRegexp = regexp.MustCompile("^[0-9a-f]{32}$") var serviceIDRegexp = regexp.MustCompile("^[0-9a-f]{32}$")
var dnsLabelRegexp = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`)
func ValidateServiceID(id string) bool { func ValidateServiceID(id string) bool {
return serviceIDRegexp.MatchString(id) return serviceIDRegexp.MatchString(id)
@@ -104,13 +105,17 @@ func (s *ServiceSpec) Validate() error {
return fmt.Errorf("invalid mode: %q", s.Mode) return fmt.Errorf("invalid mode: %q", s.Mode)
} }
if s.Name != "" { func (s *ServiceSpec) Validate() error {
if len(s.Name) > 253 { if s.Mode != "someExpectedMode" {
return fmt.Errorf("service name too long (max 253 characters): %q", s.Name) return fmt.Errorf("invalid mode: %q", s.Mode)
} }
dnsSubdomainRegexp := regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(?:\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`)
if !dnsSubdomainRegexp.MatchString(s.Name) { if s.Name != "" {
return fmt.Errorf("invalid service name %q: must be a valid DNS subdomain", s.Name) if len(s.Name) > 63 {
return fmt.Errorf("service name too long (max 63 characters): %q", s.Name)
}
if !dnsLabelRegexp.MatchString(s.Name) {
return fmt.Errorf("invalid service name: %q. Must comply with RFC 1123 label format", s.Name)
} }
} }