Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Adjust regex and add tests (#1602)
Bill Richcda88ebdf003
diff --git a/pkg/detectors/paypaloauth/paypaloauth.go b/pkg/detectors/paypaloauth/paypaloauth.goindex e76f76576..774d4fba0 100644--- a/pkg/detectors/paypaloauth/paypaloauth.go+++ b/pkg/detectors/paypaloauth/paypaloauth.go@@ -22,8 +22,8 @@ var (client = common.SaneHttpClient()// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.- idPat = regexp.MustCompile(`\b([A-Za-z0-9_\.]{7}-[A-Za-z0-9_\.]{72}|[A-Za-z0-9_\.]{5}-[A-Za-z0-9_\.]{34})\b`)- keyPat = regexp.MustCompile(`\b([A-Za-z0-9_\.\-]{40,80})\b`)+ idPat = regexp.MustCompile(`\b([A-Za-z0-9_\.]{7}-[A-Za-z0-9_\.]{72}|[A-Za-z0-9_\.]{5}-[A-Za-z0-9_\.]{38})\b`)+ keyPat = regexp.MustCompile(`\b([A-Za-z0-9_\.\-]{44,80})\b`))// Keywords are used for efficiently pre-filtering chunks.@@ -39,24 +39,24 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultmatches := keyPat.FindAllStringSubmatch(dataStr, -1)idmatches := idPat.FindAllStringSubmatch(dataStr, -1)- for _, match := range matches {- if len(match) != 2 {+ for _, idMatch := range idmatches {+ if len(idMatch) != 2 {continue}- resMatch := strings.TrimSpace(match[1])- for _, idMatch := range idmatches {- if len(idMatch) != 2 {+ resIDMatch := strings.TrimSpace(idMatch[1])+ for _, secretMatch := range matches {+ if len(secretMatch) != 2 {continue}- residMatch := strings.TrimSpace(idMatch[1])+ resSecretMatch := strings.TrimSpace(secretMatch[1])s1 := detectors.Result{DetectorType: detectorspb.DetectorType_PaypalOauth,- Raw: []byte(resMatch),+ Raw: []byte(resSecretMatch),}if verify {- data := fmt.Sprintf("%s:%s", residMatch, resMatch)+ data := fmt.Sprintf("%s:%s", resIDMatch, resSecretMatch)encoded := b64.StdEncoding.EncodeToString([]byte(data))payload := strings.NewReader("grant_type=client_credentials")req, err := http.NewRequestWithContext(ctx, "POST", "https://api-m.sandbox.paypal.com/v1/oauth2/token", payload)@@ -74,7 +74,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results1.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) {+ if detectors.IsKnownFalsePositive(resIDMatch, detectors.DefaultFalsePositives, true) {continue}}diff --git a/pkg/detectors/paypaloauth/paypaloauth_test.go b/pkg/detectors/paypaloauth/paypaloauth_test.goindex f4d5e3204..b94ff993b 100644--- a/pkg/detectors/paypaloauth/paypaloauth_test.go+++ b/pkg/detectors/paypaloauth/paypaloauth_test.go@@ -19,7 +19,7 @@ import (func TestPaypalOauth_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)}@@ -27,6 +27,9 @@ func TestPaypalOauth_FromChunk(t *testing.T) {inactiveSecret := testSecrets.MustGetField("PAYPALOAUTH_SECRET_INACTIVE")id := testSecrets.MustGetField("PAYPALOAUTH_CLIENTID")+ newId := testSecrets.MustGetField("PAYPALOAUTH_NEW_INACTIVE_CLIENTID")+ newSecret := testSecrets.MustGetField("PAYPALOAUTH_NEW_INACTIVE_SECRET")+type args struct {ctx context.Contextdata []byte@@ -79,6 +82,26 @@ func TestPaypalOauth_FromChunk(t *testing.T) {},wantErr: false,},+ {+ name: "new format, unverified",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a paypaloauth secret %s within %s but not valid", newSecret, newId)), // the secret would satisfy the regex but not pass validation+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_PaypalOauth,+ Verified: false,+ },+ {+ DetectorType: detectorspb.DetectorType_PaypalOauth,+ Verified: false,+ },+ },+ wantErr: false,+ },{name: "not found",s: Scanner{},