Skip to content

Find, verify, and analyze leaked credentials

Changed GithubOauth2 detector to only rotate on particular error message (#5396)

Jordan Tunstill0e427395344c
diff --git a/pkg/detectors/github_oauth2/github_oauth2.go b/pkg/detectors/github_oauth2/github_oauth2.go
index 8f18d4a28..fec9597ed 100644
--- a/pkg/detectors/github_oauth2/github_oauth2.go
+++ b/pkg/detectors/github_oauth2/github_oauth2.go
@@ -36,7 +36,9 @@ var (
)
const (
- githubBadVerificationCodeError = "bad_verification_code"
+ githubBadVerificationCodeError = "bad_verification_code"
+ githubIncorrectClientCredentialsError = "incorrect_client_credentials"
+ githubClientNotFoundError = "404 Not Found"
)
// Keywords are used for efficiently pre-filtering chunks.
@@ -95,11 +97,22 @@ func (s Scanner) VerifyResult(ctx context.Context, result *detectors.Result) {
}
_, err := clientCredentials.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-code
- if 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 unverified
+ // a valid pair is rejected with bad_verification_code and an invalid pair with
+ // incorrect_client_credentials.
+ // docs: https://docs.github.com/en/apps/oauth-apps/maintaining-oauth-apps/troubleshooting-oauth-app-access-token-request-errors
+ if err == nil {
+ return
+ }
+
+ errMsg := err.Error()
+ switch {
+ case strings.Contains(errMsg, githubBadVerificationCodeError):
result.Verified = true
+ case strings.Contains(errMsg, githubIncorrectClientCredentialsError),
+ strings.Contains(errMsg, githubClientNotFoundError):
+ // dead credential, leave unverified with no error
+ default:
+ result.SetVerificationError(err, clientSecret)
}
}
diff --git a/pkg/detectors/github_oauth2/github_oauth2_test.go b/pkg/detectors/github_oauth2/github_oauth2_test.go
index daf072bed..fe3ed0f42 100644
--- a/pkg/detectors/github_oauth2/github_oauth2_test.go
+++ b/pkg/detectors/github_oauth2/github_oauth2_test.go
@@ -90,11 +90,12 @@ func TestGithubOAuth2_VerifyResult(t *testing.T) {
)
tests := []struct {
- name string
- status int
- contentType string
- body string
- wantVerified bool
+ name string
+ status int
+ contentType string
+ body string
+ wantVerified bool
+ wantVerificationError bool
}{
{
// GitHub answers a valid ID/secret pair with bad_verification_code, because the
@@ -107,6 +108,13 @@ func TestGithubOAuth2_VerifyResult(t *testing.T) {
body: "error=bad_verification_code&error_description=The+code+passed+is+incorrect+or+expired.",
wantVerified: true,
},
+ {
+ name: "live credential - bad_verification_code as JSON",
+ status: http.StatusOK,
+ contentType: "application/json",
+ body: `{"error":"bad_verification_code","error_description":"The code passed is incorrect or expired."}`,
+ wantVerified: true,
+ },
{
name: "dead credential - incorrect_client_credentials",
status: http.StatusUnauthorized,
@@ -115,16 +123,33 @@ func TestGithubOAuth2_VerifyResult(t *testing.T) {
wantVerified: false,
},
{
- // A transient server failure currently lands here as "not verified" rather than
- // "unknown", because verification never calls SetVerificationError. That is a
- // known false negative tracked on its own ticket; this case pins today's behavior
- // so that changing it later is deliberate rather than accidental.
- name: "server error - recorded unverified rather than unknown",
- status: http.StatusInternalServerError,
- contentType: "text/plain",
- body: "boom",
+ name: "dead credential - unknown client ID returns 404",
+ status: http.StatusNotFound,
+ contentType: "text/plain; charset=utf-8",
+ body: "Not Found",
wantVerified: false,
},
+ {
+ name: "server error - unknown",
+ status: http.StatusInternalServerError,
+ contentType: "text/plain",
+ body: "boom",
+ wantVerificationError: true,
+ },
+ {
+ name: "rate limited - unknown",
+ status: http.StatusTooManyRequests,
+ contentType: "text/html",
+ body: "<html>rate limited</html>",
+ wantVerificationError: true,
+ },
+ {
+ name: "unrecognized error code - unknown",
+ status: http.StatusOK,
+ contentType: "application/x-www-form-urlencoded",
+ body: "error=slow_down",
+ wantVerificationError: true,
+ },
}
for _, test := range tests {
@@ -160,6 +185,7 @@ func TestGithubOAuth2_VerifyResult(t *testing.T) {
Scanner{}.VerifyResult(ctx, &result)
assert.Equal(t, test.wantVerified, result.Verified)
+ assert.Equal(t, test.wantVerificationError, result.VerificationError() != nil, "verification error: %v", result.VerificationError())
// Confirms the pair travelled from SecretParts into the token request, which is
// the contract the verification cache relies on when it verifies a lone result.
assert.Equal(t, clientID, gotID)