diff --git a/pkg/detectors/faceplusplus/faceplusplus.go b/pkg/detectors/faceplusplus/faceplusplus.go index d0163eaf..d304a282 100644 --- a/pkg/detectors/faceplusplus/faceplusplus.go +++ b/pkg/detectors/faceplusplus/faceplusplus.go @@ -1,11 +1,11 @@ package faceplusplus import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -20,54 +20,53 @@ var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"faceplusplus"}) + `\b([0-9a-zA-Z_-]{32})\b`) secretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"faceplusplus"}) + `\b([0-9a-zA-Z_-]{32})\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{"faceplusplus"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("faceplusplus")} } -// FromData will find and optionally verify Faceplusplus secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - matches := keyPat.FindAllStringSubmatch(dataStr, -1) - secretMatches := secretPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) + secretMatches := secretPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + + resMatch := bytes.TrimSpace(match[1]) + for _, secretMatch := range secretMatches { if len(secretMatch) != 2 { continue } - resSecret := strings.TrimSpace(secretMatch[1]) + + resSecret := bytes.TrimSpace(secretMatch[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_FacePlusPlus, - Raw: []byte(resMatch), - RawV2: []byte(resMatch + resSecret), + Raw: resMatch, + RawV2: append(resMatch, resSecret...), } if verify { - req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("https://api-us.faceplusplus.com/facepp/v3/faceset/getfacesets?api_key=%s&api_secret=%s", resMatch, resSecret), nil) + req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("https://api-us.faceplusplus.com/facepp/v3/faceset/getfacesets?api_key=%s&api_secret=%s", string(resMatch), string(resSecret)), nil) if err != nil { continue } + req.Header.Add("Content-Type", "application/json") res, err := client.Do(req) + if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/fakejson/fakejson.go b/pkg/detectors/fakejson/fakejson.go index d3cf365d..1575a5d7 100644 --- a/pkg/detectors/fakejson/fakejson.go +++ b/pkg/detectors/fakejson/fakejson.go @@ -1,6 +1,7 @@ package fakejson import ( + "bytes" "context" "fmt" "io" @@ -15,37 +16,31 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"fakejson"}) + `\b([a-zA-Z0-9]{22})\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{"fakejson"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("fakejson")} } -// FromData will find and optionally verify FakeJSON secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_FakeJSON, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -59,17 +54,11 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err == nil { bodyBytes, err := io.ReadAll(res.Body) if err == nil { - bodyString := string(bodyBytes) - validResponse := strings.Contains(bodyString, `"user_email":`) + validResponse := bytes.Contains(bodyBytes, []byte(`"user_email":`)) defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { - if validResponse { - s1.Verified = true - } else { - s1.Verified = false - } + s1.Verified = validResponse } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/falsepositives.go b/pkg/detectors/falsepositives.go index e63de9ff..a38158e4 100644 --- a/pkg/detectors/falsepositives.go +++ b/pkg/detectors/falsepositives.go @@ -1,14 +1,18 @@ package detectors import ( + "bytes" _ "embed" - "strings" "unicode" ) -var DefaultFalsePositives = []FalsePositive{"example", "xxxxxx", "aaaaaa", "abcde", "00000", "sample", "www"} +// var DefaultFalsePositives = []FalsePositive{"example", "xxxxxx", "aaaaaa", "abcde", "00000", "sample", "www"} +// +// type FalsePositive string -type FalsePositive string +var DefaultFalsePositives = []FalsePositive{[]byte("example"), []byte("xxxxxx"), []byte("aaaaaa"), []byte("abcde"), []byte("00000"), []byte("sample"), []byte("www")} + +type FalsePositive []byte //go:embed "badlist.txt" var badList []byte @@ -20,9 +24,9 @@ var wordList []byte var programmingBookWords []byte type Wordlists struct { - wordList []string - badList []string - programmingBookWords []string + wordList [][]byte + badList [][]byte + programmingBookWords [][]byte } var FalsePositiveWordlists = Wordlists{ @@ -34,10 +38,10 @@ var FalsePositiveWordlists = Wordlists{ // IsKnownFalsePositives will not return a valid secret finding if any of the disqualifying conditions are met // Currently that includes: No number, english word in key, or matches common example pattens. // Only the secret key material should be passed into this function -func IsKnownFalsePositive(match string, falsePositives []FalsePositive, wordCheck bool) bool { - +func IsKnownFalsePositive(match []byte, falsePositives []FalsePositive, wordCheck bool) bool { + matchLower := bytes.ToLower(match) for _, fp := range falsePositives { - if strings.Contains(strings.ToLower(match), string(fp)) { + if bytes.Contains(matchLower, fp) { return true } } @@ -61,31 +65,31 @@ func IsKnownFalsePositive(match string, falsePositives []FalsePositive, wordChec return false } -func hasDictWord(wordList []string, token string) bool { - lower := strings.ToLower(token) +func hasDictWord(wordList [][]byte, token []byte) bool { + lower := bytes.ToLower(token) for _, word := range wordList { - if strings.Contains(lower, word) { + if bytes.Contains(lower, word) { return true } } return false } -func HasDigit(key string) bool { - for _, ch := range key { +func HasDigit(key []byte) bool { + for _, ch := range string(key) { if unicode.IsDigit(ch) { return true } } - return false } -func bytesToCleanWordList(data []byte) []string { - words := []string{} - for _, word := range strings.Split(string(data), "\n") { - if strings.TrimSpace(word) != "" { - words = append(words, strings.TrimSpace(strings.ToLower(word))) +func bytesToCleanWordList(data []byte) [][]byte { + words := [][]byte{} + for _, word := range bytes.Split(data, []byte("\n")) { + trimmedWord := bytes.TrimSpace(word) + if len(trimmedWord) != 0 { + words = append(words, bytes.ToLower(trimmedWord)) } } return words diff --git a/pkg/detectors/fastforex/fastforex.go b/pkg/detectors/fastforex/fastforex.go index 6d05255b..63729cba 100644 --- a/pkg/detectors/fastforex/fastforex.go +++ b/pkg/detectors/fastforex/fastforex.go @@ -1,11 +1,11 @@ package fastforex import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -26,29 +26,27 @@ var ( // 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{"fastforex"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("fastforex")} } // FromData will find and optionally verify FastForex secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_FastForex, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.fastforex.io/fetch-all?api_key=%s", resMatch), nil) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.fastforex.io/fetch-all?api_key=%s", string(resMatch)), nil) if err != nil { continue } @@ -58,7 +56,6 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/fastlypersonaltoken/fastlypersonaltoken.go b/pkg/detectors/fastlypersonaltoken/fastlypersonaltoken.go index 9a2416a0..0673db88 100644 --- a/pkg/detectors/fastlypersonaltoken/fastlypersonaltoken.go +++ b/pkg/detectors/fastlypersonaltoken/fastlypersonaltoken.go @@ -1,13 +1,13 @@ package fastlypersonaltoken import ( + "bytes" "context" "encoding/json" "fmt" "io" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -16,19 +16,15 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"fastly"}) + `\b([A-Za-z0-9_-]{32})\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{"fastly"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("fastly")} } type fastlyUserRes struct { @@ -39,21 +35,18 @@ type fastlyUserRes struct { Locked bool `json:"locked"` } -// FromData will find and optionally verify FastlyPersonalToken secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_FastlyPersonalToken, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -61,7 +54,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("Fastly-Key", resMatch) + req.Header.Add("Fastly-Key", string(resMatch)) res, err := client.Do(req) if err == nil { bodyBytes, err := io.ReadAll(res.Body) @@ -84,7 +77,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result "two_factor_auth_enabled": fmt.Sprintf("%t", userRes.TwoFactorAuthEnabled), } } else { - if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { + if detectors.IsKnownFalsePositive([]byte(resMatch), detectors.DefaultFalsePositives, true) { continue } } diff --git a/pkg/detectors/feedier/feedier.go b/pkg/detectors/feedier/feedier.go index 94b41d59..994f0b34 100644 --- a/pkg/detectors/feedier/feedier.go +++ b/pkg/detectors/feedier/feedier.go @@ -1,11 +1,11 @@ package feedier import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,37 +14,30 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"feedier"}) + `\b([a-z0-9A-Z]{32})\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{"feedier"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("feedier")} } -// FromData will find and optionally verify Feedier secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Feedier, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -52,15 +45,15 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } + req.Header.Add("Accept", "application/json") - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch)) + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", string(resMatch))) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/fetchrss/fetchrss.go b/pkg/detectors/fetchrss/fetchrss.go index 19490cfe..8fba7c2f 100644 --- a/pkg/detectors/fetchrss/fetchrss.go +++ b/pkg/detectors/fetchrss/fetchrss.go @@ -1,11 +1,11 @@ package fetchrss import ( + "bytes" "context" "io" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -26,29 +26,27 @@ var ( // 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{"fetchrss"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("fetchrss")} } // FromData will find and optionally verify Fetchrss secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Fetchrss, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", "https://fetchrss.com/api/v1/feed/list?auth="+resMatch, nil) + req, err := http.NewRequestWithContext(ctx, "GET", "https://fetchrss.com/api/v1/feed/list?auth="+string(resMatch), nil) if err != nil { continue } @@ -59,16 +57,14 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - body := string(bodyBytes) - if !strings.Contains(body, "Not authorised") { + if !bytes.Contains(bodyBytes, []byte("Not authorised")) { s1.Verified = true } else { if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } } - } } diff --git a/pkg/detectors/fibery/fibery.go b/pkg/detectors/fibery/fibery.go index 11f11eb3..a1eab664 100644 --- a/pkg/detectors/fibery/fibery.go +++ b/pkg/detectors/fibery/fibery.go @@ -1,12 +1,11 @@ package fibery import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" - "time" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -21,58 +20,50 @@ var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"fibery"}) + `\b([0-9a-f]{8}.[0-9a-f]{35})\b`) domainPat = regexp.MustCompile(detectors.PrefixRegex([]string{"fibery", "domain"}) + `\b([0-9A-Za-z]{2,40})\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{"fibery"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("fibery")} } -// FromData will find and optionally verify Fibery secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) - domainMatches := domainPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) + domainMatches := domainPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + + resMatch := bytes.TrimSpace(match[1]) for _, domainMatch := range domainMatches { if len(domainMatch) != 2 { continue } - resDomainMatch := strings.TrimSpace(domainMatch[1]) + resDomainMatch := bytes.TrimSpace(domainMatch[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Fibery, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - timeout := 10 * time.Second - client.Timeout = timeout - req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("https://%s.fibery.io/api/commands", resDomainMatch), nil) + req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("https://%s.fibery.io/api/commands", string(resDomainMatch)), nil) if err != nil { continue } req.Header.Add("Content-Type", "application/json") - req.Header.Add("Authorization", fmt.Sprintf("Token %s", resMatch)) + req.Header.Add("Authorization", fmt.Sprintf("Token %s", string(resMatch))) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/figmapersonalaccesstoken/figmapersonalaccesstoken.go b/pkg/detectors/figmapersonalaccesstoken/figmapersonalaccesstoken.go index ed9a605e..02d93b2f 100644 --- a/pkg/detectors/figmapersonalaccesstoken/figmapersonalaccesstoken.go +++ b/pkg/detectors/figmapersonalaccesstoken/figmapersonalaccesstoken.go @@ -1,10 +1,10 @@ package figmapersonalaccesstoken import ( + "bytes" "context" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -13,7 +13,6 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( @@ -22,27 +21,23 @@ var ( keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"figma"}) + `\b([0-9]{6}-[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12})\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{"figma"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("figma")} } -// FromData will find and optionally verify FigmaPersonalAccessToken secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_FigmaPersonalAccessToken, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -50,7 +45,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("X-Figma-Token", resMatch) + req.Header.Add("X-Figma-Token", string(resMatch)) res, err := client.Do(req) if err == nil { defer res.Body.Close() diff --git a/pkg/detectors/fileio/fileio.go b/pkg/detectors/fileio/fileio.go index 4bb6cdb8..c8e796bc 100644 --- a/pkg/detectors/fileio/fileio.go +++ b/pkg/detectors/fileio/fileio.go @@ -1,13 +1,11 @@ package fileio import ( + "bytes" "context" - "encoding/json" "fmt" - "io" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -16,37 +14,29 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"fileio"}) + `\b([A-Z0-9.-]{39})\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{"fileio"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("fileio")} } -// FromData will find and optionally verify FileIO secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_FileIO, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -54,24 +44,15 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch)) + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", string(resMatch))) res, err := client.Do(req) if err == nil { - bodyBytes, err := io.ReadAll(res.Body) - if err == nil { - isJson := json.Valid(bodyBytes) - defer res.Body.Close() - if res.StatusCode >= 200 && res.StatusCode < 300 { - if isJson { - s1.Verified = true - } else { - s1.Verified = false - } - } 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(resMatch, detectors.DefaultFalsePositives, true) { - continue - } + defer res.Body.Close() + if res.StatusCode >= 200 && res.StatusCode < 300 { + s1.Verified = true + } else { + if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { + continue } } } diff --git a/pkg/detectors/finage/finage.go b/pkg/detectors/finage/finage.go index 1901ed60..b0b8867e 100644 --- a/pkg/detectors/finage/finage.go +++ b/pkg/detectors/finage/finage.go @@ -1,11 +1,11 @@ package finage import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -19,36 +19,29 @@ var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. - keyPat = regexp.MustCompile(`\b(API_KEY[0-9A-Z]{32})\b`) + keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"API_KEY"}) + `\b([0-9A-Z]{32})\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{"finage"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("finage")} } -// FromData will find and optionally verify Finage secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Finage, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.finage.co.uk/symbol-list/crypto?apikey=%s", resMatch), nil) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.finage.co.uk/symbol-list/crypto?apikey=%s", string(resMatch)), nil) if err != nil { continue } @@ -59,7 +52,6 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/financialmodelingprep/financialmodelingprep.go b/pkg/detectors/financialmodelingprep/financialmodelingprep.go index cf7d7a17..5832a104 100644 --- a/pkg/detectors/financialmodelingprep/financialmodelingprep.go +++ b/pkg/detectors/financialmodelingprep/financialmodelingprep.go @@ -1,12 +1,12 @@ package financialmodelingprep import ( + "bytes" "context" "fmt" "io" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -21,31 +21,29 @@ var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. + // Keeping the regexp patterns unchanged keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"financialmodelingprep"}) + `\b([a-zA-Z0-9]{32})\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{"financialmodelingprep"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("financialmodelingprep")} } // FromData will find and optionally verify FinancialModelingPrep secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_FinancialModelingPrep, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -55,22 +53,14 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result } res, err := client.Do(req) if err == nil { - bodyBytes, err := io.ReadAll(res.Body) - bodyString := string(bodyBytes) - if err == nil { + bodyBytes, err2 := io.ReadAll(res.Body) + if err2 == nil { // valid response should be an array of currencies - // error response is in json - validResponse := strings.Contains(bodyString, `[ "`) - + validResponse := bytes.Contains(bodyBytes, []byte(`[ "`)) defer res.Body.Close() - if res.StatusCode >= 200 && res.StatusCode < 300 { - if validResponse { - s1.Verified = true - } else { - s1.Verified = false - } + if res.StatusCode >= 200 && res.StatusCode < 300 && validResponse { + s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/findl/findl.go b/pkg/detectors/findl/findl.go index 323cd90c..df9d8fd3 100644 --- a/pkg/detectors/findl/findl.go +++ b/pkg/detectors/findl/findl.go @@ -1,11 +1,10 @@ package findl import ( + "bytes" "context" "net/http" "regexp" - "strings" - "time" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,54 +13,44 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"findl"}) + `\b([a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{12})\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{"findl"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("findl")} } -// FromData will find and optionally verify Findl secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Findl, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - timeout := 5 * time.Second - client.Timeout = timeout req, err := http.NewRequestWithContext(ctx, "GET", "https://api.findl.com/v1.0/query?limit=6", nil) if err != nil { continue } - req.Header.Add("X-API-Key", resMatch) + req.Header.Add("X-API-Key", string(resMatch)) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/finnhub/finnhub.go b/pkg/detectors/finnhub/finnhub.go index 57e0caaf..94d56364 100644 --- a/pkg/detectors/finnhub/finnhub.go +++ b/pkg/detectors/finnhub/finnhub.go @@ -1,11 +1,11 @@ package finnhub import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,41 +14,33 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"finnhub"}) + `\b([0-9a-z]{20})\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{"finnhub"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("finnhub")} } -// FromData will find and optionally verify Finnhub secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Finnhub, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://finnhub.io/api/v1/calendar/economic?token=%s", resMatch), nil) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://finnhub.io/api/v1/calendar/economic?token=%s", string(resMatch)), nil) if err != nil { continue } @@ -59,7 +51,6 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/fixerio/fixerio.go b/pkg/detectors/fixerio/fixerio.go index 17df2ef9..f7fd521e 100644 --- a/pkg/detectors/fixerio/fixerio.go +++ b/pkg/detectors/fixerio/fixerio.go @@ -1,11 +1,11 @@ package fixerio import ( + "bytes" "context" "io" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,41 +14,33 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"fixer"}) + `\b([A-Za-z0-9]{32})\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{"fixer"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("fixer")} } -// FromData will find and optionally verify FixerIO secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_FixerIO, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", "https://data.fixer.io/api/latest?access_key="+resMatch, nil) + req, err := http.NewRequestWithContext(ctx, "GET", "https://data.fixer.io/api/latest?access_key="+string(resMatch), nil) if err != nil { continue } @@ -60,13 +52,8 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - body := string(bodyBytes) - // if client_id and client_secret is valid -> 403 {"error":"invalid_grant","error_description":"Invalid authorization code"} - // if invalid -> 401 {"error":"access_denied","error_description":"Unauthorized"} - // ingenious! - - validResponse := strings.Contains(body, `"success": true`) || strings.Contains(body, `"info":"Access Restricted - Your current Subscription Plan does not support HTTPS Encryption."`) + validResponse := bytes.Contains(bodyBytes, []byte(`"success": true`)) || bytes.Contains(bodyBytes, []byte(`"info":"Access Restricted - Your current Subscription Plan does not support HTTPS Encryption."`)) defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 && validResponse { @@ -76,13 +63,10 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result continue } } - } } - results = append(results, s1) } - return results, nil } diff --git a/pkg/detectors/flatio/flatio.go b/pkg/detectors/flatio/flatio.go index bffb97a9..74338c75 100644 --- a/pkg/detectors/flatio/flatio.go +++ b/pkg/detectors/flatio/flatio.go @@ -1,11 +1,11 @@ package flatio import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,37 +14,29 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"flat"}) + `\b([0-9a-z]{128})\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{"flat"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("flat")} } -// FromData will find and optionally verify FlatIO secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_FlatIO, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -53,14 +45,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result continue } req.Header.Add("Accept", "application/json") - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch)) + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", string(resMatch))) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/fleetbase/fleetbase.go b/pkg/detectors/fleetbase/fleetbase.go index 0b5746ef..cba6d04b 100644 --- a/pkg/detectors/fleetbase/fleetbase.go +++ b/pkg/detectors/fleetbase/fleetbase.go @@ -1,11 +1,11 @@ package fleetbase import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,37 +14,29 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = common.SaneHttpClient() - - // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. keyPat = regexp.MustCompile(`\b(flb_live_[0-9a-zA-Z]{20})\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{"fleetbase"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("fleetbase")} } -// FromData will find and optionally verify Fleetbase secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Fleetbase, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -52,14 +44,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch)) + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", string(resMatch))) res, err := client.Do(req) if err == nil { defer res.Body.Close() if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/flickr/flickr.go b/pkg/detectors/flickr/flickr.go index 2eabda60..7f70898e 100644 --- a/pkg/detectors/flickr/flickr.go +++ b/pkg/detectors/flickr/flickr.go @@ -1,12 +1,12 @@ package flickr import ( + "bytes" "context" "fmt" "io" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -15,44 +15,37 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"flickr"}) + `\b([0-9a-z]{32})\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{"flickr"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("flickr")} } -// FromData will find and optionally verify Flickr secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_Flickr, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://www.flickr.com/services/rest/?method=flickr.tags.getHotList&api_key=%s", resMatch), nil) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://www.flickr.com/services/rest/?method=flickr.tags.getHotList&api_key=%s", string(resMatch)), nil) if err != nil { continue } + req.Header.Add("Content-Type", "application/json") res, err := client.Do(req) if err == nil { @@ -61,11 +54,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if err != nil { continue } - body := string(bodyBytes) - if (res.StatusCode >= 200 && res.StatusCode < 300) && strings.Contains(body, "owner=") { + if res.StatusCode >= 200 && res.StatusCode < 300 && bytes.Contains(bodyBytes, []byte("owner=")) { s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/flightapi/flightapi.go b/pkg/detectors/flightapi/flightapi.go index a6249c30..ea189e31 100644 --- a/pkg/detectors/flightapi/flightapi.go +++ b/pkg/detectors/flightapi/flightapi.go @@ -1,11 +1,11 @@ package flightapi import ( + "bytes" "context" "fmt" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -14,41 +14,33 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time. var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"flightapi"}) + `\b([a-z0-9]{24})\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{"flightapi"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("flightapi")} } -// FromData will find and optionally verify FlightApi secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_FlightApi, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { - req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.flightapi.io/iata/%s/london/airport", resMatch), nil) + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.flightapi.io/iata/%s/london/airport", string(resMatch)), nil) if err != nil { continue } @@ -58,7 +50,6 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result if res.StatusCode >= 200 && res.StatusCode < 300 { s1.Verified = true } 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(resMatch, detectors.DefaultFalsePositives, true) { continue } diff --git a/pkg/detectors/flightlabs/flightlabs.go b/pkg/detectors/flightlabs/flightlabs.go index 18a3e390..328d92df 100644 --- a/pkg/detectors/flightlabs/flightlabs.go +++ b/pkg/detectors/flightlabs/flightlabs.go @@ -1,12 +1,11 @@ package flightlabs import ( + "bytes" "context" "fmt" - "io" "net/http" "regexp" - "strings" "github.com/trufflesecurity/trufflehog/v3/pkg/common" "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" @@ -15,37 +14,30 @@ import ( type Scanner struct{} -// Ensure the Scanner satisfies the interface at compile time var _ detectors.Detector = (*Scanner)(nil) var ( client = 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{"flightlabs"}) + `\b(ey[a-zA-Z0-9]{34}.ey[a-zA-Z0-9._-]{300,350})\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{"flightlabs"} +func (s Scanner) Keywords() [][]byte { + return [][]byte{[]byte("flightlabs")} } -// FromData will find and optionally verify FlightLabs secrets in a given set of bytes. func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { - dataStr := string(data) - - matches := keyPat.FindAllStringSubmatch(dataStr, -1) + matches := keyPat.FindAllSubmatch(data, -1) for _, match := range matches { if len(match) != 2 { continue } - resMatch := strings.TrimSpace(match[1]) + resMatch := bytes.TrimSpace(match[1]) s1 := detectors.Result{ DetectorType: detectorspb.DetectorType_FlightLabs, - Raw: []byte(resMatch), + Raw: resMatch, } if verify { @@ -55,22 +47,12 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result } res, err := client.Do(req) - if err == nil { - bodyBytes, err := io.ReadAll(res.Body) - if err != nil { + if err != nil || res.StatusCode < 200 || res.StatusCode >= 300 { + if detectors.IsKnownFalsePositive(resMatch, detectors.DefaultFalsePositives, true) { continue } - bodyString := string(bodyBytes) - validResponse := strings.Contains(bodyString, `"id"`) - defer res.Body.Close() - if res.StatusCode >= 200 && res.StatusCode < 300 && validResponse { - s1.Verified = true - } 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(resMatch, detectors.DefaultFalsePositives, true) { - continue - } - } + } else { + s1.Verified = bytes.Contains(resMatch, []byte(`"id"`)) } }