@@ -2,11 +2,14 @@ package scrapingant
2
2
3
3
import (
4
4
"context"
5
- regexp "github.com/wasilibs/go-re2"
5
+ "fmt"
6
+ "io"
6
7
"net/http"
7
8
"strings"
8
9
"time"
9
10
11
+ regexp "github.com/wasilibs/go-re2"
12
+
10
13
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
11
14
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
12
15
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
@@ -48,20 +51,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
48
51
}
49
52
50
53
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 )
65
57
}
66
58
67
59
results = append (results , s1 )
@@ -77,3 +69,33 @@ func (s Scanner) Type() detectorspb.DetectorType {
77
69
func (s Scanner ) Description () string {
78
70
return "ScrapingAnt is a web scraping service that provides API keys to authenticate and make requests to their scraping endpoints."
79
71
}
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
+ }
0 commit comments