diff --git a/pkg/config/types.go b/pkg/config/types.go index e6389ee318..fe9f81f445 100644 --- a/pkg/config/types.go +++ b/pkg/config/types.go @@ -87,13 +87,14 @@ func newFilter(kind, namespace, name string) filter { } } +var submatchallRegex = regexp.MustCompile(`\[([^\[\]]*)\]`) + // ParseKinds parses the kinds if a single string contains comma separated kinds // {"1,2,3","4","5"} => {"1","2","3","4","5"} func parseKinds(in string) []filter { resources := []filter{} var resource filter - re := regexp.MustCompile(`\[([^\[\]]*)\]`) - submatchall := re.FindAllString(in, -1) + submatchall := submatchallRegex.FindAllString(in, -1) for _, element := range submatchall { element = strings.Trim(element, "[") element = strings.Trim(element, "]") diff --git a/pkg/validation/policy/validate.go b/pkg/validation/policy/validate.go index 26e6471f86..8ce0009ef9 100644 --- a/pkg/validation/policy/validate.go +++ b/pkg/validation/policy/validate.go @@ -47,17 +47,14 @@ var ( errOperationForbidden = errors.New("variables are forbidden in the path of a JSONPatch") ) +var allowedJsonPatch = regexp.MustCompile("^/") + // validateJSONPatchPathForForwardSlash checks for forward slash func validateJSONPatchPathForForwardSlash(patch string) error { // Replace all variables in PatchesJSON6902, all variable checks should have happened already. // This prevents further checks from failing unexpectedly. patch = variables.ReplaceAllVars(patch, func(s string) string { return "kyvernojsonpatchvariable" }) - re, err := regexp.Compile("^/") - if err != nil { - return err - } - jsonPatch, err := yaml.ToJSON([]byte(patch)) if err != nil { return err @@ -74,7 +71,7 @@ func validateJSONPatchPathForForwardSlash(patch string) error { return err } - val := re.MatchString(path) + val := allowedJsonPatch.MatchString(path) if !val { return fmt.Errorf("%s", path)