Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Verify BoxOauth credentials only when Box issues a token for a subject (#5498)
Dave Hubbardea68ca03c865
diff --git a/pkg/detectors/boxoauth/boxoauth.go b/pkg/detectors/boxoauth/boxoauth.goindex 5363e19e9..fc9e6bf30 100644--- a/pkg/detectors/boxoauth/boxoauth.go+++ b/pkg/detectors/boxoauth/boxoauth.go@@ -2,10 +2,12 @@ package boxoauthimport ("context"+ "encoding/json"+ "errors""fmt""io""net/http"- "slices"+ "net/url""strings"regexp "github.com/wasilibs/go-re2"@@ -22,17 +24,68 @@ type Scanner struct {// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)+const tokenURL = "https://api.box.com/oauth2/token"++// maxSubjectIDCandidates bounds how many nearby subject IDs are tried per+// client ID and secret pair. Each candidate costs up to one token request per+// subject type, every request counts against the customer's Box API+// allocation, and a long run of failed subject attempts against one client+// looks like enumeration. Real configs usually carry one or two genuine IDs,+// and candidates are ranked so the most likely ones fall inside the cap.+const maxSubjectIDCandidates = 5++// subjectTypes are the box_subject_type values tried for each subject ID, in+// order. "enterprise" authenticates as the app's Service Account; "user"+// authenticates as an App User, Managed User or admin. User-subject tokens act+// as a real person, so dropping "user" here is the single switch for+// narrowing that footprint.+var subjectTypes = []string{"enterprise", "user"}+var (defaultClient = common.SaneHttpClient()// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.clientIdPat = regexp.MustCompile(detectors.PrefixRegex([]string{"id"}) + `\b([a-zA-Z0-9]{32})\b`)clientSecretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"secret"}) + `\b([a-zA-Z0-9]{32})\b`)+// Box enterprise and user IDs are numeric strings.- subjectIdPat = regexp.MustCompile(detectors.PrefixRegex([]string{- "enterprise", "enterprise_id", "user", "user_id", "subject", "box_subject",- }) + `\b([0-9]{6,20})\b`)+ subjectKeywords = []string{"enterprise", "enterprise_id", "user", "user_id", "subject", "box_subject"}+ subjectIdPat = regexp.MustCompile(detectors.PrefixRegex(subjectKeywords) + `\b([0-9]{6,20})\b`)++ // subjectLabelPat finds the subject keywords inside a subjectIdPat match.+ // A match starts at the leftmost keyword, which can be a different label+ // than the one beside the number when two labels fall within the 40+ // characters before it, so ranking reads the last keyword instead.+ subjectLabelPat = regexp.MustCompile(`(?i)` + strings.Join(subjectKeywords, "|"))+)++// strongSubjectKeywords are labels that name a Box subject specifically.+// Numbers next to them are tried before numbers next to generic labels like+// "user", which also precede timestamps, phone numbers and unrelated IDs.+var strongSubjectKeywords = map[string]struct{}{+ "enterprise": {},+ "enterprise_id": {},+ "box_subject": {},+}++var (+ // errUnauthorizedClient is Box's unauthorized_client response to a request+ // carrying only the client ID and secret. Box defines it as "Client is not+ // authorized for requested grant type" and treats it as inconclusive: it+ // doesn't confirm the secret, so only a token issued for a subject can.+ errUnauthorizedClient = errors.New("unauthorized_client: cannot verify incomplete Box credentials without an enterprise, user, or subject id found nearby")++ // errInvalidClient is Box's invalid_client response, which Box defines as+ // an invalid client ID or secret. It's a definite rejection, so it never+ // surfaces as a verification error.+ errInvalidClient = errors.New("invalid_client"))+// tokenErrorResponse is the OAuth 2.0 error body Box returns from the token endpoint.+type tokenErrorResponse struct {+ Error string `json:"error"`+ ErrorDescription string `json:"error_description"`+}+func (s Scanner) getClient() *http.Client {if s.client != nil {return s.client@@ -47,7 +100,8 @@ func (s Scanner) Keywords() []string {}func (s Scanner) Description() string {- 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."+ 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. " ++ "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."}// 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) (resultuniqueSecretMatches[match[1]] = struct{}{}}- var subjectIds []string- uniqueSubjectIdMatches := make(map[string]struct{})- for _, match := range subjectIdPat.FindAllStringSubmatch(dataStr, -1) {- if _, seen := uniqueSubjectIdMatches[match[1]]; !seen {- uniqueSubjectIdMatches[match[1]] = struct{}{}- subjectIds = append(subjectIds, match[1])- }- }- slices.Sort(subjectIds)+ subjectIds := rankSubjectIDs(dataStr)for resIdMatch := range uniqueIdMatches {for resSecretMatch := range uniqueSecretMatches {@@ -91,18 +137,15 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result}if verify {- isVerified, verificationErr := verifyMatch(ctx, s.getClient(), resIdMatch, resSecretMatch)+ isVerified, subjectId, verificationErr := verifyMatch(ctx, s.getClient(), resIdMatch, resSecretMatch, subjectIds)s1.Verified = isVerified- s1.SetVerificationError(verificationErr, resIdMatch)-- if isVerified {- for _, subjectId := range subjectIds {- if verifySubjectID(ctx, s.getClient(), resIdMatch, resSecretMatch, subjectId) {- s1.SecretParts["subject_id"] = subjectId- break- }- }+ // subject_id feeds the Box analyzer, which needs the same+ // subject to authenticate, so it's only recorded once Box has+ // accepted it.+ if subjectId != "" {+ s1.SecretParts["subject_id"] = subjectId}+ s1.SetVerificationError(verificationErr, resIdMatch, resSecretMatch)}results = append(results, s1)@@ -117,67 +160,121 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn}-func verifyMatch(ctx context.Context, client *http.Client, id string, secret string) (bool, error) {- url := "https://api.box.com/oauth2/token"- payload := strings.NewReader("grant_type=client_credentials&client_id=" + id + "&client_secret=" + secret)- req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, payload)- if err != nil {- return false, err+// verifyMatch applies Box's verification rules to one client ID and secret+// pair, as Box defines them:+//+// - invalid_client on the subject-less request: unverified (dead).+// - unauthorized_client: inconclusive on its own. The pair is Verified only+// when a request naming a nearby subject ID returns HTTP 200. If there's no+// subject ID, or every subject attempt fails, the result stays unknown,+// because Box won't say whether the secret or the subject caused a failure.+// - Any other response: unknown, with no subject requests.+//+// It returns the accepted subject ID alongside a Verified result.+func verifyMatch(ctx context.Context, client *http.Client, id, secret string, subjectIds []string) (bool, string, error) {+ err := probeClientCredentials(ctx, client, id, secret)+ switch {+ case errors.Is(err, errInvalidClient):+ return false, "", nil+ case !errors.Is(err, errUnauthorizedClient):+ return false, "", err+ }++ if len(subjectIds) == 0 {+ return false, "", err+ }++ candidates := subjectIds+ if len(candidates) > maxSubjectIDCandidates {+ candidates = candidates[:maxSubjectIDCandidates]+ }++ for _, subjectId := range candidates {+ if verifySubjectID(ctx, client, id, secret, subjectId) {+ return true, subjectId, nil+ }+ }++ // These messages use %v, not %w: Result.SetVerificationError keeps only the+ // innermost wrapped error, which would drop the context added here.+ msg := fmt.Sprintf("%v; no nearby subject ID was accepted", err)+ // When the cap skipped candidates, say so, so an unknown result on a noisy+ // chunk is explainable. Counts only: the IDs themselves stay out of the+ // stored error.+ if len(subjectIds) > len(candidates) {+ msg = fmt.Sprintf("%s (tried %d of %d subject ID candidates)", msg, len(candidates), len(subjectIds))}+ return false, "", errors.New(msg)+}- req.Header = http.Header{"content-type": []string{"application/x-www-form-urlencoded"}}+// probeClientCredentials sends the client ID and secret to Box's token+// endpoint without a subject. Box requires a subject for client-credentials+// tokens, so this request never issues one; its only purpose is to sort the+// pair into Box's rule buckets. It returns errInvalidClient,+// errUnauthorizedClient, or an error describing an unexpected response.+func probeClientCredentials(ctx context.Context, client *http.Client, id string, secret string) error {+ form := url.Values{+ "grant_type": {"client_credentials"},+ "client_id": {id},+ "client_secret": {secret},+ }+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, strings.NewReader(form.Encode()))+ if err != nil {+ return err+ }+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")res, err := client.Do(req)if err != nil {- return false, err+ return err}defer func() {_, _ = io.Copy(io.Discard, res.Body)_ = res.Body.Close()}()- // We are using malformed request to check if the client id and secret are valid.- // In this case, the Box OAuth API returns a 400 status code even if the credentials are valid.- //- // - If the client ID/secret are valid, the response contains "unauthorized_client"- // - If the credentials are invalid, the response contains "invalid_client"- //- // So we check the response body for one of these keywords.- switch res.StatusCode {- case http.StatusBadRequest:- {- bodyBytes, err := io.ReadAll(res.Body)- if err != nil {- return false, err- }- body := string(bodyBytes)- if strings.Contains(body, "unauthorized_client") {- return true, nil- } else if strings.Contains(body, "invalid_client") {- return false, nil- } else {- return false, fmt.Errorf("response body missing expected keyword")- }- }+ if res.StatusCode != http.StatusBadRequest {+ return fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)+ }++ var tokenErr tokenErrorResponse+ if err := json.NewDecoder(res.Body).Decode(&tokenErr); err != nil {+ return fmt.Errorf("failed to decode token error response: %v", err)+ }++ switch tokenErr.Error {+ case "invalid_client":+ return errInvalidClient+ case "unauthorized_client":+ return errUnauthorizedClientdefault:- return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)+ return fmt.Errorf("unexpected token error %q", tokenErr.Error)}}// verifySubjectID checks whether a subject ID is valid for the given client// credentials by attempting the Box CCG (Client Credentials Grant) auth flow.-// It mirrors the analyzer's Authenticate logic: try enterprise subject type-// first, then fall back to user subject type. Returns true on the first-// successful authentication (HTTP 200).+// It mirrors the analyzer's Authenticate logic: try each of subjectTypes in+// order, and return true on the first successful authentication (HTTP 200).+//+// A 200 means Box issued a real access token, valid for 60 minutes with no+// refresh token. That's unavoidable: Box confirmed that a token issued for a+// subject is the only valid verification signal. The body is drained without+// being parsed, so the token never reaches results, logs or errors. It is+// deliberately not revoked. Revoking would add another request against the+// customer's API allocation and another audit event, and the token never+// leaves this process, so it is left to expire on its own.func verifySubjectID(ctx context.Context, client *http.Client, clientID, clientSecret, subjectID string) bool {- for _, subjectType := range []string{"enterprise", "user"} {- payload := fmt.Sprintf(- "grant_type=client_credentials&client_id=%s&client_secret=%s&box_subject_type=%s&box_subject_id=%s",- clientID, clientSecret, subjectType, subjectID,- )-- req, err := http.NewRequestWithContext(- ctx, http.MethodPost, "https://api.box.com/oauth2/token", strings.NewReader(payload))+ for _, subjectType := range subjectTypes {+ form := url.Values{+ "grant_type": {"client_credentials"},+ "client_id": {clientID},+ "client_secret": {clientSecret},+ "box_subject_type": {subjectType},+ "box_subject_id": {subjectID},+ }++ req, err := http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, strings.NewReader(form.Encode()))if err != nil {continue}@@ -197,6 +294,45 @@ func verifySubjectID(ctx context.Context, client *http.Client, clientID, clientSreturn false}+// rankSubjectIDs returns the unique subject ID candidates in data, ordered so+// the ones most likely to be a real Box subject come first. A number's label+// is the keyword closest before it; numbers labeled with a strong keyword (see+// strongSubjectKeywords) go ahead of numbers next to generic labels, so a real+// enterprise ID survives maxSubjectIDCandidates even after several user IDs.+// Within each group, numbers keep the order they appear in the text, which+// keeps the result deterministic and keeps a config's own ID near the front. A+// number seen under both kinds of label counts as strong.+func rankSubjectIDs(data string) []string {+ var order []string+ strong := make(map[string]bool)+ for _, match := range subjectIdPat.FindAllStringSubmatch(data, -1) {+ subjectId := match[1]+ // The match always begins with a keyword, so labels is never empty.+ labels := subjectLabelPat.FindAllString(match[0][:len(match[0])-len(subjectId)], -1)+ keyword := strings.ToLower(labels[len(labels)-1])+ _, isStrong := strongSubjectKeywords[keyword]+ if seenStrong, seen := strong[subjectId]; seen {+ strong[subjectId] = seenStrong || isStrong+ continue+ }+ strong[subjectId] = isStrong+ order = append(order, subjectId)+ }++ ranked := make([]string, 0, len(order))+ for _, subjectId := range order {+ if strong[subjectId] {+ ranked = append(ranked, subjectId)+ }+ }+ for _, subjectId := range order {+ if !strong[subjectId] {+ ranked = append(ranked, subjectId)+ }+ }+ return ranked+}+func (s Scanner) Type() detector_typepb.DetectorType {return detector_typepb.DetectorType_BoxOauth}diff --git a/pkg/detectors/boxoauth/boxoauth_integration_test.go b/pkg/detectors/boxoauth/boxoauth_integration_test.goindex a140d3408..ea27b09e6 100644--- a/pkg/detectors/boxoauth/boxoauth_integration_test.go+++ b/pkg/detectors/boxoauth/boxoauth_integration_test.go@@ -28,6 +28,29 @@ func TestBoxOauth_FromChunk(t *testing.T) {secret := testSecrets.MustGetField("BOXOAUTH_SECRET")invalidSecret := testSecrets.MustGetField("BOXOAUTH_INVALID_SECRET")+ // The analyzer test app shares the Box test tenant and comes with a subject+ // ID it is authorized for, which is the only input Box will issue a token+ // for. The detectors5 pair has no subject ID, so it can only ever be unknown.+ analyzerSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "analyzers1")+ if err != nil {+ t.Fatalf("could not get analyzer test secrets from GCP: %s", err)+ }+ subjectClientID := analyzerSecrets.MustGetField("BOX_CLIENT_ID")+ subjectClientSecret := analyzerSecrets.MustGetField("BOX_CLIENT_SECRET")+ subjectID := analyzerSecrets.MustGetField("BOX_SUBJECT_ID")++ unknownResult := detectors.Result{+ DetectorType: detector_typepb.DetectorType_BoxOauth,+ Verified: false,+ Raw: []byte(id),+ RawV2: []byte(id + secret),+ SecretParts: map[string]string{+ "client_id": id,+ "client_secret": secret,+ },+ }+ unknownResult.SetVerificationError(errUnauthorizedClient)+type args struct {ctx context.Contextdata []byte@@ -41,27 +64,39 @@ func TestBoxOauth_FromChunk(t *testing.T) {wantErr bool}{{- name: "found, verified",+ name: "found, verified with nearby subject ID",s: Scanner{},args: args{ctx: context.Background(),- data: []byte(fmt.Sprintf("You can find a box id %s with secret %s", id, secret)),+ data: []byte(fmt.Sprintf("You can find a box id %s with secret %s and enterprise_id %s", subjectClientID, subjectClientSecret, subjectID)),verify: true,},want: []detectors.Result{{DetectorType: detector_typepb.DetectorType_BoxOauth,Verified: true,- Raw: []byte(id),- RawV2: []byte(id + secret),+ Raw: []byte(subjectClientID),+ RawV2: []byte(subjectClientID + subjectClientSecret),SecretParts: map[string]string{- "client_id": id,- "client_secret": secret,+ "client_id": subjectClientID,+ "client_secret": subjectClientSecret,+ "subject_id": subjectID,},},},wantErr: false,},+ {+ name: "found, real credentials without subject ID are unknown",+ s: Scanner{},+ args: args{+ ctx: context.Background(),+ data: []byte(fmt.Sprintf("You can find a box id %s with secret %s", id, secret)),+ verify: true,+ },+ want: []detectors.Result{unknownResult},+ wantErr: false,+ },{name: "found, unverified",s: Scanner{},@@ -109,7 +144,7 @@ func TestBoxOauth_FromChunk(t *testing.T) {t.Fatalf("no raw secret present: \n %+v", got[i])}- if len(got[i].Raw) == 0 {+ if len(got[i].RawV2) == 0 {t.Fatalf("no rawV2 secret present: \n %+v", got[i])}}diff --git a/pkg/detectors/boxoauth/boxoauth_test.go b/pkg/detectors/boxoauth/boxoauth_test.goindex 9573f98c3..bcc1c2f43 100644--- a/pkg/detectors/boxoauth/boxoauth_test.go+++ b/pkg/detectors/boxoauth/boxoauth_test.go@@ -4,6 +4,7 @@ import ("context""fmt""net/http"+ "strings""testing""github.com/stretchr/testify/assert"@@ -99,132 +100,239 @@ func TestBoxOauth_Pattern(t *testing.T) {}}-func TestBoxOauth_AnalysisInfo_VerifiedWithValidSubjectId(t *testing.T) {- client := common.SaneHttpClient()- d := Scanner{client: client}-- defer gock.Off()- defer gock.RestoreClient(client)- gock.InterceptClient(client)-- // Mock 1: verifyMatch → valid pair (400 with "unauthorized_client")- gock.New("https://api.box.com").- Post("/oauth2/token").- Reply(http.StatusBadRequest).- BodyString(`{"error":"unauthorized_client","error_description":"The grant type is unauthorized for this client_id"}`)-- // Mock 2: verifySubjectID enterprise attempt → success (200)- gock.New("https://api.box.com").- Post("/oauth2/token").- Reply(http.StatusOK).- BodyString(`{"access_token":"test_token","expires_in":4169,"token_type":"bearer"}`)-- input := fmt.Sprintf("box id = '%s' box secret = '%s' enterprise = '%s'", clientId, clientSecret, subjectId)-- results, err := d.FromData(context.Background(), true, []byte(input))- require.NoError(t, err)- require.Len(t, results, 1)-- r := results[0]- assert.True(t, r.Verified)- require.NotNil(t, r.SecretParts)- assert.Equal(t, clientId, r.SecretParts["client_id"])- assert.Equal(t, clientSecret, r.SecretParts["client_secret"])- assert.Equal(t, subjectId, r.SecretParts["subject_id"])+// tokenMock describes one expected request to Box's token endpoint and the+// reply it gets. bodyPattern is a regular expression matched against the+// form-encoded request body; an empty pattern matches any request.+type tokenMock struct {+ bodyPattern string+ status int+ body string}-func TestBoxOauth_AnalysisInfo_VerifiedNoSubjectId(t *testing.T) {- client := common.SaneHttpClient()- d := Scanner{client: client}-- defer gock.Off()- defer gock.RestoreClient(client)- gock.InterceptClient(client)-- // Mock: verifyMatch → valid pair- gock.New("https://api.box.com").- Post("/oauth2/token").- Reply(http.StatusBadRequest).- BodyString(`{"error":"unauthorized_client","error_description":"The grant type is unauthorized for this client_id"}`)-- input := fmt.Sprintf("box id = '%s' box secret = '%s'", clientId, clientSecret)-- results, err := d.FromData(context.Background(), true, []byte(input))- require.NoError(t, err)- require.Len(t, results, 1)+const (+ unauthorizedClientBody = `{"error":"unauthorized_client","error_description":"The grant type is unauthorized for this client_id"}`+ invalidClientBody = `{"error":"invalid_client","error_description":"The client credentials are not valid"}`+ invalidGrantBody = `{"error":"invalid_grant","error_description":"Cannot obtain token based on the enterprise configuration for your app"}`+ tokenBody = `{"access_token":"test_token","expires_in":3600,"token_type":"bearer"}`+)- r := results[0]- assert.True(t, r.Verified)- require.NotNil(t, r.SecretParts)- assert.Equal(t, clientId, r.SecretParts["client_id"])- assert.Equal(t, clientSecret, r.SecretParts["client_secret"])- assert.Empty(t, r.SecretParts["subject_id"])+// subjectRequest matches a subject-ID token request for one subject type.+// url.Values encodes keys in sorted order, so box_subject_id precedes+// box_subject_type in the body.+func subjectRequest(subjectID, subjectType string) string {+ return "box_subject_id=" + subjectID + "&box_subject_type=" + subjectType}-func TestBoxOauth_AnalysisInfo_VerifiedInvalidSubjectId(t *testing.T) {- client := common.SaneHttpClient()- d := Scanner{client: client}-- defer gock.Off()- defer gock.RestoreClient(client)- gock.InterceptClient(client)-- // Mock 1: verifyMatch → valid pair- gock.New("https://api.box.com").- Post("/oauth2/token").- Reply(http.StatusBadRequest).- BodyString(`{"error":"unauthorized_client","error_description":"The grant type is unauthorized for this client_id"}`)-- // Mock 2: verifySubjectID enterprise attempt → fail (400)- gock.New("https://api.box.com").- Post("/oauth2/token").- Reply(http.StatusBadRequest).- BodyString(`{"error":"invalid_grant","error_description":"Cannot obtain token based on the enterprise configuration for your app"}`)-- // Mock 3: verifySubjectID user attempt → fail (400)- gock.New("https://api.box.com").- Post("/oauth2/token").- Reply(http.StatusBadRequest).- BodyString(`{"error":"invalid_grant","error_description":"Cannot obtain token based on the enterprise configuration for your app"}`)-- input := fmt.Sprintf("box id = '%s' box secret = '%s' enterprise = '%s'", clientId, clientSecret, subjectId)-- results, err := d.FromData(context.Background(), true, []byte(input))- require.NoError(t, err)- require.Len(t, results, 1)-- r := results[0]- assert.True(t, r.Verified)- require.NotNil(t, r.SecretParts)- assert.Equal(t, clientId, r.SecretParts["client_id"])- assert.Equal(t, clientSecret, r.SecretParts["client_secret"])- assert.Empty(t, r.SecretParts["subject_id"])-}+// TestBoxOauth_Verification covers each of Box's verification rules. Every case+// registers exactly the requests it expects, in order, and fails if any mock is+// left unused or any other request is made. That also guarantees the detector+// never calls /oauth2/revoke.+func TestBoxOauth_Verification(t *testing.T) {+ failedSubjectAttempts := func(n int) []tokenMock {+ mocks := make([]tokenMock, n)+ for i := range mocks {+ mocks[i] = tokenMock{status: http.StatusBadRequest, body: invalidGrantBody}+ }+ return mocks+ }-func TestBoxOauth_AnalysisInfo_UnverifiedWithSubjectId(t *testing.T) {- client := common.SaneHttpClient()- d := Scanner{client: client}+ manyUserIDs := func(n int) string {+ var b strings.Builder+ for i := 0; i < n; i++ {+ fmt.Fprintf(&b, "user_id = '%d'\n", 1000001+i)+ }+ return b.String()+ }++ tests := []struct {+ name string+ input string+ mocks []tokenMock+ wantVerified bool+ wantSubjectID string+ // wantErr is a substring of the verification error, or "" for no error.+ wantErr string+ wantErrAbsent string+ }{+ {+ name: "invalid_client is unverified with no subject requests",+ input: fmt.Sprintf("box id = '%s' box secret = '%s' enterprise = '%s'", clientId, clientSecret, subjectId),+ mocks: []tokenMock{+ {status: http.StatusBadRequest, body: invalidClientBody},+ },+ },+ {+ name: "unauthorized_client with no subject ID is unknown",+ input: fmt.Sprintf("box id = '%s' box secret = '%s'", clientId, clientSecret),+ mocks: []tokenMock{+ {status: http.StatusBadRequest, body: unauthorizedClientBody},+ },+ wantErr: "unauthorized_client",+ wantErrAbsent: "no nearby subject ID was accepted",+ },+ {+ name: "enterprise subject token verifies the pair",+ input: fmt.Sprintf("box id = '%s' box secret = '%s' enterprise = '%s'", clientId, clientSecret, subjectId),+ mocks: []tokenMock{+ {status: http.StatusBadRequest, body: unauthorizedClientBody},+ {bodyPattern: subjectRequest(subjectId, "enterprise"), status: http.StatusOK, body: tokenBody},+ },+ wantVerified: true,+ wantSubjectID: subjectId,+ },+ {+ name: "user subject token verifies the pair after enterprise fails",+ input: fmt.Sprintf("box id = '%s' box secret = '%s' user = '%s'", clientId, clientSecret, subjectId),+ mocks: []tokenMock{+ {status: http.StatusBadRequest, body: unauthorizedClientBody},+ {bodyPattern: subjectRequest(subjectId, "enterprise"), status: http.StatusBadRequest, body: invalidGrantBody},+ {bodyPattern: subjectRequest(subjectId, "user"), status: http.StatusOK, body: tokenBody},+ },+ wantVerified: true,+ wantSubjectID: subjectId,+ },+ {+ name: "unauthorized_client with every subject attempt failing is unknown",+ input: fmt.Sprintf("box id = '%s' box secret = '%s' enterprise = '%s'", clientId, clientSecret, subjectId),+ mocks: append(+ []tokenMock{{status: http.StatusBadRequest, body: unauthorizedClientBody}},+ failedSubjectAttempts(2)...,+ ),+ wantErr: "no nearby subject ID was accepted",+ wantErrAbsent: "subject ID candidates",+ },+ {+ name: "unexpected status is unknown with no subject requests",+ input: fmt.Sprintf("box id = '%s' box secret = '%s' enterprise = '%s'", clientId, clientSecret, subjectId),+ mocks: []tokenMock{+ {status: http.StatusInternalServerError, body: `{}`},+ },+ wantErr: "unexpected HTTP response status 500",+ },+ {+ name: "unexpected token error is unknown with no subject requests",+ input: fmt.Sprintf("box id = '%s' box secret = '%s' enterprise = '%s'", clientId, clientSecret, subjectId),+ mocks: []tokenMock{+ {status: http.StatusBadRequest, body: `{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}`},+ },+ wantErr: `unexpected token error "invalid_request"`,+ },+ {+ name: "subject candidates are capped and truncation is reported",+ input: fmt.Sprintf("box id = '%s' box secret = '%s'\n%s", clientId, clientSecret, manyUserIDs(maxSubjectIDCandidates+2)),+ mocks: append(+ []tokenMock{{status: http.StatusBadRequest, body: unauthorizedClientBody}},+ failedSubjectAttempts(maxSubjectIDCandidates*len(subjectTypes))...,+ ),+ wantErr: fmt.Sprintf("tried %d of %d subject ID candidates", maxSubjectIDCandidates, maxSubjectIDCandidates+2),+ },+ {+ name: "no truncation is reported when the pair verifies",+ input: fmt.Sprintf("box id = '%s' box secret = '%s'\n%s", clientId, clientSecret, manyUserIDs(maxSubjectIDCandidates+2)),+ mocks: []tokenMock{+ {status: http.StatusBadRequest, body: unauthorizedClientBody},+ {bodyPattern: subjectRequest("1000001", "enterprise"), status: http.StatusOK, body: tokenBody},+ },+ wantVerified: true,+ wantSubjectID: "1000001",+ },+ {+ name: "strongly labeled subject ID is tried before an earlier weak one",+ input: fmt.Sprintf("box id = '%s' box secret = '%s' user = '%s' enterprise_id = '%s'",+ clientId, clientSecret, subjectId, subjectId2),+ mocks: []tokenMock{+ {status: http.StatusBadRequest, body: unauthorizedClientBody},+ {bodyPattern: subjectRequest(subjectId2, "enterprise"), status: http.StatusOK, body: tokenBody},+ },+ wantVerified: true,+ wantSubjectID: subjectId2,+ },+ }- defer gock.Off()- defer gock.RestoreClient(client)- gock.InterceptClient(client)+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ client := common.SaneHttpClient()+ d := Scanner{client: client}++ defer gock.OffAll()+ defer gock.RestoreClient(client)+ gock.InterceptClient(client)++ for _, m := range tt.mocks {+ req := gock.New("https://api.box.com").Post("/oauth2/token")+ if m.bodyPattern != "" {+ req = req.MatchType("url").BodyString(m.bodyPattern)+ }+ req.Reply(m.status).BodyString(m.body)+ }- // Mock: verifyMatch → invalid pair (400 with "invalid_client")- gock.New("https://api.box.com").- Post("/oauth2/token").- Reply(http.StatusBadRequest).- BodyString(`{"error":"invalid_client","error_description":"The client_id is invalid"}`)+ results, err := d.FromData(context.Background(), true, []byte(tt.input))+ require.NoError(t, err)+ require.Len(t, results, 1)++ r := results[0]+ assert.Equal(t, tt.wantVerified, r.Verified)+ assert.Equal(t, clientId, r.SecretParts["client_id"])+ assert.Equal(t, clientSecret, r.SecretParts["client_secret"])+ assert.Equal(t, tt.wantSubjectID, r.SecretParts["subject_id"])++ if tt.wantErr == "" {+ assert.NoError(t, r.VerificationError())+ } else {+ require.Error(t, r.VerificationError())+ assert.Contains(t, r.VerificationError().Error(), tt.wantErr)+ }+ if tt.wantErrAbsent != "" && r.VerificationError() != nil {+ assert.NotContains(t, r.VerificationError().Error(), tt.wantErrAbsent)+ }- input := fmt.Sprintf("box id = '%s' box secret = '%s' enterprise = '%s'", clientId, clientSecret, subjectId)+ assert.True(t, gock.IsDone(), "expected every mocked request to be made")+ assert.False(t, gock.HasUnmatchedRequest(), "unexpected requests: %v", gock.GetUnmatchedRequests())+ })+ }+}- results, err := d.FromData(context.Background(), true, []byte(input))- require.NoError(t, err)- require.Len(t, results, 1)+func TestRankSubjectIDs(t *testing.T) {+ tests := []struct {+ name string+ input string+ want []string+ }{+ {+ name: "no candidates",+ input: "box id = 'abc'",+ want: []string{},+ },+ {+ name: "strong labels first, appearance order within each group",+ input: "user = '3333333' subject = '1111111' enterprise_id = '2222222' BOX_SUBJECT_ID = '4444444'",+ want: []string{"2222222", "4444444", "3333333", "1111111"},+ },+ {+ name: "number seen under weak and strong labels counts as strong",+ input: "user = '5555555' user = '6666666' enterprise = '5555555'",+ want: []string{"5555555", "6666666"},+ },+ {+ name: "duplicates are dropped",+ input: "user = '7777777' user = '7777777'",+ want: []string{"7777777"},+ },+ {+ name: "label beside the number wins over an earlier label in the same window",+ input: "user = '8888888' owner user: bob enterprise_id: 1234567",+ want: []string{"1234567", "8888888"},+ },+ {+ name: "box_subject is not read as subject",+ input: "user = '8888888' box_subject: 2345678",+ want: []string{"2345678", "8888888"},+ },+ }- r := results[0]- assert.False(t, r.Verified)- require.NotNil(t, r.SecretParts)- assert.Equal(t, clientId, r.SecretParts["client_id"])- assert.Equal(t, clientSecret, r.SecretParts["client_secret"])- assert.Empty(t, r.SecretParts["subject_id"])+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ assert.Equal(t, tt.want, rankSubjectIDs(tt.input))+ })+ }}