Skip to content

Commit 928b01c

Browse files
committed
feat: add mercadopago detector
1 parent fc3f35c commit 928b01c

File tree

6 files changed

+364
-7
lines changed

6 files changed

+364
-7
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package mercadopago
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
9+
regexp "github.com/wasilibs/go-re2"
10+
11+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
12+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
13+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
14+
)
15+
16+
type Scanner struct {
17+
client *http.Client
18+
}
19+
20+
// Ensure the Scanner satisfies the interface at compile time.
21+
var _ detectors.Detector = (*Scanner)(nil)
22+
23+
var (
24+
defaultClient = common.SaneHttpClient()
25+
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
26+
accessToken = regexp.MustCompile(`\b(APP_USR-\d{16}-\d{6}-[a-f0-9]{32}__[A-Z]{2}_[A-Z]{2}__-\d+)\b`)
27+
)
28+
29+
// Keywords are used for efficiently pre-filtering chunks.
30+
// Use identifiers in the secret preferably, or the provider name.
31+
func (s Scanner) Keywords() []string {
32+
return []string{"MERCADO_PAGO", "MERCADOPAGO", "MP_ACCESS_TOKEN", "MP_", "APP_USR-"}
33+
}
34+
35+
// FromData will find and optionally verify Mercadopago secrets in a given set of bytes.
36+
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
37+
dataStr := string(data)
38+
39+
uniqueMatches := make(map[string]struct{})
40+
for _, match := range accessToken.FindAllStringSubmatch(dataStr, -1) {
41+
uniqueMatches[match[1]] = struct{}{}
42+
}
43+
44+
for match := range uniqueMatches {
45+
s1 := detectors.Result{
46+
DetectorType: detectorspb.DetectorType_MercadoPago,
47+
Raw: []byte(match),
48+
}
49+
50+
if verify {
51+
client := s.client
52+
if client == nil {
53+
client = defaultClient
54+
}
55+
56+
isVerified, extraData, verificationErr := verifyMatch(ctx, client, match)
57+
s1.Verified = isVerified
58+
s1.ExtraData = extraData
59+
s1.SetVerificationError(verificationErr, match)
60+
}
61+
62+
results = append(results, s1)
63+
}
64+
65+
return
66+
}
67+
68+
func verifyMatch(ctx context.Context, client *http.Client, token string) (bool, map[string]string, error) {
69+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.mercadopago.com/v1/payments/search", nil)
70+
if err != nil {
71+
return false, nil, err
72+
}
73+
req.Header.Set("Authorization", "Bearer "+token)
74+
res, err := client.Do(req)
75+
if err != nil {
76+
return false, nil, err
77+
}
78+
defer func() {
79+
_, _ = io.Copy(io.Discard, res.Body)
80+
_ = res.Body.Close()
81+
}()
82+
83+
switch res.StatusCode {
84+
case http.StatusOK:
85+
// If the endpoint returns useful information, we can return it as a map.
86+
return true, nil, nil
87+
case http.StatusUnauthorized:
88+
// The secret is determinately not verified (nothing to do)
89+
return false, nil, nil
90+
default:
91+
return false, nil, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
92+
}
93+
}
94+
95+
func (s Scanner) Type() detectorspb.DetectorType {
96+
return detectorspb.DetectorType_MercadoPago
97+
}
98+
99+
func (s Scanner) Description() string {
100+
return "MercadoPago is one of the largest payment processing platforms in Latin America, processing billions of dollars in transactions annually."
101+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
//go:build detectors
2+
// +build detectors
3+
4+
package mercadopago
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"testing"
10+
"time"
11+
12+
"github.com/google/go-cmp/cmp"
13+
"github.com/google/go-cmp/cmp/cmpopts"
14+
15+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
16+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
17+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
18+
)
19+
20+
func TestMercadopago_FromChunk(t *testing.T) {
21+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
22+
defer cancel()
23+
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")
24+
if err != nil {
25+
t.Fatalf("could not get test secrets from GCP: %s", err)
26+
}
27+
secret := testSecrets.MustGetField("MERCADOPAGO")
28+
inactiveSecret := testSecrets.MustGetField("MERCADOPAGO_INACTIVE")
29+
30+
type args struct {
31+
ctx context.Context
32+
data []byte
33+
verify bool
34+
}
35+
tests := []struct {
36+
name string
37+
s Scanner
38+
args args
39+
want []detectors.Result
40+
wantErr bool
41+
wantVerificationErr bool
42+
}{
43+
{
44+
name: "found, verified",
45+
s: Scanner{},
46+
args: args{
47+
ctx: context.Background(),
48+
data: []byte(fmt.Sprintf("You can find a mercadopago secret %s within", secret)),
49+
verify: true,
50+
},
51+
want: []detectors.Result{
52+
{
53+
DetectorType: detectorspb.DetectorType_MercadoPago,
54+
Verified: true,
55+
},
56+
},
57+
wantErr: false,
58+
wantVerificationErr: false,
59+
},
60+
{
61+
name: "found, unverified",
62+
s: Scanner{},
63+
args: args{
64+
ctx: context.Background(),
65+
data: []byte(fmt.Sprintf("You can find a mercadopago secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation
66+
verify: true,
67+
},
68+
want: []detectors.Result{
69+
{
70+
DetectorType: detectorspb.DetectorType_MercadoPago,
71+
Verified: false,
72+
},
73+
},
74+
wantErr: false,
75+
wantVerificationErr: false,
76+
},
77+
{
78+
name: "not found",
79+
s: Scanner{},
80+
args: args{
81+
ctx: context.Background(),
82+
data: []byte("You cannot find the secret within"),
83+
verify: true,
84+
},
85+
want: nil,
86+
wantErr: false,
87+
wantVerificationErr: false,
88+
},
89+
{
90+
name: "found, would be verified if not for timeout",
91+
s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)},
92+
args: args{
93+
ctx: context.Background(),
94+
data: []byte(fmt.Sprintf("You can find a mercadopago secret %s within", secret)),
95+
verify: true,
96+
},
97+
want: []detectors.Result{
98+
{
99+
DetectorType: detectorspb.DetectorType_MercadoPago,
100+
Verified: false,
101+
},
102+
},
103+
wantErr: false,
104+
wantVerificationErr: true,
105+
},
106+
{
107+
name: "found, verified but unexpected api surface",
108+
s: Scanner{client: common.ConstantResponseHttpClient(404, "")},
109+
args: args{
110+
ctx: context.Background(),
111+
data: []byte(fmt.Sprintf("You can find a mercadopago secret %s within", secret)),
112+
verify: true,
113+
},
114+
want: []detectors.Result{
115+
{
116+
DetectorType: detectorspb.DetectorType_MercadoPago,
117+
Verified: false,
118+
},
119+
},
120+
wantErr: false,
121+
wantVerificationErr: true,
122+
},
123+
}
124+
for _, tt := range tests {
125+
t.Run(tt.name, func(t *testing.T) {
126+
got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
127+
if (err != nil) != tt.wantErr {
128+
t.Errorf("Mercadopago.FromData() error = %v, wantErr %v", err, tt.wantErr)
129+
return
130+
}
131+
for i := range got {
132+
if len(got[i].Raw) == 0 {
133+
t.Fatalf("no raw secret present: \n %+v", got[i])
134+
}
135+
if (got[i].VerificationError() != nil) != tt.wantVerificationErr {
136+
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError())
137+
}
138+
}
139+
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError")
140+
if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
141+
t.Errorf("Mercadopago.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
142+
}
143+
})
144+
}
145+
}
146+
147+
func BenchmarkFromData(benchmark *testing.B) {
148+
ctx := context.Background()
149+
s := Scanner{}
150+
for name, data := range detectors.MustGetBenchmarkData() {
151+
benchmark.Run(name, func(b *testing.B) {
152+
b.ResetTimer()
153+
for n := 0; n < b.N; n++ {
154+
_, err := s.FromData(ctx, false, data)
155+
if err != nil {
156+
b.Fatal(err)
157+
}
158+
}
159+
})
160+
}
161+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package mercadopago
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
11+
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
12+
)
13+
14+
func TestMercadopago_Pattern(t *testing.T) {
15+
d := Scanner{}
16+
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
17+
tests := []struct {
18+
name string
19+
input string
20+
want []string
21+
}{
22+
{
23+
name: "valid pattern",
24+
input: `
25+
[INFO] Sending request to the mercadopago API
26+
[DEBUG] Using Key=APP_USR-1234567890123456-010122-a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6
27+
[INFO] Response received: 200 OK
28+
`,
29+
want: []string{"APP_USR-1234567890123456-010122-a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6"},
30+
},
31+
{
32+
name: "finds all matches",
33+
input: `
34+
[INFO] Sending request to the mercadopago API
35+
[DEBUG] Using Key=APP_USR-1234567890123456-010122-a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d4
36+
[ERROR] Response received 401 UnAuthorized
37+
[DEBUG] Using mercadopago Key=APP_USR-1234567890123456-010122-a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6
38+
[INFO] Response received: 200 OK
39+
`,
40+
want: []string{"APP_USR-1234567890123456-010122-a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d4", "APP_USR-1234567890123456-010122-a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6"},
41+
},
42+
{
43+
name: "invalid pattern",
44+
input: `
45+
[INFO] Sending request to the mercadopago API
46+
[DEBUG] Using Key=APP_USR-0123456-010122-a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6__LA_LD__-987654321
47+
[ERROR] Response received: 401 UnAuthorized
48+
`,
49+
want: []string{},
50+
},
51+
}
52+
53+
for _, test := range tests {
54+
t.Run(test.name, func(t *testing.T) {
55+
matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
56+
if len(matchedDetectors) == 0 {
57+
t.Errorf("test %q failed: expected keywords %v to be found in the input", test.name, d.Keywords())
58+
return
59+
}
60+
61+
results, err := d.FromData(context.Background(), false, []byte(test.input))
62+
require.NoError(t, err)
63+
64+
if len(results) != len(test.want) {
65+
t.Errorf("mismatch in result count: expected %d, got %d", len(test.want), len(results))
66+
return
67+
}
68+
69+
actual := make(map[string]struct{}, len(results))
70+
for _, r := range results {
71+
if len(r.RawV2) > 0 {
72+
actual[string(r.RawV2)] = struct{}{}
73+
} else {
74+
actual[string(r.Raw)] = struct{}{}
75+
}
76+
}
77+
78+
expected := make(map[string]struct{}, len(test.want))
79+
for _, v := range test.want {
80+
expected[v] = struct{}{}
81+
}
82+
83+
if diff := cmp.Diff(expected, actual); diff != "" {
84+
t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
85+
}
86+
})
87+
}
88+
}

pkg/engine/defaults/defaults.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ import (
462462
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/mediastack"
463463
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/meistertask"
464464
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/meraki"
465+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/mercadopago"
465466
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/mesibo"
466467
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/messagebird"
467468
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/metaapi"
@@ -1741,6 +1742,7 @@ func buildDetectorList() []detectors.Detector {
17411742
&zohocrm.Scanner{},
17421743
&zonkafeedback.Scanner{},
17431744
&zulipchat.Scanner{},
1745+
&mercadopago.Scanner{},
17441746
}
17451747
}
17461748

0 commit comments

Comments
 (0)