Skip to content

Find, verify, and analyze leaked credentials

fixed skybiometery detector and integration tests (#3747)

Kashif Khanf9ce3e8f8bdb
diff --git a/pkg/detectors/skybiometry/skybiometry.go b/pkg/detectors/skybiometry/skybiometry.go
index 121f1afeb..6eeddbc03 100644
--- a/pkg/detectors/skybiometry/skybiometry.go
+++ b/pkg/detectors/skybiometry/skybiometry.go
@@ -2,17 +2,18 @@ package skybiometry
import (
"context"
- regexp "github.com/wasilibs/go-re2"
+ "fmt"
+ "io"
"net/http"
- "net/url"
- "strings"
+
+ 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"
)
-type Scanner struct{
+type Scanner struct {
detectors.DefaultMultiPartCredentialProvider
}
@@ -37,43 +38,31 @@ func (s Scanner) Keywords() []string {
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)
- matches := keyPat.FindAllStringSubmatch(dataStr, -1)
- secretMatches := secretPat.FindAllStringSubmatch(dataStr, -1)
+ uniqueKeyMatches, uniqueSecretMatches := make(map[string]struct{}), make(map[string]struct{})
- for _, match := range matches {
- if len(match) != 2 {
- continue
- }
- resMatch := strings.TrimSpace(match[1])
+ for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) {
+ uniqueKeyMatches[match[1]] = struct{}{}
+ }
+
+ for _, match := range secretPat.FindAllStringSubmatch(dataStr, -1) {
+ uniqueSecretMatches[match[1]] = struct{}{}
+ }
- for _, secretMatch := range secretMatches {
- if len(secretMatch) != 2 {
+ for key := range uniqueKeyMatches {
+ for secret := range uniqueSecretMatches {
+ if key == secret {
continue
}
- resSecretMatch := strings.TrimSpace(secretMatch[1])
s1 := detectors.Result{
DetectorType: detectorspb.DetectorType_SkyBiometry,
- Raw: []byte(resSecretMatch),
+ Raw: []byte(secret),
}
if verify {
-
- payload := url.Values{}
- payload.Add("api_key", resMatch)
- payload.Add("api_secret", resSecretMatch)
-
- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.skybiometry.com/fc/account/authenticate?"+payload.Encode(), nil)
- if err != nil {
- continue
- }
- res, err := client.Do(req)
- if err == nil {
- defer res.Body.Close()
- if res.StatusCode >= 200 && res.StatusCode < 300 {
- s1.Verified = true
- }
- }
+ isVerified, verificationErr := verifySkyBiometery(ctx, client, key, secret)
+ s1.Verified = isVerified
+ s1.SetVerificationError(verificationErr)
}
results = append(results, s1)
@@ -90,3 +79,31 @@ func (s Scanner) Type() detectorspb.DetectorType {
func (s Scanner) Description() string {
return "SkyBiometry is a facial recognition service. SkyBiometry API keys can be used to access and utilize their facial recognition API."
}
+
+func verifySkyBiometery(ctx context.Context, client *http.Client, apiKey, apiSecret string) (bool, error) {
+ apiURL := fmt.Sprintf("https://api.skybiometry.com/fc/account/authenticate?api_key=%s&api_secret=%s", apiKey, apiSecret)
+
+ req, err := http.NewRequestWithContext(ctx, "GET", apiURL, nil)
+ if err != nil {
+ return false, err
+ }
+
+ resp, err := client.Do(req)
+ if err != nil {
+ return false, err
+ }
+
+ defer func() {
+ _, _ = io.Copy(io.Discard, resp.Body)
+ _ = resp.Body.Close()
+ }()
+
+ switch resp.StatusCode {
+ case http.StatusOK:
+ return true, nil
+ case http.StatusBadRequest, http.StatusUnauthorized:
+ return false, nil
+ default:
+ return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
+ }
+}
diff --git a/pkg/detectors/skybiometry/skybiometry_test.go b/pkg/detectors/skybiometry/skybiometry_test.go
index 2d829723f..f7d96e29b 100644
--- a/pkg/detectors/skybiometry/skybiometry_test.go
+++ b/pkg/detectors/skybiometry/skybiometry_test.go
@@ -10,14 +10,14 @@ 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"
)
func TestSkyBiometry_FromChunk(t *testing.T) {
- ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors2")
if err != nil {
@@ -53,6 +53,10 @@ func TestSkyBiometry_FromChunk(t *testing.T) {
DetectorType: detectorspb.DetectorType_SkyBiometry,
Verified: true,
},
+ {
+ DetectorType: detectorspb.DetectorType_SkyBiometry,
+ Verified: false,
+ },
},
wantErr: false,
},
@@ -69,6 +73,10 @@ func TestSkyBiometry_FromChunk(t *testing.T) {
DetectorType: detectorspb.DetectorType_SkyBiometry,
Verified: false,
},
+ {
+ DetectorType: detectorspb.DetectorType_SkyBiometry,
+ Verified: false,
+ },
},
wantErr: false,
},