Skip to content

Commit 7550057

Browse files
authored
use first capture group in custom detector regex if available (#3853)
1 parent 20a3840 commit 7550057

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

pkg/custom_detectors/custom_detectors.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ func (c *CustomRegexWebhook) createResults(ctx context.Context, match map[string
129129
var raw string
130130
for _, values := range match {
131131
// values[0] contains the entire regex match.
132-
raw += values[0]
132+
secret := values[0]
133+
if len(values) > 1 {
134+
secret = values[1]
135+
}
136+
raw += secret
133137
}
134138
result := detectors.Result{
135139
DetectorType: detectorspb.DetectorType_CustomRegex,

pkg/custom_detectors/custom_detectors_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,13 @@ func TestDetector(t *testing.T) {
199199
// "password" is normally flagged as a false positive, but CustomRegex
200200
// should allow the user to decide and report it as a result.
201201
Keywords: []string{"password"},
202-
Regex: map[string]string{"regex": "password=.*"},
202+
Regex: map[string]string{"regex": "password=\"(.*)\""},
203203
})
204204
assert.NoError(t, err)
205205
results, err := detector.FromData(context.Background(), false, []byte(`password="123456"`))
206206
assert.NoError(t, err)
207207
assert.Equal(t, 1, len(results))
208-
assert.Equal(t, results[0].Raw, []byte(`password="123456"`))
208+
assert.Equal(t, results[0].Raw, []byte(`123456`))
209209
}
210210

211211
func BenchmarkProductIndices(b *testing.B) {

0 commit comments

Comments
 (0)