Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Fixed github oauth2 token detector (#3353)
Kashif Khaneffee2a912e2
diff --git a/pkg/detectors/github_oauth2/github_oauth2.go b/pkg/detectors/github_oauth2/github_oauth2.goindex 09e240fe0..6554bc1da 100644--- a/pkg/detectors/github_oauth2/github_oauth2.go+++ b/pkg/detectors/github_oauth2/github_oauth2.go@@ -23,7 +23,7 @@ var _ detectors.Detector = (*Scanner)(nil)var (// Oauth2 client ID and secret- oauth2ClientIDPat = regexp.MustCompile(detectors.PrefixRegex([]string{"github"}) + `\b([a-f0-9]{20})\b`)+ oauth2ClientIDPat = regexp.MustCompile(detectors.PrefixRegex([]string{"github"}) + `\b([a-zA-Z0-9]{20})\b`)oauth2ClientSecretPat = regexp.MustCompile(detectors.PrefixRegex([]string{"github"}) + `\b([a-f0-9]{40})\b`))@@ -70,7 +70,10 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result}if verify {_, err := config.Token(ctx)+ // if client id and client secret is correct, it will return bad verification code error as we do not pass any verification code+ // docs: https://docs.github.com/en/apps/oauth-apps/maintaining-oauth-apps/troubleshooting-oauth-app-access-token-request-errors#bad-verification-codeif err != nil && strings.Contains(err.Error(), githubBadVerificationCodeError) {+ // mark result as verified only in case of bad verification code error, for any other error the result will be unverifieds1.Verified = true}}diff --git a/pkg/detectors/github_oauth2/github_oauth2_test.go b/pkg/detectors/github_oauth2/github_oauth2_test.gonew file mode 100644index 000000000..cef1a2403--- /dev/null+++ b/pkg/detectors/github_oauth2/github_oauth2_test.go@@ -0,0 +1,74 @@+package github_oauth2++import (+ "context"+ "github.com/google/go-cmp/cmp"+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors"+ "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"+ "testing"+)++func TestGithubOAuth2_Pattern(t *testing.T) {+ d := Scanner{}+ ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})+ tests := []struct {+ name string+ input string+ want []string+ }{+ {+ name: "typical pattern - with keyword github",+ input: "github = '9c14koUc3f04PrzlpCcU', 'caa3224b5ddb83924e6a487ee3f6543bae428309'",+ want: []string{+ "9c14koUc3f04PrzlpCcUcaa3224b5ddb83924e6a487ee3f6543bae428309",+ },+ },+ {+ name: "typical pattern - invalid client ID",+ input: "github = '10c14koUc3f04PrzlpCcU', 'caa3224b5ddb83924e6a487ee3f6543bae428309'",+ want: nil,+ },+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input))+ if len(matchedDetectors) == 0 {+ t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input)+ return+ }++ results, err := d.FromData(context.Background(), false, []byte(test.input))+ if err != nil {+ t.Errorf("error = %v", err)+ return+ }++ if len(results) != len(test.want) {+ if len(results) == 0 {+ t.Errorf("did not receive result")+ } else {+ t.Errorf("expected %d results, only received %d", len(test.want), len(results))+ }+ return+ }++ actual := make(map[string]struct{}, len(results))+ for _, r := range results {+ if len(r.RawV2) > 0 {+ actual[string(r.RawV2)] = struct{}{}+ } else {+ actual[string(r.Raw)] = struct{}{}+ }+ }+ expected := make(map[string]struct{}, len(test.want))+ for _, v := range test.want {+ expected[v] = struct{}{}+ }++ if diff := cmp.Diff(expected, actual); diff != "" {+ t.Errorf("%s diff: (-want +got)\n%s", test.name, diff)+ }+ })+ }+}