Skip to content

Commit fb9f0a5

Browse files
fixed scrapingAnt detector (#3736)
1 parent 166aa83 commit fb9f0a5

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

pkg/detectors/scrapingant/scrapingant.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package scrapingant
22

33
import (
44
"context"
5-
regexp "github.com/wasilibs/go-re2"
5+
"fmt"
6+
"io"
67
"net/http"
78
"strings"
89
"time"
910

11+
regexp "github.com/wasilibs/go-re2"
12+
1013
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
1114
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1215
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
@@ -48,20 +51,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
4851
}
4952

5053
if verify {
51-
timeout := 10 * time.Second
52-
client.Timeout = timeout
53-
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.scrapingant.com/v1/general?url=google.com", nil)
54-
if err != nil {
55-
continue
56-
}
57-
req.Header.Add("x-api-key", resMatch)
58-
res, err := client.Do(req)
59-
if err == nil {
60-
defer res.Body.Close()
61-
if res.StatusCode >= 200 && res.StatusCode < 300 {
62-
s1.Verified = true
63-
}
64-
}
54+
isVerified, verificationErr := verifyScrapingAnt(ctx, client, resMatch)
55+
s1.Verified = isVerified
56+
s1.SetVerificationError(verificationErr, resMatch)
6557
}
6658

6759
results = append(results, s1)
@@ -77,3 +69,33 @@ func (s Scanner) Type() detectorspb.DetectorType {
7769
func (s Scanner) Description() string {
7870
return "ScrapingAnt is a web scraping service that provides API keys to authenticate and make requests to their scraping endpoints."
7971
}
72+
73+
func verifyScrapingAnt(ctx context.Context, client *http.Client, apiKey string) (bool, error) {
74+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
75+
defer cancel()
76+
77+
// do not use google.com as url as it cannot be used under free subscription
78+
apiUrl := fmt.Sprintf("https://api.scrapingant.com/v1/general?url=example.com&x-api-key=%s", apiKey)
79+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiUrl, http.NoBody)
80+
if err != nil {
81+
return false, err
82+
}
83+
84+
resp, err := client.Do(req)
85+
if err != nil {
86+
return false, nil
87+
}
88+
89+
defer func() {
90+
_, _ = io.Copy(io.Discard, resp.Body)
91+
_ = resp.Body.Close()
92+
}()
93+
94+
if resp.StatusCode == http.StatusOK {
95+
return true, nil
96+
} else if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {
97+
return false, nil
98+
} else {
99+
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
100+
}
101+
}

pkg/detectors/scrapingant/scrapingant_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"time"
1111

1212
"github.com/kylelemons/godebug/pretty"
13-
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1413

1514
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
15+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1616
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
1717
)
1818

0 commit comments

Comments
 (0)