Skip to content

Find, verify, and analyze leaked credentials

Add Analyzers interface for Square (#3141)

Abdul Basitc1645e8c2727
diff --git a/pkg/analyzer/analyzers/square/square.go b/pkg/analyzer/analyzers/square/square.go
index 0fe7fc8d1..e7a04283c 100644
--- a/pkg/analyzer/analyzers/square/square.go
+++ b/pkg/analyzer/analyzers/square/square.go
@@ -2,6 +2,7 @@ package square
import (
"encoding/json"
+ "errors"
"net/http"
"os"
"strconv"
@@ -11,8 +12,97 @@ import (
"github.com/jedib0t/go-pretty/table"
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers"
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/config"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/pb/analyzerpb"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/context"
)
+var _ analyzers.Analyzer = (*Analyzer)(nil)
+
+type Analyzer struct {
+ Cfg *config.Config
+}
+
+func (Analyzer) Type() analyzerpb.AnalyzerType { return analyzerpb.AnalyzerType_Square }
+
+func (a Analyzer) Analyze(_ context.Context, credInfo map[string]string) (*analyzers.AnalyzerResult, error) {
+ key, ok := credInfo["key"]
+ if !ok {
+ return nil, errors.New("key not found in credentialInfo")
+ }
+ info, err := AnalyzePermissions(a.Cfg, key)
+ if err != nil {
+ return nil, err
+ }
+ return secretInfoToAnalyzerResult(info), nil
+}
+
+func secretInfoToAnalyzerResult(info *SecretInfo) *analyzers.AnalyzerResult {
+ if info == nil {
+ return nil
+ }
+ result := analyzers.AnalyzerResult{
+ AnalyzerType: analyzerpb.AnalyzerType_Square,
+ Metadata: map[string]any{
+ "team_members": info.Team.TeamMembers,
+ "expires_at": info.Permissions.ExpiresAt,
+ "client_id": info.Permissions.ClientID,
+ "merchant_id": info.Permissions.MerchantID,
+ },
+ }
+
+ bindings, unboundedResources := getBindingsAndUnboundedResources(info.Permissions.Scopes)
+
+ result.Bindings = bindings
+ result.UnboundedResources = unboundedResources
+
+ return &result
+}
+
+// Build a list of Bindings and UnboundedResources by referencing the category permissions list and
+// checking with the given scopes
+func getBindingsAndUnboundedResources(scopes []string) ([]analyzers.Binding, []analyzers.Resource) {
+ bindings := []analyzers.Binding{}
+ unboundedResources := []analyzers.Resource{}
+ for _, permissions_category := range permissions_slice {
+ for category, permissions := range permissions_category {
+ parentResource := analyzers.Resource{
+ Name: category,
+ FullyQualifiedName: category,
+ Type: "category",
+ Metadata: nil,
+ Parent: nil,
+ }
+ categoryBinding := make([]analyzers.Binding, 0)
+ for endpoint, requiredPermissions := range permissions {
+ resource := analyzers.Resource{
+ Name: endpoint,
+ FullyQualifiedName: endpoint,
+ Type: "endpoint",
+ Metadata: nil,
+ Parent: &parentResource,
+ }
+ for _, permission := range requiredPermissions {
+ if contains(scopes, permission) {
+ categoryBinding = append(categoryBinding, analyzers.Binding{
+ Resource: resource,
+ Permission: analyzers.Permission{
+ Value: permission,
+ },
+ })
+ }
+ }
+ }
+ if len(categoryBinding) == 0 {
+ unboundedResources = append(unboundedResources, parentResource)
+ } else {
+ bindings = append(bindings, categoryBinding...)
+ }
+ }
+ }
+
+ return bindings, unboundedResources
+}
+
type TeamJSON struct {
TeamMembers []struct {
IsOwner bool `json:"is_owner"`
diff --git a/pkg/detectors/square/square.go b/pkg/detectors/square/square.go
index 08f840ed1..777dc4cef 100644
--- a/pkg/detectors/square/square.go
+++ b/pkg/detectors/square/square.go
@@ -3,10 +3,11 @@ package square
import (
"context"
"fmt"
- regexp "github.com/wasilibs/go-re2"
"net/http"
"strings"
+ regexp "github.com/wasilibs/go-re2"
+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
@@ -43,11 +44,11 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
if len(secMatch) != 2 {
continue
}
- res := strings.TrimSpace(secMatch[1])
+ resMatch := strings.TrimSpace(secMatch[1])
s := detectors.Result{
DetectorType: detectorspb.DetectorType_Square,
- Raw: []byte(res),
+ Raw: []byte(resMatch),
}
s.ExtraData = map[string]string{
"rotation_guide": "https://howtorotate.com/docs/tutorials/square/",
@@ -65,7 +66,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
if err != nil {
continue
}
- req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", res))
+ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
req.Header.Add("Content-Type", "application/json")
// unclear if this version needs to be set or matters, seems to work without, but docs want it
// req.Header.Add("Square-Version", "2020-08-12")
@@ -79,6 +80,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
s.Verified = true
}
}
+ s.AnalysisInfo = map[string]string{"key": resMatch}
}
results = append(results, s)
diff --git a/pkg/detectors/square/square_test.go b/pkg/detectors/square/square_test.go
index 913cf9d0e..57d5fa21b 100644
--- a/pkg/detectors/square/square_test.go
+++ b/pkg/detectors/square/square_test.go
@@ -94,6 +94,7 @@ func TestSquare_FromChunk(t *testing.T) {
t.Fatal("no raw secret present")
}
got[i].Raw = nil
+ got[i].AnalysisInfo = nil
}
if diff := pretty.Compare(got, tt.want); diff != "" {
t.Errorf("Square.FromData() %s diff: (-got +want)\n%s", tt.name, diff)