Skip to content

Commit 20a6bce

Browse files
Merge branch 'main' into update/oss-243
2 parents 9917a07 + 78435c8 commit 20a6bce

File tree

10 files changed

+1025
-905
lines changed

10 files changed

+1025
-905
lines changed

pkg/detectors/clientary/clientary.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
RoninApp rebranded to Clientary
3+
4+
Article: https://www.clientary.com/articles/a-new-brand/
5+
*/
6+
package clientary
7+
8+
import (
9+
"context"
10+
"errors"
11+
"fmt"
12+
"io"
13+
"net/http"
14+
15+
regexp "github.com/wasilibs/go-re2"
16+
17+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
18+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
19+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
20+
)
21+
22+
type Scanner struct {
23+
detectors.DefaultMultiPartCredentialProvider
24+
}
25+
26+
// Ensure the Scanner satisfies the interface at compile time.
27+
var _ detectors.Detector = (*Scanner)(nil)
28+
29+
var (
30+
client = common.SaneHttpClient()
31+
32+
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
33+
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"ronin", "clientary"}) + `\b([0-9a-zA-Z]{24,26})\b`)
34+
idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"ronin", "clientary"}) + `\b([0-9Aa-zA-Z-]{4,25})\b`)
35+
36+
errAccountNotFound = errors.New("account not found")
37+
)
38+
39+
// Keywords are used for efficiently pre-filtering chunks.
40+
// Use identifiers in the secret preferably, or the provider name.
41+
func (s Scanner) Keywords() []string {
42+
return []string{"ronin", "clientary"}
43+
}
44+
45+
func (s Scanner) Type() detectorspb.DetectorType {
46+
return detectorspb.DetectorType_Clientary
47+
}
48+
49+
func (s Scanner) Description() string {
50+
return "Clientary is a one software app to manage Clients, Invoices, Projects, Proposals, Estimates, Hours, Payments, Contractors and Staff. Clientary keys can be used to access and manage invoices and other resources."
51+
}
52+
53+
// FromData will find and optionally verify RoninApp secrets in a given set of bytes.
54+
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
55+
dataStr := string(data)
56+
57+
var uniqueIDs, uniqueAPIKeys = make(map[string]struct{}), make(map[string]struct{})
58+
59+
for _, match := range idPat.FindAllStringSubmatch(dataStr, -1) {
60+
uniqueIDs[match[1]] = struct{}{}
61+
}
62+
63+
for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) {
64+
uniqueAPIKeys[match[1]] = struct{}{}
65+
}
66+
67+
for apiKey := range uniqueAPIKeys {
68+
for id := range uniqueIDs {
69+
// since regex matches can overlap, continue only if both apiKey and id are the same.
70+
if apiKey == id {
71+
continue
72+
}
73+
74+
s1 := detectors.Result{
75+
DetectorType: detectorspb.DetectorType_Clientary,
76+
Raw: []byte(apiKey),
77+
RawV2: []byte(apiKey + ":" + id),
78+
ExtraData: make(map[string]string),
79+
}
80+
81+
if verify {
82+
isVerified, verificationErr := verifyClientaryAPIKey(ctx, client, id, apiKey)
83+
s1.Verified = isVerified
84+
if verificationErr != nil {
85+
// remove the account ID if not found to prevent reuse during other API key checks.
86+
if errors.Is(verificationErr, errAccountNotFound) {
87+
delete(uniqueIDs, id)
88+
continue
89+
}
90+
91+
s1.SetVerificationError(verificationErr, apiKey)
92+
}
93+
94+
// If a verified result is found, attach rebranding documentation to inform the user about the RoninApp rebranding to Clientary.
95+
if s1.Verified {
96+
s1.ExtraData["Rebrading Docs"] = "https://www.clientary.com/articles/a-new-brand/"
97+
}
98+
}
99+
100+
results = append(results, s1)
101+
}
102+
103+
}
104+
105+
return results, nil
106+
}
107+
108+
// docs: https://www.clientary.com/api
109+
func verifyClientaryAPIKey(ctx context.Context, client *http.Client, id, apiKey string) (bool, error) {
110+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://"+id+".clientary.com/api/v2/invoices", http.NoBody)
111+
if err != nil {
112+
return false, nil
113+
}
114+
115+
req.SetBasicAuth(apiKey, apiKey)
116+
req.Header.Add("Accept", "application/json")
117+
118+
resp, err := client.Do(req)
119+
if err != nil {
120+
return false, err
121+
}
122+
123+
defer func() {
124+
_, _ = io.Copy(io.Discard, resp.Body)
125+
_ = resp.Body.Close()
126+
}()
127+
128+
switch resp.StatusCode {
129+
case http.StatusOK:
130+
return true, nil
131+
case http.StatusForbidden, http.StatusUnauthorized:
132+
return false, nil
133+
case http.StatusNotFound:
134+
// API return 404 if the account id does not exist
135+
return false, errAccountNotFound
136+
default:
137+
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
138+
}
139+
}

pkg/detectors/roninapp/roninapp_integration_test.go renamed to pkg/detectors/clientary/clientary_integration_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//go:build detectors
22
// +build detectors
33

4-
package roninapp
4+
package clientary
55

66
import (
77
"context"
@@ -44,13 +44,20 @@ func TestRoninApp_FromChunk(t *testing.T) {
4444
s: Scanner{},
4545
args: args{
4646
ctx: context.Background(),
47-
data: []byte(fmt.Sprintf("You can find a roninapp secret %s within roninappdomain %s", secret, domain)),
47+
data: []byte(fmt.Sprintf("You can find a clientary secret %s and clientaryDomain %s", secret, domain)),
4848
verify: true,
4949
},
5050
want: []detectors.Result{
5151
{
52-
DetectorType: detectorspb.DetectorType_RoninApp,
52+
DetectorType: detectorspb.DetectorType_Clientary,
53+
Verified: false,
54+
},
55+
{
56+
DetectorType: detectorspb.DetectorType_Clientary,
5357
Verified: true,
58+
ExtraData: map[string]string{
59+
"Rebrading Docs": "https://www.clientary.com/articles/a-new-brand/",
60+
},
5461
},
5562
},
5663
wantErr: false,
@@ -60,12 +67,12 @@ func TestRoninApp_FromChunk(t *testing.T) {
6067
s: Scanner{},
6168
args: args{
6269
ctx: context.Background(),
63-
data: []byte(fmt.Sprintf("You can find a roninapp secret %s within roninappdomain %s but not valid", inactiveSecret, domain)), // the secret would satisfy the regex but not pass validation
70+
data: []byte(fmt.Sprintf("You can find a ronin secret %s and ronaindomain %s but not valid", inactiveSecret, domain)), // the secret would satisfy the regex but not pass validation
6471
verify: true,
6572
},
6673
want: []detectors.Result{
6774
{
68-
DetectorType: detectorspb.DetectorType_RoninApp,
75+
DetectorType: detectorspb.DetectorType_Clientary,
6976
Verified: false,
7077
},
7178
},
@@ -96,6 +103,7 @@ func TestRoninApp_FromChunk(t *testing.T) {
96103
t.Fatalf("no raw secret present: \n %+v", got[i])
97104
}
98105
got[i].Raw = nil
106+
got[i].RawV2 = nil
99107
}
100108
if diff := pretty.Compare(got, tt.want); diff != "" {
101109
t.Errorf("RoninApp.FromData() %s diff: (-got +want)\n%s", tt.name, diff)

pkg/detectors/roninapp/roninapp_test.go renamed to pkg/detectors/clientary/clientary_test.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package roninapp
1+
package clientary
22

33
import (
44
"context"
5-
"fmt"
65
"testing"
76

87
"github.com/google/go-cmp/cmp"
@@ -11,14 +10,6 @@ import (
1110
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
1211
)
1312

14-
var (
15-
validKey = "tmvSxOOxP32WgoPfF2tWEmtsq8"
16-
invalidKey = "tmvSxOOxP32Wg?PfF2tWEmtsq8"
17-
validId = "XEZOhulA1oP7vPSc4K"
18-
invalidId = "?EZOhulA1oP7vPSc4?"
19-
keyword = "roninapp"
20-
)
21-
2213
func TestRoninApp_Pattern(t *testing.T) {
2314
d := Scanner{}
2415
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
@@ -28,14 +19,34 @@ func TestRoninApp_Pattern(t *testing.T) {
2819
want []string
2920
}{
3021
{
31-
name: "valid pattern - with keyword roninapp",
32-
input: fmt.Sprintf("%s token - '%s'\n%s token - '%s'\n", keyword, validKey, keyword, validId),
33-
want: []string{validKey, validKey},
22+
name: "valid pattern - with keyword ronin",
23+
input: `
24+
# some random code
25+
data := getIDFromDatabase(ctx)
26+
roninAPIKey := ZycQ0G6IBgNsBWytwzwVKixyz
27+
roninDomain := truffle-dev.roninapp.com
28+
`,
29+
want: []string{"ZycQ0G6IBgNsBWytwzwVKixyz:truffle-dev"},
30+
},
31+
{
32+
name: "valid pattern - with keyword clientary",
33+
input: `
34+
# some random code
35+
data := getIDFromDatabase(ctx)
36+
clientaryAPIKey := ZycQ0G6IBgNsBWytwzwVKixyz
37+
clientaryDomain := truffle-dev.clientary.com
38+
`,
39+
want: []string{"ZycQ0G6IBgNsBWytwzwVKixyz:truffle-dev"},
3440
},
3541
{
36-
name: "invalid pattern",
37-
input: fmt.Sprintf("%s token - '%s'\n%s token - '%s'\n", keyword, invalidKey, keyword, invalidId),
38-
want: []string{},
42+
name: "invalid pattern",
43+
input: `
44+
# some random code
45+
data := getIDFromDatabase(ctx)
46+
roninAPIKey := ZycQ0G6IBg-NsBWytwzwVKixyz
47+
rominDomain := t_de.roninapp.com
48+
`,
49+
want: []string{},
3950
},
4051
}
4152

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
}

0 commit comments

Comments
 (0)