Skip to content

Commit

Permalink
Enhanced the easyinsight detector (#3384)
Browse files Browse the repository at this point in the history
* Enhanced the easyinsight detector

* restructured verification code and resolved comments

* resolved comments

* added basic auth

* updated statuscode logic
  • Loading branch information
kashifkhan0771 authored Oct 9, 2024
1 parent 23afcd7 commit 321813f
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 122 deletions.
96 changes: 63 additions & 33 deletions pkg/detectors/easyinsight/easyinsight.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package easyinsight

import (
"context"
b64 "encoding/base64"
"fmt"
regexp "github.com/wasilibs/go-re2"
"io"
"net/http"
"strings"

"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{
type Scanner struct {
detectors.DefaultMultiPartCredentialProvider
}

Expand All @@ -24,8 +23,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{"easyinsight", "easy-insight"}) + `\b([0-9Aa-zA-Z]{20})\b`)
idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"easyinsight", "easy-insight"}) + `\b([a-zA-Z0-9]{20})\b`)
keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"easyinsight", "easy-insight", "key"}) + `\b([0-9a-zA-Z]{20})\b`)
idPat = regexp.MustCompile(detectors.PrefixRegex([]string{"easyinsight", "easy-insight", "id"}) + `\b([a-zA-Z0-9]{20})\b`)
)

// Keywords are used for efficiently pre-filtering chunks.
Expand All @@ -38,49 +37,45 @@ func (s Scanner) Keywords() []string {
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)

matches := keyPat.FindAllStringSubmatch(dataStr, -1)
var keyMatches, idMatches = make(map[string]struct{}), make(map[string]struct{})

idmatches := idPat.FindAllStringSubmatch(dataStr, -1)
// get unique key and id matches
for _, matches := range keyPat.FindAllStringSubmatch(dataStr, -1) {
keyMatches[matches[1]] = struct{}{}
}

for _, match := range matches {
if len(match) != 2 {
continue
}
resMatch := strings.TrimSpace(match[1])
for _, idmatch := range idmatches {
if len(idmatch) != 2 {
for _, matches := range idPat.FindAllStringSubmatch(dataStr, -1) {
idMatches[matches[1]] = struct{}{}
}

for keyMatch := range keyMatches {
for idMatch := range idMatches {
//as key and id regex are same, the strings captured by both regex will be same.
//avoid processing when key is same as id. This will allow detector to process only different combinations
if keyMatch == idMatch {
continue
}
resIdMatch := strings.TrimSpace(idmatch[1])

s1 := detectors.Result{
DetectorType: detectorspb.DetectorType_EasyInsight,
Raw: []byte(resMatch),
RawV2: []byte(resMatch + resIdMatch),
Raw: []byte(keyMatch),
RawV2: []byte(keyMatch + idMatch),
}

if verify {
data := fmt.Sprintf("%s:%s", resIdMatch, resMatch)
sEnc := b64.StdEncoding.EncodeToString([]byte(data))
req, err := http.NewRequestWithContext(ctx, "GET", "https://www.easy-insight.com/app/api/users.json", nil)
if err != nil {
continue
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", sEnc))
res, err := client.Do(req)
if err == nil {
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 {
s1.Verified = true
}
verified, verificationErr := verifyEasyInsight(ctx, idMatch, keyMatch)
s1.Verified = verified
if verificationErr != nil {
s1.SetVerificationError(verificationErr)
}
}

results = append(results, s1)
// if key id combination is verified, skip other idMatches for that key
if s1.Verified {
break
}
}

}

return results, nil
Expand All @@ -93,3 +88,38 @@ func (s Scanner) Type() detectorspb.DetectorType {
func (s Scanner) Description() string {
return "EasyInsight is a business intelligence tool that provides data visualization and reporting. EasyInsight API keys can be used to access and manage data within the platform."
}

func verifyEasyInsight(ctx context.Context, id, key string) (bool, error) {
// docs: https://www.easy-insight.com/api/users.html
req, err := http.NewRequestWithContext(ctx, "GET", "https://www.easy-insight.com/app/api/users.json", nil)
if err != nil {
return false, err
}

// add required headers to the request
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
// set basic auth for the request
req.SetBasicAuth(id, key)

res, reqErr := client.Do(req)
if reqErr != nil {
return false, reqErr
}
defer func() {
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
}()

switch res.StatusCode {
// id, key verified
case http.StatusOK:
return true, nil
// id, key unverified
case http.StatusUnauthorized:
return false, nil
// something invalid
default:
return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)
}
}
121 changes: 121 additions & 0 deletions pkg/detectors/easyinsight/easyinsight_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//go:build detectors
// +build detectors

package easyinsight

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 TestEasyInsight_FromChunk(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors1")
if err != nil {
t.Fatalf("could not get test secrets from GCP: %s", err)
}
secret := testSecrets.MustGetField("EASYINSIGHT")
inactiveSecret := testSecrets.MustGetField("EASYINSIGHT_INACTIVE")
id := testSecrets.MustGetField("EASYINSIGHT_ID")

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 easyinsight secret %s within easyid %s", secret, id)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_EasyInsight,
Verified: true,
},
},
wantErr: false,
},
{
name: "found, unverified",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a easyinsight secret %s within easyid %s but not valid", inactiveSecret, id)), // the secret would satisfy the regex but not pass validation
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_EasyInsight,
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("EasyInsight.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("EasyInsight.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)
}
}
})
}
}
Loading

0 comments on commit 321813f

Please sign in to comment.