Skip to content
Changes

Reference

square · Sep 28, 2026

What this change means

How responses are judged changed, Verification error handling changed. Changes how the detector verifies or what it treats as the credential.

Field changes
FieldBeforeAfter
Expected statusesBefore: 200/403 (StatusOK, StatusForbidden) → verified
After: 200 (StatusOK) → verified; 401 (StatusUnauthorized) → not verified; 403 (StatusForbidden) → error or unknown; other (default) → error or unk… show all
200 (StatusOK) → verified; 401 (StatusUnauthorized) → not verified; 403 (StatusForbidden) → error or unknown; other (default) → error or unknown
pkg/detectors/square/square.go+100−25View file
@@ -2,7 +2,9 @@ package square
22
33import (
44 "context"
5added: "encoding/json"
56 "fmt"
7added: "io"
68 "net/http"
79 "strings"
@@ -14,12 +16,19 @@ import (
1416 "github.com/trufflesecurity/trufflehog/v3/pkg/common"
1517)
1618
17removed: type Scanner struct{}
19added: type Scanner struct {
20added: client *http.Client
21added: }
1822
1923// Ensure the Scanner satisfies the interface at compile time.
2024var _ detectors.Detector = (*Scanner)(nil)
2125
2226var (
27added: defaultClient = common.SaneHttpClient()
28added:
29added: // there are a few endpoints we can check, but merchants seems the least sensitive.
30added: verifyURL = "https://connect.squareupsandbox.com/v2/merchants"
31added:
2332 // more context to be added if this is too generic
2433 secretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"square"}) + `(EAAA[a-zA-Z0-9\-_+=]{60})`)
2534)
@@ -53,31 +62,14 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
5362 }
5463
5564 if verify {
56removed: // there are a few endpoints we can check, but templates seems the least sensitive.
57removed: // 403 will be issued if the scope is wrong but the key is correct
58removed: baseURL := "https://connect.squareupsandbox.com/v2/merchants"
59removed:
60removed: client := common.SaneHttpClient()
61removed:
62removed: // test `merchants` scope - its commonly allowed and low sensitivity
63removed: req, err := http.NewRequestWithContext(ctx, "GET", baseURL, nil)
64removed: if err != nil {
65removed: continue
66removed: }
67removed: req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
68removed: req.Header.Add("Content-Type", "application/json")
69removed: // unclear if this version needs to be set or matters, seems to work without, but docs want it
70removed: // req.Header.Add("Square-Version", "2020-08-12")
71removed: res, err := client.Do(req)
72removed: if err == nil {
73removed: _ = res.Body.Close() // The request body is unused.
74removed:
75removed: // 200 means good key and has `merchants` scope - default allowed by square
76removed: // 401 is bad key
77removed: if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusForbidden {
78removed: result.Verified = true
79removed: }
65added: client := s.client
66added: if client == nil {
67added: client = defaultClient
8068 }
69added:
70added: isVerified, verificationErr := verifyMatch(ctx, client, resMatch)
71added: result.Verified = isVerified
72added: result.SetVerificationError(verificationErr, resMatch)
8173 }
8274
8375 results = append(results, result)
@@ -86,6 +78,89 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
8678 return
8779}
8880
81added: func verifyMatch(ctx context.Context, client *http.Client, token string) (bool, error) {
82added: req, err := http.NewRequestWithContext(ctx, http.MethodGet, verifyURL, nil)
83added: if err != nil {
84added: return false, err
85added: }
86added: req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
87added: req.Header.Add("Content-Type", "application/json")
88added: // unclear if this version needs to be set or matters, seems to work without, but docs want it
89added: // req.Header.Add("Square-Version", "2020-08-12")
90added:
91added: res, err := client.Do(req)
92added: if err != nil {
93added: return false, err
94added: }
95added: defer func() {
96added: _, _ = io.Copy(io.Discard, res.Body)
97added: _ = res.Body.Close()
98added: }()
99added:
100added: switch res.StatusCode {
101added: case http.StatusOK:
102added: // good key and has `merchants` scope - default allowed by square
103added: return true, nil
104added: case http.StatusUnauthorized:
105added: return false, nil
106added: case http.StatusForbidden:
107added: // A 403 only proves the key is valid when Square itself says the scope is insufficient.
108added: // Anything else may come from a TLS-intercepting proxy or Square's edge (rate limiting, bot protection).
109added: body := readBody(res)
110added: if hasErrorCode(body, "INSUFFICIENT_SCOPES") {
111added: return true, nil
112added: }
113added: return false, fmt.Errorf("unexpected 403 from Square: %s", truncateBody(body))
114added: default:
115added: return false, fmt.Errorf("unexpected HTTP response status %d: %s", res.StatusCode, truncateBody(readBody(res)))
116added: }
117added: }
118added:
119added: // squareErrorResponse is Square's error envelope for non-2xx responses.
120added: // See https://developer.squareup.com/docs/build-basics/handling-errors.
121added: type squareErrorResponse struct {
122added: Errors []struct {
123added: Category string `json:"category"`
124added: Code string `json:"code"`
125added: Detail string `json:"detail"`
126added: } `json:"errors"`
127added: }
128added:
129added: const (
130added: maxBodySize = 4 << 10 // cap how much of the response body we read
131added: maxErrorBodySize = 512 // cap how much of the body we put in an error
132added: )
133added:
134added: func readBody(res *http.Response) []byte {
135added: body, _ := io.ReadAll(io.LimitReader(res.Body, maxBodySize))
136added: return body
137added: }
138added:
139added: func hasErrorCode(body []byte, code string) bool {
140added: var apiErr squareErrorResponse
141added: if err := json.Unmarshal(body, &apiErr); err != nil {
142added: return false
143added: }
144added: for _, e := range apiErr.Errors {
145added: if e.Code == code {
146added: return true
147added: }
148added: }
149added: return false
150added: }
151added:
152added: // truncateBody collapses whitespace and caps the body so an HTML block page doesn't flood the output.
153added: func truncateBody(body []byte) string {
154added: s := strings.Join(strings.Fields(string(body)), " ")
155added: if s == "" {
156added: return "<empty body>"
157added: }
158added: if len(s) > maxErrorBodySize {
159added: return s[:maxErrorBodySize] + "..."
160added: }
161added: return s
162added: }
163added:
89164func (s Scanner) Type() detector_typepb.DetectorType {
90165 return detector_typepb.DetectorType_Square
91166}