What this change means
Regex changed, Capture groups changed, How the credential is sent changed, How responses are judged changed, Verification error handling changed, Description changed. Changes how the detector verifies or what it treats as the credential.
| Field | Before | After |
|---|---|---|
| Regex patterns | Before: \b([a-zA-Z0-9]{32})\b, \b([a-zA-Z0-9]{32})\b, \b([0-9]{6,20})\b | After: \b([a-zA-Z0-9]{32})\b, \b([a-zA-Z0-9]{32})\b |
| Expected statuses | Before: 400 (StatusBadRequest) → error or unknown; other (default) → error or unknown | After: none |
| How the credential is sent | Before: client_id and client_secret are posted as application/x-www-form-urlencoded | After: credential is sent in a JSON body field named client_id |
| Description | Before: Box is a service offering various service for secure collaboration, content management, and workflow. Box Oauth credentials can be used to a… show allBox is a service offering various service for secure collaboration, content management, and workflow. Box Oauth credentials can be used to access and interact with this data. | After: Box is a service offering various service for secure collaboration, content management, and workflow. Box Oauth credentials can be used to a… show allBox is a service offering various service for secure collaboration, content management, and workflow. Box Oauth credentials can be used to access and interact with this data. |
| @@ -2,10 +2,12 @@ package boxoauth | ||
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | 4 | "context" |
| 5 | added: "encoding/json" | |
| 6 | added: "errors" | |
| 5 | 7 | "fmt" |
| 6 | 8 | "io" |
| 7 | 9 | "net/http" |
| 8 | removed: "slices" | |
| 10 | added: "net/url" | |
| 9 | 11 | "strings" |
| 10 | 12 | |
| 11 | 13 | regexp "github.com/wasilibs/go-re2" |
| @@ -22,17 +24,68 @@ type Scanner struct { | ||
| 22 | 24 | // Ensure the Scanner satisfies the interface at compile time. |
| 23 | 25 | var _ detectors.Detector = (*Scanner)(nil) |
| 24 | 26 | |
| 27 | added: const tokenURL = "https://api.box.com/oauth2/token" | |
| 28 | added: | |
| 29 | added: // maxSubjectIDCandidates bounds how many nearby subject IDs are tried per | |
| 30 | added: // client ID and secret pair. Each candidate costs up to one token request per | |
| 31 | added: // subject type, every request counts against the customer's Box API | |
| 32 | added: // allocation, and a long run of failed subject attempts against one client | |
| 33 | added: // looks like enumeration. Real configs usually carry one or two genuine IDs, | |
| 34 | added: // and candidates are ranked so the most likely ones fall inside the cap. | |
| 35 | added: const maxSubjectIDCandidates = 5 | |
| 36 | added: | |
| 37 | added: // subjectTypes are the box_subject_type values tried for each subject ID, in | |
| 38 | added: // order. "enterprise" authenticates as the app's Service Account; "user" | |
| 39 | added: // authenticates as an App User, Managed User or admin. User-subject tokens act | |
| 40 | added: // as a real person, so dropping "user" here is the single switch for | |
| 41 | added: // narrowing that footprint. | |
| 42 | added: var subjectTypes = []string{"enterprise", "user"} | |
| 43 | added: | |
| 25 | 44 | var ( |
| 26 | 45 | defaultClient = common.SaneHttpClient() |
| 27 | 46 | // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. |
| 28 | 47 | clientIdPat = regexp.MustCompile(detectors.PrefixRegex([]string{"id"}) + `\b([a-zA-Z0-9]{32})\b`) |
| 29 | 48 | clientSecretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"secret"}) + `\b([a-zA-Z0-9]{32})\b`) |
| 49 | added: | |
| 30 | 50 | // Box enterprise and user IDs are numeric strings. |
| 31 | removed: subjectIdPat = regexp.MustCompile(detectors.PrefixRegex([]string{ | |
| 32 | removed: "enterprise", "enterprise_id", "user", "user_id", "subject", "box_subject", | |
| 33 | removed: }) + `\b([0-9]{6,20})\b`) | |
| 51 | added: subjectKeywords = []string{"enterprise", "enterprise_id", "user", "user_id", "subject", "box_subject"} | |
| 52 | added: subjectIdPat = regexp.MustCompile(detectors.PrefixRegex(subjectKeywords) + `\b([0-9]{6,20})\b`) | |
| 53 | added: | |
| 54 | added: // subjectLabelPat finds the subject keywords inside a subjectIdPat match. | |
| 55 | added: // A match starts at the leftmost keyword, which can be a different label | |
| 56 | added: // than the one beside the number when two labels fall within the 40 | |
| 57 | added: // characters before it, so ranking reads the last keyword instead. | |
| 58 | added: subjectLabelPat = regexp.MustCompile(`(?i)` + strings.Join(subjectKeywords, "|")) | |
| 59 | added: ) | |
| 60 | added: | |
| 61 | added: // strongSubjectKeywords are labels that name a Box subject specifically. | |
| 62 | added: // Numbers next to them are tried before numbers next to generic labels like | |
| 63 | added: // "user", which also precede timestamps, phone numbers and unrelated IDs. | |
| 64 | added: var strongSubjectKeywords = map[string]struct{}{ | |
| 65 | added: "enterprise": {}, | |
| 66 | added: "enterprise_id": {}, | |
| 67 | added: "box_subject": {}, | |
| 68 | added: } | |
| 69 | added: | |
| 70 | added: var ( | |
| 71 | added: // errUnauthorizedClient is Box's unauthorized_client response to a request | |
| 72 | added: // carrying only the client ID and secret. Box defines it as "Client is not | |
| 73 | added: // authorized for requested grant type" and treats it as inconclusive: it | |
| 74 | added: // doesn't confirm the secret, so only a token issued for a subject can. | |
| 75 | added: errUnauthorizedClient = errors.New("unauthorized_client: cannot verify incomplete Box credentials without an enterprise, user, or subject id found nearby") | |
| 76 | added: | |
| 77 | added: // errInvalidClient is Box's invalid_client response, which Box defines as | |
| 78 | added: // an invalid client ID or secret. It's a definite rejection, so it never | |
| 79 | added: // surfaces as a verification error. | |
| 80 | added: errInvalidClient = errors.New("invalid_client") | |
| 34 | 81 | ) |
| 35 | 82 | |
| 83 | added: // tokenErrorResponse is the OAuth 2.0 error body Box returns from the token endpoint. | |
| 84 | added: type tokenErrorResponse struct { | |
| 85 | added: Error string `json:"error"` | |
| 86 | added: ErrorDescription string `json:"error_description"` | |
| 87 | added: } | |
| 88 | added: | |
| 36 | 89 | func (s Scanner) getClient() *http.Client { |
| 37 | 90 | if s.client != nil { |
| 38 | 91 | return s.client |
| @@ -47,7 +100,8 @@ func (s Scanner) Keywords() []string { | ||
| 47 | 100 | } |
| 48 | 101 | |
| 49 | 102 | func (s Scanner) Description() string { |
| 50 | removed: return "Box is a service offering various service for secure collaboration, content management, and workflow. Box Oauth credentials can be used to access and interact with this data." | |
| 103 | added: return "Box is a service offering various service for secure collaboration, content management, and workflow. Box Oauth credentials can be used to access and interact with this data. " + | |
| 104 | added: "Box only confirms a client ID and secret by issuing a token for an enterprise or user ID, so these credentials verify only when such an ID appears nearby." | |
| 51 | 105 | } |
| 52 | 106 | |
| 53 | 107 | // FromData will find and optionally verify Box secrets in a given set of bytes. |
| @@ -64,15 +118,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result | ||
| 64 | 118 | uniqueSecretMatches[match[1]] = struct{}{} |
| 65 | 119 | } |
| 66 | 120 | |
| 67 | removed: var subjectIds []string | |
| 68 | removed: uniqueSubjectIdMatches := make(map[string]struct{}) | |
| 69 | removed: for _, match := range subjectIdPat.FindAllStringSubmatch(dataStr, -1) { | |
| 70 | removed: if _, seen := uniqueSubjectIdMatches[match[1]]; !seen { | |
| 71 | removed: uniqueSubjectIdMatches[match[1]] = struct{}{} | |
| 72 | removed: subjectIds = append(subjectIds, match[1]) | |
| 73 | removed: } | |
| 74 | removed: } | |
| 75 | removed: slices.Sort(subjectIds) | |
| 121 | added: subjectIds := rankSubjectIDs(dataStr) | |
| 76 | 122 | |
| 77 | 123 | for resIdMatch := range uniqueIdMatches { |
| 78 | 124 | for resSecretMatch := range uniqueSecretMatches { |
| @@ -91,18 +137,15 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result | ||
| 91 | 137 | } |
| 92 | 138 | |
| 93 | 139 | if verify { |
| 94 | removed: isVerified, verificationErr := verifyMatch(ctx, s.getClient(), resIdMatch, resSecretMatch) | |
| 140 | added: isVerified, subjectId, verificationErr := verifyMatch(ctx, s.getClient(), resIdMatch, resSecretMatch, subjectIds) | |
| 95 | 141 | s1.Verified = isVerified |
| 96 | removed: s1.SetVerificationError(verificationErr, resIdMatch) | |
| 97 | removed: | |
| 98 | removed: if isVerified { | |
| 99 | removed: for _, subjectId := range subjectIds { | |
| 100 | removed: if verifySubjectID(ctx, s.getClient(), resIdMatch, resSecretMatch, subjectId) { | |
| 101 | removed: s1.SecretParts["subject_id"] = subjectId | |
| 102 | removed: break | |
| 103 | removed: } | |
| 104 | removed: } | |
| 142 | added: // subject_id feeds the Box analyzer, which needs the same | |
| 143 | added: // subject to authenticate, so it's only recorded once Box has | |
| 144 | added: // accepted it. | |
| 145 | added: if subjectId != "" { | |
| 146 | added: s1.SecretParts["subject_id"] = subjectId | |
| 105 | 147 | } |
| 148 | added: s1.SetVerificationError(verificationErr, resIdMatch, resSecretMatch) | |
| 106 | 149 | } |
| 107 | 150 | |
| 108 | 151 | results = append(results, s1) |
| @@ -117,67 +160,121 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result | ||
| 117 | 160 | return |
| 118 | 161 | } |
| 119 | 162 | |
| 120 | removed: func verifyMatch(ctx context.Context, client *http.Client, id string, secret string) (bool, error) { | |
| 121 | removed: url := "https://api.box.com/oauth2/token" | |
| 122 | removed: payload := strings.NewReader("grant_type=client_credentials&client_id=" + id + "&client_secret=" + secret) | |
| 123 | removed: req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, payload) | |
| 124 | removed: if err != nil { | |
| 125 | removed: return false, err | |
| 163 | added: // verifyMatch applies Box's verification rules to one client ID and secret | |
| 164 | added: // pair, as Box defines them: | |
| 165 | added: // | |
| 166 | added: // - invalid_client on the subject-less request: unverified (dead). | |
| 167 | added: // - unauthorized_client: inconclusive on its own. The pair is Verified only | |
| 168 | added: // when a request naming a nearby subject ID returns HTTP 200. If there's no | |
| 169 | added: // subject ID, or every subject attempt fails, the result stays unknown, | |
| 170 | added: // because Box won't say whether the secret or the subject caused a failure. | |
| 171 | added: // - Any other response: unknown, with no subject requests. | |
| 172 | added: // | |
| 173 | added: // It returns the accepted subject ID alongside a Verified result. | |
| 174 | added: func verifyMatch(ctx context.Context, client *http.Client, id, secret string, subjectIds []string) (bool, string, error) { | |
| 175 | added: err := probeClientCredentials(ctx, client, id, secret) | |
| 176 | added: switch { | |
| 177 | added: case errors.Is(err, errInvalidClient): | |
| 178 | added: return false, "", nil | |
| 179 | added: case !errors.Is(err, errUnauthorizedClient): | |
| 180 | added: return false, "", err | |
| 181 | added: } | |
| 182 | added: | |
| 183 | added: if len(subjectIds) == 0 { | |
| 184 | added: return false, "", err | |
| 185 | added: } | |
| 186 | added: | |
| 187 | added: candidates := subjectIds | |
| 188 | added: if len(candidates) > maxSubjectIDCandidates { | |
| 189 | added: candidates = candidates[:maxSubjectIDCandidates] | |
| 190 | added: } | |
| 191 | added: | |
| 192 | added: for _, subjectId := range candidates { | |
| 193 | added: if verifySubjectID(ctx, client, id, secret, subjectId) { | |
| 194 | added: return true, subjectId, nil | |
| 195 | added: } | |
| 196 | added: } | |
| 197 | added: | |
| 198 | added: // These messages use %v, not %w: Result.SetVerificationError keeps only the | |
| 199 | added: // innermost wrapped error, which would drop the context added here. | |
| 200 | added: msg := fmt.Sprintf("%v; no nearby subject ID was accepted", err) | |
| 201 | added: // When the cap skipped candidates, say so, so an unknown result on a noisy | |
| 202 | added: // chunk is explainable. Counts only: the IDs themselves stay out of the | |
| 203 | added: // stored error. | |
| 204 | added: if len(subjectIds) > len(candidates) { | |
| 205 | added: msg = fmt.Sprintf("%s (tried %d of %d subject ID candidates)", msg, len(candidates), len(subjectIds)) | |
| 126 | 206 | } |
| 207 | added: return false, "", errors.New(msg) | |
| 208 | added: } | |
| 127 | 209 | |
| 128 | removed: req.Header = http.Header{"content-type": []string{"application/x-www-form-urlencoded"}} | |
| 210 | added: // probeClientCredentials sends the client ID and secret to Box's token | |
| 211 | added: // endpoint without a subject. Box requires a subject for client-credentials | |
| 212 | added: // tokens, so this request never issues one; its only purpose is to sort the | |
| 213 | added: // pair into Box's rule buckets. It returns errInvalidClient, | |
| 214 | added: // errUnauthorizedClient, or an error describing an unexpected response. | |
| 215 | added: func probeClientCredentials(ctx context.Context, client *http.Client, id string, secret string) error { | |
| 216 | added: form := url.Values{ | |
| 217 | added: "grant_type": {"client_credentials"}, | |
| 218 | added: "client_id": {id}, | |
| 219 | added: "client_secret": {secret}, | |
| 220 | added: } | |
| 221 | added: req, err := http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, strings.NewReader(form.Encode())) | |
| 222 | added: if err != nil { | |
| 223 | added: return err | |
| 224 | added: } | |
| 225 | added: req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
| 129 | 226 | |
| 130 | 227 | res, err := client.Do(req) |
| 131 | 228 | if err != nil { |
| 132 | removed: return false, err | |
| 229 | added: return err | |
| 133 | 230 | } |
| 134 | 231 | defer func() { |
| 135 | 232 | _, _ = io.Copy(io.Discard, res.Body) |
| 136 | 233 | _ = res.Body.Close() |
| 137 | 234 | }() |
| 138 | 235 | |
| 139 | removed: // We are using malformed request to check if the client id and secret are valid. | |
| 140 | removed: // In this case, the Box OAuth API returns a 400 status code even if the credentials are valid. | |
| 141 | removed: // | |
| 142 | removed: // - If the client ID/secret are valid, the response contains "unauthorized_client" | |
| 143 | removed: // - If the credentials are invalid, the response contains "invalid_client" | |
| 144 | removed: // | |
| 145 | removed: // So we check the response body for one of these keywords. | |
| 146 | removed: switch res.StatusCode { | |
| 147 | removed: case http.StatusBadRequest: | |
| 148 | removed: { | |
| 149 | removed: bodyBytes, err := io.ReadAll(res.Body) | |
| 150 | removed: if err != nil { | |
| 151 | removed: return false, err | |
| 152 | removed: } | |
| 153 | removed: body := string(bodyBytes) | |
| 154 | removed: if strings.Contains(body, "unauthorized_client") { | |
| 155 | removed: return true, nil | |
| 156 | removed: } else if strings.Contains(body, "invalid_client") { | |
| 157 | removed: return false, nil | |
| 158 | removed: } else { | |
| 159 | removed: return false, fmt.Errorf("response body missing expected keyword") | |
| 160 | removed: } | |
| 161 | removed: } | |
| 236 | added: if res.StatusCode != http.StatusBadRequest { | |
| 237 | added: return fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | |
| 238 | added: } | |
| 239 | added: | |
| 240 | added: var tokenErr tokenErrorResponse | |
| 241 | added: if err := json.NewDecoder(res.Body).Decode(&tokenErr); err != nil { | |
| 242 | added: return fmt.Errorf("failed to decode token error response: %v", err) | |
| 243 | added: } | |
| 244 | added: | |
| 245 | added: switch tokenErr.Error { | |
| 246 | added: case "invalid_client": | |
| 247 | added: return errInvalidClient | |
| 248 | added: case "unauthorized_client": | |
| 249 | added: return errUnauthorizedClient | |
| 162 | 250 | default: |
| 163 | removed: return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | |
| 251 | added: return fmt.Errorf("unexpected token error %q", tokenErr.Error) | |
| 164 | 252 | } |
| 165 | 253 | } |
| 166 | 254 | |
| 167 | 255 | // verifySubjectID checks whether a subject ID is valid for the given client |
| 168 | 256 | // credentials by attempting the Box CCG (Client Credentials Grant) auth flow. |
| 169 | removed: // It mirrors the analyzer's Authenticate logic: try enterprise subject type | |
| 170 | removed: // first, then fall back to user subject type. Returns true on the first | |
| 171 | removed: // successful authentication (HTTP 200). | |
| 257 | added: // It mirrors the analyzer's Authenticate logic: try each of subjectTypes in | |
| 258 | added: // order, and return true on the first successful authentication (HTTP 200). | |
| 259 | added: // | |
| 260 | added: // A 200 means Box issued a real access token, valid for 60 minutes with no | |
| 261 | added: // refresh token. That's unavoidable: Box confirmed tha | |
Diff truncated for size. See the full file on GitHub.