Skip to content

Commit f36fdc7

Browse files
Merge pull request #6184 from arpitjain099/fix/image-match-invalid-regex
fix: do not panic on an invalid image name regexp
2 parents 79bb1aa + d32ba3f commit f36fdc7

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

api/internal/image/image.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ func IsImageMatched(s, t string) bool {
2121
// using any OCI-valid digest algorithm match consistently with Split,
2222
// which accepts any algorithm.
2323
// See https://github.com/opencontainers/image-spec/blob/main/descriptor.md#digests
24-
pattern, _ := regexp.Compile("^" + t + "(:[a-zA-Z0-9_.{}-]*)?(@[a-zA-Z0-9]+([.+_-][a-zA-Z0-9]+)*:[a-zA-Z0-9_.{}-]*)?$")
24+
// The name t comes from kustomization images[].name and is interpolated
25+
// into the pattern directly, so it can be an invalid regexp (for example
26+
// "["). When it fails to compile, treat it as matching nothing rather than
27+
// dereferencing a nil *Regexp, which would panic during the build.
28+
pattern, err := regexp.Compile("^" + t + "(:[a-zA-Z0-9_.{}-]*)?(@[a-zA-Z0-9]+([.+_-][a-zA-Z0-9]+)*:[a-zA-Z0-9_.{}-]*)?$")
29+
if err != nil {
30+
return false
31+
}
2532
return pattern.MatchString(s)
2633
}
2734

api/internal/image/image_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ func TestIsImageMatched(t *testing.T) {
7575
name: "nginx",
7676
isMatched: false,
7777
},
78+
{
79+
// A name that is not a valid regexp must not panic. It can't
80+
// compile, so it should simply match nothing.
81+
testName: "name is an invalid regexp",
82+
value: "nginx",
83+
name: "[",
84+
isMatched: false,
85+
},
7886
}
7987

8088
for _, tc := range testCases {

api/krusty/transformersimage_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,42 @@ spec:
401401
image: solsa-echo:foo
402402
`)
403403
}
404+
405+
// An images[].name value that is not a valid regexp (here "[") used to crash
406+
// the build with a nil-pointer panic. It should be treated as matching no
407+
// container image, so the resource passes through untouched.
408+
func TestTransfomersImageInvalidNameRegexp(t *testing.T) {
409+
th := kusttest_test.MakeHarness(t)
410+
th.WriteK(".", `
411+
resources:
412+
- deploy.yaml
413+
images:
414+
- name: "["
415+
newTag: v2
416+
`)
417+
th.WriteF("deploy.yaml", `
418+
apiVersion: apps/v1
419+
kind: Deployment
420+
metadata:
421+
name: deploy
422+
spec:
423+
template:
424+
spec:
425+
containers:
426+
- name: nginx
427+
image: nginx:1.7.9
428+
`)
429+
m := th.Run(".", th.MakeDefaultOptions())
430+
th.AssertActualEqualsExpected(m, `
431+
apiVersion: apps/v1
432+
kind: Deployment
433+
metadata:
434+
name: deploy
435+
spec:
436+
template:
437+
spec:
438+
containers:
439+
- image: nginx:1.7.9
440+
name: nginx
441+
`)
442+
}

0 commit comments

Comments
 (0)