Validate format of timeZone

This commit is contained in:
Jordan Liggitt
2023-01-28 10:30:46 -05:00
parent 5a2dd64801
commit 7e19850f31
2 changed files with 646 additions and 22 deletions

View File

@@ -18,10 +18,12 @@ package validation
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/robfig/cron/v3"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
unversionedvalidation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
@@ -502,6 +504,16 @@ func validateScheduleFormat(schedule string, timeZone *string, fldPath *field.Pa
return allErrs
}
// https://data.iana.org/time-zones/theory.html#naming
// * A name must not be empty, or contain '//', or start or end with '/'.
// * Do not use the file name components '.' and '..'.
// * Within a file name component, use only ASCII letters, '.', '-' and '_'.
// * Do not use digits, as that might create an ambiguity with POSIX TZ strings.
// * A file name component must not exceed 14 characters or start with '-'
//
// 0-9 and + characters are tolerated to accommodate legacy compatibility names
var validTimeZoneCharacters = regexp.MustCompile(`^[A-Za-z\.\-_0-9+]{1,14}$`)
func validateTimeZone(timeZone *string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if timeZone == nil {
@@ -513,6 +525,13 @@ func validateTimeZone(timeZone *string, fldPath *field.Path) field.ErrorList {
return allErrs
}
for _, part := range strings.Split(*timeZone, "/") {
if part == "." || part == ".." || strings.HasPrefix(part, "-") || !validTimeZoneCharacters.MatchString(part) {
allErrs = append(allErrs, field.Invalid(fldPath, timeZone, fmt.Sprintf("unknown time zone %s", *timeZone)))
return allErrs
}
}
if strings.EqualFold(*timeZone, "Local") {
allErrs = append(allErrs, field.Invalid(fldPath, timeZone, "timeZone must be an explicit time zone as defined in https://www.iana.org/time-zones"))
}