From b0d9e54820a735461cf6be2422d8ac0d16e6c85a Mon Sep 17 00:00:00 2001 From: Sahil Silare Date: Thu, 17 Oct 2024 15:07:56 +0530 Subject: [PATCH 1/3] fix: fixed verification endpoint for AyrShare --- pkg/detectors/ayrshare/ayrshare.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/detectors/ayrshare/ayrshare.go b/pkg/detectors/ayrshare/ayrshare.go index db2f43cccadc..3e75c0060f98 100644 --- a/pkg/detectors/ayrshare/ayrshare.go +++ b/pkg/detectors/ayrshare/ayrshare.go @@ -3,10 +3,11 @@ package ayrshare import ( "context" "fmt" - regexp "github.com/wasilibs/go-re2" "net/http" "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" @@ -21,7 +22,7 @@ var ( client = common.SaneHttpClient() // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. - keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"ayrshare"}) + `\b([A-Z]{7}-[A-Z0-9]{7}-[A-Z0-9]{7}-[A-Z0-9]{7})\b`) + keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"ayrshare"}) + `\b([A-Z0-9]{8}-[A-Z0-9]{8}-[A-Z0-9]{8}-[A-Z0-9]{8})\b`) ) // Keywords are used for efficiently pre-filtering chunks. From 38dfcd26ea68f3e520c8e43bd0d2b3c539b04138 Mon Sep 17 00:00:00 2001 From: Sahil Silare Date: Thu, 17 Oct 2024 15:13:13 +0530 Subject: [PATCH 2/3] fix: changed verification endpoint for ayrshare --- pkg/detectors/ayrshare/ayrshare.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/detectors/ayrshare/ayrshare.go b/pkg/detectors/ayrshare/ayrshare.go index 3e75c0060f98..34cef81f5e91 100644 --- a/pkg/detectors/ayrshare/ayrshare.go +++ b/pkg/detectors/ayrshare/ayrshare.go @@ -49,7 +49,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", "https://app.ayrshare.com/api/analytics/links", nil) + req, err := http.NewRequestWithContext(ctx, "GET", "https://app.ayrshare.com/api/user", nil) if err != nil { continue } From 16d9d78772245414848b7563ebc1acd01f527d47 Mon Sep 17 00:00:00 2001 From: Sahil Silare Date: Fri, 18 Oct 2024 12:13:01 +0530 Subject: [PATCH 3/3] fix: fixed error handling --- pkg/detectors/ayrshare/ayrshare.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/pkg/detectors/ayrshare/ayrshare.go b/pkg/detectors/ayrshare/ayrshare.go index 34cef81f5e91..c4204ca7b346 100644 --- a/pkg/detectors/ayrshare/ayrshare.go +++ b/pkg/detectors/ayrshare/ayrshare.go @@ -2,7 +2,9 @@ package ayrshare import ( "context" + "encoding/json" "fmt" + "io" "net/http" "strings" @@ -56,10 +58,29 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch)) res, err := client.Do(req) if err == nil { - defer res.Body.Close() - if res.StatusCode >= 200 && res.StatusCode < 300 { + defer func() { + _, _ = io.Copy(io.Discard, res.Body) + _ = res.Body.Close() + }() + + if res.StatusCode == http.StatusOK { s1.Verified = true + bodyBytes, err := io.ReadAll(res.Body) + if err != nil { + continue + } + + var responseBody map[string]interface{} + if err := json.Unmarshal(bodyBytes, &responseBody); err == nil { + if email, ok := responseBody["email"].(string); ok { + s1.ExtraData = map[string]string{ + "email": email, + } + } + } } + } else { + s1.SetVerificationError(err, resMatch) } }