From f7252c525a89fd538e629c4faa7ffd0b2cc16a17 Mon Sep 17 00:00:00 2001 From: Sahil Silare <32628578+sahil9001@users.noreply.github.com> Date: Sat, 19 Oct 2024 02:02:17 +0530 Subject: [PATCH] fix: fixed autoklose verification endpoint (#3447) * fix: fixed autoklose verification endpoint * fix: addressed review comments * fix: addressed review comments * fix: added guard checks for 401 and 200 * fix: added code --- pkg/detectors/autoklose/autoklose.go | 38 ++++++++++++++++------- pkg/detectors/autoklose/autoklose_test.go | 3 ++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/pkg/detectors/autoklose/autoklose.go b/pkg/detectors/autoklose/autoklose.go index 9ee8fde5d..a73b73a95 100644 --- a/pkg/detectors/autoklose/autoklose.go +++ b/pkg/detectors/autoklose/autoklose.go @@ -4,11 +4,12 @@ import ( "context" "encoding/json" "fmt" - regexp "github.com/wasilibs/go-re2" "io" "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" @@ -50,24 +51,37 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.autoklose.com/api/campaigns/?api_token=%s", resMatch), nil) + // API Documentation: https://api.aklab.xyz/#auth-info-fd71acd1-2e41-4991-8789-3edfd258479a + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.autoklose.com/api/me/?api_token=%s", resMatch), nil) if err != nil { continue } + req.Header.Add("Accept", "application/json") res, err := client.Do(req) if err == nil { - bodyBytes, err := io.ReadAll(res.Body) - if err != nil { - continue - } - defer res.Body.Close() - if res.StatusCode >= 200 && res.StatusCode < 300 { - if json.Valid(bodyBytes) { - s1.Verified = true - } else { - s1.Verified = false + 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) } } diff --git a/pkg/detectors/autoklose/autoklose_test.go b/pkg/detectors/autoklose/autoklose_test.go index 3976ccf76..9675d22cb 100644 --- a/pkg/detectors/autoklose/autoklose_test.go +++ b/pkg/detectors/autoklose/autoklose_test.go @@ -50,6 +50,9 @@ func TestAutoklose_FromChunk(t *testing.T) { { DetectorType: detectorspb.DetectorType_Autoklose, Verified: true, + ExtraData: map[string]string{ + "email": "mladen.stevanovic@vanillasoft.com", + }, }, }, wantErr: false,