Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pkg/validation/strfmt/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
"w": 7 * 24 * time.Hour,
}

durationMatcher = regexp.MustCompile(`((\d+)\s*([A-Za-zµ]+))`)
durationMatcher = regexp.MustCompile(`(((?:-\s?)?\d+)\s*([A-Za-zµ]+))`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you clarify what additional values this allows? it's not clear to me that accepting new internal whitespaces between - and the number is a good idea here

)

// IsDuration returns true if the provided string is a valid duration
Copy link
Member

@liggitt liggitt Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ParseDuration uses durationMatcher.FindAllStringSubmatch with an unpinned regex, which will skip and ignore any random junk before / between / after matches of the regex.

See https://go.dev/play/p/fCur5WS4zK5 for the type of nonsense IsDuration would accept currently.

To actually address the problem of decimal junk being accepted but ignored, we need the regex to fully define what we allow, and be pinned to the beginning / end of the string. Something like this

singleDurationMatcher = `-?(\d+)\s*([A-Za-zµ]+)`
// I'm not sure what separators we *intend* to allow between the items, whitespace? comma? something else?
multiDuration = `^(`+singleDurationMatcher+`)(\s*`+singleDurationMatcher+`)*$`
durationMatcher = regexp.MustCompile(multiDuration)
...

Expand Down Expand Up @@ -94,10 +94,18 @@ func ParseDuration(cand string) (time.Duration, error) {
ok := false
for _, match := range durationMatcher.FindAllStringSubmatch(cand, -1) {

factor, err := strconv.Atoi(match[2]) // converts string to int
// remove possible leading - and spaces
value, negative := strings.CutPrefix(match[2], "-")

// if the string is a valid duration, parse it
factor, err := strconv.Atoi(strings.TrimSpace(value)) // converts string to int
if err != nil {
return 0, err
}

if negative {
factor = -factor
}
unit := strings.ToLower(strings.TrimSpace(match[3]))

for _, variants := range timeUnits {
Expand Down
8 changes: 7 additions & 1 deletion pkg/validation/strfmt/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ func TestDurationParser(t *testing.T) {
}

for str, dur := range testcases {
testDurationParser(t, str, dur)
t.Run(str, func(t *testing.T) {
testDurationParser(t, str, dur)

// negative duration
testDurationParser(t, "-"+str, -dur)
testDurationParser(t, "- "+str, -dur)
})
}
}
func TestIsDuration_Caveats(t *testing.T) {
Expand Down
Loading