Skip to content

Commit 79a8ed8

Browse files
authored
Refactor Netlify Detector (#4102)
* test: added netlify v2 tokens in GCP detectors5 * refactor: refactored netlify detector based on new detector implementations * refactor: response status handling using switch case * refactor: PR feedback incorporated. Unchanged variables moved to const. ExtraData removed from verifyMatch function. * task: added AnalysisInfo map in netlify detectors for analyzer * fix: analyzed required key for analyzer and modified
1 parent 3547a6e commit 79a8ed8

File tree

4 files changed

+97
-39
lines changed

4 files changed

+97
-39
lines changed

pkg/detectors/netlify/v1/netlify_v1.go

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package netlify
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"net/http"
78
"strconv"
8-
"strings"
99

1010
regexp "github.com/wasilibs/go-re2"
1111

@@ -22,10 +22,14 @@ var _ detectors.Versioner = (*Scanner)(nil)
2222

2323
var (
2424
client = common.SaneHttpClient()
25-
2625
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"netlify"}) + `\b([A-Za-z0-9_-]{43,45})\b`)
2726
)
2827

28+
const (
29+
rotationGuideUrl = "https://howtorotate.com/docs/tutorials/netlify/"
30+
verificationUrl = "https://api.netlify.com/api/v1/sites"
31+
)
32+
2933
func (Scanner) Version() int { return 1 }
3034

3135
// Keywords are used for efficiently pre-filtering chunks.
@@ -38,32 +42,29 @@ func (s Scanner) Keywords() []string {
3842
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
3943
dataStr := string(data)
4044

41-
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
45+
uniqueMatches := make(map[string]struct{})
4246

43-
for _, match := range matches {
44-
resMatch := strings.TrimSpace(match[1])
47+
for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) {
48+
uniqueMatches[match[1]] = struct{}{}
49+
}
4550

51+
for match := range uniqueMatches {
4652
s1 := detectors.Result{
4753
DetectorType: detectorspb.DetectorType_Netlify,
48-
Raw: []byte(resMatch),
54+
Raw: []byte(match),
4955
}
5056
s1.ExtraData = map[string]string{
51-
"rotation_guide": "https://howtorotate.com/docs/tutorials/netlify/",
57+
"rotation_guide": rotationGuideUrl,
5258
"version": strconv.Itoa(s.Version()),
5359
}
5460

5561
if verify {
56-
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.netlify.com/api/v1/sites", nil)
57-
if err != nil {
58-
continue
59-
}
60-
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
61-
res, err := client.Do(req)
62-
if err == nil {
63-
defer res.Body.Close()
64-
if res.StatusCode >= 200 && res.StatusCode < 300 {
65-
s1.Verified = true
66-
}
62+
isVerified, verificationErr := verifyMatch(ctx, client, match)
63+
s1.Verified = isVerified
64+
s1.SetVerificationError(verificationErr, match)
65+
66+
if s1.Verified {
67+
s1.AnalysisInfo = map[string]string{"key": match}
6768
}
6869
}
6970

@@ -73,6 +74,33 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
7374
return results, nil
7475
}
7576

77+
func verifyMatch(ctx context.Context, client *http.Client, token string) (bool, error) {
78+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, verificationUrl, nil)
79+
if err != nil {
80+
return false, nil
81+
}
82+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
83+
res, err := client.Do(req)
84+
85+
if err != nil {
86+
return false, err
87+
}
88+
89+
defer func() {
90+
_, _ = io.Copy(io.Discard, res.Body)
91+
_ = res.Body.Close()
92+
}()
93+
94+
switch res.StatusCode {
95+
case http.StatusOK:
96+
return true, nil
97+
case http.StatusUnauthorized:
98+
return false, nil
99+
default:
100+
return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
101+
}
102+
}
103+
76104
func (s Scanner) Type() detectorspb.DetectorType {
77105
return detectorspb.DetectorType_Netlify
78106
}

pkg/detectors/netlify/v1/netlify_v1_integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func TestNetlify_FromChunk(t *testing.T) {
103103
t.Fatalf("no raw secret present: \n %+v", got[i])
104104
}
105105
got[i].Raw = nil
106+
got[i].AnalysisInfo = nil
106107
}
107108
if diff := pretty.Compare(got, tt.want); diff != "" {
108109
t.Errorf("Netlify.FromData() %s diff: (-got +want)\n%s", tt.name, diff)

pkg/detectors/netlify/v2/netlify_v2.go

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package netlify
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"net/http"
78
"strconv"
8-
"strings"
99

1010
regexp "github.com/wasilibs/go-re2"
1111

@@ -22,10 +22,14 @@ var _ detectors.Versioner = (*Scanner)(nil)
2222

2323
var (
2424
client = common.SaneHttpClient()
25-
2625
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"netlify"}) + `\b(nfp_[a-zA-Z0-9_]{36})\b`)
2726
)
2827

28+
const (
29+
rotationGuideUrl = "https://howtorotate.com/docs/tutorials/netlify/"
30+
verificationUrl = "https://api.netlify.com/api/v1/sites"
31+
)
32+
2933
func (Scanner) Version() int { return 2 }
3034

3135
// Keywords are used for efficiently pre-filtering chunks.
@@ -38,32 +42,29 @@ func (s Scanner) Keywords() []string {
3842
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
3943
dataStr := string(data)
4044

41-
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
45+
uniqueMatches := make(map[string]struct{})
4246

43-
for _, match := range matches {
44-
resMatch := strings.TrimSpace(match[1])
47+
for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) {
48+
uniqueMatches[match[1]] = struct{}{}
49+
}
4550

51+
for match := range uniqueMatches {
4652
s1 := detectors.Result{
4753
DetectorType: detectorspb.DetectorType_Netlify,
48-
Raw: []byte(resMatch),
54+
Raw: []byte(match),
4955
}
5056
s1.ExtraData = map[string]string{
51-
"rotation_guide": "https://howtorotate.com/docs/tutorials/netlify/",
57+
"rotation_guide": rotationGuideUrl,
5258
"version": strconv.Itoa(s.Version()),
5359
}
5460

5561
if verify {
56-
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.netlify.com/api/v1/sites", nil)
57-
if err != nil {
58-
continue
59-
}
60-
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
61-
res, err := client.Do(req)
62-
if err == nil {
63-
defer res.Body.Close()
64-
if res.StatusCode >= 200 && res.StatusCode < 300 {
65-
s1.Verified = true
66-
}
62+
isVerified, verificationErr := verifyMatch(ctx, client, match)
63+
s1.Verified = isVerified
64+
s1.SetVerificationError(verificationErr, match)
65+
66+
if s1.Verified {
67+
s1.AnalysisInfo = map[string]string{"key": match}
6768
}
6869
}
6970

@@ -73,6 +74,33 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
7374
return results, nil
7475
}
7576

77+
func verifyMatch(ctx context.Context, client *http.Client, token string) (bool, error) {
78+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, verificationUrl, nil)
79+
if err != nil {
80+
return false, nil
81+
}
82+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
83+
res, err := client.Do(req)
84+
85+
if err != nil {
86+
return false, err
87+
}
88+
89+
defer func() {
90+
_, _ = io.Copy(io.Discard, res.Body)
91+
_ = res.Body.Close()
92+
}()
93+
94+
switch res.StatusCode {
95+
case http.StatusOK:
96+
return true, nil
97+
case http.StatusUnauthorized:
98+
return false, nil
99+
default:
100+
return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
101+
}
102+
}
103+
76104
func (s Scanner) Type() detectorspb.DetectorType {
77105
return detectorspb.DetectorType_Netlify
78106
}

pkg/detectors/netlify/v2/netlify_v2_integration_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import (
1919
func TestNetlify_FromChunk(t *testing.T) {
2020
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
2121
defer cancel()
22-
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors3")
22+
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")
2323
if err != nil {
2424
t.Fatalf("could not get test secrets from GCP: %s", err)
2525
}
26-
secret := testSecrets.MustGetField("NETLIFY_TOKEN")
27-
inactiveSecret := testSecrets.MustGetField("NETLIFY_INACTIVE")
26+
secret := testSecrets.MustGetField("NETLIFY_V2_TOKEN")
27+
inactiveSecret := testSecrets.MustGetField("NETLIFY_V2_INACTIVE")
2828

2929
type args struct {
3030
ctx context.Context
@@ -103,6 +103,7 @@ func TestNetlify_FromChunk(t *testing.T) {
103103
t.Fatalf("no raw secret present: \n %+v", got[i])
104104
}
105105
got[i].Raw = nil
106+
got[i].AnalysisInfo = nil
106107
}
107108
if diff := pretty.Compare(got, tt.want); diff != "" {
108109
t.Errorf("Netlify.FromData() %s diff: (-got +want)\n%s", tt.name, diff)

0 commit comments

Comments
 (0)