Skip to content

Find, verify, and analyze leaked credentials

Int 595 auth errors (#5259)

Shahzad Haider449d8a369cc6
diff --git a/pkg/detectors/gitlab/v1/gitlab.go b/pkg/detectors/gitlab/v1/gitlab.go
index d08cdb421..2aab2d094 100644
--- a/pkg/detectors/gitlab/v1/gitlab.go
+++ b/pkg/detectors/gitlab/v1/gitlab.go
@@ -99,7 +99,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
s1.Verified = isVerified
maps.Copy(s1.ExtraData, extraData)
- s1.SetVerificationError(verificationErr)
+ s1.SetVerificationError(verificationErr, resMatch)
// for verified keys break out of the endpoint loop to continue to next secret
if s1.Verified {
diff --git a/pkg/detectors/gitlab/v2/gitlab_v2.go b/pkg/detectors/gitlab/v2/gitlab_v2.go
index 3135171b0..30789f382 100644
--- a/pkg/detectors/gitlab/v2/gitlab_v2.go
+++ b/pkg/detectors/gitlab/v2/gitlab_v2.go
@@ -82,7 +82,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
s1.Verified = isVerified
maps.Copy(s1.ExtraData, extraData)
- s1.SetVerificationError(verificationErr)
+ s1.SetVerificationError(verificationErr, resMatch)
// for verified keys break out of the endpoint loop to continue to next secret
if s1.Verified {
diff --git a/pkg/detectors/gitlab/v3/gitlab_v3.go b/pkg/detectors/gitlab/v3/gitlab_v3.go
index bbbde5792..0a4a26633 100644
--- a/pkg/detectors/gitlab/v3/gitlab_v3.go
+++ b/pkg/detectors/gitlab/v3/gitlab_v3.go
@@ -83,7 +83,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
s1.Verified = isVerified
maps.Copy(s1.ExtraData, extraData)
- s1.SetVerificationError(verificationErr)
+ s1.SetVerificationError(verificationErr, resMatch)
// for verified keys break out of the endpoint loop to continue to next secret
if s1.Verified {
diff --git a/pkg/detectors/jdbc/jdbc.go b/pkg/detectors/jdbc/jdbc.go
index 137ac48e6..963c77be3 100644
--- a/pkg/detectors/jdbc/jdbc.go
+++ b/pkg/detectors/jdbc/jdbc.go
@@ -117,7 +117,11 @@ matchLoop:
}
}
} else if verify {
- continue
+ // The connection string could not be parsed, so the credential was never tested.
+ // That is an indeterminate outcome, not a rejection: we report it as unverified carrying the parse error.
+ // Dropping it here made the reported findings depend on whether verification was requested,
+ // since the unparseable match is kept when verify is false.
+ result.SetVerificationError(parseErr, jdbcConn)
}
results = append(results, result)
diff --git a/pkg/detectors/mongodb/mongodb.go b/pkg/detectors/mongodb/mongodb.go
index 052749a08..b94293ec9 100644
--- a/pkg/detectors/mongodb/mongodb.go
+++ b/pkg/detectors/mongodb/mongodb.go
@@ -46,7 +46,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
dataStr := string(data)
type mongoMatch struct {
- password string
+ password string
parsedURL *url.URL
}
uniqueMatches := make(map[string]mongoMatch)
@@ -102,8 +102,8 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
r := detectors.Result{
DetectorType: detector_typepb.DetectorType_MongoDB,
Raw: []byte(connStr),
- ExtraData: extraData,
- SecretParts: map[string]string{"key": connStr},
+ ExtraData: extraData,
+ SecretParts: map[string]string{"key": connStr},
}
if verify {
@@ -114,10 +114,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
isVerified, vErr := verifyUri(ctx, connStr, timeout)
r.Verified = isVerified
- if isErrDeterminate(vErr) {
- continue
+ // An auth error means the server tested the credential and rejected it. That is a determinate result,
+ // so it is reported as unverified with no verification error attached. Anything else,
+ // a timeout, an unreachable host, a malformed connection string is indeterminate and keeps its
+ // error so the credential is retried on a later scan.
+ if !isErrDeterminate(vErr) {
+ r.SetVerificationError(vErr, m.password)
}
- r.SetVerificationError(vErr, m.password)
}
results = append(results, r)
}
diff --git a/pkg/detectors/postgres/postgres.go b/pkg/detectors/postgres/postgres.go
index 68e4df53c..e52bac099 100644
--- a/pkg/detectors/postgres/postgres.go
+++ b/pkg/detectors/postgres/postgres.go
@@ -186,16 +186,21 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) ([]dete
if verify {
// pq appears to ignore the context deadline, so we copy any timeout that's been set into the connection
// parameters themselves.
- if timeout, ok := getDeadlineInSeconds(ctx); ok && timeout > 0 {
- params[pgConnectTimeout] = strconv.Itoa(timeout)
- } else if ok && timeout <= 0 {
- // Deadline in the context has already exceeded.
- break
- }
+ timeout, hasDeadline := getDeadlineInSeconds(ctx)
+ if hasDeadline && timeout <= 0 {
+ // The deadline has already passed, so this credential cannot be tested. Report it as indeterminate
+ // rather than abandoning this candidate and every remaining one. The IsDone check at the top of the
+ // loop ends the scan on the next iteration.
+ result.SetVerificationError(context.DeadlineExceeded, password)
+ } else {
+ if hasDeadline {
+ params[pgConnectTimeout] = strconv.Itoa(timeout)
+ }
- isVerified, verificationErr := verifyPostgres(ctx, params)
- result.Verified = isVerified
- result.SetVerificationError(verificationErr, password)
+ isVerified, verificationErr := verifyPostgres(ctx, params)
+ result.Verified = isVerified
+ result.SetVerificationError(verificationErr, password)
+ }
}
// We gather SSL information into ExtraData in case it's useful for later reporting.
diff --git a/pkg/detectors/uri/uri.go b/pkg/detectors/uri/uri.go
index e36ccdf56..a04ffd131 100644
--- a/pkg/detectors/uri/uri.go
+++ b/pkg/detectors/uri/uri.go
@@ -18,6 +18,8 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb"
)
+var errNoSuchHost = errors.New("no such host")
+
type Scanner struct {
allowKnownTestSites bool
client *http.Client
@@ -112,21 +114,25 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
if verify {
hostname := parsedURL.Hostname()
if hostNotFoundCache.Exists(hostname) {
- logger.V(3).Info("Skipping uri: no such host", "host", hostname)
- continue
- }
-
- if s.client == nil {
- s.client = defaultClient
- }
- isVerified, vErr := verifyURL(ctx, s.client, parsedURL)
- r.Verified = isVerified
- if vErr != nil {
- var dnsErr *net.DNSError
- if errors.As(vErr, &dnsErr) && dnsErr.IsNotFound {
- hostNotFoundCache.Set(hostname, struct{}{})
+ // An earlier candidate already proved this host does not resolve, so the lookup is skipped.
+ // The finding is still reported: dropping it here means the reported secrets depend both on
+ // whether verification was requested and on the order candidates happened to be processed,
+ // since the first candidate for the host was reported and later ones were not.
+ logger.V(3).Info("Skipping uri verification: no such host", "host", hostname)
+ r.SetVerificationError(errNoSuchHost, password)
+ } else {
+ if s.client == nil {
+ s.client = defaultClient
+ }
+ isVerified, vErr := verifyURL(ctx, s.client, parsedURL)
+ r.Verified = isVerified
+ if vErr != nil {
+ var dnsErr *net.DNSError
+ if errors.As(vErr, &dnsErr) && dnsErr.IsNotFound {
+ hostNotFoundCache.Set(hostname, struct{}{})
+ }
+ r.SetVerificationError(vErr, password)
}
- r.SetVerificationError(vErr, password)
}
}