Skip to content

Find, verify, and analyze leaked credentials

add tri-state verification to mongodb detector (#1575)

Cody Rose7d2f126411ed
diff --git a/pkg/detectors/mongodb/mongodb.go b/pkg/detectors/mongodb/mongodb.go
index 80546e6f7..a9832123e 100644
--- a/pkg/detectors/mongodb/mongodb.go
+++ b/pkg/detectors/mongodb/mongodb.go
@@ -2,6 +2,8 @@ package mongodb
import (
"context"
+ "go.mongodb.org/mongo-driver/x/mongo/driver/auth"
+ "go.mongodb.org/mongo-driver/x/mongo/driver/topology"
"regexp"
"strings"
"time"
@@ -14,12 +16,15 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
-type Scanner struct{}
+type Scanner struct {
+ timeout time.Duration // Zero value means "default timeout"
+}
// Ensure the Scanner satisfies the interface at compile time.
var _ detectors.Detector = (*Scanner)(nil)
var (
+ defaultTimeout = 2 * time.Second
// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
keyPat = regexp.MustCompile(`\b(mongodb(\+srv)?://[\S]{3,50}:([\S]{3,88})@[-.%\w\/:]+)\b`)
// TODO: Add support for sharded cluster, replica set and Atlas Deployment.
@@ -46,23 +51,15 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
}
if verify {
- func() {
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
- defer cancel()
- client, err := mongo.Connect(ctx, options.Client().ApplyURI(resMatch))
- if err != nil {
- return
- }
- defer func() {
- if err := client.Disconnect(ctx); err != nil {
- return
- }
- }()
- if err := client.Ping(ctx, readpref.Primary()); err != nil {
- return
- }
- s1.Verified = true
- }()
+ timeout := s.timeout
+ if timeout == 0 {
+ timeout = defaultTimeout
+ }
+ err := verifyUri(resMatch, timeout)
+ s1.Verified = err == nil
+ if !isErrDeterminate(err) {
+ s1.VerificationError = err
+ }
}
results = append(results, s1)
}
@@ -70,6 +67,33 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
return results, nil
}
+func isErrDeterminate(err error) bool {
+ switch e := err.(type) {
+ case topology.ConnectionError:
+ switch e.Unwrap().(type) {
+ case *auth.Error:
+ return true
+ default:
+ return false
+ }
+ default:
+ return false
+ }
+}
+
+func verifyUri(uri string, timeout time.Duration) error {
+ ctx, cancel := context.WithTimeout(context.Background(), timeout)
+ defer cancel()
+ client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
+ if err != nil {
+ return err
+ }
+ defer func() {
+ _ = client.Disconnect(ctx)
+ }()
+ return client.Ping(ctx, readpref.Primary())
+}
+
func (s Scanner) Type() detectorspb.DetectorType {
return detectorspb.DetectorType_MongoDB
}
diff --git a/pkg/detectors/mongodb/mongodb_test.go b/pkg/detectors/mongodb/mongodb_test.go
index 5c6fa5204..4c805c9b8 100644
--- a/pkg/detectors/mongodb/mongodb_test.go
+++ b/pkg/detectors/mongodb/mongodb_test.go
@@ -6,6 +6,7 @@ package mongodb
import (
"context"
"fmt"
+ "strings"
"testing"
"time"
@@ -34,11 +35,12 @@ func TestMongoDB_FromChunk(t *testing.T) {
verify bool
}
tests := []struct {
- name string
- s Scanner
- args args
- want []detectors.Result
- wantErr bool
+ name string
+ s Scanner
+ args args
+ want []detectors.Result
+ wantErr bool
+ wantVerificationErr bool
}{
{
name: "found, verified",
@@ -72,6 +74,40 @@ func TestMongoDB_FromChunk(t *testing.T) {
},
wantErr: false,
},
+ {
+ name: "found, would be verified but for connection timeout",
+ s: Scanner{timeout: 1 * time.Microsecond},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a mongodb secret %s within", secret)),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_MongoDB,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ wantVerificationErr: true,
+ },
+ {
+ name: "found, bad host",
+ s: Scanner{},
+ args: args{
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf("You can find a mongodb secret %s within", strings.ReplaceAll(secret, ".mongodb.net", ".mongodb.net.bad"))),
+ verify: true,
+ },
+ want: []detectors.Result{
+ {
+ DetectorType: detectorspb.DetectorType_MongoDB,
+ Verified: false,
+ },
+ },
+ wantErr: false,
+ wantVerificationErr: true,
+ },
{
name: "not found",
s: Scanner{},
@@ -86,8 +122,7 @@ func TestMongoDB_FromChunk(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- s := Scanner{}
- got, err := s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
+ got, err := tt.s.FromData(tt.args.ctx, tt.args.verify, tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("MongoDB.FromData() error = %v, wantErr %v", err, tt.wantErr)
return
@@ -97,8 +132,11 @@ func TestMongoDB_FromChunk(t *testing.T) {
t.Fatalf("no raw secret present: \n %+v", got[i])
}
got[i].Raw = nil
+ if (got[i].VerificationError != nil) != tt.wantVerificationErr {
+ t.Fatalf("wantVerificationErr = %v, verification error = %v", tt.wantVerificationErr, got[i].VerificationError)
+ }
}
- ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "RawV2")
+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "RawV2", "VerificationError")
if diff := cmp.Diff(tt.want, got, ignoreOpts); diff != "" {
t.Errorf("MongoDB.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
}