Skip to content

Commit

Permalink
add salesforce detector (#1608)
Browse files Browse the repository at this point in the history
* setup

* update time out case to return detector result

* fix

* remove unneeded comment

* remove debug print

* cleanup

* more robust error handling

* reflect new detector template changes

* fixes

* mark response body check err as indeterminate
  • Loading branch information
zubairk14 authored Aug 16, 2023
1 parent 6ad5659 commit 62d359e
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 10 deletions.
113 changes: 113 additions & 0 deletions pkg/detectors/salesforce/salesforce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package salesforce

import (
"context"
"fmt"
"net/http"
"regexp"
"strings"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)

const (
currentVersion = "58.0" // current Salesforce version
)

type Scanner struct {
client *http.Client
}

// Ensure the Scanner satisfies the interface at compile time.
var _ detectors.Detector = (*Scanner)(nil)

var (
defaultClient = common.SaneHttpClient()
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
accessTokenPat = regexp.MustCompile(`\b00[a-zA-Z0-9]{13}![a-zA-Z0-9_.]{96}\b`)
instancePat = regexp.MustCompile(`\bhttps://[0-9a-zA-Z-\.]{1,100}\.my\.salesforce\.com\b`)
)

// 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{"salesforce"}
}

// FromData will find and optionally verify Salesforce 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)

instanceMatches := instancePat.FindAllStringSubmatch(dataStr, -1)
tokenMatches := accessTokenPat.FindAllStringSubmatch(dataStr, -1)

fmt.Printf("instanceMatches: %v\n", instanceMatches)

for _, instance := range instanceMatches {
if len(instance) != 1 {
continue
}

instanceMatch := strings.TrimSpace(instance[0])

for _, token := range tokenMatches {
if len(token) != 1 {
continue
}

tokenMatch := strings.TrimSpace(token[0])

s1 := detectors.Result{
DetectorType: detectorspb.DetectorType_Salesforce,
Raw: []byte(tokenMatch),
}

if verify {
client := s.client
if client == nil {
client = defaultClient
}

req, err := http.NewRequestWithContext(ctx, "GET", instanceMatch+"/services/data/v"+currentVersion+"/query?q=SELECT+name+from+Account", nil)
if err != nil {
continue
}
req.Header.Set("Authorization", "Bearer "+tokenMatch)

res, err := client.Do(req)

if err != nil {
// End execution, append Detector Result if request fails to prevent panic on response body checks
s1.VerificationError = err
results = append(results, s1)
continue
}

verifiedBodyResponse, err := common.ResponseContainsSubstring(res.Body, "records")

defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 && verifiedBodyResponse {
s1.Verified = true
} else if res.StatusCode >= 200 && res.StatusCode < 300 && !verifiedBodyResponse {
s1.Verified = false
} else if res.StatusCode == 401 {
s1.Verified = false
} else {
s1.VerificationError = fmt.Errorf("request to %v returned status %d with error %+v", res.Request.URL, res.StatusCode, err)
}

}

results = append(results, s1)

}
}

return results, nil
}

func (s Scanner) Type() detectorspb.DetectorType {
return detectorspb.DetectorType_Salesforce
}
167 changes: 167 additions & 0 deletions pkg/detectors/salesforce/salesforce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
//go:build detectors
// +build detectors

package salesforce

import (
"context"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"testing"
"time"

"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"

"github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)

func TestSalesforce_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)
}
instance := testSecrets.MustGetField("SALESFORCE_INSTANCE")
// Salesforce secrets are not valid for long, so we may need to regenerate them.
// Steps to regenerate:
// 1. Install Salesforce CLI
// 2. Authenticate with `sfdx org web login`. This will open a browser window. Use the credentials in the detection test accounts 1Pass vault.
// 3. Run `sfdx org display --targetusername [email protected]`to obtain new token.
// Source: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_oauth.htm
token := testSecrets.MustGetField("SALESFORCE_TOKEN")
inactiveToken := testSecrets.MustGetField("SALESFORCE_INACTIVE_TOKEN")

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 salesforce secret within %s for %s", token, instance)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Salesforce,
Verified: true,
},
},
wantErr: false,
wantVerificationErr: false,
},
{
name: "found, unverified",
s: Scanner{},
args: args{
ctx: context.Background(),
data: []byte(fmt.Sprintf("You can find a salesforce secret %s within but not valid for %s", inactiveToken, instance)), // the secret would satisfy the regex but not pass validation
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Salesforce,
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 salesforce secret within %s for %s", token, instance)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Salesforce,
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 salesforce secret within %s for %s", token, instance)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Salesforce,
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("Salesforce.FromData() error = %v, wantErr %v", err, tt.wantErr)
}
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("Salesforce.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) {
for n := 0; n < b.N; n++ {
_, err := s.FromData(ctx, false, data)
if err != nil {
b.Fatal(err)
}
}
})
}
}
21 changes: 12 additions & 9 deletions pkg/pb/detectorspb/detectors.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/detectors.proto
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ enum DetectorType {
TrufflehogEnterprise = 922;
EnvoyApiKey = 923;
GitHubOauth2 = 924;
// 925 reserved for Salesforce
Salesforce = 925;
HuggingFace = 926;
}

Expand Down

0 comments on commit 62d359e

Please sign in to comment.