Skip to content

Commit

Permalink
Validate format of timeZone
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Jan 30, 2023
1 parent 5a2dd64 commit 7e19850
Show file tree
Hide file tree
Showing 2 changed files with 646 additions and 22 deletions.
19 changes: 19 additions & 0 deletions pkg/apis/batch/validation/validation.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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"))
}
Expand Down

0 comments on commit 7e19850

Please sign in to comment.