Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
update slack webhook with tri-state verification (#1724)
Zubair Khan07702ea06d17
diff --git a/pkg/detectors/slackwebhook/slackwebhook.go b/pkg/detectors/slackwebhook/slackwebhook.goindex 6a4a9d418..2f8fe6ab6 100644--- a/pkg/detectors/slackwebhook/slackwebhook.go+++ b/pkg/detectors/slackwebhook/slackwebhook.go@@ -2,6 +2,7 @@ package slackwebhookimport ("context"+ "fmt""io""net/http""regexp"@@ -12,14 +13,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.SaneHttpClient()-+ defaultClient = common.SaneHttpClient()// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.keyPat = regexp.MustCompile(`(https://hooks\.slack\.com/services/T[A-Z0-9]+/B[A-Z0-9]+/[A-Za-z0-9]{23,25})`))@@ -48,6 +50,12 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result}if verify {++ client := s.client+ if client == nil {+ client = defaultClient+ }+payload := strings.NewReader(`{"text": ""}`)req, err := http.NewRequestWithContext(ctx, "POST", resMatch, payload)if err != nil {@@ -62,9 +70,18 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultcontinue}body := string(bodyBytes)- if (res.StatusCode >= 200 && res.StatusCode < 300) || (res.StatusCode == 400 && (strings.Contains(body, "no_text") || strings.Contains(body, "missing_text"))) {++ defer res.Body.Close()++ if res.StatusCode >= 200 && res.StatusCode < 300 || (res.StatusCode == 400 && (strings.Contains(body, "no_text") || strings.Contains(body, "missing_text"))) {s1.Verified = true+ } else if res.StatusCode == 401 || res.StatusCode == 403 {+ // The secret is determinately not verified (nothing to do)+ } else {+ s1.VerificationError = fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)}+ } else {+ s1.VerificationError = err}}diff --git a/pkg/detectors/slackwebhook/slackwebhook_test.go b/pkg/detectors/slackwebhook/slackwebhook_test.goindex dd9b35f47..2d7e80a3a 100644--- a/pkg/detectors/slackwebhook/slackwebhook_test.go+++ b/pkg/detectors/slackwebhook/slackwebhook_test.go@@ -6,10 +6,11 @@ package slackwebhookimport ("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"@@ -32,11 +33,12 @@ func TestSlackWebhook_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",@@ -52,7 +54,8 @@ func TestSlackWebhook_FromChunk(t *testing.T) {Verified: true,},},- wantErr: false,+ wantErr: false,+ wantVerificationErr: false,},{name: "found, unverified",@@ -68,7 +71,8 @@ func TestSlackWebhook_FromChunk(t *testing.T) {Verified: false,},},- wantErr: false,+ wantErr: false,+ wantVerificationErr: false,},{name: "not found",@@ -78,14 +82,48 @@ func TestSlackWebhook_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 slackwebhook secret %s within", secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_SlackWebhook,+ 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 slackwebhook secret %s within", secret)),+ verify: true,+ },+ want: []detectors.Result{+ {+ DetectorType: detectorspb.DetectorType_SlackWebhook,+ 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("SlackWebhook.FromData() error = %v, wantErr %v", err, tt.wantErr)return@@ -94,9 +132,12 @@ func TestSlackWebhook_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.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError)+ }}- 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("SlackWebhook.FromData() %s diff: (-got +want)\n%s", tt.name, diff)}})