Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Add Okta OAuth support in Okta detector (#5323)
Shreyas Sriram013308ddb030
diff --git a/pkg/detectors/okta/okta.go b/pkg/detectors/okta/okta.goindex 27e22d2c6..0312fe411 100644--- a/pkg/detectors/okta/okta.go+++ b/pkg/detectors/okta/okta.go@@ -20,12 +20,18 @@ type Scanner struct {// Ensure the Scanner satisfies the interface at compile time.var _ detectors.Detector = (*Scanner)(nil)+var _ detectors.CustomResultsCleaner = (*Scanner)(nil)var (defaultClient = detectors.DetectorHttpClientWithNoLocalAddressesdomainPat = regexp.MustCompile(`\b[a-z0-9-]{1,40}\.okta(?:preview|-emea){0,1}\.com\b`)tokenPat = regexp.MustCompile(`\b00[a-zA-Z0-9_-]{40}\b`)- // TODO: Oauth client secrets++ // Okta app/client IDs: "0oa" + 17 base62 characters.+ oauthClientIDPat = regexp.MustCompile(`\b0oa[a-zA-Z0-9]{17}\b`)+ // Delimiters consume a char instead of using \b, since \b breaks on secrets+ // starting/ending in '-' (a non-word char that's also part of the charset).+ oauthClientSecretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"client_secret", "clientsecret", "client-secret"}) + `(?:^|[^a-zA-Z0-9_-])([a-zA-Z0-9_-]{40,64})(?:[^a-zA-Z0-9_-]|$)`))// Keywords are used for efficiently pre-filtering chunks.@@ -38,7 +44,10 @@ func (s Scanner) Keywords() []string {func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {dataStr := string(data)- var uniqueTokens, uniqueDomains = make(map[string]struct{}), make(map[string]struct{})+ uniqueTokens := make(map[string]struct{})+ uniqueDomains := make(map[string]struct{})+ uniqueClientIDs := make(map[string]struct{})+ uniqueClientSecrets := make(map[string]struct{})for _, matches := range tokenPat.FindAllStringSubmatch(dataStr, -1) {uniqueTokens[matches[0]] = struct{}{}@@ -48,11 +57,20 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultuniqueDomains[matches[0]] = struct{}{}}+ for _, matches := range oauthClientIDPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueClientIDs[matches[0]] = struct{}{}+ }++ for _, matches := range oauthClientSecretPat.FindAllStringSubmatch(dataStr, -1) {+ uniqueClientSecrets[matches[1]] = struct{}{}+ }+for token := range uniqueTokens {for domain := range uniqueDomains {s1 := detectors.Result{DetectorType: detector_typepb.DetectorType_Okta,Raw: []byte(token),+ Redacted: fmt.Sprintf("%s:%s...%s", domain, token[:4], token[len(token)-4:]),SecretParts: map[string]string{"domain": domain,"token": token,@@ -75,6 +93,37 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result}}+ for clientID := range uniqueClientIDs {+ for clientSecret := range uniqueClientSecrets {+ for domain := range uniqueDomains {+ s2 := detectors.Result{+ DetectorType: detector_typepb.DetectorType_Okta,+ Raw: []byte(clientSecret),+ Redacted: fmt.Sprintf("%s:%s", domain, clientID),+ SecretParts: map[string]string{+ "domain": domain,+ "client_id": clientID,+ "client_secret": clientSecret,+ },+ RawV2: []byte(fmt.Sprintf("%s:%s:%s", domain, clientID, clientSecret)),+ }++ if verify {+ client := s.client+ if client == nil {+ client = defaultClient+ }++ isVerified, verificationErr := verifyOktaOAuthClientCredentials(ctx, client, domain, clientID, clientSecret)+ s2.Verified = isVerified+ s2.SetVerificationError(verificationErr)+ }++ results = append(results, s2)+ }+ }+ }+return}@@ -111,6 +160,89 @@ func verifyOktaToken(ctx context.Context, client *http.Client, domain, token str}}+func verifyOktaOAuthClientCredentials(ctx context.Context, client *http.Client, domain, clientID, clientSecret string) (bool, error) {+ // curl -v -X POST \+ // -H "Accept: application/json" \+ // -H "Content-Type: application/x-www-form-urlencoded" \+ // -u "clientID:clientSecret" \+ // -d "grant_type=client_credentials" \+ // "https://subdomain.okta.com/oauth2/default/v1/token"++ const body = "grant_type=client_credentials"++ url := fmt.Sprintf("https://%s/oauth2/default/v1/token", domain)+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, strings.NewReader(body))+ if err != nil {+ return false, err+ }+ req.SetBasicAuth(clientID, clientSecret)+ req.Header.Set("Accept", "application/json")+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")++ resp, err := client.Do(req)+ if err != nil {+ return false, err+ }+ defer func() { _ = resp.Body.Close() }()++ switch resp.StatusCode {+ case http.StatusOK:+ respBody, err := io.ReadAll(resp.Body)+ if err != nil {+ return false, err+ }+ return strings.Contains(string(respBody), "\"access_token\""), nil+ case http.StatusBadRequest:+ respBody, err := io.ReadAll(resp.Body)+ if err != nil {+ return false, err+ }+ // Okta authenticates the client before grant-type checks, so "unauthorized_client" still confirms the secret.+ if strings.Contains(string(respBody), "unauthorized_client") {+ return true, nil+ }+ if strings.Contains(string(respBody), "invalid_client") {+ return false, nil+ }+ // Other 400s (e.g. invalid_scope) say nothing about the secret; surface as an error.+ return false, fmt.Errorf("response body missing expected keyword")+ case http.StatusUnauthorized:+ return false, nil+ default:+ return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)+ }+}++// CleanResults keeps the best (preferably verified) result per distinct secret+// identity (Redacted), rather than collapsing unrelated SSWS and OAuth findings+// down to a single arbitrary result the way detectors.CleanResults would.+func (s Scanner) CleanResults(results []detectors.Result, _ bool) []detectors.Result {+ if len(results) == 0 {+ return results+ }++ byIdentity := make(map[string]detectors.Result, len(results))+ for _, r := range results {+ if r.Verified {+ byIdentity[r.Redacted] = r+ continue+ }+ if _, exists := byIdentity[r.Redacted]; !exists {+ byIdentity[r.Redacted] = r+ }+ }++ cleaned := make([]detectors.Result, 0, len(byIdentity))+ for _, r := range byIdentity {+ cleaned = append(cleaned, r)+ }+ return cleaned+}++func (s Scanner) ShouldCleanResultsIrrespectiveOfConfiguration() bool {+ return false+}+func (s Scanner) Type() detector_typepb.DetectorType {return detector_typepb.DetectorType_Okta}diff --git a/pkg/detectors/okta/okta_test.go b/pkg/detectors/okta/okta_test.goindex c8fc81a61..f1ed3726f 100644--- a/pkg/detectors/okta/okta_test.go+++ b/pkg/detectors/okta/okta_test.go@@ -7,16 +7,27 @@ import ("github.com/google/go-cmp/cmp"+ "github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/detectors""github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick")var (- validDomain = "a254u0kjk-y64jh762weozo374t.okta.com"- invalidDomain = "a254u0kjk-y64jh762?eozo374t.okta.com"- validToken = "00JIBRGk12UbkjQaaw4L7VYy9EE8zAEEkIeqGNcSQm"- invalidToken = "00JIBRGk12UbkjQaaw4L7?Yy9EE8zAEEkIeqGNcSQm"- keyword = "okta"+ validDomain = "a254u0kjk-y64jh762weozo374t.okta.com"+ invalidDomain = "a254u0kjk-y64jh762?eozo374t.okta.com"+ validToken = "00JIBRGk12UbkjQaaw4L7VYy9EE8zAEEkIeqGNcSQm"+ invalidToken = "00JIBRGk12UbkjQaaw4L7?Yy9EE8zAEEkIeqGNcSQm"+ validClientID = "0oakDa9U4UqGWlG6g3Ot"+ invalidClientID = "0obkDa9U4UqGWlG6g3Ot"+ validClientSecret = "OhbVrpoiVgRV5IfLBcbfnoGMbJmTPSIAoCLrZ3aW"+ invalidClientSecret = "OhbVrpoiVgRV5IfLBcbfnoGMbJmTPSIAoCLrZ3a?"+ // Regression fixtures for a boundary bug where `\b` around a charset that includes+ // '-' either failed to match, or silently dropped a leading/trailing hyphen.+ leadingHyphenSecret40 = "-u8jzPde0IgxLd6GncfBAepfJBd0Kh8oOOL8dKLz"+ trailingHyphenSecret40 = "u8jzPde0IgxLd6GncfBAepfJBd0Kh8oOOL8dKLz-"+ leadingHyphenSecret41 = "-u8jzPde0IgxLd6GncfBAepfJBd0Kh8oOOL8dKLzd"+ trailingHyphenSecret41 = "u8jzPde0IgxLd6GncfBAepfJBd0Kh8oOOL8dKLzd-"+ keyword = "okta")func TestOkta_Pattern(t *testing.T) {@@ -37,6 +48,36 @@ func TestOkta_Pattern(t *testing.T) {input: fmt.Sprintf("%s token - '%s'\n%s token - '%s'\n", keyword, invalidDomain, keyword, invalidToken),want: []string{},},+ {+ name: "valid pattern - oauth client credentials",+ input: fmt.Sprintf("%s domain - '%s'\n%s client_id - '%s'\nclient_secret: '%s'\n", keyword, validDomain, keyword, validClientID, validClientSecret),+ want: []string{validDomain + ":" + validClientID + ":" + validClientSecret},+ },+ {+ name: "invalid pattern - oauth client credentials",+ input: fmt.Sprintf("%s domain - '%s'\n%s client_id - '%s'\nclient_secret: '%s'\n", keyword, validDomain, keyword, invalidClientID, invalidClientSecret),+ want: []string{},+ },+ {+ name: "valid pattern - oauth client_secret (40 chars) starting with hyphen",+ input: fmt.Sprintf("%s domain - '%s'\n%s client_id - '%s'\nclient_secret: '%s'\n", keyword, validDomain, keyword, validClientID, leadingHyphenSecret40),+ want: []string{validDomain + ":" + validClientID + ":" + leadingHyphenSecret40},+ },+ {+ name: "valid pattern - oauth client_secret (40 chars) ending with hyphen",+ input: fmt.Sprintf("%s domain - '%s'\n%s client_id - '%s'\nclient_secret: '%s'\n", keyword, validDomain, keyword, validClientID, trailingHyphenSecret40),+ want: []string{validDomain + ":" + validClientID + ":" + trailingHyphenSecret40},+ },+ {+ name: "valid pattern - oauth client_secret (41 chars) starting with hyphen",+ input: fmt.Sprintf("%s domain - '%s'\n%s client_id - '%s'\nclient_secret: '%s'\n", keyword, validDomain, keyword, validClientID, leadingHyphenSecret41),+ want: []string{validDomain + ":" + validClientID + ":" + leadingHyphenSecret41},+ },+ {+ name: "valid pattern - oauth client_secret (41 chars) ending with hyphen",+ input: fmt.Sprintf("%s domain - '%s'\n%s client_id - '%s'\nclient_secret: '%s'\n", keyword, validDomain, keyword, validClientID, trailingHyphenSecret41),+ want: []string{validDomain + ":" + validClientID + ":" + trailingHyphenSecret41},+ },}for _, test := range tests {@@ -81,3 +122,139 @@ func TestOkta_Pattern(t *testing.T) {})}}++func TestOkta_OAuthVerification(t *testing.T) {+ tests := []struct {+ name string+ statusCode int+ body string+ wantVerified bool+ wantErr bool+ }{+ {+ name: "200 OK with access token is verified",+ statusCode: 200,+ body: `{"access_token":"eyJhbGciOiJIUzI1NiJ9...","token_type":"Bearer","expires_in":3600}`,+ wantVerified: true,+ },+ {+ name: "400 unauthorized_client (valid secret, grant type not enabled) is verified",+ statusCode: 400,+ body: `{"error":"unauthorized_client","error_description":"The client is not authorized to use the provided grant type."}`,+ wantVerified: true,+ },+ {+ name: "400 invalid_client (wrong secret) is unverified",+ statusCode: 400,+ body: `{"error":"invalid_client","error_description":"The client secret supplied for a confidential client is invalid."}`,+ wantVerified: false,+ },+ {+ name: "400 invalid_client (nonexistent client id) is unverified",+ statusCode: 400,+ body: `{"errorCode":"invalid_client","errorSummary":"Invalid value for 'client_id' parameter."}`,+ wantVerified: false,+ },+ {+ name: "400 without recognized keyword (e.g. invalid_scope) returns verification error",+ statusCode: 400,+ body: `{"error":"invalid_scope","error_description":"The authorization server resource does not have any configured default scopes."}`,+ wantVerified: false,+ wantErr: true,+ },+ {+ name: "401 is unverified",+ statusCode: 401,+ body: ``,+ wantVerified: false,+ },+ {+ name: "unexpected status code returns verification error",+ statusCode: 500,+ body: ``,+ wantVerified: false,+ wantErr: true,+ },+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ s := Scanner{client: common.ConstantResponseHttpClient(test.statusCode, test.body)}+ data := []byte(fmt.Sprintf("%s domain - '%s'\n%s client_id - '%s'\nclient_secret: '%s'\n", keyword, validDomain, keyword, validClientID, validClientSecret))++ results, err := s.FromData(context.Background(), true, data)+ if err != nil {+ t.Fatalf("FromData() unexpected error = %v", err)+ }+ if len(results) != 1 {+ t.Fatalf("expected 1 result, got %d", len(results))+ }++ if results[0].Verified != test.wantVerified {+ t.Errorf("Verified = %v, want %v", results[0].Verified, test.wantVerified)+ }+ if gotErr := results[0].VerificationError() != nil; gotErr != test.wantErr {+ t.Errorf("verification error present = %v, want %v (err = %v)", gotErr, test.wantErr, results[0].VerificationError())+ }+ })+ }+}++// TestOkta_CleanResults is a regression test: the generic detectors.CleanResults+// collapses an entire result set down to results[:1] whenever nothing verifies, which+// would silently drop a real OAuth client_secret finding in favor of an unrelated+// unverified SSWS-token finding from the same chunk (or vice versa). Okta's custom+// CleanResults must instead keep one result per distinct secret identity (Redacted).+func TestOkta_CleanResults(t *testing.T) {+ s := Scanner{}++ t.Run("distinct unverified findings both survive", func(t *testing.T) {+ tokenResult := detectors.Result{Redacted: "domain:token-identity", Raw: []byte(validToken)}+ oauthResult := detectors.Result{Redacted: "domain:client-id-identity", Raw: []byte(validClientSecret)}++ cleaned := s.CleanResults([]detectors.Result{tokenResult, oauthResult}, true)+ if len(cleaned) != 2 {+ t.Fatalf("expected both distinct findings to survive cleaning, got %d: %+v", len(cleaned), cleaned)+ }+ })++ t.Run("verified duplicate wins over unverified duplicate with the same identity", func(t *testing.T) {+ unverifiedDup := detectors.Result{Redacted: "same-identity", Verified: false, Raw: []byte("a")}+ verifiedDup := detectors.Result{Redacted: "same-identity", Verified: true, Raw: []byte("b")}++ cleaned := s.CleanResults([]detectors.Result{unverifiedDup, verifiedDup}, true)+ if len(cleaned) != 1 || !cleaned[0].Verified {+ t.Fatalf("expected the single verified duplicate to win, got %+v", cleaned)+ }+ })+}++// TestOkta_FromData_DistinctRedacted confirms FromData itself assigns non-empty,+// distinct Redacted identities to the token and OAuth result shapes, which is what+// TestOkta_CleanResults relies on to avoid conflating unrelated findings.+func TestOkta_FromData_DistinctRedacted(t *testing.T) {+ d := Scanner{}+ data := []byte(fmt.Sprintf(+ "%s token - '%s'\n%s domain - '%s'\n%s client_id - '%s'\nclient_secret: '%s'\n",+ keyword, validToken, keyword, validDomain, keyword, validClientID, validClientSecret,+ ))++ results, err := d.FromData(context.Background(), false, data)+ if err != nil {+ t.Fatalf("FromData() unexpected error = %v", err)+ }+ if len(results) != 2 {+ t.Fatalf("expected 2 results (token + oauth), got %d: %+v", len(results), results)+ }++ seen := make(map[string]struct{}, len(results))+ for _, r := range results {+ if r.Redacted == "" {+ t.Errorf("result has empty Redacted identity: %+v", r)+ }+ seen[r.Redacted] = struct{}{}+ }+ if len(seen) != len(results) {+ t.Errorf("expected distinct Redacted identities per result, got %v", seen)+ }+}