Skip to content
Changes

Reference

boxoauth · Nov 3, 2026

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

Diff truncated for size. See the full file on GitHub.