Skip to content

Commit 3b52c2a

Browse files
Fixed Aha detector (#4354)
* Fixed Aha detector * fixed linter issue
1 parent a95f8bc commit 3b52c2a

File tree

2 files changed

+73
-56
lines changed

2 files changed

+73
-56
lines changed

pkg/detectors/aha/aha.go

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ func (s Scanner) Keywords() []string {
3434
return []string{"aha.io"}
3535
}
3636

37+
func (s Scanner) Type() detectorspb.DetectorType {
38+
return detectorspb.DetectorType_Aha
39+
}
40+
41+
func (s Scanner) Description() string {
42+
return "Aha is a product management software suite. Aha API keys can be used to access and modify product data and workflows."
43+
}
44+
3745
func (s Scanner) getClient() *http.Client {
3846
if s.client != nil {
3947
return s.client
@@ -44,30 +52,39 @@ func (s Scanner) getClient() *http.Client {
4452
// FromData will find and optionally verify Aha secrets in a given set of bytes.
4553
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
4654
dataStr := string(data)
55+
56+
var uniqueFoundUrls = make(map[string]struct{})
57+
4758
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
48-
URLmatches := URLPat.FindAllStringSubmatch(dataStr, -1)
4959

50-
resURLMatch := "aha.io"
51-
for _, URLmatch := range URLmatches {
52-
resURLMatch = strings.TrimSpace(URLmatch[1])
60+
for _, match := range URLPat.FindAllStringSubmatch(dataStr, -1) {
61+
uniqueFoundUrls[match[1]] = struct{}{}
5362
}
5463

55-
for _, match := range matches {
56-
resMatch := strings.TrimSpace(match[1])
57-
58-
s1 := detectors.Result{
59-
DetectorType: detectorspb.DetectorType_Aha,
60-
Raw: []byte(resMatch),
61-
}
64+
// if no url was found use the default
65+
if len(uniqueFoundUrls) == 0 {
66+
uniqueFoundUrls["aha.io"] = struct{}{}
67+
}
6268

63-
if verify {
64-
client := s.getClient()
65-
isVerified, verificationErr := verifyAha(ctx, client, resMatch, resURLMatch)
66-
s1.Verified = isVerified
67-
s1.SetVerificationError(verificationErr, resMatch)
69+
for _, match := range matches {
70+
for url := range uniqueFoundUrls {
71+
resMatch := strings.TrimSpace(match[1])
72+
73+
s1 := detectors.Result{
74+
DetectorType: detectorspb.DetectorType_Aha,
75+
Raw: []byte(resMatch),
76+
RawV2: []byte(resMatch + url),
77+
}
78+
79+
if verify {
80+
client := s.getClient()
81+
isVerified, verificationErr := verifyAha(ctx, client, resMatch, url)
82+
s1.Verified = isVerified
83+
s1.SetVerificationError(verificationErr, resMatch)
84+
}
85+
86+
results = append(results, s1)
6887
}
69-
70-
results = append(results, s1)
7188
}
7289

7390
return results, nil
@@ -98,11 +115,3 @@ func verifyAha(ctx context.Context, client *http.Client, resMatch, resURLMatch s
98115
return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
99116
}
100117
}
101-
102-
func (s Scanner) Type() detectorspb.DetectorType {
103-
return detectorspb.DetectorType_Aha
104-
}
105-
106-
func (s Scanner) Description() string {
107-
return "Aha is a product management software suite. Aha API keys can be used to access and modify product data and workflows."
108-
}

pkg/detectors/aha/aha_test.go

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package aha
22

33
import (
44
"context"
5-
"fmt"
6-
"strings"
75
"testing"
86

97
"github.com/google/go-cmp/cmp"
@@ -12,52 +10,62 @@ import (
1210
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
1311
)
1412

15-
var (
16-
validPattern = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff/example.aha.io"
17-
invalidPattern = "00112233445566778899aabbCC$%eeff00112233445566778899aabbccddeeff/example.fake.io"
18-
)
19-
2013
func TestAha_Pattern(t *testing.T) {
2114
d := Scanner{}
2215
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
2316

24-
key := strings.Split(validPattern, "/")[0]
25-
url := strings.Split(validPattern, "/")[1]
26-
2717
tests := []struct {
2818
name string
2919
input string
3020
want []string
3121
}{
3222
{
33-
name: "valid pattern",
34-
input: fmt.Sprintf("aha.io = '%s'", validPattern),
35-
want: []string{key},
36-
},
37-
{
38-
name: "valid pattern - detect URL far away from keyword",
39-
input: fmt.Sprintf("aha.io = '%s\n URL is not close to the keyword but should be detected %s'", key, url),
40-
want: []string{key},
23+
name: "valid pattern",
24+
input: `
25+
[INFO] sending request to the aha.io API
26+
[DEBUG] using key = 81a1411a7e276fd88819df3137eb406e0f281f8a8c417947ca4b025890c8541c
27+
[DEBUG] using host = example.aha.io
28+
[INFO] response received: 200 OK
29+
`,
30+
want: []string{"81a1411a7e276fd88819df3137eb406e0f281f8a8c417947ca4b025890c8541cexample.aha.io"},
4131
},
4232
{
43-
name: "valid pattern - key out of prefix range",
44-
input: fmt.Sprintf("aha.io keyword is not close to the real key and secret = '%s'", validPattern),
45-
want: nil,
33+
name: "valid pattern - key out of prefix range",
34+
input: `
35+
[INFO] sending request to the aha.io API
36+
[WARN] Do not commit the secrets
37+
[DEBUG] using key = 81a1411a7e276fd88819df3137eb406e0f281f8a8c417947ca4b025890c8541c
38+
[DEBUG] using host = example.aha.io
39+
[INFO] response received: 200 OK
40+
`,
41+
want: nil,
4642
},
4743
{
48-
name: "valid pattern - only key",
49-
input: fmt.Sprintf("aha.io %s", key),
50-
want: []string{key},
44+
name: "valid pattern - only key",
45+
input: `
46+
[INFO] sending request to the aha.io API
47+
[DEBUG] using key = 81a1411a7e276fd88819df3137eb406e0f281f8a8c417947ca4b025890c8541c
48+
[INFO] response received: 200 OK
49+
`,
50+
want: []string{"81a1411a7e276fd88819df3137eb406e0f281f8a8c417947ca4b025890c8541caha.io"},
5151
},
5252
{
53-
name: "valid pattern - only URL",
54-
input: fmt.Sprintf("aha.io %s", url),
55-
want: nil,
53+
name: "valid pattern - only URL",
54+
input: `
55+
[INFO] sending request to the example.aha.io API
56+
[INFO] response received: 200 OK
57+
`,
58+
want: nil,
5659
},
5760
{
58-
name: "invalid pattern",
59-
input: fmt.Sprintf("aha.io %s", invalidPattern),
60-
want: nil,
61+
name: "invalid pattern",
62+
input: `
63+
[INFO] sending request to the aha.io API
64+
[DEBUG] using key = 81a1411a7e276fd88819df3137eJ406e0f281f8a8c417947ca4b025890c8541c
65+
[DEBUG] using host = 1test.aha.io
66+
[INFO] response received: 200 OK
67+
`,
68+
want: nil,
6169
},
6270
}
6371

0 commit comments

Comments
 (0)