Skip to content

Find, verify, and analyze leaked credentials

[fix] False Positive Verification in Auth0oauth Detectors (#3901)

Abdul Basit985eb75a4692
diff --git a/pkg/detectors/auth0oauth/auth0oauth.go b/pkg/detectors/auth0oauth/auth0oauth.go
index b6c074b9b..543fa060b 100644
--- a/pkg/detectors/auth0oauth/auth0oauth.go
+++ b/pkg/detectors/auth0oauth/auth0oauth.go
@@ -2,6 +2,7 @@ package auth0oauth
import (
"context"
+ "fmt"
"io"
"net/http"
"net/url"
@@ -15,13 +16,14 @@ import (
type Scanner struct {
detectors.DefaultMultiPartCredentialProvider
+ client *http.Client
}
// Ensure the Scanner satisfies the interface at compile time.
var _ detectors.Detector = (*Scanner)(nil)
var (
- client = detectors.DetectorHttpClientWithLocalAddresses
+ defaultClient = detectors.DetectorHttpClientWithLocalAddresses
clientIdPat = regexp.MustCompile(detectors.PrefixRegex([]string{"auth0"}) + `\b([a-zA-Z0-9_-]{32,60})\b`)
clientSecretPat = regexp.MustCompile(`\b([a-zA-Z0-9_-]{64,})\b`)
@@ -61,42 +63,17 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
}
if verify {
- /*
- curl --request POST \
- --url 'https://YOUR_DOMAIN/oauth/token' \
- --header 'content-type: application/x-www-form-urlencoded' \
- --data 'grant_type=authorization_code&client_id=W44JmL3qD6LxHeEJyKe9lMuhcwvPOaOq&client_secret=YOUR_CLIENT_SECRET&code=AUTHORIZATION_CODE&redirect_uri=undefined'
- */
-
- data := url.Values{}
- data.Set("grant_type", "authorization_code")
- data.Set("client_id", clientIdRes)
- data.Set("client_secret", clientSecretRes)
- data.Set("code", "AUTHORIZATION_CODE")
- data.Set("redirect_uri", "undefined")
-
- req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://"+domainRes+"/oauth/token", strings.NewReader(data.Encode())) // URL-encoded payload
- if err != nil {
- continue
+
+ client := s.client
+ if client == nil {
+ client = defaultClient
}
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- res, err := client.Do(req)
- if err == nil {
- defer res.Body.Close()
- bodyBytes, err := io.ReadAll(res.Body)
- if err != nil {
- continue
- }
- body := string(bodyBytes)
-
- // if client_id and client_secret is valid -> 403 {"error":"invalid_grant","error_description":"Invalid authorization code"}
- // if invalid -> 401 {"error":"access_denied","error_description":"Unauthorized"}
- // ingenious!
-
- if !strings.Contains(body, "access_denied") {
- s1.Verified = true
- }
+
+ isVerified, err := verifyTuple(ctx, client, domainRes, clientIdRes, clientSecretRes)
+ if err != nil {
+ s1.SetVerificationError(err, clientIdRes)
}
+ s1.Verified = isVerified
}
results = append(results, s1)
@@ -107,6 +84,61 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
return results, nil
}
+func verifyTuple(ctx context.Context, client *http.Client, domainRes, clientId, clientSecret string) (bool, error) {
+ /*
+ curl --request POST \
+ --url 'https://YOUR_DOMAIN/oauth/token' \
+ --header 'content-type: application/x-www-form-urlencoded' \
+ --data 'grant_type=authorization_code&client_id=W44JmL3qD6LxHeEJyKe9lMuhcwvPOaOq&client_secret=YOUR_CLIENT_SECRET&code=AUTHORIZATION_CODE&redirect_uri=undefined'
+ */
+
+ data := url.Values{}
+ data.Set("grant_type", "authorization_code")
+ data.Set("client_id", clientId)
+ data.Set("client_secret", clientSecret)
+ data.Set("code", "AUTHORIZATION_CODE")
+ data.Set("redirect_uri", "undefined")
+
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://"+domainRes+"/oauth/token", strings.NewReader(data.Encode())) // URL-encoded payload
+ if err != nil {
+ return false, err
+ }
+ req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
+ resp, err := client.Do(req)
+ if err != nil {
+ return false, err
+ }
+
+ defer func() {
+ _, _ = io.Copy(io.Discard, resp.Body)
+ _ = resp.Body.Close()
+ }()
+
+ switch resp.StatusCode {
+ case http.StatusOK:
+ // This condition will never meet due to invalid request body
+ return true, nil
+ case http.StatusUnauthorized:
+ return false, nil
+ case http.StatusForbidden:
+ // cross check about 'invalid_grant' or 'unauthorized_client' in response body
+ bodyBytes, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return false, err
+ }
+ bodyStr := string(bodyBytes)
+ if strings.Contains(bodyStr, "invalid_grant") || strings.Contains(bodyStr, "unauthorized_client") {
+ return true, nil
+ }
+ return false, nil
+ case http.StatusNotFound:
+ // domain does not exists - 404 not found
+ return false, nil
+ default:
+ return false, fmt.Errorf("unexpected HTTP response status %d", resp.StatusCode)
+ }
+}
+
func (s Scanner) Type() detectorspb.DetectorType {
return detectorspb.DetectorType_Auth0oauth
}
diff --git a/pkg/detectors/auth0oauth/auth0oauth_integeration_test.go b/pkg/detectors/auth0oauth/auth0oauth_integeration_test.go
index 2363df8aa..2a6780301 100644
--- a/pkg/detectors/auth0oauth/auth0oauth_integeration_test.go
+++ b/pkg/detectors/auth0oauth/auth0oauth_integeration_test.go
@@ -10,23 +10,29 @@ import (
"time"
"github.com/kylelemons/godebug/pretty"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
- "github.com/trufflesecurity/trufflehog/v3/pkg/common"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
func TestAuth0oauth_FromChunk(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
- testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors3")
+ testSecrets, err := common.GetSecret(ctx, "trufflehog-testing", "detectors5")
if err != nil {
t.Fatalf("could not get test secrets from GCP: %s", err)
}
domain := testSecrets.MustGetField("AUTH0_DOMAIN")
- clientId := testSecrets.MustGetField("AUTH0_CLIENT_ID") // there is AUTH0_CLIENT_ID2 and AUTH0_CLIENT_SECRET2 pair as well
+ clientId := testSecrets.MustGetField("AUTH0_CLIENT_ID")
clientSecret := testSecrets.MustGetField("AUTH0_CLIENT_SECRET")
+
+ domainUnauthorized := testSecrets.MustGetField("AUTH0_DOMAIN_UNAUTHORIZED")
+ clientIdUnauthorized := testSecrets.MustGetField("AUTH0_CLIENT_ID_UNAUTHORIZED")
+ clientSecretUnauthorized := testSecrets.MustGetField("AUTH0_CLIENT_SECRET_UNAUTHORIZED")
+
+ notFoundDomain := testSecrets.MustGetField("AUTH0_DOMAIN_NOT_FOUND")
inactiveClientSecret := testSecrets.MustGetField("AUTH0_CLIENT_SECRET_INACTIVE")
type args struct {
@@ -58,6 +64,23 @@ func TestAuth0oauth_FromChunk(t *testing.T) {
},
wantErr: false,
},
+ {
+ name: "found, verified but unauthorized",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a auth0 client id %s client secret %s domain %s", clientIdUnauthorized, clientSecretUnauthorized, domainUnauthorized)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Auth0oauth,
+ Redacted: clientIdUnauthorized,
+ Verified: true,
+ },
+ },
+ wantErr: false,
+ },
{
name: "found, unverified",
s: Scanner{},
@@ -86,6 +109,23 @@ func TestAuth0oauth_FromChunk(t *testing.T) {
want: nil,
wantErr: false,
},
+ {
+ name: "domain does not exists",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a auth0 client id %s client secret %s domain %s", clientId, clientSecret, notFoundDomain)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_Auth0oauth,
+ Redacted: clientId,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
diff --git a/pkg/engine/defaults/defaults.go b/pkg/engine/defaults/defaults.go
index d7bc25e2a..47dc3e43f 100644
--- a/pkg/engine/defaults/defaults.go
+++ b/pkg/engine/defaults/defaults.go
@@ -54,6 +54,7 @@ import (
atlassianv2 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/atlassian/v2"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/audd"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/auth0managementapitoken"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/auth0oauth"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/autodesk"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/autoklose"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/autopilot"
@@ -882,7 +883,7 @@ func buildDetectorList() []detectors.Detector {
&atlassianv2.Scanner{},
&audd.Scanner{},
&auth0managementapitoken.Scanner{},
- // &auth0oauth.Scanner{},
+ &auth0oauth.Scanner{},
&autodesk.Scanner{},
&autoklose.Scanner{},
&autopilot.Scanner{},