Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Only treat Square 403 as verified on INSUFFICIENT_SCOPES (#5367)
Muneeb Ullah Khan74d1c44c2b4e
diff --git a/pkg/detectors/square/square.go b/pkg/detectors/square/square.goindex 1bf2f0272..638a9814a 100644--- a/pkg/detectors/square/square.go+++ b/pkg/detectors/square/square.go@@ -2,7 +2,9 @@ package squareimport ("context"+ "encoding/json""fmt"+ "io""net/http""strings"@@ -14,12 +16,19 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/common")-type Scanner struct{}+type Scanner struct {+ client *http.Client+}// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)var (+ defaultClient = common.SaneHttpClient()++ // there are a few endpoints we can check, but merchants seems the least sensitive.+ verifyURL = "https://connect.squareupsandbox.com/v2/merchants"+// more context to be added if this is too genericsecretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"square"}) + `(EAAA[a-zA-Z0-9\-_+=]{60})`))@@ -53,31 +62,14 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result}if verify {- // there are a few endpoints we can check, but templates seems the least sensitive.- // 403 will be issued if the scope is wrong but the key is correct- baseURL := "https://connect.squareupsandbox.com/v2/merchants"-- client := common.SaneHttpClient()-- // test `merchants` scope - its commonly allowed and low sensitivity- req, err := http.NewRequestWithContext(ctx, "GET", baseURL, nil)- if err != nil {- continue- }- 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")- res, err := client.Do(req)- if err == nil {- _ = res.Body.Close() // The request body is unused.-- // 200 means good key and has `merchants` scope - default allowed by square- // 401 is bad key- if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusForbidden {- result.Verified = true- }+ client := s.client+ if client == nil {+ client = defaultClient}++ isVerified, verificationErr := verifyMatch(ctx, client, resMatch)+ result.Verified = isVerified+ result.SetVerificationError(verificationErr, resMatch)}results = append(results, result)@@ -86,6 +78,89 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn}+func verifyMatch(ctx context.Context, client *http.Client, token string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, verifyURL, nil)+ if err != nil {+ return false, err+ }+ req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))+ 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")++ res, err := client.Do(req)+ if err != nil {+ return false, err+ }+ defer func() {+ _, _ = io.Copy(io.Discard, res.Body)+ _ = res.Body.Close()+ }()++ switch res.StatusCode {+ case http.StatusOK:+ // good key and has `merchants` scope - default allowed by square+ return true, nil+ case http.StatusUnauthorized:+ return false, nil+ case http.StatusForbidden:+ // A 403 only proves the key is valid when Square itself says the scope is insufficient.+ // Anything else may come from a TLS-intercepting proxy or Square's edge (rate limiting, bot protection).+ body := readBody(res)+ if hasErrorCode(body, "INSUFFICIENT_SCOPES") {+ return true, nil+ }+ return false, fmt.Errorf("unexpected 403 from Square: %s", truncateBody(body))+ default:+ return false, fmt.Errorf("unexpected HTTP response status %d: %s", res.StatusCode, truncateBody(readBody(res)))+ }+}++// squareErrorResponse is Square's error envelope for non-2xx responses.+// See https://developer.squareup.com/docs/build-basics/handling-errors.+type squareErrorResponse struct {+ Errors []struct {+ Category string `json:"category"`+ Code string `json:"code"`+ Detail string `json:"detail"`+ } `json:"errors"`+}++const (+ maxBodySize = 4 << 10 // cap how much of the response body we read+ maxErrorBodySize = 512 // cap how much of the body we put in an error+)++func readBody(res *http.Response) []byte {+ body, _ := io.ReadAll(io.LimitReader(res.Body, maxBodySize))+ return body+}++func hasErrorCode(body []byte, code string) bool {+ var apiErr squareErrorResponse+ if err := json.Unmarshal(body, &apiErr); err != nil {+ return false+ }+ for _, e := range apiErr.Errors {+ if e.Code == code {+ return true+ }+ }+ return false+}++// truncateBody collapses whitespace and caps the body so an HTML block page doesn't flood the output.+func truncateBody(body []byte) string {+ s := strings.Join(strings.Fields(string(body)), " ")+ if s == "" {+ return "<empty body>"+ }+ if len(s) > maxErrorBodySize {+ return s[:maxErrorBodySize] + "..."+ }+ return s+}+func (s Scanner) Type() detector_typepb.DetectorType {return detector_typepb.DetectorType_Square}diff --git a/pkg/detectors/square/square_test.go b/pkg/detectors/square/square_test.goindex dadda930f..c04b1dd68 100644--- a/pkg/detectors/square/square_test.go+++ b/pkg/detectors/square/square_test.go@@ -3,10 +3,13 @@ package squareimport ("context""fmt"+ "net/http"+ "strings""testing""github.com/google/go-cmp/cmp"+ "github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick")@@ -89,3 +92,108 @@ func TestSquare_Pattern(t *testing.T) {})}}++func TestSquare_Verification(t *testing.T) {+ input := fmt.Sprintf("%s token = '%s'", keyword, validPattern)++ tests := []struct {+ name string+ status int+ body string+ wantVerified bool+ wantErr bool+ wantErrParts []string+ }{+ {+ name: "200 is verified",+ status: http.StatusOK,+ body: `{"merchant":[]}`,+ wantVerified: true,+ },+ {+ name: "401 is unverified",+ status: http.StatusUnauthorized,+ body: `{"errors":[{"category":"AUTHENTICATION_ERROR","code":"UNAUTHORIZED","detail":"This request could not be authorized."}]}`,+ },+ {+ name: "403 with INSUFFICIENT_SCOPES is verified",+ status: http.StatusForbidden,+ body: `{"errors":[{"category":"AUTHENTICATION_ERROR","code":"INSUFFICIENT_SCOPES","detail":"The merchant has not given your application sufficient permissions."}]}`,+ wantVerified: true,+ },+ {+ name: "403 with HTML body is indeterminate",+ status: http.StatusForbidden,+ body: "<html>\n <body>Blocked by corporate proxy</body>\n</html>",+ wantErr: true,+ wantErrParts: []string{"403", "<html> <body>Blocked by corporate proxy</body> </html>"},+ },+ {+ name: "403 with other Square code is indeterminate",+ status: http.StatusForbidden,+ body: `{"errors":[{"category":"AUTHENTICATION_ERROR","code":"FORBIDDEN","detail":"Forbidden"}]}`,+ wantErr: true,+ wantErrParts: []string{"403", "FORBIDDEN"},+ },+ {+ name: "403 with empty body is indeterminate",+ status: http.StatusForbidden,+ wantErr: true,+ wantErrParts: []string{"403", "<empty body>"},+ },+ {+ name: "429 is indeterminate",+ status: http.StatusTooManyRequests,+ body: `{"errors":[{"category":"RATE_LIMIT_ERROR","code":"RATE_LIMITED"}]}`,+ wantErr: true,+ wantErrParts: []string{"429", "RATE_LIMITED"},+ },+ {+ name: "500 with large body is truncated",+ status: http.StatusInternalServerError,+ body: strings.Repeat("x", 10000),+ wantErr: true,+ wantErrParts: []string{"500", strings.Repeat("x", maxErrorBodySize) + "..."},+ },+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ s := Scanner{client: common.ConstantResponseHttpClient(test.status, test.body)}++ results, err := s.FromData(context.Background(), true, []byte(input))+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if len(results) != 1 {+ t.Fatalf("expected 1 result, got %d", len(results))+ }++ r := results[0]+ if r.Verified != test.wantVerified {+ t.Errorf("Verified = %v, want %v", r.Verified, test.wantVerified)+ }++ verificationErr := r.VerificationError()+ if (verificationErr != nil) != test.wantErr {+ t.Fatalf("VerificationError = %v, wantErr %v", verificationErr, test.wantErr)+ }+ if verificationErr == nil {+ return+ }++ msg := verificationErr.Error()+ for _, part := range test.wantErrParts {+ if !strings.Contains(msg, part) {+ t.Errorf("VerificationError %q does not contain %q", msg, part)+ }+ }+ if strings.Contains(msg, validPattern) {+ t.Errorf("VerificationError leaks the token: %q", msg)+ }+ if len(msg) > maxErrorBodySize+64 {+ t.Errorf("VerificationError not truncated: %d bytes", len(msg))+ }+ })+ }+}