Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed skybiometery detector and integration tests #3747

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 48 additions & 31 deletions pkg/detectors/skybiometry/skybiometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand All @@ -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)
}
}
12 changes: 10 additions & 2 deletions pkg/detectors/skybiometry/skybiometry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -53,6 +53,10 @@ func TestSkyBiometry_FromChunk(t *testing.T) {
DetectorType: detectorspb.DetectorType_SkyBiometry,
Verified: true,
},
{
DetectorType: detectorspb.DetectorType_SkyBiometry,
Verified: false,
},
},
wantErr: false,
},
Expand All @@ -69,6 +73,10 @@ func TestSkyBiometry_FromChunk(t *testing.T) {
DetectorType: detectorspb.DetectorType_SkyBiometry,
Verified: false,
},
{
DetectorType: detectorspb.DetectorType_SkyBiometry,
Verified: false,
},
},
wantErr: false,
},
Expand Down
Loading