Skip to content

Find, verify, and analyze leaked credentials

removed "unauthorized" as exception for rotated graphana secrets (#5068)

Jordan Tunstill6261f5cd38e1
diff --git a/pkg/detectors/grafana/grafana.go b/pkg/detectors/grafana/grafana.go
index 3a43bd801..00cdc4046 100644
--- a/pkg/detectors/grafana/grafana.go
+++ b/pkg/detectors/grafana/grafana.go
@@ -96,16 +96,38 @@ func verifyGrafanaKey(ctx context.Context, client *http.Client, token string) (b
}()
switch resp.StatusCode {
- case http.StatusOK:
+ case http.StatusOK, http.StatusForbidden:
+ // 200: token is valid and has permission to list tokens.
+ // 403: token is valid (authenticated) but lacks permission for this
+ // resource. Either way the credentials are genuine, so it's verified.
return true, nil
case http.StatusUnauthorized:
+ // Grafana returns 401 for two very different cases that can only be
+ // told apart by the response body:
+ //
+ // 1. A valid token that authenticated successfully but lacks the
+ // accesspolicies:read scope. The body reports a permission/scope
+ // error, e.g.:
+ // {"code":"Unauthorized","message":"invalid permission: access
+ // policy missing required scope [accesspolicies:read], ..."}
+ //
+ // 2. An invalid/revoked token that failed authentication, e.g.:
+ // {"code":"InvalidCredentials","message":"Token could not be parsed"}
+ //
+ // A permission/scope error can only be produced after authentication
+ // succeeds, so use that as the positive signal. We deliberately do not
+ // match the looser "Unauthorized" string because revoked tokens can
+ // also surface it, which previously caused false positives.
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
+ body := string(bodyBytes)
- // token is valid but has restricted permissions
- return strings.Contains(string(bodyBytes), "Unauthorized"), nil
+ if strings.Contains(body, "invalid permission") || strings.Contains(body, "required scope") {
+ return true, nil
+ }
+ return false, nil
default:
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
diff --git a/pkg/detectors/grafana/grafana_test.go b/pkg/detectors/grafana/grafana_test.go
index fa459a176..11fc608c1 100644
--- a/pkg/detectors/grafana/grafana_test.go
+++ b/pkg/detectors/grafana/grafana_test.go
@@ -2,10 +2,12 @@ package grafana
import (
"context"
+ "fmt"
"testing"
"github.com/google/go-cmp/cmp"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
)
@@ -86,3 +88,84 @@ func TestGrafana_Pattern(t *testing.T) {
})
}
}
+
+func TestGrafana_Verification(t *testing.T) {
+ tests := []struct {
+ name string
+ statusCode int
+ body string
+ wantVerified bool
+ wantErr bool
+ }{
+ {
+ name: "200 OK is verified",
+ statusCode: 200,
+ body: `[{"id":"abc"}]`,
+ wantVerified: true,
+ },
+ {
+ name: "403 Forbidden (valid token, restricted permissions) is verified",
+ statusCode: 403,
+ body: `{"message":"You'll need additional permissions to perform this action."}`,
+ wantVerified: true,
+ },
+ {
+ // Valid token that authenticated but lacks accesspolicies:read scope.
+ // Grafana returns 401 with a permission/scope error -> verified.
+ name: "401 with permission/scope error (valid token) is verified",
+ statusCode: 401,
+ body: `{"code":"Unauthorized","message":"invalid permission: access policy missing required scope [accesspolicies:read], received [accesspolicies:write]","requestId":"fc800c18-fb65-4013-bb8b-2e047a698974"}`,
+ wantVerified: true,
+ },
+ {
+ // Invalid/revoked token that failed authentication -> unverified.
+ name: "401 InvalidCredentials (revoked token) is unverified",
+ statusCode: 401,
+ body: `{"code":"InvalidCredentials","message":"Token could not be parsed","requestId":"538c7b37-2882-4727-9fae-6fc87321aa5c"}`,
+ wantVerified: false,
+ },
+ {
+ // Regression: a revoked token whose body merely contains
+ // "Unauthorized" (but no permission/scope error) must NOT be verified.
+ name: "401 generic Unauthorized (revoked token) is unverified",
+ statusCode: 401,
+ body: `{"code":"Unauthorized","message":"Unauthorized"}`,
+ wantVerified: false,
+ },
+ {
+ name: "401 with empty body is unverified",
+ statusCode: 401,
+ body: ``,
+ wantVerified: false,
+ },
+ {
+ name: "unexpected status code returns verification error",
+ statusCode: 404,
+ body: ``,
+ wantVerified: false,
+ wantErr: true,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ s := Scanner{client: common.ConstantResponseHttpClient(test.statusCode, test.body)}
+ data := []byte(fmt.Sprintf("a grafana secret %s here", secret))
+
+ results, err := s.FromData(context.Background(), true, data)
+ if err != nil {
+ t.Fatalf("FromData() unexpected error = %v", err)
+ }
+ if len(results) != 1 {
+ t.Fatalf("expected 1 result, got %d", len(results))
+ }
+
+ if results[0].Verified != test.wantVerified {
+ t.Errorf("Verified = %v, want %v", results[0].Verified, test.wantVerified)
+ }
+ if gotErr := results[0].VerificationError() != nil; gotErr != test.wantErr {
+ t.Errorf("verification error present = %v, want %v (err = %v)", gotErr, test.wantErr, results[0].VerificationError())
+ }
+ })
+ }
+}