Skip to content

Find, verify, and analyze leaked credentials

Separate gitlab detectors (#1819)

āh̳̕mͭͭͨͩ̐e̘ͬ́͋ͬ̊̓͂d5df6afdbf494
diff --git a/pkg/detectors/gitlab/gitlab.go b/pkg/detectors/gitlab/gitlab.go
index a464c7af8..bf2aac0aa 100644
--- a/pkg/detectors/gitlab/gitlab.go
+++ b/pkg/detectors/gitlab/gitlab.go
@@ -14,21 +14,20 @@ import (
type Scanner struct {
client *http.Client
-
detectors.EndpointSetter
}
// Ensure the Scanner satisfies the interfaces at compile time.
var _ detectors.Detector = (*Scanner)(nil)
-var _ detectors.Versioner = (*Scanner)(nil)
var _ detectors.EndpointCustomizer = (*Scanner)(nil)
+var _ detectors.Versioner = (*Scanner)(nil)
func (Scanner) Version() int { return 1 }
func (Scanner) DefaultEndpoint() string { return "https://gitlab.com" }
var (
defaultClient = common.SaneHttpClient()
- keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"gitlab"}) + `\b((?:glpat|)[a-zA-Z0-9\-=_]{20,22})\b`)
+ keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"gitlab"}) + `\b([a-zA-Z0-9\-=_]{20,22})\b`)
)
// Keywords are used for efficiently pre-filtering chunks.
@@ -42,73 +41,80 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
dataStr := string(data)
matches := keyPat.FindAllStringSubmatch(dataStr, -1)
-
for _, match := range matches {
if len(match) != 2 {
continue
}
- resMatch := strings.TrimSpace(match[1])
- if strings.Contains(match[0], "glpat") {
- keyString := strings.Split(match[0], " ")
- resMatch = keyString[len(keyString)-1]
+ // ignore v2 detectors which have a prefix of `glpat-`
+ if strings.Contains(match[0], "glpat-") {
+ continue
}
+ resMatch := strings.TrimSpace(match[1])
- secret := detectors.Result{
+ s1 := detectors.Result{
DetectorType: detectorspb.DetectorType_Gitlab,
- Raw: []byte(match[1]),
+ Raw: []byte(resMatch),
}
if verify {
- // there are 4 read 'scopes' for a gitlab token: api, read_user, read_repo, and read_registry
- // they all grant access to different parts of the API. I couldn't find an endpoint that every
- // one of these scopes has access to, so we just check an example endpoint for each scope. If any
- // of them contain data, we know we have a valid key, but if they all fail, we don't
-
- client := s.client
- if client == nil {
- client = defaultClient
- }
- for _, baseURL := range s.Endpoints(s.DefaultEndpoint()) {
- // test `read_user` scope
- req, err := http.NewRequestWithContext(ctx, "GET", baseURL+"/api/v4/user", nil)
- if err != nil {
- continue
- }
- req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
- res, err := client.Do(req)
- if err == nil {
- res.Body.Close() // The request body is unused.
-
- // 200 means good key and has `read_user` scope
- // 403 means good key but not the right scope
- // 401 is bad key
- switch res.StatusCode {
- case http.StatusOK:
- secret.Verified = true
- case http.StatusForbidden:
- // Good key but not the right scope
- secret.Verified = true
- case http.StatusUnauthorized:
- // Nothing to do; zero values are the ones we want
- default:
- secret.VerificationError = fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
- }
- } else {
- secret.VerificationError = err
- }
- }
+ isVerified, verificationErr := s.verifyGitlab(ctx, resMatch)
+ s1.Verified = isVerified
+ s1.VerificationError = verificationErr
}
- if !secret.Verified && detectors.IsKnownFalsePositive(string(secret.Raw), detectors.DefaultFalsePositives, true) {
+ if !s1.Verified && detectors.IsKnownFalsePositive(string(s1.Raw), detectors.DefaultFalsePositives, true) {
continue
}
- results = append(results, secret)
+ results = append(results, s1)
}
return results, nil
}
+func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, error) {
+ // there are 4 read 'scopes' for a gitlab token: api, read_user, read_repo, and read_registry
+ // they all grant access to different parts of the API. I couldn't find an endpoint that every
+ // one of these scopes has access to, so we just check an example endpoint for each scope. If any
+ // of them contain data, we know we have a valid key, but if they all fail, we don't
+
+ client := s.client
+ if client == nil {
+ client = defaultClient
+ }
+ for _, baseURL := range s.Endpoints(s.DefaultEndpoint()) {
+ // test `read_user` scope
+ req, err := http.NewRequestWithContext(ctx, "GET", baseURL+"/api/v4/user", nil)
+ if err != nil {
+ continue
+ }
+ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
+ res, err := client.Do(req)
+ if err != nil {
+ return false, err
+ }
+ defer res.Body.Close() // The request body is unused.
+
+ // 200 means good key and has `read_user` scope
+ // 403 means good key but not the right scope
+ // 401 is bad key
+ switch res.StatusCode {
+ case http.StatusOK:
+ return true, nil
+ case http.StatusForbidden:
+ // Good key but not the right scope
+ return true, nil
+ case http.StatusUnauthorized:
+ // Nothing to do; zero values are the ones we want
+ return false, nil
+ default:
+ return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
+ }
+
+ }
+ return false, nil
+}
+
func (s Scanner) Type() detectorspb.DetectorType {
return detectorspb.DetectorType_Gitlab
}
diff --git a/pkg/detectors/gitlab/gitlab_test.go b/pkg/detectors/gitlab/gitlab_v1_test.go
similarity index 99%
rename from pkg/detectors/gitlab/gitlab_test.go
rename to pkg/detectors/gitlab/gitlab_v1_test.go
index 04e23e8be..60ea59516 100644
--- a/pkg/detectors/gitlab/gitlab_test.go
+++ b/pkg/detectors/gitlab/gitlab_v1_test.go
@@ -6,11 +6,12 @@ package gitlab
import (
"context"
"fmt"
- "github.com/google/go-cmp/cmp"
- "github.com/google/go-cmp/cmp/cmpopts"
"testing"
"time"
+ "github.com/google/go-cmp/cmp"
+ "github.com/google/go-cmp/cmp/cmpopts"
+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
diff --git a/pkg/detectors/gitlab/gitlabv2_test.go b/pkg/detectors/gitlab/gitlab_v2_test.go
similarity index 66%
rename from pkg/detectors/gitlab/gitlabv2_test.go
rename to pkg/detectors/gitlab/gitlab_v2_test.go
index a3f0dfec0..1c5f3a030 100644
--- a/pkg/detectors/gitlab/gitlabv2_test.go
+++ b/pkg/detectors/gitlab/gitlab_v2_test.go
@@ -1,3 +1,6 @@
+//go:build detectors
+// +build detectors
+
package gitlab
import (
@@ -6,13 +9,14 @@ import (
"testing"
"time"
- "github.com/kylelemons/godebug/pretty"
+ "github.com/google/go-cmp/cmp"
+ "github.com/google/go-cmp/cmp/cmpopts"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
- "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
-func TestGitlabv2_FromChunk(t *testing.T) {
+// This test ensures gitlab v1 detector does not work on gitlab v2 secrets
+func TestGitlab_FromChunk_WithV2Secrets(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors4")
@@ -21,48 +25,40 @@ func TestGitlabv2_FromChunk(t *testing.T) {
}
secret := testSecrets.MustGetField("GITLABV2")
secretInactive := testSecrets.MustGetField("GITLABV2_INACTIVE")
+
type args struct {
ctx context.Context
data []byte
verify bool
}
tests := []struct {
- name string
- s Scanner
- args args
- want []detectors.Result
- wantErr bool
+ name string
+ s Scanner
+ args args
+ want []detectors.Result
+ wantErr bool
+ wantVerificationErr bool
}{
{
- name: "found",
+ name: "verified secret, not found",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a gitlab secret %s within", secret)),
verify: true,
},
- want: []detectors.Result{
- {
- DetectorType: detectorspb.DetectorType_Gitlab,
- Verified: true,
- },
- },
+ want: nil,
wantErr: false,
},
{
- name: "found, unverified",
+ name: "unverified secret, not found",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a gitlab secret %s within", secretInactive)),
verify: true,
},
- want: []detectors.Result{
- {
- DetectorType: detectorspb.DetectorType_Gitlab,
- Verified: false,
- },
- },
+ want: nil,
wantErr: false,
},
{
@@ -79,8 +75,7 @@ func TestGitlabv2_FromChunk(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- s := Scanner{}
- got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
+ got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("Gitlab.FromData() error = %v, wantErr %v", err, tt.wantErr)
return
@@ -89,16 +84,19 @@ func TestGitlabv2_FromChunk(t *testing.T) {
if len(got[i].Raw) == 0 {
t.Fatal("no raw secret present")
}
- got[i].Raw = nil
+ if (got[i].VerificationError != nil) != tt.wantVerificationErr {
+ t.Fatalf(" wantVerificationError = %v, verification error = %v,", tt.wantVerificationErr, got[i].VerificationError)
+ }
}
- if diff := pretty.Compare(got, tt.want); diff != "" {
+ opts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "VerificationError")
+ if diff := cmp.Diff(got, tt.want, opts); diff != "" {
t.Errorf("Gitlab.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
}
})
}
}
-func BenchmarkFromData2(benchmark *testing.B) {
+func BenchmarkV2FromData(benchmark *testing.B) {
ctx := context.Background()
s := Scanner{}
for name, data := range detectors.MustGetBenchmarkData() {
diff --git a/pkg/detectors/gitlabv2/gitlab.go b/pkg/detectors/gitlabv2/gitlab.go
deleted file mode 100644
index c522751df..000000000
--- a/pkg/detectors/gitlabv2/gitlab.go
+++ /dev/null
@@ -1,90 +0,0 @@
-package gitlabv2
-
-import (
- "context"
- "fmt"
- "net/http"
- "regexp"
-
- "github.com/trufflesecurity/trufflehog/v3/pkg/common"
- "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
- "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
-)
-
-type Scanner struct{ detectors.EndpointSetter }
-
-// Ensure the Scanner satisfies the interfaces at compile time.
-var _ detectors.Detector = (*Scanner)(nil)
-var _ detectors.Versioner = (*Scanner)(nil)
-var _ detectors.EndpointCustomizer = (*Scanner)(nil)
-
-func (Scanner) Version() int { return 2 }
-func (Scanner) DefaultEndpoint() string { return "https://gitlab.com" }
-
-var (
- keyPat = regexp.MustCompile(`\b(glpat-[a-zA-Z0-9\-=_]{20,22})\b`)
-)
-
-// Keywords are used for efficiently pre-filtering chunks.
-// Use identifiers in the secret preferably, or the provider name.
-func (s Scanner) Keywords() []string {
- return []string{"glpat-"}
-}
-
-// FromData will find and optionally verify Gitlab secrets in a given set of bytes.
-func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
- dataStr := string(data)
-
- matches := keyPat.FindAllStringSubmatch(dataStr, -1)
-
- for _, match := range matches {
- if len(match) != 2 {
- continue
- }
-
- secret := detectors.Result{
- DetectorType: detectorspb.DetectorType_Gitlab,
- Raw: []byte(match[1]),
- }
-
- if verify {
- // there are 4 read 'scopes' for a gitlab token: api, read_user, read_repo, and read_registry
- // they all grant access to different parts of the API. I couldn't find an endpoint that every
- // one of these scopes has access to, so we just check an example endpoint for each scope. If any
- // of them contain data, we know we have a valid key, but if they all fail, we don't
- client := common.SaneHttpClient()
- for _, baseURL := range s.Endpoints(s.DefaultEndpoint()) {
- // test `read_user` scope
- req, err := http.NewRequestWithContext(ctx, "GET", baseURL+"/api/v4/user", nil)
- if err != nil {
- continue
- }
- req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", match[1]))
- res, err := client.Do(req)
- if err == nil {
- res.Body.Close() // The request body is unused.
-
- // 200 means good key and has `read_user` scope
- // 403 means good key but not the right scope
- // 401 is bad key
- if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusForbidden {
- secret.Verified = true
- }
- }
- }
-
- }
-
- if !secret.Verified && detectors.IsKnownFalsePositive(string(secret.Raw), detectors.DefaultFalsePositives, true) {
- continue
- }
-
- results = append(results, secret)
- }
-
- return
-}
-
-func (s Scanner) Type() detectorspb.DetectorType {
- return detectorspb.DetectorType_Gitlab
-}
diff --git a/pkg/detectors/gitlabv2/gitlab_test.go b/pkg/detectors/gitlabv2/gitlab_v1_test.go
similarity index 55%
rename from pkg/detectors/gitlabv2/gitlab_test.go
rename to pkg/detectors/gitlabv2/gitlab_v1_test.go
index bafef652f..2c91a85f3 100644
--- a/pkg/detectors/gitlabv2/gitlab_test.go
+++ b/pkg/detectors/gitlabv2/gitlab_v1_test.go
@@ -9,56 +9,64 @@ import (
"testing"
"time"
- "github.com/kylelemons/godebug/pretty"
+ "github.com/google/go-cmp/cmp"
+ "github.com/google/go-cmp/cmp/cmpopts"
+
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
- "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
-func TestGitlab_FromChunk(t *testing.T) {
+// This test ensures gitlab v2 detector does not work on gitlab v1 secrets
+func TestGitlabV2_FromChunk_WithV1Secrets(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
- testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors2")
+ testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors4")
if err != nil {
t.Fatalf("could not get test secrets from GCP: %s", err)
}
- secret := testSecrets.MustGetField("GITLABV2")
- secretInactive := testSecrets.MustGetField("GITLABV2_INACTIVE")
-
+ secret := testSecrets.MustGetField("GITLAB")
+ secretInactive := testSecrets.MustGetField("GITLAB_INACTIVE")
type args struct {
ctx context.Context
data []byte
verify bool
}
tests := []struct {
- name string
- s Scanner
- args args
- want []detectors.Result
- wantErr bool
+ name string
+ s Scanner
+ args args
+ want []detectors.Result
+ wantErr bool
+ wantVerificationErr bool
}{
{
- name: "found, verified",
+ name: "verified v1 secret, not found",
s: Scanner{},
args: args{
ctx: context.Background(),
- data: []byte(fmt.Sprintf("You can find a aws secret %s within", secret)),
+ data: []byte(fmt.Sprintf("You can find a gitlab super secret %s within", secret)),
verify: true,
},
- want: []detectors.Result{
- {
- DetectorType: detectorspb.DetectorType_Gitlab,
- Verified: true,
- },
+ want: nil,
+ wantErr: false,
+ },
+ {
+ name: "verified v1 secret, not found",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("gitlab %s", secret)),
+ verify: true,
},
+ want: nil,
wantErr: false,
},
{
- name: "found unverified",
+ name: "unverified v1 secret, not found",
s: Scanner{},
args: args{
ctx: context.Background(),
- data: []byte(fmt.Sprintf("You can find a aws secret %s within", secretInactive)),
+ data: []byte(fmt.Sprintf("You can find a gitlab secret %s within", secretInactive)),
verify: true,
},
want: nil,
@@ -78,8 +86,7 @@ func TestGitlab_FromChunk(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- s := Scanner{}
- got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
+ got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("Gitlab.FromData() error = %v, wantErr %v", err, tt.wantErr)
return
@@ -88,9 +95,12 @@ func TestGitlab_FromChunk(t *testing.T) {
if len(got[i].Raw) == 0 {
t.Fatal("no raw secret present")
}
- got[i].Raw = nil
+ if (got[i].VerificationError != nil) != tt.wantVerificationErr {
+ t.Fatalf(" wantVerificationError = %v, verification error = %v,", tt.wantVerificationErr, got[i].VerificationError)
+ }
}
- if diff := pretty.Compare(got, tt.want); diff != "" {
+ opts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "VerificationError")
+ if diff := cmp.Diff(got, tt.want, opts); diff != "" {
t.Errorf("Gitlab.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
}
})
diff --git a/pkg/detectors/gitlabv2/gitlab_v2.go b/pkg/detectors/gitlabv2/gitlab_v2.go
new file mode 100644
index 000000000..dc8b237be
--- /dev/null
+++ b/pkg/detectors/gitlabv2/gitlab_v2.go
@@ -0,0 +1,116 @@
+package gitlabv2
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "regexp"
+ "strings"
+
+ "github.com/trufflesecurity/trufflehog/v3/pkg/common"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
+)
+
+type Scanner struct {
+ client *http.Client
+ detectors.EndpointSetter
+}
+
+// Ensure the Scanner satisfies the interfaces at compile time.
+var _ detectors.Detector = (*Scanner)(nil)
+var _ detectors.EndpointCustomizer = (*Scanner)(nil)
+var _ detectors.Versioner = (*Scanner)(nil)
+
+func (Scanner) Version() int { return 2 }
+func (Scanner) DefaultEndpoint() string { return "https://gitlab.com" }
+
+var (
+ defaultClient = common.SaneHttpClient()
+ keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"gitlab"}) + `\b(glpat-[a-zA-Z0-9\-=_]{20,22})\b`)
+)
+
+// Keywords are used for efficiently pre-filtering chunks.
+// Use identifiers in the secret preferably, or the provider name.
+func (s Scanner) Keywords() []string {
+ return []string{"gitlab"}
+}
+
+// FromData will find and optionally verify Gitlab secrets in a given set of bytes.
+func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
+ dataStr := string(data)
+
+ matches := keyPat.FindAllStringSubmatch(dataStr, -1)
+ for _, match := range matches {
+ if len(match) != 2 {
+ continue
+ }
+
+ resMatch := strings.TrimSpace(match[1])
+ s1 := detectors.Result{
+ DetectorType: detectorspb.DetectorType_Gitlab,
+ Raw: []byte(resMatch),
+ }
+
+ if verify {
+ isVerified, verificationErr := s.verifyGitlab(ctx, resMatch)
+ s1.Verified = isVerified
+ s1.VerificationError = verificationErr
+ }
+
+ if !s1.Verified && detectors.IsKnownFalsePositive(string(s1.Raw), detectors.DefaultFalsePositives, true) {
+ continue
+ }
+
+ results = append(results, s1)
+ }
+
+ return results, nil
+}
+
+func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, error) {
+ // there are 4 read 'scopes' for a gitlab token: api, read_user, read_repo, and read_registry
+ // they all grant access to different parts of the API. I couldn't find an endpoint that every
+ // one of these scopes has access to, so we just check an example endpoint for each scope. If any
+ // of them contain data, we know we have a valid key, but if they all fail, we don't
+
+ client := s.client
+ if client == nil {
+ client = defaultClient
+ }
+ for _, baseURL := range s.Endpoints(s.DefaultEndpoint()) {
+ // test `read_user` scope
+ req, err := http.NewRequestWithContext(ctx, "GET", baseURL+"/api/v4/user", nil)
+ if err != nil {
+ continue
+ }
+ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
+ res, err := client.Do(req)
+ if err != nil {
+ return false, err
+ }
+ defer res.Body.Close() // The request body is unused.
+
+ // 200 means good key and has `read_user` scope
+ // 403 means good key but not the right scope
+ // 401 is bad key
+ switch res.StatusCode {
+ case http.StatusOK:
+ return true, nil
+ case http.StatusForbidden:
+ // Good key but not the right scope
+ return true, nil
+ case http.StatusUnauthorized:
+ // Nothing to do; zero values are the ones we want
+ return false, nil
+ default:
+ return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
+ }
+
+ }
+ return false, nil
+}
+
+func (s Scanner) Type() detectorspb.DetectorType {
+ return detectorspb.DetectorType_Gitlab
+}
diff --git a/pkg/detectors/gitlabv2/gitlab_v2_test.go b/pkg/detectors/gitlabv2/gitlab_v2_test.go
new file mode 100644
index 000000000..d074c6fb0
--- /dev/null
+++ b/pkg/detectors/gitlabv2/gitlab_v2_test.go
@@ -0,0 +1,173 @@
+//go:build detectors
+// +build detectors
+
+package gitlabv2
+
+import (
+ "context"
+ "fmt"
+ "testing"
+ "time"
+
+ "github.com/google/go-cmp/cmp"
+ "github.com/google/go-cmp/cmp/cmpopts"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/common"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
+)
+
+func TestGitlabV2_FromChunk(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
+ defer cancel()
+ testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors4")
+ if err != nil {
+ t.Fatalf("could not get test secrets from GCP: %s", err)
+ }
+ secret := testSecrets.MustGetField("GITLABV2")
+ secretInactive := testSecrets.MustGetField("GITLABV2_INACTIVE")
+
+ type args struct {
+ ctx context.Context
+ data []byte
+ verify bool
+ }
+ tests := []struct {
+ name string
+ s Scanner
+ args args
+ want []detectors.Result
+ wantErr bool
+ wantVerificationErr bool
+ }{
+ {
+ name: "found, verified",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a gitlab secret %s within", secret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Gitlab,
+ Verified: true,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "found unverified",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a gitlab secret %s within", secretInactive)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Gitlab,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "found, would be verified but for timeout",
+ s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a gitlab super secret %s within", secret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Gitlab,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ wantVerificationErr: true,
+ },
+ {
+ name: "found and valid but unexpected api response",
+ s: Scanner{client: common.ConstantResponseHttpClient(500, "")},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a gitlab super secret %s within", secret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Gitlab,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ wantVerificationErr: true,
+ },
+ {
+ name: "found, good key but wrong scope",
+ s: Scanner{client: common.ConstantResponseHttpClient(403, "")},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a gitlab super secret %s within", secret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Gitlab,
+ Verified: true,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "not found",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte("You cannot find the secret within"),
+ verify: true,
+ },
+ want: nil,
+ wantErr: false,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("Gitlab.FromData() error = %v, wantErr %v", err, tt.wantErr)
+ return
+ }
+ for i := range got {
+ if len(got[i].Raw) == 0 {
+ t.Fatal("no raw secret present")
+ }
+ if (got[i].VerificationError != nil) != tt.wantVerificationErr {
+ t.Fatalf(" wantVerificationError = %v, verification error = %v,", tt.wantVerificationErr, got[i].VerificationError)
+ }
+ }
+ opts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "VerificationError")
+ if diff := cmp.Diff(got, tt.want, opts); diff != "" {
+ t.Errorf("Gitlab.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
+ }
+ })
+ }
+}
+
+func BenchmarkV2FromData(benchmark *testing.B) {
+ ctx := context.Background()
+ s := Scanner{}
+ for name, data := range detectors.MustGetBenchmarkData() {
+ benchmark.Run(name, func(b *testing.B) {
+ b.ResetTimer()
+ for n := 0; n < b.N; n++ {
+ _, err := s.FromData(ctx, false, data)
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+ })
+ }
+}
diff --git a/pkg/engine/defaults.go b/pkg/engine/defaults.go
index 6ef90fe64..bef3dbc59 100644
--- a/pkg/engine/defaults.go
+++ b/pkg/engine/defaults.go
@@ -771,8 +771,8 @@ func DefaultDetectors() []detectors.Detector {
aws.New(),
&azure.Scanner{},
&slack.Scanner{}, // has 4 secret types
- &gitlabv2.Scanner{},
&gitlab.Scanner{},
+ &gitlabv2.Scanner{},
&sendgrid.Scanner{},
&mailchimp.Scanner{},
&okta.Scanner{},