Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Add DocuSign detector (#1382)
Zubair Khandfb1a0cd3874
diff --git a/pkg/detectors/docusign/docusign.go b/pkg/detectors/docusign/docusign.gonew file mode 100644index 000000000..18bf32ccb--- /dev/null+++ b/pkg/detectors/docusign/docusign.go@@ -0,0 +1,124 @@+package docusign++import (+ "context"+ "encoding/base64"+ "encoding/json"+ "fmt"+ "github.com/go-errors/errors"+ "io"+ "net/http"+ "regexp"+ "strings"++ "github.com/trufflesecurity/trufflehog/v3/pkg/common"+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"+)++type Scanner struct{}++type Response struct {+ AccessToken string `json:"access_token"`+}++// Ensure the Scanner satisfies the interface at compile time.+var _ detectors.Detector = (*Scanner)(nil)++var (+ client = common.SaneHttpClient()++ // Make sure that your group is surrounded in boundary characters such as below to reduce false positives.+ idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"integration", "id"}) + common.UUIDPattern)+ secretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"secret"}) + common.UUIDPattern)+)++// 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{"docusign"}+}++// FromData will find and optionally verify Docusign secrets in a given set of bytes.+func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {+ dataStr := string(data)++ idMatches := idPat.FindAllStringSubmatch(dataStr, -1)+ secretMatches := secretPat.FindAllStringSubmatch(dataStr, -1)++ for _, idMatch := range idMatches {+ if len(idMatch) != 2 {+ continue+ }+ resIDMatch := strings.TrimSpace(idMatch[1])++ for _, secretMatch := range secretMatches {+ if len(secretMatch) != 2 {+ continue+ }+ resSecretMatch := strings.TrimSpace(secretMatch[1])++ s1 := detectors.Result{+ DetectorType: detectorspb.DetectorType_Docusign,+ Raw: []byte(resIDMatch),+ Redacted: resIDMatch,+ RawV2: []byte(resIDMatch + resSecretMatch),+ }++ // Verify client id and secret pair by using an *undocumented* client_credentials grant type on the oauth2 endpoint.+ // If verifier breaks in the future, confirm that the oauth2 endpoint is still accepting the client_credentials grant type.+ if verify {+ req, err := http.NewRequestWithContext(ctx, "POST", "https://account-d.docusign.com/oauth/token?grant_type=client_credentials", nil)+ if err != nil {+ continue+ }++ encodedCredentials := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", resIDMatch, resSecretMatch)))++ req.Header.Add("Accept", "application/vnd.docusign+json; version=3")+ req.Header.Add("Authorization", fmt.Sprintf("Basic %s", encodedCredentials))+ res, err := client.Do(req)++ if err != nil {+ return nil, errors.WrapPrefix(err, "Error making request", 0)+ }++ // Read the response body+ body, err := io.ReadAll(res.Body)++ if err != nil {+ return nil, errors.WrapPrefix(err, "Error reading response body", 0)+ }++ // Close the response body+ res.Body.Close()++ // Parse the response body into a Response struct+ var parsedResponse Response+ err = json.Unmarshal(body, &parsedResponse)+ if err != nil {+ return nil, errors.WrapPrefix(err, "Error parsing response", 0)+ }++ if err == nil {+ if res.StatusCode >= 200 && res.StatusCode < 300 && strings.HasPrefix(parsedResponse.AccessToken, "ey") {+ 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(resIDMatch, detectors.DefaultFalsePositives, true) {+ continue+ }+ }+ }+ }++ results = append(results, s1)+ }+ }++ return results, nil+}++func (s Scanner) Type() detectorspb.DetectorType {+ return detectorspb.DetectorType_Docusign+}diff --git a/pkg/detectors/docusign/docusign_test.go b/pkg/detectors/docusign/docusign_test.gonew file mode 100644index 000000000..6519658fe--- /dev/null+++ b/pkg/detectors/docusign/docusign_test.go@@ -0,0 +1,124 @@+//go:build detectors+// +build detectors++package docusign++import (+ "context"+ "fmt"+ "testing"+ "time"++ "github.com/kylelemons/godebug/pretty"+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"++ "github.com/trufflesecurity/trufflehog/v3/pkg/common"+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"+)++func TestDocusign_FromChunk(t *testing.T) {+ ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)+ defer cancel()+ testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")+ if err != nil {+ t.Fatalf("could not get test secrets from GCP: %s", err)+ }+ integrationKey := testSecrets.MustGetField("DOCUSIGN_INTEGRATION_KEY_ACTIVE")+ activeSecret := testSecrets.MustGetField("DOCUSIGN_SECRET_ACTIVE")+ inactiveSecret := testSecrets.MustGetField("DOCUSIGN_SECRET_INACTIVE")++ type args struct {+ ctx context.Context+ data []byte+ verify bool+ }+ tests := []struct {+ name string+ s Scanner+ args args+ want []detectors.Result+ wantErr bool+ }{+ {+ name: "found, verified",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a docusign id %s and secret %s within", integrationKey, activeSecret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_Docusign,+ Verified: true,+ RawV2: []byte(integrationKey + activeSecret),+ Redacted: integrationKey,+ },+ },+ wantErr: false,+ },+ {+ name: "found, unverified",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a docusign id %s and secret %s within", integrationKey, inactiveSecret)), // the secret would satisfy the regex but not pass validation+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_Docusign,+ Verified: false,+ RawV2: []byte(integrationKey + inactiveSecret),+ Redacted: integrationKey,+ },+ },+ wantErr: false,+ },+ {+ name: "not found",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte("You cannot find the secret within"),+ verify: true,+ },+ want: nil,+ wantErr: false,+ },+ }+ 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)+ if (err != nil) != tt.wantErr {+ t.Errorf("Docusign.FromData() error = %v, wantErr %v", err, tt.wantErr)+ return+ }+ for i := range got {+ if len(got[i].Raw) == 0 {+ t.Fatalf("no raw secret present: \n %+v", got[i])+ }+ got[i].Raw = nil+ }+ if diff := pretty.Compare(got, tt.want); diff != "" {+ t.Errorf("Docusign.FromData() %s diff: (-got +want)\n%s", tt.name, diff)+ }+ })+ }+}++func BenchmarkFromData(benchmark *testing.B) {+ ctx := context.Background()+ s := Scanner{}+ for name, data := range detectors.MustGetBenchmarkData() {+ benchmark.Run(name, func(b *testing.B) {+ for n := 0; n < b.N; n++ {+ _, err := s.FromData(ctx, false, data)+ if err != nil {+ b.Fatal(err)+ }+ }+ })+ }+}diff --git a/pkg/engine/defaults.go b/pkg/engine/defaults.goindex 08f4de1cf..5bf41cf0e 100644--- a/pkg/engine/defaults.go+++ b/pkg/engine/defaults.go@@ -200,6 +200,7 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/detectors/dnscheck""github.com/trufflesecurity/trufflehog/v3/pkg/detectors/docparser""github.com/trufflesecurity/trufflehog/v3/pkg/detectors/documo"+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/docusign""github.com/trufflesecurity/trufflehog/v3/pkg/detectors/doppler""github.com/trufflesecurity/trufflehog/v3/pkg/detectors/dotmailer""github.com/trufflesecurity/trufflehog/v3/pkg/detectors/dovico"@@ -975,6 +976,7 @@ func DefaultDetectors() []detectors.Detector {&pandadoc.Scanner{},&juro.Scanner{},&documo.Scanner{},+ &docusign.Scanner{},&roninapp.Scanner{},&doppler.Scanner{},&codacy.Scanner{},diff --git a/pkg/pb/detectorspb/detectors.pb.go b/pkg/pb/detectorspb/detectors.pb.goindex 961cb1c9f..03bc22721 100644--- a/pkg/pb/detectorspb/detectors.pb.go+++ b/pkg/pb/detectorspb/detectors.pb.go@@ -990,6 +990,7 @@ const (DetectorType_NuGetApiKey DetectorType = 916DetectorType_Aiven DetectorType = 917DetectorType_Prefect DetectorType = 918+ DetectorType_Docusign DetectorType = 919)// Enum value maps for DetectorType.@@ -1910,6 +1911,7 @@ var (916: "NuGetApiKey",917: "Aiven",918: "Prefect",+ 919: "Docusign",}DetectorType_value = map[string]int32{"Alibaba": 0,@@ -2827,6 +2829,7 @@ var ("NuGetApiKey": 916,"Aiven": 917,"Prefect": 918,+ "Docusign": 919,})@@ -3191,7 +3194,7 @@ var file_detectors_proto_rawDesc = []byte{0x0a, 0x0b, 0x44, 0x65, 0x63, 0x6f, 0x64, 0x65, 0x72, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0b, 0x0a,0x07, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x50, 0x4c,0x41, 0x49, 0x4e, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x42, 0x41, 0x53, 0x45, 0x36, 0x34, 0x10,- 0x02, 0x12, 0x09, 0x0a, 0x05, 0x55, 0x54, 0x46, 0x31, 0x36, 0x10, 0x03, 0x2a, 0xea, 0x72, 0x0a,+ 0x02, 0x12, 0x09, 0x0a, 0x05, 0x55, 0x54, 0x46, 0x31, 0x36, 0x10, 0x03, 0x2a, 0xf9, 0x72, 0x0a,0x0c, 0x44, 0x65, 0x74, 0x65, 0x63, 0x74, 0x6f, 0x72, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0b, 0x0a,0x07, 0x41, 0x6c, 0x69, 0x62, 0x61, 0x62, 0x61, 0x10, 0x00, 0x12, 0x08, 0x0a, 0x04, 0x41, 0x4d,0x51, 0x50, 0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x41, 0x57, 0x53, 0x10, 0x02, 0x12, 0x09, 0x0a,@@ -4110,11 +4113,12 @@ var file_detectors_proto_rawDesc = []byte{0x61, 0x62, 0x61, 0x73, 0x65, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x10, 0x93, 0x07, 0x12, 0x10, 0x0a,0x0b, 0x4e, 0x75, 0x47, 0x65, 0x74, 0x41, 0x70, 0x69, 0x4b, 0x65, 0x79, 0x10, 0x94, 0x07, 0x12,0x0a, 0x0a, 0x05, 0x41, 0x69, 0x76, 0x65, 0x6e, 0x10, 0x95, 0x07, 0x12, 0x0c, 0x0a, 0x07, 0x50,- 0x72, 0x65, 0x66, 0x65, 0x63, 0x74, 0x10, 0x96, 0x07, 0x42, 0x3d, 0x5a, 0x3b, 0x67, 0x69, 0x74,- 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x72, 0x75, 0x66, 0x66, 0x6c, 0x65, 0x73,- 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2f, 0x74, 0x72, 0x75, 0x66, 0x66, 0x6c, 0x65, 0x68,- 0x6f, 0x67, 0x2f, 0x76, 0x33, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x62, 0x2f, 0x64, 0x65, 0x74,- 0x65, 0x63, 0x74, 0x6f, 0x72, 0x73, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,+ 0x72, 0x65, 0x66, 0x65, 0x63, 0x74, 0x10, 0x96, 0x07, 0x12, 0x0d, 0x0a, 0x08, 0x44, 0x6f, 0x63,+ 0x75, 0x73, 0x69, 0x67, 0x6e, 0x10, 0x97, 0x07, 0x42, 0x3d, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68,+ 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x72, 0x75, 0x66, 0x66, 0x6c, 0x65, 0x73, 0x65,+ 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x2f, 0x74, 0x72, 0x75, 0x66, 0x66, 0x6c, 0x65, 0x68, 0x6f,+ 0x67, 0x2f, 0x76, 0x33, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x62, 0x2f, 0x64, 0x65, 0x74, 0x65,+ 0x63, 0x74, 0x6f, 0x72, 0x73, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,}var (diff --git a/proto/detectors.proto b/proto/detectors.protoindex 90762fc06..175e5bc54 100644--- a/proto/detectors.proto+++ b/proto/detectors.proto@@ -927,6 +927,7 @@ enum DetectorType {NuGetApiKey = 916;Aiven = 917;Prefect = 918;+ Docusign = 919;}message Result {