Skip to content

Commit 96c9c5b

Browse files
authored
[THOG-234] Update security trails detector's regex and keywords. (#429)
* Update detectors PrefixRegex to allow for new line and carriage returns. Add additional keyword for security trails. Add additional unit tests for security trails and PrefixRegex * Update catpure group.
1 parent f954c3a commit 96c9c5b

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

pkg/detectors/detectors.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func CleanResults(results []Result) []Result {
9292
func PrefixRegex(keywords []string) string {
9393
pre := `(?i)(?:`
9494
middle := strings.Join(keywords, "|")
95-
post := `).{0,40}`
95+
post := `)(?:.|[\n\r]){0,40}`
9696
return pre + middle + post
9797
}
9898

pkg/detectors/detectors_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package detectors
2+
3+
import "testing"
4+
5+
func TestPrefixRegex(t *testing.T) {
6+
tests := []struct {
7+
keywords []string
8+
expected string
9+
}{
10+
{
11+
keywords: []string{"securitytrails"},
12+
expected: `(?i)(?:securitytrails).|(?:[\n\r]){0,40}`,
13+
},
14+
{
15+
keywords: []string{"zipbooks"},
16+
expected: `(?i)(?:zipbooks).|(?:[\n\r]){0,40}`,
17+
},
18+
{
19+
keywords: []string{"wrike"},
20+
expected: `(?i)(?:wrike).|(?:[\n\r]){0,40}`,
21+
},
22+
}
23+
for _, tt := range tests {
24+
got := PrefixRegex(tt.keywords)
25+
if got != tt.expected {
26+
t.Errorf("PrefixRegex(%v) got: %v want: %v", tt.keywords, got, tt.expected)
27+
}
28+
}
29+
}
30+
31+
func BenchmarkPrefixRegex(b *testing.B) {
32+
kws := []string{"securitytrails"}
33+
for i := 0; i < b.N; i++ {
34+
PrefixRegex(kws)
35+
}
36+
}

pkg/detectors/securitytrails/securitytrails.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var (
2626
// Keywords are used for efficiently pre-filtering chunks.
2727
// Use identifiers in the secret preferably, or the provider name.
2828
func (s Scanner) Keywords() []string {
29-
return []string{"securitytrails"}
29+
return []string{"securitytrails", "security trails"}
3030
}
3131

3232
// FromData will find and optionally verify SecurityTrails secrets in a given set of bytes.

pkg/detectors/securitytrails/securitytrails_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ func TestSecurityTrails_FromChunk(t *testing.T) {
3838
{
3939
name: "found, verified",
4040
s: Scanner{},
41+
args: args{
42+
ctx: context.Background(),
43+
data: []byte(fmt.Sprintf("You can find a securitytrails secret\n %s within", secret)),
44+
verify: true,
45+
},
46+
want: []detectors.Result{
47+
{
48+
DetectorType: detectorspb.DetectorType_SecurityTrails,
49+
Verified: true,
50+
},
51+
},
52+
wantErr: false,
53+
},
54+
{
55+
name: "found, verified inline",
56+
s: Scanner{},
4157
args: args{
4258
ctx: context.Background(),
4359
data: []byte(fmt.Sprintf("You can find a securitytrails secret %s within", secret)),

0 commit comments

Comments
 (0)