Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
check for invalid_grant to avoid setting verification error (#3935)
ahravcc0e79a4e992
diff --git a/pkg/detectors/gcp/gcp.go b/pkg/detectors/gcp/gcp.goindex 85cefa57f..6551e03c8 100644--- a/pkg/detectors/gcp/gcp.go+++ b/pkg/detectors/gcp/gcp.go@@ -141,6 +141,9 @@ func verifyMatch(ctx context.Context, credBytes []byte) (bool, error) {}if _, err = credentials.TokenSource.Token(); err != nil {+ if strings.Contains(err.Error(), "invalid_grant") {+ return false, nil+ }return false, err}return true, nildiff --git a/pkg/detectors/gcp/gcp_integration_test.go b/pkg/detectors/gcp/gcp_integration_test.goindex d9f6ab847..8b21e1254 100644--- a/pkg/detectors/gcp/gcp_integration_test.go+++ b/pkg/detectors/gcp/gcp_integration_test.go@@ -9,7 +9,8 @@ import ("testing""time"- "github.com/kylelemons/godebug/pretty"+ "github.com/google/go-cmp/cmp"+ "github.com/google/go-cmp/cmp/cmpopts""github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors"@@ -25,6 +26,13 @@ func TestGCP_FromChunk(t *testing.T) {}secret := testSecrets.MustGetField("GCP_SECRET")secretInactive := testSecrets.MustGetField("GCP_INACTIVE")++ testSecrets2, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")+ if err != nil {+ t.Fatalf("could not get test secrets from GCP: %s", err)+ }+ secretDisabled := testSecrets2.MustGetField("GCP_DISABLED")+type args struct {ctx context.Contextdata []byte@@ -71,6 +79,23 @@ func TestGCP_FromChunk(t *testing.T) {},wantErr: false,},+ {+ name: "found, disabled",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a gcp secret %s within", secretDisabled)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_GCP,+ Verified: false,+ Redacted: "[email protected]",+ },+ },+ wantErr: false,+ },{name: "not found",s: Scanner{},@@ -83,6 +108,7 @@ func TestGCP_FromChunk(t *testing.T) {wantErr: false,},}+for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {s := Scanner{}@@ -97,7 +123,8 @@ func TestGCP_FromChunk(t *testing.T) {}got[i].Raw = nil}- if diff := pretty.Compare(got, tt.want); diff != "" {+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "ExtraData", "verificationError", "AnalysisInfo")+ if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {t.Errorf("GCP.FromData() %s diff: (-got +want)\n%s", tt.name, diff)}})