Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Add tri-state verification to pubnub publish key detector (#1616)
Cody Rose18f854d14209
diff --git a/pkg/detectors/pubnubpublishkey/pubnubpublishkey.go b/pkg/detectors/pubnubpublishkey/pubnubpublishkey.goindex 04b4a98d0..92fc62ffb 100644--- a/pkg/detectors/pubnubpublishkey/pubnubpublishkey.go+++ b/pkg/detectors/pubnubpublishkey/pubnubpublishkey.go@@ -2,6 +2,7 @@ package pubnubpublishkeyimport ("context"+ "fmt""net/http""regexp""strings"@@ -11,13 +12,15 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = common.SaneHttpClient()// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.pubPat = regexp.MustCompile(`\b(pub-c-[0-9a-z]{8}-[0-9a-z]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})\b`)@@ -60,19 +63,31 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultif err != nil {continue}+ client := s.client+ if client == nil {+ client = defaultClient+ }res, err := client.Do(req)if err == nil {defer res.Body.Close()if res.StatusCode >= 200 && res.StatusCode < 300 {s1.Verified = true+ } else if res.StatusCode == 400 || res.StatusCode == 403 {+ // 403 is suggested by the API docs (https://www.pubnub.com/docs/sdks/rest-api/send-signal-to-channel)+ // 400 is what actually seems to be coming back for invalid credentials+ // There's nothing to do here, because zero values of Result are what we want} else {- // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key.- if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) {- continue- }+ s1.VerificationError = fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)}+ } else {+ s1.VerificationError = err}}+ // This function will check false positives for common test words, but also it will make sure the key+ // appears 'random' enough to be a real key.+ if !s1.Verified && detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) {+ continue+ }results = append(results, s1)}diff --git a/pkg/detectors/pubnubpublishkey/pubnubpublishkey_test.go b/pkg/detectors/pubnubpublishkey/pubnubpublishkey_test.goindex 174f8421f..312c5eb99 100644--- a/pkg/detectors/pubnubpublishkey/pubnubpublishkey_test.go+++ b/pkg/detectors/pubnubpublishkey/pubnubpublishkey_test.go@@ -6,10 +6,11 @@ package pubnubpublishkeyimport ("context""fmt"+ "github.com/google/go-cmp/cmp"+ "github.com/google/go-cmp/cmp/cmpopts""testing""time"- "github.com/kylelemons/godebug/pretty""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/common"@@ -33,11 +34,12 @@ func TestPubNubPublishKey_FromChunk(t *testing.T) {verify bool}tests := []struct {- name string- s Scanner- args args- want []detectors.Result- wantErr bool+ name string+ s Scanner+ args args+ want []detectors.Result+ wantErr bool+ wantVerificationErr bool}{{name: "found, verified",@@ -71,6 +73,40 @@ func TestPubNubPublishKey_FromChunk(t *testing.T) {},wantErr: false,},+ {+ name: "found, would be verified but for timeout",+ s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a pubnub secret %s within pubnub %s", pub, sub)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_PubNubPublishKey,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,+ },+ {+ name: "found, valid but unexpected api response",+ s: Scanner{client: common.ConstantResponseHttpClient(500, "")},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a pubnub secret %s within pubnub %s", pub, sub)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_PubNubPublishKey,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,+ },{name: "not found",s: Scanner{},@@ -85,8 +121,7 @@ func TestPubNubPublishKey_FromChunk(t *testing.T) {}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {- s := Scanner{}- got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)+ got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)if (err != nil) != tt.wantErr {t.Errorf("PubNubPublishKey.FromData() error = %v, wantErr %v", err, tt.wantErr)return@@ -95,9 +130,12 @@ func TestPubNubPublishKey_FromChunk(t *testing.T) {if len(got[i].Raw) == 0 {t.Fatalf("no raw secret present: \n %+v", got[i])}- got[i].Raw = nil+ if (got[i].VerificationError != nil) != tt.wantVerificationErr {+ t.Fatalf(" wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError)+ }}- if diff := pretty.Compare(got, tt.want); diff != "" {+ opts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "VerificationError")+ if diff := cmp.Diff(got, tt.want, opts); diff != "" {t.Errorf("PubNubPublishKey.FromData() %s diff: (-got +want)\n%s", tt.name, diff)}})