Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Close Couchbase clusters after verification and honor the context (#5489)
Dave Hubbard69734591624d
diff --git a/pkg/detectors/couchbase/couchbase.go b/pkg/detectors/couchbase/couchbase.goindex 930494c68..2c4de0b03 100644--- a/pkg/detectors/couchbase/couchbase.go+++ b/pkg/detectors/couchbase/couchbase.go@@ -78,7 +78,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result}if verify {- isVerified, verificationErr := verifyCouchBase(username, password, connString)+ isVerified, verificationErr := verifyCouchBase(ctx, username, password, connString)s1.Verified = isVerifieds1.SetVerificationError(verificationErr)s1.SetPrimarySecretValue(connString)@@ -91,7 +91,13 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (resultreturn results, nil}-func verifyCouchBase(username, password, connString string) (bool, error) {+// verifyCouchBase reports whether the credentials reach a healthy endpoint on the cluster.+//+// Every gocb.Cluster owns background goroutines and sockets until Close is called, so the+// cluster is closed on every return path; otherwise each verification leaks a client for the+// life of the process. The caller's ctx bounds the ping, which is where verification spends+// nearly all of its time, so the engine's detector timeout actually stops verification.+func verifyCouchBase(ctx context.Context, username, password, connString string) (bool, error) {options := gocb.ClusterOptions{Authenticator: gocb.PasswordAuthenticator{Username: username,@@ -106,17 +112,25 @@ func verifyCouchBase(username, password, connString string) (bool, error) {return false, err}- // Initialize the Connection+ // gocb.Connect takes no context, so check ctx before building a client. FromData verifies+ // candidates serially, and once ctx ends there is no point connecting for the rest.+ if err := ctx.Err(); err != nil {+ return false, err+ }+cluster, err := gocb.Connect(connString, options)if err != nil {return false, err}+ defer func() { _ = cluster.Close(nil) }()- // We'll ping the KV nodes in our cluster.+ // Ping the cluster's services. The ping ends at whichever comes first: its own timeout or+ // the caller's ctx. Either way gocb reports the outcome per endpoint rather than as an+ // error, so a ping cut short by ctx looks like a cluster with no healthy endpoint.pings, err := cluster.Ping(&gocb.PingOptions{Timeout: time.Second * 5,+ Context: ctx,})-if err != nil {return false, err}@@ -129,6 +143,11 @@ func verifyCouchBase(username, password, connString string) (bool, error) {}}+ // No healthy endpoint is only a determinate "unverified" if the ping ran to completion.+ // When ctx ended first, report its error so the result is marked indeterminate instead.+ if err := ctx.Err(); err != nil {+ return false, err+ }return false, nil}diff --git a/pkg/detectors/couchbase/couchbase_integration_test.go b/pkg/detectors/couchbase/couchbase_integration_test.goindex 999a424a0..202b76914 100644--- a/pkg/detectors/couchbase/couchbase_integration_test.go+++ b/pkg/detectors/couchbase/couchbase_integration_test.go@@ -99,8 +99,8 @@ func TestCouchbase_FromChunk(t *testing.T) {t.Fatalf("no raw secret present: \n %+v", got[i])}}- ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "verificationError", "primarySecret")- if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "SecretParts")+ if diff := cmp.Diff(got, tt.want, ignoreOpts, cmpopts.IgnoreUnexported(detectors.Result{})); diff != "" {t.Errorf("Couchbase.FromData() %s diff: (-got +want)\n%s", tt.name, diff)}})diff --git a/pkg/detectors/couchbase/couchbase_test.go b/pkg/detectors/couchbase/couchbase_test.goindex 267f95437..f3a9e2393 100644--- a/pkg/detectors/couchbase/couchbase_test.go+++ b/pkg/detectors/couchbase/couchbase_test.go@@ -1,8 +1,13 @@package couchbaseimport (+ "bytes""context"+ "errors"+ "runtime/pprof"+ "strings""testing"+ "time""github.com/google/go-cmp/cmp"@@ -10,6 +15,89 @@ import ("github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick")+// unreachableConnString points gocb at a local port nothing listens on. gocb.Connect succeeds+// immediately (it bootstraps in the background) and pings never find a healthy endpoint, which+// exercises the full connect, ping, close lifecycle without network access or a real cluster.+const unreachableConnString = "couchbases://127.0.0.1:1"++// gocbGoroutines counts live goroutines whose stacks run through gocb or gocbcore. Each open+// gocb.Cluster keeps several of these alive until Close, so a nonzero count after verification+// returns means a client leaked.+func gocbGoroutines() int {+ var buf bytes.Buffer+ _ = pprof.Lookup("goroutine").WriteTo(&buf, 2)+ count := 0+ for _, stack := range strings.Split(buf.String(), "\n\n") {+ if strings.Contains(stack, "github.com/couchbase/gocb") {+ count+++ }+ }+ return count+}++// requireNoGocbGoroutines waits briefly for Close to finish tearing down background goroutines,+// then fails if any gocb goroutines remain.+func requireNoGocbGoroutines(t *testing.T) {+ t.Helper()+ deadline := time.Now().Add(5 * time.Second)+ for gocbGoroutines() > 0 && time.Now().Before(deadline) {+ time.Sleep(50 * time.Millisecond)+ }+ if n := gocbGoroutines(); n > 0 {+ t.Fatalf("%d gocb goroutines still running; a cluster was not closed", n)+ }+}++func TestVerifyCouchBase_ContextCancellationStopsVerificationAndClosesCluster(t *testing.T) {+ ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)+ defer cancel()++ start := time.Now()+ verified, err := verifyCouchBase(ctx, "user", "Passw0rd!", unreachableConnString)+ elapsed := time.Since(start)++ if verified {+ t.Errorf("expected unverified result against an unreachable cluster")+ }+ if !errors.Is(err, context.DeadlineExceeded) {+ t.Errorf("expected context.DeadlineExceeded, got %v", err)+ }+ // The ping's own timeout is 5s; finishing well under it shows ctx ended the ping.+ if elapsed > 2*time.Second {+ t.Errorf("verification took %s after ctx expired at 200ms", elapsed)+ }+ requireNoGocbGoroutines(t)+}++func TestVerifyCouchBase_ClosesClusterWhenNoEndpointIsHealthy(t *testing.T) {+ verified, err := verifyCouchBase(context.Background(), "user", "Passw0rd!", unreachableConnString)+ if verified {+ t.Errorf("expected unverified result against an unreachable cluster")+ }+ if err != nil {+ t.Errorf("expected no error when ping completes without a healthy endpoint, got %v", err)+ }+ requireNoGocbGoroutines(t)+}++func TestVerifyCouchBase_EndedContextSkipsConnecting(t *testing.T) {+ ctx, cancel := context.WithCancel(context.Background())+ cancel()++ verified, err := verifyCouchBase(ctx, "user", "Passw0rd!", unreachableConnString)+ if verified {+ t.Errorf("expected unverified result when ctx has already ended")+ }+ if !errors.Is(err, context.Canceled) {+ t.Errorf("expected context.Canceled, got %v", err)+ }+ // Checked immediately rather than with requireNoGocbGoroutines: no client should ever have+ // been built, so there is nothing to wait for.+ if n := gocbGoroutines(); n > 0 {+ t.Errorf("%d gocb goroutines running; a client was built after ctx ended", n)+ }+}+func TestCouchBase_Pattern(t *testing.T) {d := Scanner{}ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d})