Skip to content

Commit 4949561

Browse files
Fix SatisMeter Detector (#3692)
* fixed and updated satismeter detector * close the response body * added tags for integration test cases
1 parent 2aa1a1a commit 4949561

File tree

3 files changed

+64
-57
lines changed

3 files changed

+64
-57
lines changed

pkg/detectors/satismeterprojectkey/satismeterprojectkey.go

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package satismeterprojectkey
22

33
import (
44
"context"
5-
b64 "encoding/base64"
65
"fmt"
6+
"io"
77
"net/http"
88
"strings"
99

@@ -25,9 +25,8 @@ var (
2525
client = common.SaneHttpClient()
2626

2727
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
28-
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"satismeter"}) + `\b([a-zA-Z0-9]{24})\b`)
29-
emailPat = regexp.MustCompile(detectors.PrefixRegex([]string{"satismeter"}) + common.EmailPattern)
30-
passPat = regexp.MustCompile(detectors.PrefixRegex([]string{"satismeter"}) + `\b([a-zA-Z0-9!=@#$%^]{6,32})`)
28+
projectPat = regexp.MustCompile(detectors.PrefixRegex([]string{"satismeter"}) + `\b([a-zA-Z0-9]{24})\b`)
29+
tokenPat = regexp.MustCompile(detectors.PrefixRegex([]string{"satismeter"}) + `\b([A-Za-z0-9]{32})\b`)
3130
)
3231

3332
// Keywords are used for efficiently pre-filtering chunks.
@@ -40,49 +39,30 @@ func (s Scanner) Keywords() []string {
4039
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
4140
dataStr := string(data)
4241

43-
uniqueEmailMatches, uniqueKeyMatches, uniquePassMatches := make(map[string]struct{}), make(map[string]struct{}), make(map[string]struct{})
44-
for _, match := range emailPat.FindAllStringSubmatch(dataStr, -1) {
45-
uniqueEmailMatches[strings.TrimSpace(match[1])] = struct{}{}
42+
uniqueProjectMatches, uniqueTokenMatches := make(map[string]struct{}), make(map[string]struct{})
43+
for _, match := range projectPat.FindAllStringSubmatch(dataStr, -1) {
44+
uniqueProjectMatches[strings.TrimSpace(match[1])] = struct{}{}
4645
}
4746

48-
for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) {
49-
uniqueKeyMatches[strings.TrimSpace(match[1])] = struct{}{}
47+
for _, match := range tokenPat.FindAllStringSubmatch(dataStr, -1) {
48+
uniqueTokenMatches[strings.TrimSpace(match[1])] = struct{}{}
5049
}
5150

52-
for _, match := range passPat.FindAllStringSubmatch(dataStr, -1) {
53-
uniquePassMatches[strings.TrimSpace(match[1])] = struct{}{}
54-
}
51+
for projectID := range uniqueProjectMatches {
52+
for token := range uniqueTokenMatches {
53+
s1 := detectors.Result{
54+
DetectorType: detectorspb.DetectorType_SatismeterProjectkey,
55+
Raw: []byte(projectID),
56+
RawV2: []byte(projectID + token),
57+
}
5558

56-
for keyMatch := range uniqueKeyMatches {
57-
for emailMatch := range uniqueEmailMatches {
58-
for passMatch := range uniquePassMatches {
59-
s1 := detectors.Result{
60-
DetectorType: detectorspb.DetectorType_SatismeterProjectkey,
61-
Raw: []byte(keyMatch),
62-
RawV2: []byte(keyMatch + passMatch),
63-
}
64-
65-
if verify {
66-
67-
data := fmt.Sprintf("%s:%s", emailMatch, passMatch)
68-
sEnc := b64.StdEncoding.EncodeToString([]byte(data))
69-
70-
req, err := http.NewRequestWithContext(ctx, "GET", "https://app.satismeter.com/api/users?project="+keyMatch, nil)
71-
if err != nil {
72-
continue
73-
}
74-
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", sEnc))
75-
res, err := client.Do(req)
76-
if err == nil {
77-
defer res.Body.Close()
78-
if res.StatusCode >= 200 && res.StatusCode < 300 {
79-
s1.Verified = true
80-
}
81-
}
82-
}
83-
84-
results = append(results, s1)
59+
if verify {
60+
isVerified, verificationErr := verifySatisMeterApp(ctx, client, projectID, token)
61+
s1.Verified = isVerified
62+
s1.SetVerificationError(verificationErr, token)
8563
}
64+
65+
results = append(results, s1)
8666
}
8767

8868
}
@@ -97,3 +77,32 @@ func (s Scanner) Type() detectorspb.DetectorType {
9777
func (s Scanner) Description() string {
9878
return "Satismeter is a customer feedback platform. Satismeter project keys can be used to access project-specific data and manage feedback settings."
9979
}
80+
81+
func verifySatisMeterApp(ctx context.Context, client *http.Client, projectID, token string) (bool, error) {
82+
req, err := http.NewRequestWithContext(ctx, "GET", "https://app.satismeter.com/api/users?project="+projectID, nil)
83+
if err != nil {
84+
return false, err
85+
}
86+
87+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
88+
resp, err := client.Do(req)
89+
if err != nil {
90+
return false, err
91+
}
92+
defer func() {
93+
_, _ = io.Copy(io.Discard, resp.Body)
94+
_ = resp.Body.Close()
95+
}()
96+
97+
switch resp.StatusCode {
98+
case http.StatusOK:
99+
return true, nil
100+
case http.StatusUnauthorized, http.StatusForbidden:
101+
return false, nil
102+
case http.StatusNotFound:
103+
// if project id is not found, api return 401
104+
return false, nil
105+
default:
106+
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
107+
}
108+
}

pkg/detectors/satismeterprojectkey/satismeterprojectkey_integration_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"time"
1111

1212
"github.com/kylelemons/godebug/pretty"
13-
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1413

1514
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
15+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
1616
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
1717
)
1818

@@ -23,10 +23,11 @@ func TestSatismeterProjectkey_FromChunk(t *testing.T) {
2323
if err != nil {
2424
t.Fatalf("could not get test secrets from GCP: %s", err)
2525
}
26-
secret := testSecrets.MustGetField("SATISMETERPROJECTKEY_TOKEN")
27-
inactiveSecret := testSecrets.MustGetField("SATISMETERPROJECTKEY_INACTIVE")
28-
email := testSecrets.MustGetField("SATISMETERPROJECTKEY_EMAIL")
29-
password := testSecrets.MustGetField("SATISMETERPROJECTKEY_PASSWORD")
26+
27+
projectID := testSecrets.MustGetField("SATISMETERPROJECTKEY")
28+
inactiveProjectID := testSecrets.MustGetField("SATISMETERPROJECTKEY_INACTIVE")
29+
token := testSecrets.MustGetField("SATISMETER_TOKEN")
30+
inactiveToken := testSecrets.MustGetField("SATISMETER_TOKEN_INACTIVE")
3031

3132
type args struct {
3233
ctx context.Context
@@ -45,13 +46,14 @@ func TestSatismeterProjectkey_FromChunk(t *testing.T) {
4546
s: Scanner{},
4647
args: args{
4748
ctx: context.Background(),
48-
data: []byte(fmt.Sprintf("You can find a satismeterprojectkey secret %s within satismeterprojectkeyemail %s satismeterprojectkeypassword %s", secret, email, password)),
49+
data: []byte(fmt.Sprintf("You can find a satismeterprojectkey id %s within satismetertoken %s", projectID, token)),
4950
verify: true,
5051
},
5152
want: []detectors.Result{
5253
{
5354
DetectorType: detectorspb.DetectorType_SatismeterProjectkey,
5455
Verified: true,
56+
RawV2: []byte(projectID + token),
5557
},
5658
},
5759
wantErr: false,
@@ -61,13 +63,14 @@ func TestSatismeterProjectkey_FromChunk(t *testing.T) {
6163
s: Scanner{},
6264
args: args{
6365
ctx: context.Background(),
64-
data: []byte(fmt.Sprintf("You can find a satismeterprojectkey secret %s within but not valid satismeterprojectkeyemail %s satismeterprojectkeypassword %s", inactiveSecret, email, password)), // the secret would satisfy the regex but not pass validation),
66+
data: []byte(fmt.Sprintf("You can find a satismeterprojectkey secret %s within satismetertoken: %s", inactiveProjectID, inactiveToken)), // the secret would satisfy the regex but not pass validation),
6567
verify: true,
6668
},
6769
want: []detectors.Result{
6870
{
6971
DetectorType: detectorspb.DetectorType_SatismeterProjectkey,
7072
Verified: false,
73+
RawV2: []byte(inactiveProjectID + inactiveToken),
7174
},
7275
},
7376
wantErr: false,

pkg/detectors/satismeterprojectkey/satismeterprojectkey_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ import (
1313

1414
var (
1515
validPattern = `
16-
satismeter_key = satismeter12345678901234
17-
satismeter_email = [email protected]
18-
satismeter_pass = satismeterSecureP@ss123
16+
satismeter_project = satismeter12345678901234
17+
satusmeter_token = VVVCVDXuoVwRFAKEiCseXmDiaC32jq7x
1918
`
20-
invalidPattern = "abcde/testing@go"
19+
invalidPattern = "satismeter45678901234/testing@go"
2120
)
2221

2322
func TestSatisMeterProjectKey_Pattern(t *testing.T) {
@@ -32,11 +31,7 @@ func TestSatisMeterProjectKey_Pattern(t *testing.T) {
3231
{
3332
name: "valid pattern",
3433
input: validPattern,
35-
want: []string{
36-
"satismeter12345678901234satismeter12345678901234",
37-
"satismeter12345678901234satismeter@example",
38-
"satismeter12345678901234satismeterSecureP@ss123",
39-
},
34+
want: []string{"satismeter12345678901234VVVCVDXuoVwRFAKEiCseXmDiaC32jq7x"},
4035
},
4136
{
4237
name: "invalid pattern",

0 commit comments

Comments
 (0)