Skip to content

Find, verify, and analyze leaked credentials

[bug] - Fix case-sensitivity issue in PrefixRegex function (#2811)

ahravf82cf8d76d7f
diff --git a/pkg/detectors/detectors.go b/pkg/detectors/detectors.go
index c316af87d..f71c61b51 100644
--- a/pkg/detectors/detectors.go
+++ b/pkg/detectors/detectors.go
@@ -3,13 +3,12 @@ package detectors
import (
"context"
"crypto/rand"
+ "errors"
"math/big"
"net/url"
"strings"
"unicode"
- "errors"
-
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
@@ -154,10 +153,10 @@ func CleanResults(results []Result) []Result {
}
// PrefixRegex ensures that at least one of the given keywords is within
-// 20 characters of the capturing group that follows.
+// 40 characters of the capturing group that follows.
// This can help prevent false positives.
func PrefixRegex(keywords []string) string {
- pre := `(?i)(?:`
+ pre := `(?i:`
middle := strings.Join(keywords, "|")
post := `)(?:.|[\n\r]){0,40}?`
return pre + middle + post
diff --git a/pkg/detectors/detectors_test.go b/pkg/detectors/detectors_test.go
index 0e4d8a936..9c6a850e8 100644
--- a/pkg/detectors/detectors_test.go
+++ b/pkg/detectors/detectors_test.go
@@ -3,7 +3,11 @@
package detectors
-import "testing"
+import (
+ "testing"
+
+ regexp "github.com/wasilibs/go-re2"
+)
func TestPrefixRegex(t *testing.T) {
tests := []struct {
@@ -31,6 +35,38 @@ func TestPrefixRegex(t *testing.T) {
}
}
+func TestPrefixRegexKeywords(t *testing.T) {
+ keywords := []string{"keyword1", "keyword2", "keyword3"}
+
+ testCases := []struct {
+ input string
+ expected bool
+ }{
+ {"keyword1 1234c4aabceeff4444442131444aab44", true},
+ {"keyword1 1234567890ABCDEF1234567890ABBBCA", false},
+ {"KEYWORD1 1234567890abcdef1234567890ababcd", true},
+ {"KEYWORD1 1234567890ABCDEF1234567890ABdaba", false},
+ {"keyword2 1234567890abcdef1234567890abeeff", true},
+ {"keyword2 1234567890ABCDEF1234567890ABadbd", false},
+ {"KEYWORD2 1234567890abcdef1234567890ababca", true},
+ {"KEYWORD2 1234567890ABCDEF1234567890ABBBBs", false},
+ {"keyword3 1234567890abcdef1234567890abccea", true},
+ {"KEYWORD3 1234567890abcdef1234567890abaabb", true},
+ {"keyword4 1234567890abcdef1234567890abzzzz", false},
+ {"keyword3 1234567890ABCDEF1234567890AB", false},
+ {"keyword4 1234567890ABCDEF1234567890AB", false},
+ }
+
+ keyPat := regexp.MustCompile(PrefixRegex(keywords) + `\b([0-9a-f]{32})\b`)
+
+ for _, tc := range testCases {
+ match := keyPat.MatchString(tc.input)
+ if match != tc.expected {
+ t.Errorf("Input: %s, Expected: %v, Got: %v", tc.input, tc.expected, match)
+ }
+ }
+}
+
func BenchmarkPrefixRegex(b *testing.B) {
kws := []string{"securitytrails"}
for i := 0; i < b.N; i++ {