Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Detector-Competition-Fix: fixed zulipchat detector (#1990)
Ankush Goelb2d541e0eace
diff --git a/pkg/detectors/zulipchat/zulipchat.go b/pkg/detectors/zulipchat/zulipchat.goindex 4af23d064..1164836c8 100644--- a/pkg/detectors/zulipchat/zulipchat.go+++ b/pkg/detectors/zulipchat/zulipchat.go@@ -12,24 +12,26 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb")-type Scanner struct{}+type Scanner struct{+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (- client = common.SaneHttpClient()+ defaultClient = common.SaneHttpClient()// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.- keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"zulipchat"}) + common.BuildRegex(common.AlphaNumPattern, "", 32))- idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"zulipchat"}) + common.EmailPattern)- domainPat = regexp.MustCompile(detectors.PrefixRegex([]string{"zulipchat", "domain"}) + common.SubDomainPattern)+ keyPat = regexp.MustCompile(common.BuildRegex(common.AlphaNumPattern, "", 32))+ idPat = regexp.MustCompile(`\b([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})\b`)+ domainPat = regexp.MustCompile(`\b([a-z0-9-]+\.zulip(?:(?:chat)?\.com|\.org))\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{"zulipchat"}+ return []string{"zulip"}}// FromData will find and optionally verify ZulipChat secrets in a given set of bytes.@@ -48,7 +50,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultfor _, idMatch := range idMatches {// getting the last word of the string- resIdMatch := strings.TrimSpace(idMatch[0][strings.LastIndex(idMatch[0], " ")+1:])+ resIdMatch := strings.TrimSpace(idMatch[1])for _, domainMatch := range domainMatches {if len(domainMatch) != 2 {@@ -60,10 +62,15 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results1 := detectors.Result{DetectorType: detectorspb.DetectorType_ZulipChat,Raw: []byte(resMatch),+ RawV2: []byte(fmt.Sprintf("%s:%s:%s",resMatch, resIdMatch, resDomainMatch)),}if verify {- req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://%s.zulipchat.com/api/v1/users", resDomainMatch), nil)+ client := s.client+ if client == nil {+ client = defaultClient+ }+ req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://%s/api/v1/users", resDomainMatch), nil)if err != nil {continue}@@ -75,14 +82,18 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultdefer res.Body.Close()if res.StatusCode >= 200 && res.StatusCode < 300 {s1.Verified = true+ } else if res.StatusCode == 401 {+ // This secret is determinately not verified, nothing to do here} else {- // This function will check false positives for common test words, but also it will make sure the key appears 'random' enough to be a real key.- if detectors.IsKnownFalsePositive(resIdMatch, detectors.DefaultFalsePositives, true) {- continue- }+ s1.VerificationError = fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)}+ } else {+ s1.VerificationError = err}}+ if !s1.Verified && detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) {+ continue+ }results = append(results, s1)}diff --git a/pkg/detectors/zulipchat/zulipchat_test.go b/pkg/detectors/zulipchat/zulipchat_test.goindex 167919d8d..c16eb2849 100644--- a/pkg/detectors/zulipchat/zulipchat_test.go+++ b/pkg/detectors/zulipchat/zulipchat_test.go@@ -6,10 +6,11 @@ package zulipchatimport ("context""fmt"+ "github.com/google/go-cmp/cmp"+ "github.com/google/go-cmp/cmp/cmpopts""testing""time"- "github.com/kylelemons/godebug/pretty""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/common"@@ -24,8 +25,8 @@ func TestZulipChat_FromChunk(t *testing.T) {t.Fatalf("could not get test secrets from GCP: %s", err)}secret := testSecrets.MustGetField("ZULIPCHAT")- id := testSecrets.MustGetField("ZULIPCHAT_ID")domain := testSecrets.MustGetField("ZULIPCHAT_DOMAINV2")+ id := testSecrets.MustGetField("ZULIPCHAT_ID")inactiveSecret := testSecrets.MustGetField("ZULIPCHAT_INACTIVE")type args struct {@@ -34,11 +35,12 @@ func TestZulipChat_FromChunk(t *testing.T) {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",@@ -54,7 +56,8 @@ func TestZulipChat_FromChunk(t *testing.T) {Verified: true,},},- wantErr: false,+ wantErr: false,+ wantVerificationErr: false,},{name: "found, unverified",@@ -70,7 +73,8 @@ func TestZulipChat_FromChunk(t *testing.T) {Verified: false,},},- wantErr: false,+ wantErr: false,+ wantVerificationErr: false,},{name: "not found",@@ -80,26 +84,63 @@ func TestZulipChat_FromChunk(t *testing.T) {data: []byte("You cannot find the secret within"),verify: true,},- want: nil,- wantErr: false,+ want: nil,+ wantErr: false,+ wantVerificationErr: false,+ },+ {+ name: "found, would be verified if not for timeout",+ s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a zulipchat secret %s within zulipchat %s and zulipchat %s", secret, id, domain)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_ZulipChat,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,+ },+ {+ name: "found, verified but unexpected api surface",+ s: Scanner{client: common.ConstantResponseHttpClient(404, "")},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a zulipchat secret %s within zulipchat %s and zulipchat %s", secret, id, domain)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_ZulipChat,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,},}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("ZulipChat.FromData() error = %v, wantErr %v", err, tt.wantErr)+ t.Errorf("Zulipchat.FromData() error = %v, wantErr %v", err, tt.wantErr)return}for i := range got {if len(got[i].Raw) == 0 {t.Fatalf("no raw secret present: \n %+v", got[i])}- 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 != "" {- t.Errorf("ZulipChat.FromData() %s diff: (-got +want)\n%s", tt.name, diff)+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "VerificationError")+ if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {+ t.Errorf("Zulipchat.FromData() %s diff: (-got +want)\n%s", tt.name, diff)}})}