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