Skip to content

Find, verify, and analyze leaked credentials

feat(typeform): add v2 detector for new key formats (#3660)

Molly2e5a7e6440fb
diff --git a/pkg/detectors/typeform/typeform.go b/pkg/detectors/typeform/v1/typeform.go
similarity index 97%
rename from pkg/detectors/typeform/typeform.go
rename to pkg/detectors/typeform/v1/typeform.go
index 9dc0fd244..3ffcdc3f9 100644
--- a/pkg/detectors/typeform/typeform.go
+++ b/pkg/detectors/typeform/v1/typeform.go
@@ -3,10 +3,11 @@ package typeform
import (
"context"
"fmt"
- regexp "github.com/wasilibs/go-re2"
"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"
@@ -24,6 +25,8 @@ var (
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"typeform"}) + `\b([0-9A-Za-z]{44})\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 {
diff --git a/pkg/detectors/typeform/typeform_test.go b/pkg/detectors/typeform/v1/typeform_test.go
similarity index 100%
rename from pkg/detectors/typeform/typeform_test.go
rename to pkg/detectors/typeform/v1/typeform_test.go
diff --git a/pkg/detectors/typeform/v2/typeform.go b/pkg/detectors/typeform/v2/typeform.go
new file mode 100644
index 000000000..8f8a1be9c
--- /dev/null
+++ b/pkg/detectors/typeform/v2/typeform.go
@@ -0,0 +1,104 @@
+package typeform
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+
+ 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 (
+ client = common.SaneHttpClient()
+
+ keyPat = regexp.MustCompile(`\btfp_[a-zA-Z0-9_]{40,59}\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{"tfp_"}
+}
+
+type TypeFormResponse struct {
+ UserID string `json:"user_id,omitempty"`
+ Email string `json:"email,omitempty"`
+ Alias string `json:"alias,omitempty"`
+ Language string `json:"language,omitempty"`
+}
+
+// FromData will find and optionally verify Typeform 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.FindAllString(dataStr, -1)
+
+ for _, match := range matches {
+ s1 := detectors.Result{
+ DetectorType: detectorspb.DetectorType_Typeform,
+ Raw: []byte(match),
+ }
+
+ if verify {
+ verified, typeformResponse, requestErr := verifyMatch(ctx, client, match)
+ s1.Verified = verified
+ if requestErr != nil {
+ s1.SetVerificationError(err, match)
+ } else {
+ s1.ExtraData = map[string]string{
+ "UserId": typeformResponse.UserID,
+ "Email": typeformResponse.Email,
+ "Alias": typeformResponse.Alias,
+ "Language": typeformResponse.Language,
+ }
+ }
+ }
+ results = append(results, s1)
+
+ }
+
+ return results, nil
+}
+
+func verifyMatch(ctx context.Context, client *http.Client, secret string) (bool, TypeFormResponse, error) {
+ var response TypeFormResponse
+
+ req, err := http.NewRequestWithContext(ctx, "GET", "https://api.typeform.com/me", nil)
+ if err != nil {
+ return false, response, nil
+ }
+ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", secret))
+ res, err := client.Do(req)
+ if err != nil {
+ return false, response, err
+ }
+ defer res.Body.Close()
+ if err = json.NewDecoder(res.Body).Decode(&response); err != nil {
+ return false, response, err
+ }
+ if res.StatusCode == 200 {
+ return true, response, nil
+ } else {
+ return false, response, fmt.Errorf("unexpected status code %d", res.StatusCode)
+ }
+}
+
+func (s Scanner) Type() detectorspb.DetectorType {
+ return detectorspb.DetectorType_Typeform
+}
+
+func (s Scanner) Description() string {
+ return "Typeform is a service for creating forms and surveys. Typeform API keys can be used to access and manage forms and responses."
+}
diff --git a/pkg/detectors/typeform/v2/typeform_integration_test.go b/pkg/detectors/typeform/v2/typeform_integration_test.go
new file mode 100644
index 000000000..28797a749
--- /dev/null
+++ b/pkg/detectors/typeform/v2/typeform_integration_test.go
@@ -0,0 +1,161 @@
+//go:build detectors
+// +build detectors
+
+package typeform
+
+import (
+ "context"
+ "fmt"
+ "testing"
+ "time"
+
+ "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"
+)
+
+func TestTypeform_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("TYPEFORM_V2")
+ inactiveSecret := testSecrets.MustGetField("TYPEFORM_V2_INACTIVE")
+
+ type args struct {
+ ctx context.Context
+ data []byte
+ verify bool
+ }
+ tests := []struct {
+ name string
+ s Scanner
+ args args
+ want []detectors.Result
+ wantErr bool
+ wantVerificationErr bool
+ }{
+ {
+ name: "found, verified",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a typeform secret %s within", secret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Typeform,
+ Verified: true,
+ },
+ },
+ wantErr: false,
+ wantVerificationErr: false,
+ },
+ {
+ name: "found, unverified",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a typeform 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_Typeform,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ wantVerificationErr: 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,
+ wantVerificationErr: false,
+ },
+ {
+ name: "found, would be verified if not for timeout",
+ s: Scanner{client: common.SaneHttpClientTimeOut(1 * time.Microsecond)},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a typeform secret %s within", secret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Typeform,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ wantVerificationErr: true,
+ },
+ {
+ name: "found, verified but unexpected api surface",
+ s: Scanner{client: common.ConstantResponseHttpClient(404, "")},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a typeform secret %s within", secret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Typeform,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ wantVerificationErr: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("TypeForm.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])
+ }
+ if (got[i].VerificationError() != nil) != tt.wantVerificationErr {
+ t.Fatalf("wantVerificationError = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError())
+ }
+ }
+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError")
+ if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
+ t.Errorf("TypeForm.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/typeform/v2/typeform_test.go b/pkg/detectors/typeform/v2/typeform_test.go
new file mode 100644
index 000000000..7f61c8bbe
--- /dev/null
+++ b/pkg/detectors/typeform/v2/typeform_test.go
@@ -0,0 +1,79 @@
+package typeform
+
+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 TestTypeformV2_Pattern(t *testing.T) {
+ d := Scanner{}
+ ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})
+ tests := []struct {
+ name string
+ input string
+ want []string
+ }{
+ {
+ name: "typical pattern (v2)",
+ input: "typeform_token = 'tfp_ABCDEfghijKLMNOPqrstuvWXYZ0123456789ABCDEFGH_ijK12340qqqBBB'",
+ want: []string{"tfp_ABCDEfghijKLMNOPqrstuvWXYZ0123456789ABCDEFGH_ijK12340qqqBBB"},
+ },
+ {
+ name: "finds all matches (v2)",
+ input: `typeform_token1 = 'tfp_ABCDEfghijKLMNOPqrstuvWXYZ0123456789ABCDEFGH_ijK12340qqqBBB'
+typeform_token2 = 'tfp_943af478d3ff3d4d760020c11af102b79c440513'`,
+ want: []string{"tfp_ABCDEfghijKLMNOPqrstuvWXYZ0123456789ABCDEFGH_ijK12340qqqBBB", "tfp_943af478d3ff3d4d760020c11af102b79c440513"},
+ },
+ {
+ name: "invalid pattern",
+ input: "typeform_token = 'tfp_1'",
+ 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 14675e7f0..a020f8a7c 100644
--- a/pkg/engine/defaults/defaults.go
+++ b/pkg/engine/defaults/defaults.go
@@ -743,7 +743,8 @@ import (
twitterv2 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/twitter/v2"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/twitterconsumerkey"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/tyntec"
- "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/typeform"
+ typeformv1 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/typeform/v1"
+ typeformv2 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/typeform/v2"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/typetalk"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/ubidots"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/uclassify"
@@ -1586,7 +1587,8 @@ func buildDetectorList() []detectors.Detector {
&twitterv1.Scanner{},
&twitterv2.Scanner{},
&tyntec.Scanner{},
- &typeform.Scanner{},
+ &typeformv1.Scanner{},
+ &typeformv2.Scanner{},
&typetalk.Scanner{},
&ubidots.Scanner{},
&uclassify.Scanner{},