Skip to content

Commit 2821fb3

Browse files
updated testingbot detector and it's integration tests (#3763)
* updated testingbot detector and it's integration tests * reduced context timeout
1 parent e716a84 commit 2821fb3

File tree

3 files changed

+57
-27
lines changed

3 files changed

+57
-27
lines changed

pkg/detectors/testingbot/testingbot.go

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ package testingbot
22

33
import (
44
"context"
5-
regexp "github.com/wasilibs/go-re2"
5+
"fmt"
6+
"io"
67
"net/http"
7-
"strings"
8+
9+
regexp "github.com/wasilibs/go-re2"
810

911
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
1012
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1113
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
1214
)
1315

14-
type Scanner struct{
16+
type Scanner struct {
1517
detectors.DefaultMultiPartCredentialProvider
1618
}
1719

@@ -36,38 +38,31 @@ func (s Scanner) Keywords() []string {
3638
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
3739
dataStr := string(data)
3840

39-
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
40-
idmatches := idPat.FindAllStringSubmatch(dataStr, -1)
41+
uniqueIDMatches, uniqueKeyMatches := make(map[string]struct{}), make(map[string]struct{})
4142

42-
for _, match := range matches {
43-
if len(match) != 2 {
44-
continue
45-
}
46-
resMatch := strings.TrimSpace(match[1])
47-
for _, idmatch := range idmatches {
48-
if len(idmatch) != 2 {
43+
for _, match := range idPat.FindAllStringSubmatch(dataStr, -1) {
44+
uniqueIDMatches[match[1]] = struct{}{}
45+
}
46+
47+
for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) {
48+
uniqueKeyMatches[match[1]] = struct{}{}
49+
}
50+
51+
for id := range uniqueIDMatches {
52+
for key := range uniqueKeyMatches {
53+
if id == key {
4954
continue
5055
}
51-
resIdMatch := strings.TrimSpace(idmatch[1])
5256

5357
s1 := detectors.Result{
5458
DetectorType: detectorspb.DetectorType_TestingBot,
55-
Raw: []byte(resMatch),
59+
Raw: []byte(key),
5660
}
5761

5862
if verify {
59-
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.testingbot.com/v1/user", nil)
60-
if err != nil {
61-
continue
62-
}
63-
req.SetBasicAuth(resIdMatch, resMatch)
64-
res, err := client.Do(req)
65-
if err == nil {
66-
defer res.Body.Close()
67-
if res.StatusCode >= 200 && res.StatusCode < 300 {
68-
s1.Verified = true
69-
}
70-
}
63+
isVerified, verificationErr := verifyTestingBot(ctx, client, id, key)
64+
s1.Verified = isVerified
65+
s1.SetVerificationError(verificationErr, key)
7166
}
7267

7368
results = append(results, s1)
@@ -84,3 +79,30 @@ func (s Scanner) Type() detectorspb.DetectorType {
8479
func (s Scanner) Description() string {
8580
return "TestingBot provides cross-browser testing services. TestingBot credentials can be used to automate tests on various browsers and devices."
8681
}
82+
83+
func verifyTestingBot(ctx context.Context, client *http.Client, id, secret string) (bool, error) {
84+
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.testingbot.com/v1/user", nil)
85+
if err != nil {
86+
return false, err
87+
}
88+
89+
req.SetBasicAuth(id, secret)
90+
resp, err := client.Do(req)
91+
if err != nil {
92+
return false, err
93+
}
94+
95+
defer func() {
96+
_, _ = io.Copy(io.Discard, resp.Body)
97+
_ = resp.Body.Close()
98+
}()
99+
100+
switch resp.StatusCode {
101+
case http.StatusOK:
102+
return true, nil
103+
case http.StatusUnauthorized, http.StatusForbidden:
104+
return false, nil
105+
default:
106+
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
107+
}
108+
}

pkg/detectors/testingbot/testingbot_integration_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func TestTestingBot_FromChunk(t *testing.T) {
4848
verify: true,
4949
},
5050
want: []detectors.Result{
51+
{
52+
DetectorType: detectorspb.DetectorType_TestingBot,
53+
Verified: false,
54+
},
5155
{
5256
DetectorType: detectorspb.DetectorType_TestingBot,
5357
Verified: true,
@@ -68,6 +72,10 @@ func TestTestingBot_FromChunk(t *testing.T) {
6872
DetectorType: detectorspb.DetectorType_TestingBot,
6973
Verified: false,
7074
},
75+
{
76+
DetectorType: detectorspb.DetectorType_TestingBot,
77+
Verified: false,
78+
},
7179
},
7280
wantErr: false,
7381
},

pkg/detectors/testingbot/testingbot_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestTestingBot_Pattern(t *testing.T) {
3030
{
3131
name: "valid pattern - with keyword testingbot",
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{validId, validKey},
3434
},
3535
{
3636
name: "invalid pattern",

0 commit comments

Comments
 (0)