Skip to content

Find, verify, and analyze leaked credentials

fix(sqlserver): use driver's default port in pre-dial check (#5527)

Jordan Tunstillf8221ca52ac5
diff --git a/pkg/detectors/sqlserver/sqlserver.go b/pkg/detectors/sqlserver/sqlserver.go
index 5466578f1..e08407a39 100644
--- a/pkg/detectors/sqlserver/sqlserver.go
+++ b/pkg/detectors/sqlserver/sqlserver.go
@@ -126,19 +126,34 @@ func shouldIgnore(uri string, ignorePatterns []*regexp.Regexp) bool {
return false
}
+const defaultSQLServerPort = 1433
+
+// preDialAddress returns the host:port the driver will dial, or false when the
+// port is resolved through SQL Browser and can't be checked ahead of time.
+func preDialAddress(config msdsn.Config) (string, bool) {
+ port := config.Port
+ if port == 0 {
+ if config.Instance != "" {
+ return "", false
+ }
+ port = defaultSQLServerPort
+ }
+ return net.JoinHostPort(config.Host, strconv.Itoa(int(port))), true
+}
+
var ping = func(ctx context.Context, config msdsn.Config) (bool, error) {
// TCP connectivity check to prevent indefinite hangs
- address := net.JoinHostPort(config.Host, strconv.Itoa(int(config.Port)))
-
- dialer := &net.Dialer{
- Timeout: 3 * time.Second,
- }
+ if address, ok := preDialAddress(config); ok {
+ dialer := &net.Dialer{
+ Timeout: 3 * time.Second,
+ }
- tcpConn, err := dialer.DialContext(ctx, "tcp", address) // respects context timeout
- if err != nil {
- return false, err
+ tcpConn, err := dialer.DialContext(ctx, "tcp", address) // respects context timeout
+ if err != nil {
+ return false, err
+ }
+ defer func() { _ = tcpConn.Close() }()
}
- defer func() { _ = tcpConn.Close() }()
cleanConfig := msdsn.Config{}
cleanConfig.Host = config.Host
diff --git a/pkg/detectors/sqlserver/sqlserver_test.go b/pkg/detectors/sqlserver/sqlserver_test.go
index 8c376eb06..249051b42 100644
--- a/pkg/detectors/sqlserver/sqlserver_test.go
+++ b/pkg/detectors/sqlserver/sqlserver_test.go
@@ -6,6 +6,8 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
+ "github.com/microsoft/go-mssqldb/msdsn"
+
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
)
@@ -108,3 +110,62 @@ func TestSqlServer_FromDataWithIgnorePattern(t *testing.T) {
t.Errorf("expected no results, but got %d", len(got))
}
}
+
+func TestSqlServer_PreDialAddress(t *testing.T) {
+ tests := []struct {
+ name string
+ connStr string
+ wantAddress string
+ wantOK bool
+ }{
+ {
+ name: "server without port uses default port",
+ connStr: "Server=db01;Database=master;User ID=sa;Password=P@ssw0rd!;",
+ wantAddress: "db01:1433",
+ wantOK: true,
+ },
+ {
+ name: "data source without port uses default port",
+ connStr: "Data Source=db01;Database=master;User ID=sa;Password=P@ssw0rd!;",
+ wantAddress: "db01:1433",
+ wantOK: true,
+ },
+ {
+ name: "port in server value",
+ connStr: "Server=db01,1444;Database=master;User ID=sa;Password=P@ssw0rd!;",
+ wantAddress: "db01:1444",
+ wantOK: true,
+ },
+ {
+ name: "port parameter",
+ connStr: "Server=db01;Port=1444;Database=master;User ID=sa;Password=P@ssw0rd!;",
+ wantAddress: "db01:1444",
+ wantOK: true,
+ },
+ {
+ name: "named instance without port is resolved by the driver",
+ connStr: `Server=db01\SALES;Database=master;User ID=sa;Password=P@ssw0rd!;`,
+ wantOK: false,
+ },
+ {
+ name: "named instance with port",
+ connStr: `Server=db01\SALES,1444;Database=master;User ID=sa;Password=P@ssw0rd!;`,
+ wantAddress: "db01:1444",
+ wantOK: true,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ config, err := msdsn.Parse(test.connStr)
+ if err != nil {
+ t.Fatalf("msdsn.Parse() error = %v", err)
+ }
+
+ address, ok := preDialAddress(config)
+ if ok != test.wantOK || address != test.wantAddress {
+ t.Errorf("preDialAddress() = (%q, %v), want (%q, %v)", address, ok, test.wantAddress, test.wantOK)
+ }
+ })
+ }
+}