Skip to content

Commit

Permalink
fixed scrapingAnt detector (#3736)
Browse files Browse the repository at this point in the history
  • Loading branch information
kashifkhan0771 authored Dec 9, 2024
1 parent 166aa83 commit fb9f0a5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
52 changes: 37 additions & 15 deletions pkg/detectors/scrapingant/scrapingant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package scrapingant

import (
"context"
regexp "github.com/wasilibs/go-re2"
"fmt"
"io"
"net/http"
"strings"
"time"

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

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
Expand Down Expand Up @@ -48,20 +51,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
}

if verify {
timeout := 10 * time.Second
client.Timeout = timeout
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.scrapingant.com/v1/general?url=google.com", nil)
if err != nil {
continue
}
req.Header.Add("x-api-key", resMatch)
res, err := client.Do(req)
if err == nil {
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 {
s1.Verified = true
}
}
isVerified, verificationErr := verifyScrapingAnt(ctx, client, resMatch)
s1.Verified = isVerified
s1.SetVerificationError(verificationErr, resMatch)
}

results = append(results, s1)
Expand All @@ -77,3 +69,33 @@ func (s Scanner) Type() detectorspb.DetectorType {
func (s Scanner) Description() string {
return "ScrapingAnt is a web scraping service that provides API keys to authenticate and make requests to their scraping endpoints."
}

func verifyScrapingAnt(ctx context.Context, client *http.Client, apiKey string) (bool, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

// do not use google.com as url as it cannot be used under free subscription
apiUrl := fmt.Sprintf("https://api.scrapingant.com/v1/general?url=example.com&x-api-key=%s", apiKey)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiUrl, http.NoBody)
if err != nil {
return false, err
}

resp, err := client.Do(req)
if err != nil {
return false, nil
}

defer func() {
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}()

if resp.StatusCode == http.StatusOK {
return true, nil
} else if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {
return false, nil
} else {
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
}
2 changes: 1 addition & 1 deletion pkg/detectors/scrapingant/scrapingant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"time"

"github.com/kylelemons/godebug/pretty"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)

Expand Down

0 comments on commit fb9f0a5

Please sign in to comment.