Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
updating microsoft teams webhook detector to use tri-state verification (#1792)
āh̳̕mͭͭͨͩ̐e̘ͬ́͋ͬ̊̓͂daf1434e05a2a
diff --git a/pkg/detectors/microsoftteamswebhook/microsoftteamswebhook.go b/pkg/detectors/microsoftteamswebhook/microsoftteamswebhook.goindex 0b3e9fa97..d9deb9f9e 100644--- a/pkg/detectors/microsoftteamswebhook/microsoftteamswebhook.go+++ b/pkg/detectors/microsoftteamswebhook/microsoftteamswebhook.go@@ -2,6 +2,7 @@ package microsoftteamswebhookimport ("context"+ "fmt""io""net/http""regexp"@@ -13,13 +14,15 @@ 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.SaneHttpClientTimeOut(5 * time.Second)+ defaultClient = common.SaneHttpClientTimeOut(5 * time.Second)// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.keyPat = regexp.MustCompile(`(https:\/\/[a-zA-Z-0-9]+\.webhook\.office\.com\/webhookb2\/[a-zA-Z-0-9]{8}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{12}\@[a-zA-Z-0-9]{8}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{12}\/IncomingWebhook\/[a-zA-Z-0-9]{32}\/[a-zA-Z-0-9]{8}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{4}-[a-zA-Z-0-9]{12})`)@@ -47,23 +50,16 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultDetectorType: detectorspb.DetectorType_MicrosoftTeamsWebhook,Raw: []byte(resMatch),}+if verify {- payload := strings.NewReader(`{'text':''}`)- req, err := http.NewRequestWithContext(ctx, "POST", resMatch, payload)- if err != nil {- continue- }- req.Header.Add("Content-Type", "application/json")- res, err := client.Do(req)- if err == nil {- body, err := io.ReadAll(res.Body)- res.Body.Close()- if err == nil {- if res.StatusCode >= 200 && strings.Contains(string(body), "Text is required") {- s1.Verified = true- }- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyWebhook(ctx, client, resMatch)+ s1.Verified = isVerified+ s1.VerificationError = verificationErr}if !s1.Verified && detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, false) {@@ -76,6 +72,38 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}+func verifyWebhook(ctx context.Context, client *http.Client, webhookURL string) (bool, error) {+ payload := strings.NewReader(`{'text':''}`)+ req, err := http.NewRequestWithContext(ctx, "POST", webhookURL, payload)+ if err != nil {+ return false, err+ }+ req.Header.Add("Content-Type", "application/json")++ res, err := client.Do(req)+ if err != nil {+ return false, err+ }+ defer res.Body.Close()++ body, err := io.ReadAll(res.Body)+ if err != nil {+ return false, err+ }++ switch {+ case res.StatusCode == http.StatusBadRequest:+ if strings.Contains(string(body), "Text is required") {+ return true, nil+ }+ return false, fmt.Errorf("unexpected response body: %s", string(body))+ case res.StatusCode < 200 || res.StatusCode >= 500:+ return false, fmt.Errorf("unexpected HTTP response status: %d", res.StatusCode)+ default:+ return false, nil+ }+}+func (s Scanner) Type() detectorspb.DetectorType {return detectorspb.DetectorType_MicrosoftTeamsWebhook}diff --git a/pkg/detectors/microsoftteamswebhook/microsoftteamswebhook_test.go b/pkg/detectors/microsoftteamswebhook/microsoftteamswebhook_test.goindex 186fb9930..0db3ff2b1 100644--- a/pkg/detectors/microsoftteamswebhook/microsoftteamswebhook_test.go+++ b/pkg/detectors/microsoftteamswebhook/microsoftteamswebhook_test.go@@ -9,7 +9,8 @@ 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/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/common"@@ -32,11 +33,12 @@ func TestMicrosoftTeamsWebhook_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,6 +56,57 @@ func TestMicrosoftTeamsWebhook_FromChunk(t *testing.T) {},wantErr: false,},+ {+ name: "found, verified but unexpected response body",+ s: Scanner{client: common.ConstantResponseHttpClient(400, "nope")},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a microsoftteamswebhook secret %s within", secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_MicrosoftTeamsWebhook,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,+ },+ {+ name: "found, verified but unexpected api surface",+ s: Scanner{client: common.ConstantResponseHttpClient(500, "")},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a microsoftteamswebhook secret %s within", secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_MicrosoftTeamsWebhook,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,+ },+ {+ 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 microsoftteamswebhook secret %s within", secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_MicrosoftTeamsWebhook,+ Verified: false,+ },+ },+ wantErr: false,+ wantVerificationErr: true,+ },{name: "found, unverified",s: Scanner{},@@ -84,8 +137,7 @@ func TestMicrosoftTeamsWebhook_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("MicrosoftTeamsWebhook.FromData() error = %v, wantErr %v", err, tt.wantErr)return@@ -94,9 +146,13 @@ func TestMicrosoftTeamsWebhook_FromChunk(t *testing.T) {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.Errorf("MicrosoftTeamsWebhook.FromData() verificationError = %v, wantVerificationErr %v", got[i].VerificationError, tt.wantVerificationErr)+ return+ }}- if diff := pretty.Compare(got, tt.want); diff != "" {+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "VerificationError")+ if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {t.Errorf("MicrosoftTeamsWebhook.FromData() %s diff: (-got +want)\n%s", tt.name, diff)}})