Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
updating sendbird detector to use tri-state verification (#1737)
ah̳̕mͭͭͨͩ̐e̘ͬ́͋ͬ̊̓͂db28a2b9177c7
diff --git a/pkg/detectors/sendbird/sendbird.go b/pkg/detectors/sendbird/sendbird.goindex 605a174e7..4963469c4 100644--- a/pkg/detectors/sendbird/sendbird.go+++ b/pkg/detectors/sendbird/sendbird.go@@ -2,6 +2,7 @@ package sendbirdimport ("context"+ "encoding/json""fmt""net/http""regexp"@@ -12,19 +13,25 @@ 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.keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"sendbird"}) + `\b([0-9a-f]{40})\b`)appIdPat = regexp.MustCompile(detectors.PrefixRegex([]string{"sendbird"}) + `\b([0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12})\b`))+type userResp struct {+ Code int `json:"code"`+}+// Keywords are used for efficiently pre-filtering chunks.// Use identifiers in the secret preferably, or the provider name.func (s Scanner) Keywords() []string {@@ -61,20 +68,40 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultcontinue}req.Header.Add("Api-Token", resMatch)++ 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 {- // 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+ } else if res.StatusCode == 400 { // Sendbird returns 400 for all errors+ var userResp userResp+ err := json.NewDecoder(res.Body).Decode(&userResp)+ if err != nil {+ s1.VerificationError = fmt.Errorf("error decoding json response body: %w", err)+ } else if userResp.Code != 400401 {+ // https://sendbird.com/docs/chat/platform-api/v3/error-codes+ // Sendbird always includes its own error codes with 400 responses+ // 400401 (InvalidApiToken) is the only one that indicates a bad token+ s1.VerificationError = fmt.Errorf("unexpected response code: %d", userResp.Code)}+ } else {+ 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/sendbird/sendbird_test.go b/pkg/detectors/sendbird/sendbird_test.goindex d590bde1f..6f987718b 100644--- a/pkg/detectors/sendbird/sendbird_test.go+++ b/pkg/detectors/sendbird/sendbird_test.go@@ -9,8 +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""github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"@@ -19,7 +19,7 @@ import (func TestSendbird_FromChunk(t *testing.T) {ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)defer cancel()- testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors3")+ testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")if err != nil {t.Fatalf("could not get test secrets from GCP: %s", err)}@@ -33,11 +33,12 @@ func TestSendbird_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",@@ -53,7 +54,8 @@ func TestSendbird_FromChunk(t *testing.T) {Verified: true,},},- wantErr: false,+ wantErr: false,+ wantVerificationErr: false,},{name: "found, unverified",@@ -69,7 +71,8 @@ func TestSendbird_FromChunk(t *testing.T) {Verified: false,},},- wantErr: false,+ wantErr: false,+ wantVerificationErr: false,},{name: "not found",@@ -79,14 +82,82 @@ func TestSendbird_FromChunk(t *testing.T) {data: []byte("You cannot find the secret within"),verify: true,},- want: nil,- wantErr: false,+ want: nil,+ wantErr: false,+ wantVerificationErr: false,+ },+ {+ name: "found, would be verified if not for timeout",+ s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a sendbird application ID %s with sendbird secret %s within", appId, secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_Sendbird,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,+ },+ {+ name: "found, verified but unexpected api surface",+ s: Scanner{client: common.ConstantResponseHttpClient(404, "")},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a sendbird application ID %s with sendbird secret %s within", appId, secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_Sendbird,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,+ },+ {+ name: "found, verified but unexpected error code",+ s: Scanner{client: common.ConstantResponseHttpClient(400, `{"code":400402}`)},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a sendbird application ID %s with sendbird secret %s within", appId, secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_Sendbird,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,+ },+ {+ name: "found, verified but failed to decode json",+ s: Scanner{client: common.ConstantResponseHttpClient(400, `{`)},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a sendbird application ID %s with sendbird secret %s within", appId, secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_Sendbird,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,},}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("Sendbird.FromData() error = %v, wantErr %v", err, tt.wantErr)return@@ -95,9 +166,12 @@ func TestSendbird_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 != "" {+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "VerificationError")+ if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {t.Errorf("Sendbird.FromData() %s diff: (-got +want)\n%s", tt.name, diff)}})