Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Add Coinbase Wallet-as-a-Service detector (#1895)
Richard Gomez96b25150d0f1
diff --git a/go.mod b/go.modindex 8c5df6286..dd6a941ce 100644--- a/go.mod+++ b/go.mod@@ -90,6 +90,7 @@ require (cloud.google.com/go/compute v1.20.1 // indirectcloud.google.com/go/compute/metadata v0.2.3 // indirectcloud.google.com/go/iam v1.1.0 // indirect+ cloud.google.com/go/longrunning v0.5.1 // indirectdario.cat/mergo v1.0.0 // indirectgithub.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirectgithub.com/99designs/keyring v1.2.2 // indirect@@ -137,6 +138,7 @@ require (github.com/bodgit/windows v1.0.0 // indirectgithub.com/cespare/xxhash/v2 v2.2.0 // indirectgithub.com/cloudflare/circl v1.3.3 // indirect+ github.com/coinbase/waas-client-library-go v1.0.8 // indirectgithub.com/connesc/cipherio v0.2.1 // indirectgithub.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirectgithub.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect@@ -175,6 +177,7 @@ require (github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirectgithub.com/gorilla/css v1.0.0 // indirectgithub.com/gregjones/httpcache v0.0.0-20171119193500-2bcd89a1743f // indirect+ github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirectgithub.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirectgithub.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirectgithub.com/hashicorp/errwrap v1.0.0 // indirect@@ -242,6 +245,7 @@ require (github.com/yuin/goldmark-emoji v1.0.1 // indirectgithub.com/yusufpapurcu/wmi v1.2.2 // indirectgithub.com/zeebo/xxh3 v1.0.2 // indirect+ go.einride.tech/aip v0.60.0 // indirectgo.opencensus.io v0.24.0 // indirectgo.uber.org/atomic v1.7.0 // indirectgo.uber.org/multierr v1.6.0 // indirect@@ -258,6 +262,7 @@ require (google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 // indirectgoogle.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirectgoogle.golang.org/grpc v1.56.2 // indirect+ gopkg.in/square/go-jose.v2 v2.6.0 // indirectgopkg.in/warnings.v0 v0.1.2 // indirectgopkg.in/yaml.v2 v2.4.0 // indirectgopkg.in/yaml.v3 v3.0.1 // indirectdiff --git a/go.sum b/go.sumindex 5b0a77d16..4424c35d7 100644--- a/go.sum+++ b/go.sum@@ -18,6 +18,8 @@ cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2Aawlcloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE=cloud.google.com/go/iam v1.1.0 h1:67gSqaPukx7O8WLLHMa0PNs3EBGd2eE4d+psbO/CO94=cloud.google.com/go/iam v1.1.0/go.mod h1:nxdHjaKfCr7fNYx/HJMM8LgiMugmveWlkatear5gVyk=+cloud.google.com/go/longrunning v0.5.1 h1:Fr7TXftcqTudoyRJa113hyaqlGdiBQkp0Gq7tErFDWI=+cloud.google.com/go/longrunning v0.5.1/go.mod h1:spvimkwdz6SPWKEt/XBij79E9fiTkHSQl/fRUUQJYJc=cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I=cloud.google.com/go/pubsub v1.1.0/go.mod h1:EwwdRX2sKPjnvnqCa270oGRyludottCI76h+R3AArQw=cloud.google.com/go/secretmanager v1.11.2 h1:52Z78hH8NBWIqbvIG0wi0EoTaAmSx99KIOAmDXIlX0M=@@ -196,6 +198,8 @@ github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XPgithub.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=+github.com/coinbase/waas-client-library-go v1.0.8 h1:AdbTmBQpsSUx947GZd5/68BhNBw1CSwTfE2PcnVy3Ao=+github.com/coinbase/waas-client-library-go v1.0.8/go.mod h1:RVKozprfdfMiK92ATZUWHRs0EFGHQj4rbEJjzzZzI1I=github.com/connesc/cipherio v0.2.1 h1:FGtpTPMbKNNWByNrr9aEBtaJtXjqOzkIXNYJp6OEycw=github.com/connesc/cipherio v0.2.1/go.mod h1:ukY0MWJDFnJEbXMQtOcn2VmTpRfzcTz4OoVrWGGJZcA=github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=@@ -401,7 +405,10 @@ github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBlgithub.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=github.com/gregjones/httpcache v0.0.0-20171119193500-2bcd89a1743f h1:kOkUP6rcVVqC+KlKKENKtgfFfJyDySYhqL9srXooghY=github.com/gregjones/httpcache v0.0.0-20171119193500-2bcd89a1743f/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=+github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=+github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 h1:gDLXvp5S9izjldquuoAhDzccbskOL6tDC5jMSyx3zxE=+github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2/go.mod h1:7pdNwVWBBHGiCxa9lAszqCJMbfTISJ7oMftp8+UGV08=github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=@@ -691,6 +698,8 @@ github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=+go.einride.tech/aip v0.60.0 h1:h6bgabZ5BCfAptbGex8jbh3VvPBRLa6xq+pQ1CAjHYw=+go.einride.tech/aip v0.60.0/go.mod h1:SdLbSbgSU60Xkb4TMkmsZEQPHeEWx0ikBoq5QnqZvdg=go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecqgE=go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ=go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=@@ -1010,6 +1019,8 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=+gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI=+gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=diff --git a/pkg/detectors/coinbase_waas/coinbase_waas.go b/pkg/detectors/coinbase_waas/coinbase_waas.gonew file mode 100644index 000000000..7f31d754c--- /dev/null+++ b/pkg/detectors/coinbase_waas/coinbase_waas.go@@ -0,0 +1,142 @@+package coinbase_waas++import (+ "context"+ "crypto/ecdsa"+ "crypto/x509"+ "encoding/pem"+ "errors"+ "github.com/coinbase/waas-client-library-go/auth"+ "github.com/coinbase/waas-client-library-go/clients"+ v1clients "github.com/coinbase/waas-client-library-go/clients/v1"+ v1 "github.com/coinbase/waas-client-library-go/gen/go/coinbase/cloud/pools/v1"+ "github.com/google/uuid"+ "google.golang.org/api/googleapi"+ "net/http"+ "regexp"+ "strings"++ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"+)++type Scanner struct {+ client *http.Client+}++// Ensure the Scanner satisfies the interface at compile time.+var _ detectors.Detector = (*Scanner)(nil)++var (+ // Reference: https://docs.cloud.coinbase.com/waas/docs/auth+ keyNamePat = regexp.MustCompile(`(organizations\\*/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\\*/apiKeys\\*/\w{8}-\w{4}-\w{4}-\w{4}-\w{12})`)+ privKeyPat = regexp.MustCompile(`(-----BEGIN EC(?:DSA)? PRIVATE KEY-----(?:\r|\n|\\+r|\\+n)(?:[a-zA-Z0-9+/]+={0,2}(?:\r|\n|\\+r|\\+n))+-----END EC(?:DSA)? PRIVATE KEY-----(?:\r|\n|\\+r|\\+n)?)`)++ nameReplacer = strings.NewReplacer("\\", "")+ keyReplacer = strings.NewReplacer(+ "\r\n", "\n",+ "\\r\\n", "\n",+ "\\n", "\n",+ "\\r", "\n",+ )+)++// 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{"organizations", "apiKeys", "begin ec"}+}++// FromData will find and optionally verify CoinbaseWaaS 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)++ keyNameMatches := keyNamePat.FindAllStringSubmatch(dataStr, -1)+ privKeyMatches := privKeyPat.FindAllStringSubmatch(dataStr, -1)++ for _, keyNameMatch := range keyNameMatches {+ resKeyNameMatch := nameReplacer.Replace(strings.TrimSpace(keyNameMatch[1]))++ for _, privKeyMatch := range privKeyMatches {+ resPrivKeyMatch := keyReplacer.Replace(strings.TrimSpace(privKeyMatch[1]))++ if !isValidECPrivateKey([]byte(resPrivKeyMatch)) {+ continue+ }++ s1 := detectors.Result{+ DetectorType: detectorspb.DetectorType_CoinbaseWaaS,+ Raw: []byte(resPrivKeyMatch),+ RawV2: []byte(resKeyNameMatch + ":" + resPrivKeyMatch),+ }++ if verify {+ isVerified, verificationErr := s.verifyMatch(ctx, resKeyNameMatch, resPrivKeyMatch)+ s1.Verified = isVerified+ s1.VerificationError = verificationErr+ }+ results = append(results, s1)++ // If we've found a verified match with this ID, we don't need to look for anymore. So move on to the next ID.+ if s1.Verified {+ break+ }+ }+ }++ return results, nil+}++func isValidECPrivateKey(pemKey []byte) bool {+ block, _ := pem.Decode(pemKey)+ if block == nil {+ return false+ }++ key, err := x509.ParseECPrivateKey(block.Bytes)+ if err != nil {+ return false+ }++ // Check the key type+ if _, ok := key.Public().(*ecdsa.PublicKey); !ok {+ return false+ }++ return true+}++func (s Scanner) verifyMatch(ctx context.Context, apiKeyName, privKey string) (bool, error) {+ authOpt := clients.WithAPIKey(&auth.APIKey{+ Name: apiKeyName,+ PrivateKey: privKey,+ })+ clientOpt := clients.WithHTTPClient(s.client)+ client, err := v1clients.NewPoolServiceClient(ctx, authOpt, clientOpt)+ if err != nil {+ return false, err+ }++ // Lookup an arbitrary pool name that shouldn't exist.+ _, err = client.GetPool(ctx, &v1.GetPoolRequest{Name: uuid.New().String()})+ if err != nil {+ var apiErr *googleapi.Error+ if errors.As(err, &apiErr) {+ if apiErr.Code == 401 {+ // Invalid |Name| or |PrivateKey|+ return false, nil+ } else if apiErr.Code == 404 {+ // Valid |Name| and |PrivateKey| but the pool doesn't exist (expected).+ return true, nil+ }+ }+ // Unhandled error.+ return false, err+ }+ // In theory this will never happen, but it also indicates a valid key.+ return true, nil+}++func (s Scanner) Type() detectorspb.DetectorType {+ return detectorspb.DetectorType_CoinbaseWaaS+}diff --git a/pkg/detectors/coinbase_waas/coinbase_waas_test.go b/pkg/detectors/coinbase_waas/coinbase_waas_test.gonew file mode 100644index 000000000..03c7c8542--- /dev/null+++ b/pkg/detectors/coinbase_waas/coinbase_waas_test.go@@ -0,0 +1,304 @@+//go:build detectors+// +build detectors++package coinbase_waas++import (+ "context"+ "encoding/base64"+ "fmt"+ "testing"+ "time"++ "github.com/google/go-cmp/cmp"+ "github.com/google/go-cmp/cmp/cmpopts"++ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"++ "github.com/trufflesecurity/trufflehog/v3/pkg/common"+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"+)++func TestCoinbaseWaaS_Pattern(t *testing.T) {+ tests := []struct {+ name string+ data string+ shouldMatch bool+ match string+ }{+ // True positives+ // https://github.com/coinbase/waas-client-library-go/issues/41+ {+ name: "valid_result1",+ data: `{ "name": "organizations/14d1742b-3575-4490-b9bc-a8a9c7e4973d/apiKeys/7473d38c-80c6-4a69-a715-1ea8fd950f6f", "principal": "8feb538e-137b-5864-b12a-7c75b60fa20a", "principalType": "USER", "publicKey": "-----BEGIN EC PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEzR0G+CW0uVJFrpLUELqB+DlsmGmO\nA03Az8Fpv7azpgjAy87ibgQTThaQy1C1BccbCDkPoEs6mOnDkOebkybAKQ==\n-----END EC PUBLIC KEY-----\n", "privateKey": "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIBddyynZ9Ya7op1B9nu1Dxyc1T6xLy72t45J2Smv9oXNoAoGCCqGSM49\nAwEHoUQDQgAEzR0G+CW0uVJFrpLUELqB+DlsmGmOA03Az8Fpv7azpgjAy87ibgQT\nThaQy1C1BccbCDkPoEs6mOnDkOebkybAKQ==\n-----END EC PRIVATE KEY-----\n", "createTime": "2023-08-19T12:29:08.938421763Z", "projectId": "5970e137-9c3d-4adc-b65d-58d33af2432d" }`,+ shouldMatch: true,+ match: "organizations/14d1742b-3575-4490-b9bc-a8a9c7e4973d/apiKeys/7473d38c-80c6-4a69-a715-1ea8fd950f6f:-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIBddyynZ9Ya7op1B9nu1Dxyc1T6xLy72t45J2Smv9oXNoAoGCCqGSM49\nAwEHoUQDQgAEzR0G+CW0uVJFrpLUELqB+DlsmGmOA03Az8Fpv7azpgjAy87ibgQT\nThaQy1C1BccbCDkPoEs6mOnDkOebkybAKQ==\n-----END EC PRIVATE KEY-----\n",+ },+ // https://github.com/coinbase/waas-client-library-go/pull/32#issuecomment-1666415017+ {+ name: "valid_result2_name_slashes",+ data: `{+ "name": "organizations\/d3f266dc-0d36-4cd0-91c3-e3a292b0b4b3\/apiKeys\/032c4fdf-d763-4b0c-9ed3-ff41a873bcc8",+ "principal": "5d5c9f00-3224-52a7-a1f7-9e6ce3ada40c",+ "principalType": "USER",+ "publicKey": "-----BEGIN EC PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEAjw43hwOqS2PF4gAFbhoxIJqCHAP\niqLdg5GFVn9QAS/0oY4/fJGrCn9rpQGOvHxHf1mtQ6j4bIWN1AtHvA/3uw==\n-----END EC PUBLIC KEY-----\n",+ "privateKey": "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIFkA1kU4DlNu36wTTHycWy6n1rsUH0UT8mfAKNtOukXHoAoGCCqGSM49\nAwEHoUQDQgAEAjw43hwOqS2PF4gAFbhoxIJqCHAPiqLdg5GFVn9QAS/0oY4/fJGr\nCn9rpQGOvHxHf1mtQ6j4bIWN1AtHvA/3uw==\n-----END EC PRIVATE KEY-----\n",+ "createTime": "2023-08-05T06:34:40.265235553Z",+ "projectId": "64b3f391-c69d-4c59-91a2-75816c1a0738"+ }`,+ shouldMatch: true,+ match: "organizations/d3f266dc-0d36-4cd0-91c3-e3a292b0b4b3/apiKeys/032c4fdf-d763-4b0c-9ed3-ff41a873bcc8:-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIFkA1kU4DlNu36wTTHycWy6n1rsUH0UT8mfAKNtOukXHoAoGCCqGSM49\nAwEHoUQDQgAEAjw43hwOqS2PF4gAFbhoxIJqCHAPiqLdg5GFVn9QAS/0oY4/fJGr\nCn9rpQGOvHxHf1mtQ6j4bIWN1AtHvA/3uw==\n-----END EC PRIVATE KEY-----\n",+ },+ {+ name: "valid_result3",+ data: `name: "organizations/7eead2d5-fa48-4423-8f40-c70d8ce398ae/apiKeys/7b9516b6-d82e-44e8-bed5-89b160452ed9",+ description: "principal": "775fb863-004f-5412-8e4c-e9449c612563" and install dependencies++ runs: "principalType": "USER",+ using: composite+ steps:"publicKey": "-----BEGIN EC PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEvHsvI08kox+n/8wSMFwCbK5hEf5b\n/g82Lmz3HpATKFmrICcOBX2lRHo99JWRrupmjUGxnD8i4sj4mZafTEokhA==\n-----END EC PUBLIC KEY-----\n",+ - name: Setup Node.js+ uses: actions/setup-node@v3+ with: "privateKey": "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIKOQ7lvGL0EiUzZ23pmH/NBPRwVV8yZsqofds5bSR9qFoAoGCCqGSM49\nAwEHoUQDQgAEvHsvI08kox+n/8wSMFwCbK5hEf5b/g82Lmz3HpATKFmrICcOBX2l\nRHo99JWRrupmjUGxnD8i4sj4mZafTEokhA==\n-----END EC PRIVATE KEY-----\n",+ node-version-file: .nvmrc++ - name: Cache dependencies`,+ shouldMatch: true,+ match: "organizations/7eead2d5-fa48-4423-8f40-c70d8ce398ae/apiKeys/7b9516b6-d82e-44e8-bed5-89b160452ed9:-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIKOQ7lvGL0EiUzZ23pmH/NBPRwVV8yZsqofds5bSR9qFoAoGCCqGSM49\nAwEHoUQDQgAEvHsvI08kox+n/8wSMFwCbK5hEf5b/g82Lmz3HpATKFmrICcOBX2l\nRHo99JWRrupmjUGxnD8i4sj4mZafTEokhA==\n-----END EC PRIVATE KEY-----\n",+ },+ {+ name: "valid_result_ecdsa",+ data: `{+ "name": "organizations/7eead2d5-fa48-4423-8f40-c70d8ce398ae/apiKeys/7b9516b6-d82e-44e8-bed5-89b160452ed9",+ "privateKey": "-----BEGIN ECDSA PRIVATE KEY-----\nMHcCAQEEINQdZMbF2r07KF0mxfLYt9Y1PNaC0C6UpZ31MxD4NEE8oAoGCCqGSM49\nAwEHoUQDQgAEeRFgMrQEHI/APWaziRH90jN7EozjdbPVxvzc1F4zqWTeCtLASwqA\nqnMugYX2epqsFhGn82xNXu2NwgORc6embQ==\n-----END ECDSA PRIVATE KEY-----\n"+}`,+ shouldMatch: true,+ match: "organizations/7eead2d5-fa48-4423-8f40-c70d8ce398ae/apiKeys/7b9516b6-d82e-44e8-bed5-89b160452ed9:-----BEGIN ECDSA PRIVATE KEY-----\nMHcCAQEEINQdZMbF2r07KF0mxfLYt9Y1PNaC0C6UpZ31MxD4NEE8oAoGCCqGSM49\nAwEHoUQDQgAEeRFgMrQEHI/APWaziRH90jN7EozjdbPVxvzc1F4zqWTeCtLASwqA\nqnMugYX2epqsFhGn82xNXu2NwgORc6embQ==\n-----END ECDSA PRIVATE KEY-----\n",+ },+ // TODO: Is it worth supporting case-insensitve headers?+ // https://github.com/coinbase/waas-sdk-react-native/blob/bbaf597e73d02ecaf64161061e71b85d9eeeb9d6/example/src/.coinbase_cloud_api_key.json#L4+ // {+ // name: "valid_result_case_insensitive",+ // data: `{+ // "name": "organizations/7eead2d5-fa48-4423-8f40-c70d8ce398ae/apiKeys/7b9516b6-d82e-44e8-bed5-89b160452ed9",+ // "privateKey": "-----BEGIN ECDSA private key-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg8id7yCfmNp0ppczu\nDhjB1pesdDB6Uwuz6KxARrenNfyhRANCAASI6DBntdr+XSOaK55J++x8ORuDxn81\nENa0RmGFjTwu4vQcWcx5rrIWNh6b7FPxy6mrZl0n3rswEtZmUci8Y5HX\n-----END ECDSA PRIVATE KEY-----\n"+ //}`,+ // shouldMatch: true,+ // match: "organizations/7eegad2d5-fa48-4423-8f40-c70d8ce398ae/apiKeys/7b9516b6-d82e-44e8-bed5-89b160452ed9:-----BEGIN ECDSA PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg8id7yCfmNp0ppczu\nDhjB1pesdDB6Uwuz6KxARrenNfyhRANCAASI6DBntdr+XSOaK55J++x8ORuDxn81\nENa0RmGFjTwu4vQcWcx5rrIWNh6b7FPxy6mrZl0n3rswEtZmUci8Y5HX\n-----END ECDSA PRIVATE KEY-----\n",+ // },++ // False positives+ // https://github.com/coinbase/waas-client-library-go/blob/main/example.go+ {+ name: `invalid_key_name1`,+ data: `const (+ // apiKeyName is the name of the API Key to use. Fill this out before running the main function.+ apiKeyName = "organizations/my-organization/apiKeys/my-api-key"++ // privKeyTemplate is the private key of the API Key to use. Fill this out before running the main function.+ privKeyTemplate = "-----BEGIN EC PRIVATE KEY-----\nmy-private-key\n-----END EC PRIVATE KEY-----\n"+)`,+ shouldMatch: false,+ },+ // https://github.com/coinbase/waas-sdk-react-native/blob/bbaf597e73d02ecaf64161061e71b85d9eeeb9d6/example/src/.coinbase_cloud_api_key.json#L4+ {+ name: `invalid_key_name2`,+ data: `{+ "name": "organizations/organizationID/apiKeys/apiKeyName",+ "privateKey": "-----BEGIN ECDSA Private Key-----ExamplePrivateKey-----END ECDSA Private Key-----\n"+}`,+ },+ {+ name: `invalid_private_key`,+ data: `{ "name": "organizations/14d1742b-3575-4490-b9bc-a8a9c7e4973d/apiKeys/7473d38c-80c6-4a69-a715-1ea8fd950f6f", "principal": "8feb538e-137b-5864-b12a-7c75b60fa20a", "principalType": "USER", "publicKey": "-----BEGIN EC PUBLIC KEY-----\ninvalid\n-----END EC PUBLIC KEY-----\n", "privateKey": "-----BEGIN EC PRIVATE KEY-----\ninvalid\n-----END EC PRIVATE KEY-----\n", "createTime": "2023-08-19T12:29:08.938421763Z", "projectId": "5970e137-9c3d-4adc-b65d-58d33af2432d" }`,+ },+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ s := Scanner{}++ results, err := s.FromData(context.Background(), false, []byte(test.data))+ if err != nil {+ t.Errorf("CoinbaseWaaS.FromData() error = %v", err)+ return+ }++ if test.shouldMatch {+ if len(results) == 0 {+ t.Errorf("%s: did not receive a match for '%v' when one was expected", test.name, test.data)+ return+ }+ expected := test.data+ if test.match != "" {+ expected = test.match+ }+ result := results[0]+ resultData := string(result.RawV2)+ if resultData != expected {+ t.Errorf("%s: did not receive expected match.\n\texpected: '%s'\n\t actual: '%s'", test.name, expected, resultData)+ return+ }+ } else {+ if len(results) > 0 {+ t.Errorf("%s: received a match for '%v' when one wasn't wanted", test.name, test.data)+ return+ }+ }+ })+ }+}++func TestCoinbaseWaaS_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)+ }+ secretb64 := testSecrets.MustGetField("COINBASE_WAAS")+ secretB, err := base64.StdEncoding.DecodeString(secretb64)+ if err != nil {+ t.Fatalf("could not decode secret: %s", err)+ }+ secret := string(secretB)++ inactiveSecretb64 := testSecrets.MustGetField("COINBASE_WAAS_INACTIVE")+ inactiveSecretB, err := base64.StdEncoding.DecodeString(inactiveSecretb64)+ if err != nil {+ t.Fatalf("could not decode secret: %s", err)+ }+ inactiveSecret := string(inactiveSecretB)++ type args struct {+ ctx context.Context+ data []byte+ verify bool+ }+ tests := []struct {+ name string+ s Scanner+ args args+ want []detectors.Result+ wantErr bool+ wantVerificationErr bool+ }{+ {+ name: "found, verified",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a coinbase_waas secret %s within", secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_CoinbaseWaaS,+ Verified: true,+ },+ },+ wantErr: false,+ wantVerificationErr: false,+ },+ {+ name: "found, unverified",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a coinbase_waas secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_CoinbaseWaaS,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: 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,+ 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 coinbase_waas secret %s within", secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_CoinbaseWaaS,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,+ },+ {+ name: "found, verified but unexpected api surface",+ s: Scanner{client: common.ConstantResponseHttpClient(500, "")},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a coinbase_waas secret %s within", secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_CoinbaseWaaS,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,+ },+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)+ if (err != nil) != tt.wantErr {+ t.Errorf("Coinbasewaas.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])+ }+ if (got[i].VerificationError != nil) != tt.wantVerificationErr {+ t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError)+ }+ }+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "VerificationError")+ if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {+ t.Errorf("Coinbasewaas.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) {+ b.ResetTimer()+ for n := 0; n < b.N; n++ {+ _, err := s.FromData(ctx, false, data)+ if err != nil {+ b.Fatal(err)+ }+ }+ })+ }+}diff --git a/pkg/detectors/voiceflow/voiceflow_test.go b/pkg/detectors/voiceflow/voiceflow_test.goindex b7191a6c4..87cb56cc9 100644--- a/pkg/detectors/voiceflow/voiceflow_test.go+++ b/pkg/detectors/voiceflow/voiceflow_test.go@@ -124,9 +124,9 @@ VERSION_ID = '646bc'`,t.Run(test.name, func(t *testing.T) {s := Scanner{}- results, err := s.FromData(context.Background(), true, []byte(test.data))+ results, err := s.FromData(context.Background(), false, []byte(test.data))if err != nil {- t.Errorf("CoinbaseWaaS.FromData() error = %v", err)+ t.Errorf("Voiceflow.FromData() error = %v", err)return}@@ -140,9 +140,6 @@ VERSION_ID = '646bc'`,expected = test.match}result := results[0]- if result.VerificationError != nil {- fmt.Printf("VerificationError: %v\n", result.VerificationError)- }resultData := string(result.Raw)if resultData != expected {t.Errorf("%s: did not receive expected match.\n\texpected: '%s'\n\t actual: '%s'", test.name, expected, resultData)diff --git a/pkg/engine/defaults.go b/pkg/engine/defaults.goindex 702ec11f8..b7f7b1d34 100644--- a/pkg/engine/defaults.go+++ b/pkg/engine/defaults.go@@ -145,6 +145,7 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/detectors/codequiry""github.com/trufflesecurity/trufflehog/v3/pkg/detectors/coinapi""github.com/trufflesecurity/trufflehog/v3/pkg/detectors/coinbase"+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/coinbase_waas""github.com/trufflesecurity/trufflehog/v3/pkg/detectors/coinlayer""github.com/trufflesecurity/trufflehog/v3/pkg/detectors/coinlib""github.com/trufflesecurity/trufflehog/v3/pkg/detectors/coinmarketcap"@@ -1557,6 +1558,7 @@ func DefaultDetectors() []detectors.Detector {appoptics.Scanner{},zerotier.Scanner{},betterstack.Scanner{},+ coinbase_waas.Scanner{},}}diff --git a/pkg/pb/detectorspb/detectors.pb.go b/pkg/pb/detectorspb/detectors.pb.goindex fa60eea7c..1f768527c 100644--- a/pkg/pb/detectorspb/detectors.pb.go+++ b/pkg/pb/detectorspb/detectors.pb.go@@ -1028,6 +1028,7 @@ const (DetectorType_ZeroTier DetectorType = 948DetectorType_AppOptics DetectorType = 949DetectorType_Metabase DetectorType = 950+ DetectorType_CoinbaseWaaS DetectorType = 951)// Enum value maps for DetectorType.@@ -1980,6 +1981,7 @@ var (948: "ZeroTier",949: "AppOptics",950: "Metabase",+ 951: "CoinbaseWaaS",}DetectorType_value = map[string]int32{"Alibaba": 0,@@ -2929,6 +2931,7 @@ var ("ZeroTier": 948,"AppOptics": 949,"Metabase": 950,+ "CoinbaseWaaS": 951,})@@ -3307,7 +3310,7 @@ var file_detectors_proto_rawDesc = []byte{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, 0xab, 0x77, 0x0a, 0x0c, 0x44,+ 0x09, 0x0a, 0x05, 0x55, 0x54, 0x46, 0x31, 0x36, 0x10, 0x03, 0x2a, 0xbe, 0x77, 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, 0x05, 0x41,@@ -4262,11 +4265,13 @@ var file_detectors_proto_rawDesc = []byte{0x65, 0x72, 0x53, 0x74, 0x61, 0x63, 0x6b, 0x10, 0xb3, 0x07, 0x12, 0x0d, 0x0a, 0x08, 0x5a, 0x65,0x72, 0x6f, 0x54, 0x69, 0x65, 0x72, 0x10, 0xb4, 0x07, 0x12, 0x0e, 0x0a, 0x09, 0x41, 0x70, 0x70,0x4f, 0x70, 0x74, 0x69, 0x63, 0x73, 0x10, 0xb5, 0x07, 0x12, 0x0d, 0x0a, 0x08, 0x4d, 0x65, 0x74,- 0x61, 0x62, 0x61, 0x73, 0x65, 0x10, 0xb6, 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,+ 0x61, 0x62, 0x61, 0x73, 0x65, 0x10, 0xb6, 0x07, 0x12, 0x11, 0x0a, 0x0c, 0x43, 0x6f, 0x69, 0x6e,+ 0x62, 0x61, 0x73, 0x65, 0x57, 0x61, 0x61, 0x53, 0x10, 0xb7, 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 39c8d554c..1fb57c107 100644--- a/proto/detectors.proto+++ b/proto/detectors.proto@@ -959,6 +959,7 @@ enum DetectorType {ZeroTier = 948;AppOptics = 949;Metabase = 950;+ CoinbaseWaaS = 951;}message Result {