Skip to content

Commit 78435c8

Browse files
updated verification method in the dwolla detector (#4282)
Co-authored-by: Shahzad Haider <[email protected]>
1 parent c61749f commit 78435c8

File tree

3 files changed

+86
-37
lines changed

3 files changed

+86
-37
lines changed

pkg/detectors/dwolla/dwolla.go

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
b64 "encoding/base64"
66
"fmt"
7+
"io"
78
"net/http"
89
"strings"
910

@@ -15,14 +16,15 @@ import (
1516
)
1617

1718
type Scanner struct {
19+
client *http.Client
1820
detectors.DefaultMultiPartCredentialProvider
1921
}
2022

2123
// Ensure the Scanner satisfies the interface at compile time.
2224
var _ detectors.Detector = (*Scanner)(nil)
2325

2426
var (
25-
client = common.SaneHttpClient()
27+
defaultClient = common.SaneHttpClient()
2628

2729
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
2830
idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"dwolla"}) + `\b([a-zA-Z-0-9]{50})\b`)
@@ -35,46 +37,44 @@ func (s Scanner) Keywords() []string {
3537
return []string{"dwolla"}
3638
}
3739

40+
func (s Scanner) getClient() *http.Client {
41+
if s.client != nil {
42+
return s.client
43+
}
44+
return defaultClient
45+
}
46+
3847
// FromData will find and optionally verify Dwolla secrets in a given set of bytes.
3948
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
4049
dataStr := string(data)
4150

42-
idMatches := idPat.FindAllStringSubmatch(dataStr, -1)
43-
secretMatches := secretPat.FindAllStringSubmatch(dataStr, -1)
44-
45-
for _, match := range idMatches {
46-
47-
idMatch := strings.TrimSpace(match[1])
51+
uniqueIDs := make(map[string]struct{})
52+
for _, matches := range idPat.FindAllStringSubmatch(dataStr, -1) {
53+
uniqueIDs[matches[1]] = struct{}{}
54+
}
4855

49-
for _, secret := range secretMatches {
56+
uniqueSecrets := make(map[string]struct{})
57+
for _, matches := range secretPat.FindAllStringSubmatch(dataStr, -1) {
58+
uniqueSecrets[matches[1]] = struct{}{}
59+
}
5060

51-
secretMatch := strings.TrimSpace(secret[1])
61+
for id := range uniqueIDs {
62+
for secret := range uniqueSecrets {
63+
if id == secret {
64+
continue // Skip if ID and secret are the same.
65+
}
5266

5367
s1 := detectors.Result{
5468
DetectorType: detectorspb.DetectorType_Dwolla,
55-
Raw: []byte(idMatch),
56-
RawV2: []byte(idMatch + secretMatch),
69+
Raw: []byte(id),
70+
RawV2: []byte(id + secret),
5771
}
5872

5973
if verify {
60-
data := fmt.Sprintf("%s:%s", idMatch, secretMatch)
61-
encoded := b64.StdEncoding.EncodeToString([]byte(data))
62-
payload := strings.NewReader("grant_type=client_credentials")
63-
64-
req, err := http.NewRequestWithContext(ctx, "POST", "https://api-sandbox.dwolla.com/token", payload)
65-
if err != nil {
66-
continue
67-
}
68-
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
69-
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", encoded))
70-
71-
res, err := client.Do(req)
72-
if err == nil {
73-
defer res.Body.Close()
74-
if res.StatusCode >= 200 && res.StatusCode < 300 {
75-
s1.Verified = true
76-
}
77-
}
74+
client := s.getClient()
75+
isVerified, err := verifyMatch(ctx, client, id, secret)
76+
s1.Verified = isVerified
77+
s1.SetVerificationError(err, id, secret)
7878
}
7979

8080
results = append(results, s1)
@@ -84,6 +84,37 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
8484
return results, nil
8585
}
8686

87+
func verifyMatch(ctx context.Context, client *http.Client, id, secret string) (bool, error) {
88+
data := fmt.Sprintf("%s:%s", id, secret)
89+
encoded := b64.StdEncoding.EncodeToString([]byte(data))
90+
payload := strings.NewReader("grant_type=client_credentials")
91+
92+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://api-sandbox.dwolla.com/token", payload)
93+
if err != nil {
94+
return false, err
95+
}
96+
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
97+
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", encoded))
98+
99+
res, err := client.Do(req)
100+
if err != nil {
101+
return false, err
102+
}
103+
defer func() {
104+
_, _ = io.Copy(io.Discard, res.Body)
105+
_ = res.Body.Close()
106+
}()
107+
108+
switch res.StatusCode {
109+
case http.StatusOK:
110+
return true, nil
111+
case http.StatusUnauthorized:
112+
return false, nil
113+
default:
114+
return false, fmt.Errorf("unexpected status code %d", res.StatusCode)
115+
}
116+
}
117+
87118
func (s Scanner) Type() detectorspb.DetectorType {
88119
return detectorspb.DetectorType_Dwolla
89120
}

pkg/detectors/dwolla/dwolla_integration_test.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
"testing"
1010
"time"
1111

12-
"github.com/kylelemons/godebug/pretty"
12+
"github.com/google/go-cmp/cmp"
13+
"github.com/google/go-cmp/cmp/cmpopts"
1314

1415
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
1516
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
@@ -19,13 +20,13 @@ import (
1920
func TestDwolla_FromChunk(t *testing.T) {
2021
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
2122
defer cancel()
22-
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors1")
23+
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors6")
2324
if err != nil {
2425
t.Fatalf("could not get test secrets from GCP: %s", err)
2526
}
26-
id := testSecrets.MustGetField("DWOLLA")
27+
id := testSecrets.MustGetField("DWOLLA_ID")
2728
secret := testSecrets.MustGetField("DWOLLA_SECRET")
28-
inactiveSecret := testSecrets.MustGetField("DWOLLA_SECRET_INACTIVE")
29+
inactiveSecret := testSecrets.MustGetField("DWOLLA_INACTIVE")
2930

3031
type args struct {
3132
ctx context.Context
@@ -48,6 +49,10 @@ func TestDwolla_FromChunk(t *testing.T) {
4849
verify: true,
4950
},
5051
want: []detectors.Result{
52+
{
53+
DetectorType: detectorspb.DetectorType_Dwolla,
54+
Verified: false,
55+
},
5156
{
5257
DetectorType: detectorspb.DetectorType_Dwolla,
5358
Verified: true,
@@ -68,6 +73,10 @@ func TestDwolla_FromChunk(t *testing.T) {
6873
DetectorType: detectorspb.DetectorType_Dwolla,
6974
Verified: false,
7075
},
76+
{
77+
DetectorType: detectorspb.DetectorType_Dwolla,
78+
Verified: false,
79+
},
7180
},
7281
wantErr: false,
7382
},
@@ -95,9 +104,20 @@ func TestDwolla_FromChunk(t *testing.T) {
95104
if len(got[i].Raw) == 0 {
96105
t.Fatalf("no raw secret present: \n %+v", got[i])
97106
}
98-
got[i].Raw = nil
107+
gotErr := ""
108+
if got[i].VerificationError() != nil {
109+
gotErr = got[i].VerificationError().Error()
110+
}
111+
wantErr := ""
112+
if tt.want[i].VerificationError() != nil {
113+
wantErr = tt.want[i].VerificationError().Error()
114+
}
115+
if gotErr != wantErr {
116+
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.want[i].VerificationError(), got[i].VerificationError())
117+
}
99118
}
100-
if diff := pretty.Compare(got, tt.want); diff != "" {
119+
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "verificationError", "primarySecret")
120+
if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
101121
t.Errorf("Dwolla.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
102122
}
103123
})

pkg/detectors/dwolla/dwolla_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ var (
3333
# - The above credentials should only be used in a secure environment.
3434
`
3535
secrets = []string{
36-
"MvkLktYDS7PSE0xRMHIYBKrAjXruEk5P1VrJUUGtgspa3KTi6rMvkLktYDS7PSE0xRMHIYBKrAjXruEk5P1VrJUUGtgspa3KTi6r",
3736
"MvkLktYDS7PSE0xRMHIYBKrAjXruEk5P1VrJUUGtgspa3KTi6rq3DZbY7iviUpewfCHEpK1I51G8XW63GuLuJyAIEqOFtEB1qlg1",
3837
"q3DZbY7iviUpewfCHEpK1I51G8XW63GuLuJyAIEqOFtEB1qlg1MvkLktYDS7PSE0xRMHIYBKrAjXruEk5P1VrJUUGtgspa3KTi6r",
39-
"q3DZbY7iviUpewfCHEpK1I51G8XW63GuLuJyAIEqOFtEB1qlg1q3DZbY7iviUpewfCHEpK1I51G8XW63GuLuJyAIEqOFtEB1qlg1",
4038
}
4139
)
4240

0 commit comments

Comments
 (0)