Skip to content

Find, verify, and analyze leaked credentials

fix(detectors/anypointoauth2): stop treating HTTP 422 as verified (#5386)

James Golovich282085cab9d0
diff --git a/pkg/detectors/anypointoauth2/anypointoauth2.go b/pkg/detectors/anypointoauth2/anypointoauth2.go
index 0a951c7c5..16ef9d9d5 100644
--- a/pkg/detectors/anypointoauth2/anypointoauth2.go
+++ b/pkg/detectors/anypointoauth2/anypointoauth2.go
@@ -115,10 +115,9 @@ func verifyMatch(ctx context.Context, client *http.Client, id, secret string) (b
}()
switch res.StatusCode {
- // The endpoint responds with status 200 for valid Organization credentials and 422 for Client credentials.
- case http.StatusOK, http.StatusUnprocessableEntity:
+ case http.StatusOK:
return true, nil
- case http.StatusUnauthorized:
+ case http.StatusUnauthorized, http.StatusUnprocessableEntity:
return false, nil
default:
return false, fmt.Errorf("unexpected status code: %d", res.StatusCode)
diff --git a/pkg/detectors/anypointoauth2/anypointoauth2_test.go b/pkg/detectors/anypointoauth2/anypointoauth2_test.go
index 1f2c4d433..4b39c2bc6 100644
--- a/pkg/detectors/anypointoauth2/anypointoauth2_test.go
+++ b/pkg/detectors/anypointoauth2/anypointoauth2_test.go
@@ -2,9 +2,12 @@ package anypointoauth2
import (
"context"
+ "net/http"
+ "net/http/httptest"
"testing"
"github.com/google/go-cmp/cmp"
+ "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
@@ -112,3 +115,61 @@ func TestAnypoint_Pattern(t *testing.T) {
})
}
}
+
+func TestAnypoint_Verification(t *testing.T) {
+ tests := []struct {
+ name string
+ status int
+ wantVerified bool
+ wantErr bool
+ }{
+ {
+ name: "200 is verified",
+ status: http.StatusOK,
+ wantVerified: true,
+ },
+ {
+ name: "401 is not verified",
+ status: http.StatusUnauthorized,
+ wantVerified: false,
+ },
+ {
+ name: "422 is not verified",
+ status: http.StatusUnprocessableEntity,
+ wantVerified: false,
+ },
+ {
+ name: "500 returns error",
+ status: http.StatusInternalServerError,
+ wantErr: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(tt.status)
+ }))
+ defer server.Close()
+
+ orig := verificationUrl
+ verificationUrl = server.URL
+ t.Cleanup(func() { verificationUrl = orig })
+
+ s := Scanner{client: server.Client()}
+ results, err := s.FromData(context.Background(), true, []byte(
+ `anypoint id e3cd10a87f53b2dfa4b5fd606e7d9eca anypoint secret ACE9d7E606Df5B4AFD2B35f78A01DC3E`,
+ ))
+ require.NoError(t, err)
+ require.Len(t, results, 1)
+
+ if tt.wantErr {
+ assert.False(t, results[0].Verified)
+ assert.Error(t, results[0].VerificationError())
+ } else {
+ assert.Equal(t, tt.wantVerified, results[0].Verified)
+ assert.NoError(t, results[0].VerificationError())
+ }
+ })
+ }
+}