Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Fixed Grafana detector (#4166)
Kashif Khana6a4aedd6b11
diff --git a/pkg/detectors/grafana/grafana.go b/pkg/detectors/grafana/grafana.goindex 6574c650e..8ae17cb2c 100644--- a/pkg/detectors/grafana/grafana.go+++ b/pkg/detectors/grafana/grafana.go@@ -3,6 +3,7 @@ package grafanaimport ("context""fmt"+ "io""net/http""strings"@@ -23,13 +24,22 @@ var _ detectors.Detector = (*Scanner)(nil)var (defaultClient = common.SaneHttpClient()// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.- keyPat = regexp.MustCompile(`\b(glc_[A-Za-z0-9+\/]{50,150}\={0,2})`)+ keyPat = regexp.MustCompile(`\b(glc_eyJ[A-Za-z0-9+\/=]{60,160})`))+func (s Scanner) getClient() *http.Client {+ client := s.client+ if client == nil {+ client = defaultClient+ }++ return client+}+// Keywords are used for efficiently pre-filtering chunks.// Use identifiers in the secret preferably, or the provider name.func (s Scanner) Keywords() []string {- return []string{"glc_"}+ return []string{"glc_eyJ"}}// FromData will find and optionally verify Grafana secrets in a given set of bytes.@@ -47,29 +57,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result}if verify {- client := s.client- if client == nil {- client = defaultClient- }- req, err := http.NewRequestWithContext(ctx, "GET", "https://grafana.com/api/v1/tokens?region=us", nil)- if err != nil {- continue- }- 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 || res.StatusCode == 403 {- s1.Verified = true- } else if res.StatusCode == 401 {- // The secret is determinately not verified (nothing to do)- } else {- err = fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)- s1.SetVerificationError(err, resMatch)- }- } else {- s1.SetVerificationError(err, resMatch)- }+ isVerified, verificationErr := verifyGrafanaKey(ctx, s.getClient(), resMatch)+ s1.Verified = isVerified+ s1.SetVerificationError(verificationErr, resMatch)}results = append(results, s1)@@ -85,3 +75,37 @@ func (s Scanner) Type() detectorspb.DetectorType {func (s Scanner) Description() string {return "Grafana is an open-source platform for monitoring and observability. Grafana API keys can be used to access and manage Grafana resources."}++func verifyGrafanaKey(ctx context.Context, client *http.Client, token string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://grafana.com/api/v1/tokens?region=us", http.NoBody)+ if err != nil {+ return false, err+ }++ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))++ 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.StatusUnauthorized:+ bodyBytes, err := io.ReadAll(resp.Body)+ if err != nil {+ return false, err+ }++ // token is valid but has restricted permissions+ return strings.Contains(string(bodyBytes), "Unauthorized"), nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)+ }+}diff --git a/pkg/detectors/grafana/grafana_integration_test.go b/pkg/detectors/grafana/grafana_integration_test.goindex 7aa739bf8..b465ffcaa 100644--- a/pkg/detectors/grafana/grafana_integration_test.go+++ b/pkg/detectors/grafana/grafana_integration_test.go@@ -136,7 +136,7 @@ func TestGrafana_FromChunk(t *testing.T) {t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError())}}- ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError")+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError", "primarySecret")if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {t.Errorf("Grafana.FromData() %s diff: (-got +want)\n%s", tt.name, diff)}diff --git a/pkg/detectors/grafana/grafana_test.go b/pkg/detectors/grafana/grafana_test.goindex 8f973cf0e..fa459a176 100644--- a/pkg/detectors/grafana/grafana_test.go+++ b/pkg/detectors/grafana/grafana_test.go@@ -19,13 +19,13 @@ var ("authentication_type": "","verification_url": "https://api.example.com/example","test_secrets": {- "grafana_secret": "glc_gnVF057+C0x9J+QwzC5JXb5uQ/WSzn98X/iIrZXKaA3Hh+lum0XBRcu56qMlW7ZaxXrNt33XoI3CXz7IRPci="+ "grafana_secret": "glc_eyJF057+C0x9J+QwzC5JXb5uQ/WSzn98X/iIrZXKaA3Hh+lum0XBRcu56qMlW7ZaxXrNt33XoI3CXz7IRPci="},"expected_response": "200","method": "GET","deprecated": false}]`- secret = "glc_gnVF057+C0x9J+QwzC5JXb5uQ/WSzn98X/iIrZXKaA3Hh+lum0XBRcu56qMlW7ZaxXrNt33XoI3CXz7IRPci="+ secret = "glc_eyJF057+C0x9J+QwzC5JXb5uQ/WSzn98X/iIrZXKaA3Hh+lum0XBRcu56qMlW7ZaxXrNt33XoI3CXz7IRPci=")func TestGrafana_Pattern(t *testing.T) {