Skip to content

Commit 5a79c79

Browse files
authored
[Fix] Updated DigitalOcean Token Detector (#4052)
* updated digital ocean detector * added build tags in detector integration test * Revert "added build tags in detector integration test" This reverts commit d569c66. * added build tags in detector integration test * updated unexpected error test case name * removed forbidden status unverification
1 parent d2b3377 commit 5a79c79

File tree

2 files changed

+83
-36
lines changed

2 files changed

+83
-36
lines changed

pkg/detectors/digitaloceantoken/digitaloceantoken.go

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7-
"strings"
87

98
regexp "github.com/wasilibs/go-re2"
109

@@ -13,15 +12,16 @@ import (
1312
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
1413
)
1514

16-
type Scanner struct{}
15+
type Scanner struct {
16+
client *http.Client
17+
}
1718

1819
// Ensure the Scanner satisfies the interface at compile time.
1920
var _ detectors.Detector = (*Scanner)(nil)
2021

2122
var (
22-
client = common.SaneHttpClient()
23-
24-
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"ocean", "do"}) + `\b([A-Za-z0-9_-]{64})\b`)
23+
defaultClient = common.SaneHttpClient()
24+
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"ocean", "do"}) + `\b([A-Za-z0-9_-]{64})\b`)
2525
)
2626

2727
// Keywords are used for efficiently pre-filtering chunks.
@@ -33,31 +33,29 @@ func (s Scanner) Keywords() []string {
3333
// FromData will find and optionally verify DigitalOceanToken secrets in a given set of bytes.
3434
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
3535
dataStr := string(data)
36+
var uniqueTokens = make(map[string]struct{})
3637

37-
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
38-
39-
for _, match := range matches {
40-
resMatch := strings.TrimSpace(match[1])
38+
for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {
39+
uniqueTokens[matches[1]] = struct{}{}
40+
}
4141

42+
for token := range uniqueTokens {
4243
s1 := detectors.Result{
4344
DetectorType: detectorspb.DetectorType_DigitalOceanToken,
44-
Raw: []byte(resMatch),
45+
Raw: []byte(token),
4546
}
4647

4748
if verify {
48-
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.digitalocean.com/v2/account", nil)
49-
if err != nil {
50-
continue
49+
client := s.client
50+
if client == nil {
51+
client = defaultClient
5152
}
52-
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
53-
res, err := client.Do(req)
54-
if err == nil {
55-
defer res.Body.Close()
56-
if res.StatusCode >= 200 && res.StatusCode < 300 {
57-
s1.Verified = true
58-
s1.AnalysisInfo = map[string]string{
59-
"key": resMatch,
60-
}
53+
isVerified, verificationErr := verifyDigitalOceanToken(ctx, client, token)
54+
s1.Verified = isVerified
55+
s1.SetVerificationError(verificationErr)
56+
if s1.Verified {
57+
s1.AnalysisInfo = map[string]string{
58+
"key": token,
6159
}
6260
}
6361
}
@@ -68,6 +66,31 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
6866
return results, nil
6967
}
7068

69+
func verifyDigitalOceanToken(ctx context.Context, client *http.Client, token string) (bool, error) {
70+
// Ref: https://docs.digitalocean.com/reference/api/digitalocean/#tag/Account
71+
72+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.digitalocean.com/v2/account", nil)
73+
if err != nil {
74+
return false, fmt.Errorf("failed to create request: %w", err)
75+
}
76+
77+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
78+
resp, err := client.Do(req)
79+
if err != nil {
80+
return false, fmt.Errorf("failed to make request: %w", err)
81+
}
82+
defer resp.Body.Close()
83+
84+
switch resp.StatusCode {
85+
case http.StatusOK:
86+
return true, nil
87+
case http.StatusUnauthorized:
88+
return false, nil
89+
default:
90+
return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
91+
}
92+
}
93+
7194
func (s Scanner) Type() detectorspb.DetectorType {
7295
return detectorspb.DetectorType_DigitalOceanToken
7396
}

pkg/detectors/digitaloceantoken/digitaloceantoken_integration_test.go

Lines changed: 38 additions & 14 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"
@@ -32,11 +33,12 @@ func TestDigitalOceanToken_FromChunk(t *testing.T) {
3233
verify bool
3334
}
3435
tests := []struct {
35-
name string
36-
s Scanner
37-
args args
38-
want []detectors.Result
39-
wantErr bool
36+
name string
37+
s Scanner
38+
args args
39+
want []detectors.Result
40+
wantErr bool
41+
wantVerificationErr bool
4042
}{
4143
{
4244
name: "found, verified",
@@ -52,7 +54,8 @@ func TestDigitalOceanToken_FromChunk(t *testing.T) {
5254
Verified: true,
5355
},
5456
},
55-
wantErr: false,
57+
wantErr: false,
58+
wantVerificationErr: false,
5659
},
5760
{
5861
name: "found, unverified",
@@ -68,7 +71,8 @@ func TestDigitalOceanToken_FromChunk(t *testing.T) {
6871
Verified: false,
6972
},
7073
},
71-
wantErr: false,
74+
wantErr: false,
75+
wantVerificationErr: false,
7276
},
7377
{
7478
name: "not found",
@@ -78,14 +82,31 @@ func TestDigitalOceanToken_FromChunk(t *testing.T) {
7882
data: []byte("You cannot find the secret within"),
7983
verify: true,
8084
},
81-
want: nil,
82-
wantErr: false,
85+
want: nil,
86+
wantErr: false,
87+
wantVerificationErr: false,
88+
},
89+
{
90+
name: "found verifiable secret, verification failed due to unexpected API response",
91+
s: Scanner{client: common.ConstantResponseHttpClient(404, "")},
92+
args: args{
93+
ctx: context.Background(),
94+
data: []byte(fmt.Sprintf("You can find a digitaloceantoken secret %s within", secret)),
95+
verify: true,
96+
},
97+
want: []detectors.Result{
98+
{
99+
DetectorType: detectorspb.DetectorType_DigitalOceanToken,
100+
Verified: false,
101+
},
102+
},
103+
wantErr: false,
104+
wantVerificationErr: true,
83105
},
84106
}
85107
for _, tt := range tests {
86108
t.Run(tt.name, func(t *testing.T) {
87-
s := Scanner{}
88-
got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
109+
got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
89110
if (err != nil) != tt.wantErr {
90111
t.Errorf("DigitalOceanToken.FromData() error = %v, wantErr %v", err, tt.wantErr)
91112
return
@@ -94,9 +115,12 @@ func TestDigitalOceanToken_FromChunk(t *testing.T) {
94115
if len(got[i].Raw) == 0 {
95116
t.Fatalf("no raw secret present: \n %+v", got[i])
96117
}
97-
got[i].Raw = nil
118+
if (got[i].VerificationError() != nil) != tt.wantVerificationErr {
119+
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError())
120+
}
98121
}
99-
if diff := pretty.Compare(got, tt.want); diff != "" {
122+
ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "verificationError", "AnalysisInfo")
123+
if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
100124
t.Errorf("DigitalOceanToken.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
101125
}
102126
})

0 commit comments

Comments
 (0)