Skip to content

Guard against negative capture group in regexp string transform#332

Open
arpitjain099 wants to merge 1 commit into
crossplane-contrib:mainfrom
arpitjain099:fix/regexp-negative-group
Open

Guard against negative capture group in regexp string transform#332
arpitjain099 wants to merge 1 commit into
crossplane-contrib:mainfrom
arpitjain099:fix/regexp-negative-group

Conversation

@arpitjain099

Copy link
Copy Markdown

What this does

stringRegexpTransform picks a capture group by index. The bounds check only covers the upper end:

g := ptr.Deref[int](r.Group, 0)
if len(groups) == 0 || g >= len(groups) {
    return "", errors.Errorf(errStringTransformTypeRegexpNoMatch, r.Match, g)
}
return groups[g], nil

Group is a *int, so a Composition can set it to a negative value. A negative g passes both len(groups) == 0 and g >= len(groups), then groups[g] panics with an index out of range.

This adds the missing lower-bound check (g < 0) so a negative group returns the existing no-match error instead of panicking.

Testing

Added a test case with Group: -1. Without the fix it panics:

panic: runtime error: index out of range [-1]
    ... stringRegexpTransform ... transforms.go:399

With the fix go test ./... passes and the negative group returns the no-match error like an out-of-range positive group already does.

The regexp string transform checks that the requested capture group index
is within the upper bound of the matched groups, but not that it is
non-negative. A negative group value in the Composition (Group is *int)
slips past len(groups) == 0 || g >= len(groups) and then panics on
groups[g] with an index out of range.

Add the lower-bound check so a negative group returns the existing
no-match error instead of panicking. Includes a test covering a negative
group value.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant