Skip to content

Find, verify, and analyze leaked credentials

Added new version for heroku detector (#4201)

Kashif Khan8b6aac3fcdb9
diff --git a/pkg/detectors/heroku/v1/heroku.go b/pkg/detectors/heroku/v1/heroku.go
new file mode 100644
index 000000000..613bac5af
--- /dev/null
+++ b/pkg/detectors/heroku/v1/heroku.go
@@ -0,0 +1,101 @@
+package heroku
+
+import (
+ "context"
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+
+ regexp "github.com/wasilibs/go-re2"
+
+ "github.com/trufflesecurity/trufflehog/v3/pkg/common"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
+)
+
+type Scanner struct{}
+
+// Ensure the Scanner satisfies the interface at compile time.
+var _ detectors.Detector = (*Scanner)(nil)
+var _ detectors.Versioner = (*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{"heroku"}) + `\b([0-9Aa-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\b`)
+)
+
+func (s Scanner) Version() int {
+ return 1
+}
+
+// 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{"heroku"}
+}
+
+// FromData will find and optionally verify Heroku 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)
+
+ for _, match := range matches {
+ resMatch := strings.TrimSpace(match[1])
+
+ s1 := detectors.Result{
+ DetectorType: detectorspb.DetectorType_Heroku,
+ Raw: []byte(resMatch),
+ }
+
+ if verify {
+ isVerified, verificationErr := VerifyHerokuAPIKey(ctx, client, resMatch)
+ s1.Verified = isVerified
+ s1.SetVerificationError(verificationErr)
+ }
+
+ results = append(results, s1)
+ }
+
+ return results, nil
+}
+
+func (s Scanner) Type() detectorspb.DetectorType {
+ return detectorspb.DetectorType_Heroku
+}
+
+func (s Scanner) Description() string {
+ return "Heroku is a cloud platform that lets companies build, deliver, monitor and scale apps. Heroku API keys can be used to access and manage Heroku applications and services."
+}
+
+func VerifyHerokuAPIKey(ctx context.Context, client *http.Client, key string) (bool, error) {
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.heroku.com/account", http.NoBody)
+ if err != nil {
+ return false, err
+ }
+
+ req.Header.Add("Accept", "application/vnd.heroku+json; version=3")
+ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", key))
+
+ resp, err := client.Do(req)
+ if err != nil {
+ return false, err
+ }
+
+ defer func() {
+ _, _ = io.Copy(io.Discard, resp.Body)
+ _ = resp.Body.Close()
+ }()
+
+ switch resp.StatusCode {
+ case http.StatusOK:
+ return true, nil
+ case http.StatusUnauthorized:
+ return false, nil
+ default:
+ return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
+ }
+}
diff --git a/pkg/detectors/heroku/heroku_integration_test.go b/pkg/detectors/heroku/v1/heroku_integration_test.go
similarity index 100%
rename from pkg/detectors/heroku/heroku_integration_test.go
rename to pkg/detectors/heroku/v1/heroku_integration_test.go
diff --git a/pkg/detectors/heroku/heroku_test.go b/pkg/detectors/heroku/v1/heroku_test.go
similarity index 75%
rename from pkg/detectors/heroku/heroku_test.go
rename to pkg/detectors/heroku/v1/heroku_test.go
index 7a406bf66..33d823e5a 100644
--- a/pkg/detectors/heroku/heroku_test.go
+++ b/pkg/detectors/heroku/v1/heroku_test.go
@@ -10,24 +10,6 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
)
-var (
- validPattern = `[{
- "_id": "1a8d0cca-e1a9-4318-bc2f-f5658ab2dcb5",
- "name": "Heroku",
- "type": "Detector",
- "api": true,
- "authentication_type": "",
- "verification_url": "https://api.example.com/example",
- "test_secrets": {
- "heroku_secret": "bAf8bA7d-7088-07ce-3f87-7ec21653297d"
- },
- "expected_response": "200",
- "method": "GET",
- "deprecated": false
- }]`
- secret = "bAf8bA7d-7088-07ce-3f87-7ec21653297d"
-)
-
func TestHeroku_Pattern(t *testing.T) {
d := Scanner{}
ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
@@ -38,9 +20,22 @@ func TestHeroku_Pattern(t *testing.T) {
want []string
}{
{
- name: "valid pattern",
- input: validPattern,
- want: []string{secret},
+ name: "valid pattern",
+ input: `[{
+ "_id": "1a8d0cca-e1a9-4318-bc2f-f5658ab2dcb5",
+ "name": "Heroku",
+ "type": "Detector",
+ "api": true,
+ "authentication_type": "",
+ "verification_url": "https://api.example.com/example",
+ "test_secrets": {
+ "heroku_secret": "bAf8bA7d-7088-07ce-3f87-7ec21653297d"
+ },
+ "expected_response": "200",
+ "method": "GET",
+ "deprecated": false
+ }]`,
+ want: []string{"bAf8bA7d-7088-07ce-3f87-7ec21653297d"},
},
}
diff --git a/pkg/detectors/heroku/heroku.go b/pkg/detectors/heroku/v2/heroku.go
similarity index 71%
rename from pkg/detectors/heroku/heroku.go
rename to pkg/detectors/heroku/v2/heroku.go
index 9c70b6668..f67f400c7 100644
--- a/pkg/detectors/heroku/heroku.go
+++ b/pkg/detectors/heroku/v2/heroku.go
@@ -2,14 +2,13 @@ package heroku
import (
"context"
- "fmt"
- "net/http"
"strings"
regexp "github.com/wasilibs/go-re2"
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
+ v1 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/heroku/v1"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
@@ -17,18 +16,23 @@ type Scanner struct{}
// Ensure the Scanner satisfies the interface at compile time.
var _ detectors.Detector = (*Scanner)(nil)
+var _ detectors.Versioner = (*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{"heroku"}) + `\b([0-9Aa-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\b`)
+ keyPat = regexp.MustCompile(`\b(HRKU-AA[0-9a-zA-Z_-]{58})\b`)
)
+func (s Scanner) Version() int {
+ return 2
+}
+
// 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{"heroku"}
+ return []string{"HRKU-AA"}
}
// FromData will find and optionally verify Heroku secrets in a given set of bytes.
@@ -46,19 +50,9 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
}
if verify {
- req, err := http.NewRequestWithContext(ctx, "GET", "https://api.heroku.com/apps", nil)
- if err != nil {
- continue
- }
- req.Header.Add("Accept", "application/vnd.heroku+json; version=3")
- req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
- res, err := client.Do(req)
- if err == nil {
- defer res.Body.Close()
- if res.StatusCode >= 200 && res.StatusCode < 300 {
- s1.Verified = true
- }
- }
+ isVerified, verificationErr := v1.VerifyHerokuAPIKey(ctx, client, resMatch)
+ s1.Verified = isVerified
+ s1.SetVerificationError(verificationErr)
}
results = append(results, s1)
diff --git a/pkg/detectors/heroku/v2/heroku_integration_test.go b/pkg/detectors/heroku/v2/heroku_integration_test.go
new file mode 100644
index 000000000..804db9970
--- /dev/null
+++ b/pkg/detectors/heroku/v2/heroku_integration_test.go
@@ -0,0 +1,120 @@
+//go:build detectors
+// +build detectors
+
+package heroku
+
+import (
+ "context"
+ "fmt"
+ "testing"
+ "time"
+
+ "github.com/kylelemons/godebug/pretty"
+
+ "github.com/trufflesecurity/trufflehog/v3/pkg/common"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
+)
+
+func TestHeroku_FromChunk(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
+ defer cancel()
+ testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")
+ if err != nil {
+ t.Fatalf("could not get test secrets from GCP: %s", err)
+ }
+ secret := testSecrets.MustGetField("HEROKU_V2_TOKEN")
+ inactiveSecret := testSecrets.MustGetField("HEROKU_V2_TOKEN_INACTIVE")
+
+ type args struct {
+ ctx context.Context
+ data []byte
+ verify bool
+ }
+ tests := []struct {
+ name string
+ s Scanner
+ args args
+ want []detectors.Result
+ wantErr bool
+ }{
+ {
+ name: "found, verified",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a heroku secret %s within", secret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Heroku,
+ Verified: true,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "found, unverified",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a heroku secret %s within but not valid", inactiveSecret)), // the secret would satisfy the regex but not pass validation
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Heroku,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "not found",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte("You cannot find the secret within"),
+ verify: true,
+ },
+ want: nil,
+ wantErr: false,
+ },
+ }
+ 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)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("Heroku.FromData() error = %v, wantErr %v", err, tt.wantErr)
+ return
+ }
+ for i := range got {
+ if len(got[i].Raw) == 0 {
+ t.Fatalf("no raw secret present: \n %+v", got[i])
+ }
+ got[i].Raw = nil
+ }
+ if diff := pretty.Compare(got, tt.want); diff != "" {
+ t.Errorf("Heroku.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
+ }
+ })
+ }
+}
+
+func BenchmarkFromData(benchmark *testing.B) {
+ ctx := context.Background()
+ s := Scanner{}
+ for name, data := range detectors.MustGetBenchmarkData() {
+ benchmark.Run(name, func(b *testing.B) {
+ b.ResetTimer()
+ for n := 0; n < b.N; n++ {
+ _, err := s.FromData(ctx, false, data)
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+ })
+ }
+}
diff --git a/pkg/detectors/heroku/v2/heroku_test.go b/pkg/detectors/heroku/v2/heroku_test.go
new file mode 100644
index 000000000..c2c91c9d9
--- /dev/null
+++ b/pkg/detectors/heroku/v2/heroku_test.go
@@ -0,0 +1,103 @@
+package heroku
+
+import (
+ "context"
+ "testing"
+
+ "github.com/google/go-cmp/cmp"
+
+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
+)
+
+func TestHeroku_Pattern(t *testing.T) {
+ d := Scanner{}
+ ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
+
+ tests := []struct {
+ name string
+ input string
+ want []string
+ }{
+ {
+ name: "valid pattern",
+ input: `[{
+ "_id": "1a8d0cca-e1a9-4318-bc2f-f5658ab2dcb5",
+ "name": "MyApp",
+ "type": "Detector",
+ "api": true,
+ "authentication_type": "",
+ "verification_url": "https://api.example.com/example",
+ "test_secrets": {
+ "secret1": "HRKU-AAlQ1aVoHDujJ9QsDHdHlHO0hbzhoERRSO45ZQusSYHg_____w4_hLrAym_u",
+ "secret2": "HRKU-AAy9Ppr_HD2pPuTyIiTYInO0hbzhoERRSO93ZQusSYHgaD7_WQ07FnF7L9FX"
+ },
+ "expected_response": "200",
+ "method": "GET",
+ "deprecated": false
+ }]`,
+ want: []string{"HRKU-AAlQ1aVoHDujJ9QsDHdHlHO0hbzhoERRSO45ZQusSYHg_____w4_hLrAym_u", "HRKU-AAy9Ppr_HD2pPuTyIiTYInO0hbzhoERRSO93ZQusSYHgaD7_WQ07FnF7L9FX"},
+ },
+ {
+ name: "invalid pattern",
+ input: `[{
+ "_id": "1a8d0cca-e1a9-4318-bc2f-f5658ab2dcb5",
+ "name": "MyApp",
+ "type": "Detector",
+ "api": true,
+ "authentication_type": "",
+ "verification_url": "https://api.example.com/example",
+ "test_secrets": {
+ "secret1": "HRKU-AAlQ1aVoHDujJ9QsDHdHlHO0hbzoERRSO45ZQusSYHg_____w4_hLrAym_u",
+ "secret2": "HRKU-AAy9Ppr_HD2pPuTyIiTYInO0h;zhoERRSO93ZQusSYHgaD7_WQ07FnF7L9FX"
+ },
+ "expected_response": "200",
+ "method": "GET",
+ "deprecated": false
+ }]`,
+ want: []string{},
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))
+ if len(matchedDetectors) == 0 {
+ t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)
+ return
+ }
+
+ results, err := d.FromData(context.Background(), false, []byte(test.input))
+ if err != nil {
+ t.Errorf("error = %v", err)
+ return
+ }
+
+ if len(results) != len(test.want) {
+ if len(results) == 0 {
+ t.Errorf("did not receive result")
+ } else {
+ t.Errorf("expected %d results, only received %d", len(test.want), len(results))
+ }
+ return
+ }
+
+ actual := make(map[string]struct{}, len(results))
+ for _, r := range results {
+ if len(r.RawV2) > 0 {
+ actual[string(r.RawV2)] = struct{}{}
+ } else {
+ actual[string(r.Raw)] = struct{}{}
+ }
+ }
+ expected := make(map[string]struct{}, len(test.want))
+ for _, v := range test.want {
+ expected[v] = struct{}{}
+ }
+
+ if diff := cmp.Diff(expected, actual); diff != "" {
+ t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)
+ }
+ })
+ }
+}
diff --git a/pkg/engine/defaults/defaults.go b/pkg/engine/defaults/defaults.go
index c7720abe4..928241c86 100644
--- a/pkg/engine/defaults/defaults.go
+++ b/pkg/engine/defaults/defaults.go
@@ -350,7 +350,8 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/helpcrunch"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/helpscout"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/hereapi"
- "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/heroku"
+ heroku_v1 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/heroku/v1"
+ heroku_v2 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/heroku/v2"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/hiveage"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/holidayapi"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/holistic"
@@ -1204,7 +1205,8 @@ func buildDetectorList() []detectors.Detector {
&helpcrunch.Scanner{},
&helpscout.Scanner{},
&hereapi.Scanner{},
- &heroku.Scanner{},
+ &heroku_v1.Scanner{},
+ &heroku_v2.Scanner{},
// &hive.Scanner{},
&hiveage.Scanner{},
&holidayapi.Scanner{},