Skip to content

Commit

Permalink
feat(typeform): add v2 detector for new key formats (#3660)
Browse files Browse the repository at this point in the history
docs:
- https://www.typeform.com/developers/get-started/personal-access-token/

40 to 59 chars is set because we've seen tokens with an extra 15 bytes at the end, the base key is 40 bytes long
  • Loading branch information
ggmolly authored Dec 4, 2024
1 parent 2169808 commit 2e5a7e6
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
File renamed without changes.
104 changes: 104 additions & 0 deletions pkg/detectors/typeform/v2/typeform.go
Original file line number Diff line number Diff line change
@@ -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."
}
161 changes: 161 additions & 0 deletions pkg/detectors/typeform/v2/typeform_integration_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
})
}
}
79 changes: 79 additions & 0 deletions pkg/detectors/typeform/v2/typeform_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
6 changes: 4 additions & 2 deletions pkg/engine/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{},
Expand Down

0 comments on commit 2e5a7e6

Please sign in to comment.