Skip to content

Commit 6bffdd0

Browse files
Optimized twitch detectors (#3987)
* avoid detecting same string for two patterns * optimized twitch detectors * updated test cases
1 parent d429422 commit 6bffdd0

File tree

5 files changed

+23
-47
lines changed

5 files changed

+23
-47
lines changed

pkg/detectors/twitch/twitch.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ var (
2929
defaultClient = common.SaneHttpClient()
3030

3131
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives
32-
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"twitch"}) + `\b([0-9a-z]{30})\b`)
33-
idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"twitch"}) + `\b([0-9a-z]{30})\b`)
32+
idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"twitch"}) + `\b([0-9a-z]{30})\b`)
33+
secretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"twitch"}) + `\b([0-9a-z]{30})\b`)
3434
)
3535

3636
// Keywords are used for efficiently pre-filtering chunks.
@@ -43,25 +43,33 @@ func (s Scanner) Keywords() []string {
4343
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
4444
dataStr := string(data)
4545

46-
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
47-
idMatches := idPat.FindAllStringSubmatch(dataStr, -1)
46+
var uniqueIDMatches, uniqueSecretMatches = make(map[string]struct{}), make(map[string]struct{})
4847

49-
for _, match := range matches {
50-
resMatch := strings.TrimSpace(match[1])
48+
for _, match := range idPat.FindAllStringSubmatch(dataStr, -1) {
49+
uniqueIDMatches[match[1]] = struct{}{}
50+
}
51+
52+
for _, match := range secretPat.FindAllStringSubmatch(dataStr, -1) {
53+
uniqueSecretMatches[match[1]] = struct{}{}
54+
}
5155

52-
for _, idMatch := range idMatches {
53-
resIdMatch := strings.TrimSpace(idMatch[1])
56+
for id := range uniqueIDMatches {
57+
for secret := range uniqueSecretMatches {
58+
// as both patterns are same, to avoid same strings
59+
if id == secret {
60+
continue
61+
}
5462

5563
s1 := detectors.Result{
5664
DetectorType: detectorspb.DetectorType_Twitch,
57-
Raw: []byte(resMatch),
65+
Raw: []byte(id),
5866
}
5967

6068
if verify {
6169
client := s.getClient()
62-
isVerified, verificationErr := verifyTwitch(ctx, client, resMatch, resIdMatch)
70+
isVerified, verificationErr := verifyTwitch(ctx, client, secret, id)
6371
s1.Verified = isVerified
64-
s1.SetVerificationError(verificationErr, resMatch)
72+
s1.SetVerificationError(verificationErr, id)
6573
}
6674

6775
results = append(results, s1)

pkg/detectors/twitch/twitch_integration_test.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,6 @@ func TestTwitch_FromChunk(t *testing.T) {
6161
DetectorType: detectorspb.DetectorType_Twitch,
6262
Verified: true,
6363
},
64-
{
65-
DetectorType: detectorspb.DetectorType_Twitch,
66-
Verified: false,
67-
},
68-
{
69-
DetectorType: detectorspb.DetectorType_Twitch,
70-
Verified: false,
71-
},
7264
},
7365
wantErr: false,
7466
},
@@ -89,14 +81,6 @@ func TestTwitch_FromChunk(t *testing.T) {
8981
DetectorType: detectorspb.DetectorType_Twitch,
9082
Verified: false,
9183
},
92-
{
93-
DetectorType: detectorspb.DetectorType_Twitch,
94-
Verified: false,
95-
},
96-
{
97-
DetectorType: detectorspb.DetectorType_Twitch,
98-
Verified: false,
99-
},
10084
},
10185
wantErr: false,
10286
wantVerificationErr: true,
@@ -118,14 +102,6 @@ func TestTwitch_FromChunk(t *testing.T) {
118102
DetectorType: detectorspb.DetectorType_Twitch,
119103
Verified: false,
120104
},
121-
{
122-
DetectorType: detectorspb.DetectorType_Twitch,
123-
Verified: false,
124-
},
125-
{
126-
DetectorType: detectorspb.DetectorType_Twitch,
127-
Verified: false,
128-
},
129105
},
130106
wantErr: false,
131107
wantVerificationErr: true,
@@ -147,14 +123,6 @@ func TestTwitch_FromChunk(t *testing.T) {
147123
DetectorType: detectorspb.DetectorType_Twitch,
148124
Verified: false,
149125
},
150-
{
151-
DetectorType: detectorspb.DetectorType_Twitch,
152-
Verified: false,
153-
},
154-
{
155-
DetectorType: detectorspb.DetectorType_Twitch,
156-
Verified: false,
157-
},
158126
},
159127
wantErr: false,
160128
},

pkg/detectors/twitch/twitch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestTwitch_Pattern(t *testing.T) {
3030
{
3131
name: "valid pattern - with keyword twitch",
3232
input: fmt.Sprintf("%s token - '%s'\n%s token - '%s'\n", keyword, validKey, keyword, validId),
33-
want: []string{validKey, validId, validId, validKey},
33+
want: []string{validKey, validId},
3434
},
3535
{
3636
name: "invalid pattern",

pkg/detectors/twitchaccesstoken/twitchaccesstoken.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var _ detectors.Detector = (*Scanner)(nil)
2323
var (
2424
defaultClient = common.SaneHttpClient()
2525
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
26-
keyPat = regexp.MustCompile(`\b([0-9a-z]{30})\b`)
26+
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"twitch"}) + `\b([0-9a-z]{30})\b`)
2727
)
2828

2929
// Keywords are used for efficiently pre-filtering chunks.

pkg/detectors/twitchaccesstoken/twitchaccesstoken_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ func TestTwitchaccesstoken_Pattern(t *testing.T) {
2828
want: []string{"abc123def456ghi789jkl012mno345"},
2929
},
3030
{
31-
name: "get request pattern",
31+
name: "get request pattern - keyword out of range",
3232
input: "curl -X GET 'https://id.twitch.tv/oauth2/validate' -H 'Authorization: OAuth xbc123def456ghi789jkl012mno345'",
33-
want: []string{"xbc123def456ghi789jkl012mno345"},
33+
want: []string{},
3434
},
3535
{
3636
name: "finds all matches",

0 commit comments

Comments
 (0)