| 1 | package helpers |
| 2 | |
| 3 | import ( |
| 4 | "github.com/DefangLabs/secret-detector/pkg/secrets" |
| 5 | ) |
| 6 | |
| 7 | type DetectionVerifier func(string, string) bool |
| 8 | |
| 9 | type regexDetector struct { |
| 10 | secretType string |
| 11 | keyValueRegex *KeyValueRegex |
| 12 | valueRegex *ValueRegex |
| 13 | verifier DetectionVerifier |
| 14 | } |
| 15 | |
| 16 | func (d *regexDetector) SecretType() string { |
| 17 | return d.secretType |
| 18 | } |
| 19 | |
| 20 | func (d *regexDetector) Scan(in string) ([]secrets.DetectedSecret, error) { |
| 21 | res := make([]secrets.DetectedSecret, 0) |
| 22 | matches, err := d.keyValueRegex.FindAll(in) |
| 23 | for _, match := range matches { |
| 24 | if d.verifyDetection(match.Key, match.Value) { |
| 25 | res = append(res, secrets.DetectedSecret{Key: match.Key, Type: d.SecretType(), Value: match.Value}) |
| 26 | } |
| 27 | } |
| 28 | return res, err |
| 29 | } |
| 30 | |
| 31 | func (d *regexDetector) ScanMap(keyValueMap map[string]string) ([]secrets.DetectedSecret, error) { |
| 32 | res := make([]secrets.DetectedSecret, 0) |
| 33 | for key, value := range keyValueMap { |
| 34 | if d.valueRegex.Match(value) && d.verifyDetection(key, value) { |
| 35 | res = append(res, secrets.DetectedSecret{Key: key, Type: d.SecretType(), Value: value}) |
| 36 | } |
| 37 | } |
| 38 | return res, nil |
| 39 | } |
| 40 | |
| 41 | func (d *regexDetector) verifyDetection(key, value string) bool { |
| 42 | if d.verifier != nil { |
| 43 | return d.verifier(key, value) |
| 44 | } |
| 45 | return true |
| 46 | } |