Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
fix(gitlab): check for valid JSON (#2218)
Richard Gomez06b137fd9306
diff --git a/pkg/detectors/gitlab/gitlab.go b/pkg/detectors/gitlab/gitlab.goindex e679959e2..33ac756b7 100644--- a/pkg/detectors/gitlab/gitlab.go+++ b/pkg/detectors/gitlab/gitlab.go@@ -2,7 +2,9 @@ package gitlabimport ("context"+ "encoding/json""fmt"+ "io""net/http""regexp""strings"@@ -18,9 +20,11 @@ type Scanner struct {}// Ensure the Scanner satisfies the interfaces at compile time.-var _ detectors.Detector = (*Scanner)(nil)-var _ detectors.EndpointCustomizer = (*Scanner)(nil)-var _ detectors.Versioner = (*Scanner)(nil)+var (+ _ detectors.Detector = (*Scanner)(nil)+ _ detectors.EndpointCustomizer = (*Scanner)(nil)+ _ detectors.Versioner = (*Scanner)(nil)+)func (Scanner) Version() int { return 1 }func (Scanner) DefaultEndpoint() string { return "https://gitlab.com" }@@ -91,19 +95,25 @@ func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, errorif err != nil {continue}+req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))res, err := client.Do(req)if err != nil {return false, err}- defer res.Body.Close() // The request body is unused.++ defer res.Body.Close()+ body, err := io.ReadAll(res.Body)+ if err != nil {+ return false, err+ }// 200 means good key and has `read_user` scope// 403 means good key but not the right scope// 401 is bad keyswitch res.StatusCode {case http.StatusOK:- return true, nil+ return json.Valid(body), nilcase http.StatusForbidden:// Good key but not the right scopereturn true, nil