diff --git a/modules/util/time_str.go b/modules/util/time_str.go index 81b132c3db3..c498e6ebe13 100644 --- a/modules/util/time_str.go +++ b/modules/util/time_str.go @@ -40,6 +40,7 @@ var timeStrGlobalVars = sync.OnceValue(func() *timeStrGlobalVarsType { }) func TimeEstimateParse(timeStr string) (int64, error) { + timeStr = strings.TrimSpace(timeStr) if timeStr == "" { return 0, nil } @@ -51,7 +52,13 @@ func TimeEstimateParse(timeStr string) (int64, error) { if matches[0][0] != 0 || matches[len(matches)-1][1] != len(timeStr) { return 0, fmt.Errorf("invalid time string: %s", timeStr) } + prevEnd := 0 for _, match := range matches { + // only whitespace may separate two units, otherwise the string contains invalid content like "1h x 2m" + if strings.TrimSpace(timeStr[prevEnd:match[0]]) != "" { + return 0, fmt.Errorf("invalid time string: %s", timeStr) + } + prevEnd = match[1] amount, err := strconv.ParseInt(timeStr[match[2]:match[3]], 10, 64) if err != nil { return 0, fmt.Errorf("invalid time string: %v", err) diff --git a/modules/util/time_str_test.go b/modules/util/time_str_test.go index 8d1de51c8e6..4efe902bee5 100644 --- a/modules/util/time_str_test.go +++ b/modules/util/time_str_test.go @@ -22,6 +22,9 @@ func TestTimeStr(t *testing.T) { {"1s", 1, false}, {"1h 1m 1s", 3600 + 60 + 1, false}, {"1d1x", 0, true}, + {"1h 2x 3m", 0, true}, + {"1h_2m", 0, true}, + {"1h,1m", 0, true}, } for _, test := range tests { t.Run(test.input, func(t *testing.T) {