Skip to content

Commit 865987e

Browse files
committed
resolve comments
1 parent 04e99b3 commit 865987e

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

pkg/detectors/phraseaccesstoken/phraseaccesstoken.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"slices"
89

910
regexp "github.com/wasilibs/go-re2"
1011

@@ -35,24 +36,18 @@ func (s Scanner) Keywords() []string {
3536
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
3637
dataStr := string(data)
3738

38-
uniqueMatches := make(map[string]struct{})
39+
var tokens []string
3940
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
4041

4142
for _, match := range matches {
42-
// Ensure we have a proper capture group
43-
if len(match) < 2 {
44-
continue
45-
}
46-
4743
token := match[1]
48-
if token == "" {
49-
continue
50-
}
5144

52-
uniqueMatches[token] = struct{}{}
45+
if !slices.Contains(tokens, token) {
46+
tokens = append(tokens, token)
47+
}
5348
}
5449

55-
for token := range uniqueMatches {
50+
for _, token := range tokens {
5651
s1 := detectors.Result{
5752
DetectorType: detectorspb.DetectorType_PhraseAccessToken,
5853
Raw: []byte(token),

pkg/detectors/phraseaccesstoken/phraseaccesstoken_test.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@ package phraseaccesstoken
22

33
import (
44
"context"
5-
"fmt"
65
"testing"
76

87
"github.com/google/go-cmp/cmp"
98
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
109
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
1110
)
1211

13-
var (
14-
validPattern = "1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890"
15-
invalidPattern = "7cf4135a4e7f7ac228d36f210f151917a86f5dbd6"
16-
keyword = "phrase"
17-
)
18-
1912
func TestPhrase_Pattern(t *testing.T) {
2013
d := Scanner{}
2114
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
@@ -26,47 +19,47 @@ func TestPhrase_Pattern(t *testing.T) {
2619
}{
2720
{
2821
name: "valid pattern - with keyword phrase",
29-
input: fmt.Sprintf("%s token = '%s'", keyword, validPattern),
30-
want: []string{validPattern},
22+
input: "phrase token = 1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890",
23+
want: []string{"1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890"},
3124
},
3225
{
3326
name: "valid pattern - ignore duplicate",
34-
input: fmt.Sprintf("%s token = '%s' | '%s'", keyword, validPattern, validPattern),
35-
want: []string{validPattern},
27+
input: "phrase token = '1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890' | '1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890'",
28+
want: []string{"1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890"},
3629
},
3730
{
3831
name: "valid pattern - key out of prefix range",
39-
input: fmt.Sprintf("%s keyword is not close to the real key in the data\n = '%s'", keyword, validPattern),
32+
input: "phrase keyword is not close to the real key in the data\n = '1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890'",
4033
want: []string{},
4134
},
4235
{
4336
name: "invalid pattern",
44-
input: fmt.Sprintf("%s = '%s'", keyword, invalidPattern),
37+
input: "phrase = 7cf4135a4e7f7ac228d36f210f151917a86f5dbd6",
4538
want: []string{},
4639
},
4740
{
4841
name: "finds all valid matches",
49-
input: fmt.Sprintf("%s token1 = '%s'\n%s token2 = '%s'", keyword, validPattern, keyword, "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"),
50-
want: []string{validPattern, "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"},
42+
input: "phrase token1 = '1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890'\n phrase token2 = 'abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890'",
43+
want: []string{"1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890", "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"},
5144
},
5245
{
5346
name: "invalid pattern - too short",
54-
input: fmt.Sprintf("%s = '%s'", keyword, "1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef12345678"),
47+
input: "phrase = '1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef12345678'",
5548
want: []string{},
5649
},
5750
{
5851
name: "invalid pattern - too long",
59-
input: fmt.Sprintf("%s = '%s'", keyword, "1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef123456789012"),
52+
input: "phrase = '1a2b3c4d5e6f7890abcdef1234567890abcdef1234567890abcdef123456789012'",
6053
want: []string{},
6154
},
6255
{
6356
name: "invalid pattern - contains uppercase",
64-
input: fmt.Sprintf("%s = '%s'", keyword, "1A2B3C4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890"),
57+
input: "phrase = '1A2B3C4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890'",
6558
want: []string{},
6659
},
6760
{
6861
name: "invalid pattern - contains special characters",
69-
input: fmt.Sprintf("%s = '%s'", keyword, "1a2b3c4d-e6f7890abcdef1234567890abcdef1234567890abcdef1234567890"),
62+
input: "phrase = '1a2b3c4d-e6f7890abcdef1234567890abcdef1234567890abcdef1234567890'",
7063
want: []string{},
7164
},
7265
}

0 commit comments

Comments
 (0)