Validate custom regular expressions on detector initialization (#1010)

* Validate custom regular expressions on detector initialization

* Add regex name to error message
This commit is contained in:
Miccah
2023-01-09 17:30:47 -06:00
committed by GitHub
parent 74831f63d5
commit e5ede17c77
2 changed files with 13 additions and 0 deletions
+6
View File
@@ -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
}
+7
View File
@@ -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) {