Skip to content

Commit

Permalink
use first capture group in custom detector regex if available (#3853)
Browse files Browse the repository at this point in the history
  • Loading branch information
zricethezav authored Jan 24, 2025
1 parent 20a3840 commit 7550057
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion pkg/custom_detectors/custom_detectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ func (c *CustomRegexWebhook) createResults(ctx context.Context, match map[string
var raw string
for _, values := range match {
// values[0] contains the entire regex match.
raw += values[0]
secret := values[0]
if len(values) > 1 {
secret = values[1]
}
raw += secret
}
result := detectors.Result{
DetectorType: detectorspb.DetectorType_CustomRegex,
Expand Down
4 changes: 2 additions & 2 deletions pkg/custom_detectors/custom_detectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ func TestDetector(t *testing.T) {
// "password" is normally flagged as a false positive, but CustomRegex
// should allow the user to decide and report it as a result.
Keywords: []string{"password"},
Regex: map[string]string{"regex": "password=.*"},
Regex: map[string]string{"regex": "password=\"(.*)\""},
})
assert.NoError(t, err)
results, err := detector.FromData(context.Background(), false, []byte(`password="123456"`))
assert.NoError(t, err)
assert.Equal(t, 1, len(results))
assert.Equal(t, results[0].Raw, []byte(`password="123456"`))
assert.Equal(t, results[0].Raw, []byte(`123456`))
}

func BenchmarkProductIndices(b *testing.B) {
Expand Down

0 comments on commit 7550057

Please sign in to comment.