Skip to content

Find, verify, and analyze leaked credentials

[chore] - fix slackwebhook detector (#1761)

ahrav90a07f03526b
diff --git a/pkg/detectors/slackwebhook/slackwebhook.go b/pkg/detectors/slackwebhook/slackwebhook.go
index 2f8fe6ab6..ed05c6d15 100644
--- a/pkg/detectors/slackwebhook/slackwebhook.go
+++ b/pkg/detectors/slackwebhook/slackwebhook.go
@@ -1,6 +1,7 @@
package slackwebhook
import (
+ "bytes"
"context"
"fmt"
"io"
@@ -69,11 +70,10 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
if err != nil {
continue
}
- body := string(bodyBytes)
defer res.Body.Close()
- if res.StatusCode >= 200 && res.StatusCode < 300 || (res.StatusCode == 400 && (strings.Contains(body, "no_text") || strings.Contains(body, "missing_text"))) {
+ if res.StatusCode >= 200 && res.StatusCode < 300 || (res.StatusCode == 400 && (bytes.Equal(bodyBytes, []byte("no_text")) || bytes.Equal(bodyBytes, []byte("missing_text")))) {
s1.Verified = true
} else if res.StatusCode == 401 || res.StatusCode == 403 {
// The secret is determinately not verified (nothing to do)
diff --git a/pkg/detectors/slackwebhook/slackwebhook_test.go b/pkg/detectors/slackwebhook/slackwebhook_test.go
index 2d7e80a3a..0d5d5c969 100644
--- a/pkg/detectors/slackwebhook/slackwebhook_test.go
+++ b/pkg/detectors/slackwebhook/slackwebhook_test.go
@@ -6,14 +6,14 @@ package slackwebhook
import (
"context"
"fmt"
- "github.com/google/go-cmp/cmp"
- "github.com/google/go-cmp/cmp/cmpopts"
"testing"
"time"
- "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
+ "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"
)
@@ -57,6 +57,40 @@ func TestSlackWebhook_FromChunk(t *testing.T) {
wantErr: false,
wantVerificationErr: false,
},
+ {
+ name: "found, verified, 400 no_text",
+ s: Scanner{client: common.ConstantResponseHttpClient(400, "no_text")},
+ 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: true,
+ },
+ },
+ wantErr: false,
+ wantVerificationErr: false,
+ },
+ {
+ name: "found, verified, 400 missing_text",
+ s: Scanner{client: common.ConstantResponseHttpClient(400, "missing_text")},
+ 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: true,
+ },
+ },
+ wantErr: false,
+ wantVerificationErr: false,
+ },
{
name: "found, unverified",
s: Scanner{},
@@ -120,6 +154,23 @@ func TestSlackWebhook_FromChunk(t *testing.T) {
wantErr: false,
wantVerificationErr: true,
},
+ {
+ name: "found, account disabled",
+ s: Scanner{client: common.ConstantResponseHttpClient(400, disabledAccountResponse)},
+ 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) {
@@ -144,6 +195,8 @@ func TestSlackWebhook_FromChunk(t *testing.T) {
}
}
+const disabledAccountResponse = `missing_text_or_fallback_or_attachments`
+
func BenchmarkFromData(benchmark *testing.B) {
ctx := context.Background()
s := Scanner{}