Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
enhancements in amplitude detector (#575)
valerie gale9d1c1e958a9b
diff --git a/pkg/detectors/amplitudeapikey/amplitudeapikey.go b/pkg/detectors/amplitudeapikey/amplitudeapikey.goindex 477843a8d..7b3c5b2eb 100644--- a/pkg/detectors/amplitudeapikey/amplitudeapikey.go+++ b/pkg/detectors/amplitudeapikey/amplitudeapikey.go@@ -20,7 +20,8 @@ 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{"amplitude"}) + `\b([a-f0-9]{32})`)+ keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"amplitude"}) + `\b([0-9a-f]{32})\b`)+ secretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"amplitude"}) + `\b([0-9a-f]{32})\b`))// Keywords are used for efficiently pre-filtering chunks.@@ -34,6 +35,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultdataStr := string(data)matches := keyPat.FindAllStringSubmatch(dataStr, -1)+ secretMatches := secretPat.FindAllStringSubmatch(dataStr, -1)for _, match := range matches {if len(match) != 2 {@@ -41,33 +43,39 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result}resMatch := strings.TrimSpace(match[1])- s1 := detectors.Result{- DetectorType: detectorspb.DetectorType_AmplitudeApiKey,- Raw: []byte(resMatch),- }-- if verify {- payload := strings.NewReader("api_key=" + resMatch + "&identification=%5B%7B%22user_id%22%3A%22datamonster%40gmail.com%22%2C%20%22user_properties%22%3A%7B%22Age%22%3A%2235%22%7D%2C%20%22groups%22%3A%7B%22company_id%22%3A%221234%22%7D%2C%20%22country%22%3A%22United%20States%22%7D%5D")- req, err := http.NewRequestWithContext(ctx, "POST", "https://api.amplitude.com/identify", payload)- if err != nil {+ for _, secretMatch := range secretMatches {+ if len(secretMatch) != 2 {continue}- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")- 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+ resSecretMatch := strings.TrimSpace(secretMatch[1])++ s1 := detectors.Result{+ DetectorType: detectorspb.DetectorType_AmplitudeApiKey,+ Raw: []byte(resMatch),+ }++ if verify {+ req, err := http.NewRequestWithContext(ctx, "GET", "https://amplitude.com/api/2/taxonomy/category", nil)+ if err != nil {+ continue+ }+ req.SetBasicAuth(resMatch, resSecretMatch)+ 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+ }}}}- }- results = append(results, s1)+ results = append(results, s1)+ }}return detectors.CleanResults(results), nildiff --git a/pkg/detectors/amplitudeapikey/amplitudeapikey_test.go b/pkg/detectors/amplitudeapikey/amplitudeapikey_test.goindex 2132b6014..b7606bbde 100644--- a/pkg/detectors/amplitudeapikey/amplitudeapikey_test.go+++ b/pkg/detectors/amplitudeapikey/amplitudeapikey_test.go@@ -20,7 +20,10 @@ func TestAmplitudeApiKey_FromChunk(t *testing.T) {if err != nil {t.Fatalf("could not get test secrets from GCP: %s", err)}- secret := testSecrets.MustGetField("AMPLITUDEAPI_TOKEN")++ key := testSecrets.MustGetField("AMPLITUDEAPI_KEY")+ secret := testSecrets.MustGetField("AMPLITUDEAPI_SECRET")+ inactiveKey := testSecrets.MustGetField("AMPLITUDEAPI_KEY_INACTIVE")inactiveSecret := testSecrets.MustGetField("AMPLITUDEAPI_INACTIVE")type args struct {@@ -40,7 +43,7 @@ func TestAmplitudeApiKey_FromChunk(t *testing.T) {s: Scanner{},args: args{ctx: context.Background(),- data: []byte(fmt.Sprintf("You can find a amplitude secret %s within", secret)),+ data: []byte(fmt.Sprintf("You can find an amplitude key %s with amplitude secret %s within", key, secret)),verify: true,},want: []detectors.Result{@@ -57,7 +60,7 @@ func TestAmplitudeApiKey_FromChunk(t *testing.T) {args: args{ctx: context.Background(),- data: []byte(fmt.Sprintf("You can find a amplitudeapikey secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation+ data: []byte(fmt.Sprintf("You can find an amplitude key %s with amplitude secret %s within but not valid", inactiveKey, inactiveSecret)), // the secret would satisfy the regex but not pass validationverify: true,},want: []detectors.Result{