From e5ede17c775a2669899f12a908f5b227aa21477b Mon Sep 17 00:00:00 2001 From: Miccah Date: Mon, 9 Jan 2023 17:30:47 -0600 Subject: [PATCH] Validate custom regular expressions on detector initialization (#1010) * Validate custom regular expressions on detector initialization * Add regex name to error message --- pkg/custom_detectors/validation.go | 6 ++++++ pkg/custom_detectors/validation_test.go | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/pkg/custom_detectors/validation.go b/pkg/custom_detectors/validation.go index 62bc14910..35f720297 100644 --- a/pkg/custom_detectors/validation.go +++ b/pkg/custom_detectors/validation.go @@ -2,6 +2,7 @@ package custom_detectors import ( "fmt" + "regexp" "strconv" "strings" ) @@ -23,6 +24,11 @@ func ValidateRegex(regex map[string]string) error { if len(regex) == 0 { return fmt.Errorf("no regex") } + for name, reg := range regex { + if _, err := regexp.Compile(reg); err != nil { + return fmt.Errorf("regex '%s': %w", name, err) + } + } return nil } diff --git a/pkg/custom_detectors/validation_test.go b/pkg/custom_detectors/validation_test.go index 1aa36f4e0..c5f7f56d6 100644 --- a/pkg/custom_detectors/validation_test.go +++ b/pkg/custom_detectors/validation_test.go @@ -53,6 +53,13 @@ func TestCustomDetectorsRegexValidation(t *testing.T) { input: map[string]string{}, wantErr: true, }, + { + name: "Test invalid regex", + input: map[string]string{ + "test": "!!?(?:?)[a-zA-Z0-9]{32}", + }, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {