Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
[INS-170] Unify JDBC URL parsing across detectors and analyzers (#4574)
Mustansir0fab92f434fc
diff --git a/pkg/detectors/jdbc/jdbc.go b/pkg/detectors/jdbc/jdbc.goindex d11c22260..173665faf 100644--- a/pkg/detectors/jdbc/jdbc.go+++ b/pkg/detectors/jdbc/jdbc.go@@ -207,9 +207,9 @@ func tryRedactRegex(conn string) (string, bool) {}var supportedSubprotocols = map[string]func(logContext.Context, string) (jdbc, error){- "mysql": parseMySQL,- "postgresql": parsePostgres,- "sqlserver": parseSqlServer,+ "mysql": ParseMySQL,+ "postgresql": ParsePostgres,+ "sqlserver": ParseSqlServer,}type pingResult struct {@@ -217,6 +217,15 @@ type pingResult struct {determinate bool}+// ConnectionInfo holds parsed connection information+type ConnectionInfo struct {+ Host string // includes port if specified, e.g., "host:port"+ Database string+ User string+ Password string+ Params map[string]string+}+type jdbc interface {ping(context.Context) pingResult}diff --git a/pkg/detectors/jdbc/mysql.go b/pkg/detectors/jdbc/mysql.goindex dbfe9241c..0a7148b11 100644--- a/pkg/detectors/jdbc/mysql.go+++ b/pkg/detectors/jdbc/mysql.go@@ -12,25 +12,30 @@ import ("github.com/go-sql-driver/mysql")-type mysqlJDBC struct {- conn string- userPass string- host string- params string+type MysqlJDBC struct {+ ConnectionInfo}-func (s *mysqlJDBC) ping(ctx context.Context) pingResult {+func (s *MysqlJDBC) ping(ctx context.Context) pingResult {return ping(ctx, "mysql", isMySQLErrorDeterminate,- buildMySQLConnectionString(s.host, "", s.userPass, s.params))+ BuildMySQLConnectionString(s.Host, "", s.User, s.Password, s.Params))}-func buildMySQLConnectionString(host, database, userPass, params string) string {+func BuildMySQLConnectionString(host, database, user, password string, params map[string]string) string {conn := host + "/" + database+ userPass := user+ if password != "" {+ userPass = userPass + ":" + password+ }if userPass != "" {conn = userPass + "@" + conn}- if params != "" {- conn = conn + "?" + params+ if len(params) > 0 {+ var paramList []string+ for k, v := range params {+ paramList = append(paramList, fmt.Sprintf("%s=%s", k, v))+ }+ conn = conn + "?" + strings.Join(paramList, "&")}return conn}@@ -51,7 +56,7 @@ func isMySQLErrorDeterminate(err error) bool {return false}-func parseMySQL(ctx logContext.Context, subname string) (jdbc, error) {+func ParseMySQL(ctx logContext.Context, subname string) (jdbc, error) {// expected form: [subprotocol:]//[user:password@]HOST[/DB][?key=val[&key=val]]if !strings.HasPrefix(subname, "//") {return nil, errors.New("expected host to start with //")@@ -70,11 +75,14 @@ func parseMySQL(ctx logContext.Context, subname string) (jdbc, error) {Info("Skipping invalid MySQL URL - no password or host found")return nil, fmt.Errorf("missing host or password in connection string")}- return &mysqlJDBC{- conn: subname[2:],- userPass: cfg.User + ":" + cfg.Passwd,- host: fmt.Sprintf("tcp(%s)", cfg.Addr),- params: "timeout=5s",+ return &MysqlJDBC{+ ConnectionInfo: ConnectionInfo{+ User: cfg.User,+ Password: cfg.Passwd,+ Host: fmt.Sprintf("tcp(%s)", cfg.Addr),+ Params: map[string]string{"timeout": "5s"},+ Database: cfg.DBName,+ },}, nil}@@ -107,13 +115,20 @@ func parseMySQLURI(ctx logContext.Context, subname string) (jdbc, error) {return nil, fmt.Errorf("missing host or password in connection string")}- userAndPass := user + ":" + pass+ // Parse database name+ dbName := strings.TrimPrefix(u.Path, "/")+ if dbName == "" {+ dbName = "mysql" // default DB+ }- return &mysqlJDBC{- conn: subname[2:],- userPass: userAndPass,- host: fmt.Sprintf("tcp(%s)", u.Host),- params: "timeout=5s",+ return &MysqlJDBC{+ ConnectionInfo: ConnectionInfo{+ User: user,+ Password: pass,+ Host: fmt.Sprintf("tcp(%s)", u.Host),+ Params: map[string]string{"timeout": "5s"},+ Database: dbName,+ },}, nil}diff --git a/pkg/detectors/jdbc/mysql_integration_test.go b/pkg/detectors/jdbc/mysql_integration_test.goindex 6fa752158..a0b3f9709 100644--- a/pkg/detectors/jdbc/mysql_integration_test.go+++ b/pkg/detectors/jdbc/mysql_integration_test.go@@ -12,6 +12,8 @@ import ("github.com/google/go-cmp/cmp""github.com/stretchr/testify/assert""github.com/testcontainers/testcontainers-go/modules/mysql"++ logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context")func TestMySQL(t *testing.T) {@@ -89,7 +91,7 @@ func TestMySQL(t *testing.T) {}for _, tt := range tests {t.Run(tt.input, func(t *testing.T) {- j, err := parseMySQL(tt.input)+ j, err := ParseMySQL(logContext.Background(), tt.input)if err != nil {got := result{ParseErr: true}diff --git a/pkg/detectors/jdbc/mysql_test.go b/pkg/detectors/jdbc/mysql_test.goindex 52a3c79e9..5af03812b 100644--- a/pkg/detectors/jdbc/mysql_test.go+++ b/pkg/detectors/jdbc/mysql_test.go@@ -2,7 +2,6 @@ package jdbcimport ("context"- "strings""testing"logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context"@@ -59,7 +58,7 @@ func TestParseMySQLMissingCredentials(t *testing.T) {for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {ctx := logContext.AddLogger(context.Background())- j, err := parseMySQL(ctx, tt.subname)+ j, err := ParseMySQL(ctx, tt.subname)if tt.shouldBeNil {if j != nil {@@ -103,15 +102,15 @@ func TestParseMySQLUsernameRecognition(t *testing.T) {for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {ctx := logContext.AddLogger(context.Background())- j, err := parseMySQL(ctx, tt.subname)+ j, err := ParseMySQL(ctx, tt.subname)if err != nil {t.Fatalf("parseMySQL() error = %v", err)}- mysqlConn := j.(*mysqlJDBC)- if !strings.Contains(mysqlConn.userPass, tt.wantUsername) {+ mysqlConn := j.(*MysqlJDBC)+ if mysqlConn.User != tt.wantUsername {t.Errorf("Connection string does not contain expected username '%s'\nGot: %s\nExpected: %s",- tt.wantUsername, mysqlConn.userPass, tt.wantUsername)+ tt.wantUsername, mysqlConn.User, tt.wantUsername)}})}diff --git a/pkg/detectors/jdbc/postgres.go b/pkg/detectors/jdbc/postgres.goindex d196dc257..ca5a057a9 100644--- a/pkg/detectors/jdbc/postgres.go+++ b/pkg/detectors/jdbc/postgres.go@@ -12,20 +12,19 @@ import ("github.com/lib/pq")-type postgresJDBC struct {- conn string- params map[string]string+type PostgresJDBC struct {+ ConnectionInfo}-func (s *postgresJDBC) ping(ctx context.Context) pingResult {+func (s *PostgresJDBC) ping(ctx context.Context) pingResult {// It is crucial that we try to build a connection string ourselves before using the one we found. This is because// if the found connection string doesn't include a username, the driver will attempt to connect using the current// user's name, which will fail in a way that looks like a determinate failure, thus terminating the waterfall. In// contrast, when we build a connection string ourselves, if there's no username, we try 'postgres' instead, which// actually has a chance of working.return ping(ctx, "postgres", isPostgresErrorDeterminate,- buildPostgresConnectionString(s.params, true),- buildPostgresConnectionString(s.params, false),+ BuildPostgresConnectionString(s.Host, s.User, s.Password, "postgres", s.Params, true),+ BuildPostgresConnectionString(s.Host, s.User, s.Password, "postgres", s.Params, false),)}@@ -59,7 +58,7 @@ func joinKeyValues(m map[string]string, sep string) string {return strings.Join(data, sep)}-func parsePostgres(ctx logContext.Context, subname string) (jdbc, error) {+func ParsePostgres(ctx logContext.Context, subname string) (jdbc, error) {// expected form: [subprotocol:]//[user:password@]HOST[/DB][?key=val[&key=val]]if !strings.HasPrefix(subname, "//") {@@ -77,16 +76,22 @@ func parsePostgres(ctx logContext.Context, subname string) (jdbc, error) {}params := map[string]string{- "host": u.Host,- "dbname": dbName,"connect_timeout": "5",}+ postgresJDBC := &PostgresJDBC{+ ConnectionInfo: ConnectionInfo{+ Host: u.Host,+ Database: dbName,+ Params: params,+ },+ }+if u.User != nil {- params["user"] = u.User.Username()+ postgresJDBC.User = u.User.Username()pass, set := u.User.Password()if set {- params["password"] = pass+ postgresJDBC.Password = pass}}@@ -95,46 +100,51 @@ func parsePostgres(ctx logContext.Context, subname string) (jdbc, error) {// https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-PROTECTIONcase "disable", "allow", "prefer","require", "verify-ca", "verify-full":- params["sslmode"] = v[0]+ postgresJDBC.Params["sslmode"] = v[0]}}if v := u.Query().Get("user"); v != "" {- params["user"] = v+ postgresJDBC.User = v}if v := u.Query().Get("password"); v != "" {- params["password"] = v+ postgresJDBC.Password = v}- if params["host"] == "" || params["password"] == "" {+ if postgresJDBC.Host == "" || postgresJDBC.Password == "" {ctx.Logger().WithName("jdbc").V(2).Info("Skipping invalid Postgres URL - no password or host found")return nil, fmt.Errorf("missing host or password in connection string")}- return &postgresJDBC{subname[2:], params}, nil+ return postgresJDBC, nil}-func buildPostgresConnectionString(params map[string]string, includeDbName bool) string {+func BuildPostgresConnectionString(host string, user string, password string, dbName string, params map[string]string, includeDbName bool) string {data := map[string]string{// default user- "user": "postgres",+ "user": "postgres",+ "password": password,+ "host": host,+ }+ if user != "" {+ data["user"] = user+ }+ if h, p, ok := strings.Cut(host, ":"); ok {+ data["host"] = h+ data["port"] = p}for key, val := range params {- if key == "host" {- if h, p, found := strings.Cut(val, ":"); found {- data["host"] = h- data["port"] = p- continue- }- }data[key] = val}- if !includeDbName {+ if includeDbName {data["dbname"] = "postgres"+ if dbName != "" {+ data["dbname"] = dbName+ }}connStr := joinKeyValues(data, " ")diff --git a/pkg/detectors/jdbc/postgres_integration_test.go b/pkg/detectors/jdbc/postgres_integration_test.goindex 3679dbc53..4fc8d4408 100644--- a/pkg/detectors/jdbc/postgres_integration_test.go+++ b/pkg/detectors/jdbc/postgres_integration_test.go@@ -15,6 +15,8 @@ import ("github.com/testcontainers/testcontainers-go""github.com/testcontainers/testcontainers-go/modules/postgres""github.com/testcontainers/testcontainers-go/wait"++ logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context")func TestPostgres(t *testing.T) {@@ -119,7 +121,7 @@ func TestPostgres(t *testing.T) {for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {- j, err := parsePostgres(tt.input)+ j, err := ParsePostgres(logContext.Background(), tt.input)if err != nil {got := result{ParseErr: true}diff --git a/pkg/detectors/jdbc/postgres_test.go b/pkg/detectors/jdbc/postgres_test.goindex 19099b6a4..bf85853b4 100644--- a/pkg/detectors/jdbc/postgres_test.go+++ b/pkg/detectors/jdbc/postgres_test.go@@ -47,7 +47,7 @@ func TestParsePostgresMissingCredentials(t *testing.T) {for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {ctx := logContext.AddLogger(context.Background())- j, err := parsePostgres(ctx, tt.subname)+ j, err := ParsePostgres(ctx, tt.subname)if tt.shouldBeNil {if j != nil {@@ -86,14 +86,14 @@ func TestParsePostgresUsernameRecognition(t *testing.T) {for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {ctx := logContext.AddLogger(context.Background())- j, err := parsePostgres(ctx, tt.subname)+ j, err := ParsePostgres(ctx, tt.subname)if err != nil {- t.Fatalf("parsePostgres() error = %v", err)+ t.Fatalf("ParsePostgres() error = %v", err)}- pgConn := j.(*postgresJDBC)- if pgConn.params["user"] != tt.wantUsername {- t.Errorf("expected username '%s', got '%s'", tt.wantUsername, pgConn.params["user"])+ pgConn := j.(*PostgresJDBC)+ if pgConn.User != tt.wantUsername {+ t.Errorf("expected username '%s', got '%s'", tt.wantUsername, pgConn.User)}})}diff --git a/pkg/detectors/jdbc/sqlserver.go b/pkg/detectors/jdbc/sqlserver.goindex 5f4d37e75..dc6746f20 100644--- a/pkg/detectors/jdbc/sqlserver.go+++ b/pkg/detectors/jdbc/sqlserver.go@@ -4,7 +4,6 @@ import ("context""errors""fmt"- "net/url""strings"logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context"@@ -12,13 +11,13 @@ import (mssql "github.com/microsoft/go-mssqldb")-type sqlServerJDBC struct {- connStr string+type SqlServerJDBC struct {+ ConnectionInfo}-func (s *sqlServerJDBC) ping(ctx context.Context) pingResult {+func (s *SqlServerJDBC) ping(ctx context.Context) pingResult {return ping(ctx, "mssql", isSqlServerErrorDeterminate,- s.connStr)+ BuildSQLServerConnectionString(s.Host, s.User, s.Password, "master", map[string]string{"connection+timeout": "5"}))}func isSqlServerErrorDeterminate(err error) bool {@@ -35,7 +34,7 @@ func isSqlServerErrorDeterminate(err error) bool {return false}-func parseSqlServer(ctx logContext.Context, subname string) (jdbc, error) {+func ParseSqlServer(ctx logContext.Context, subname string) (jdbc, error) {if !strings.HasPrefix(subname, "//") {return nil, errors.New("expected connection to start with //")}@@ -43,7 +42,9 @@ func parseSqlServer(ctx logContext.Context, subname string) (jdbc, error) {port := "1433"user := "sa"+ database := "master"var password, host string+ params := make(map[string]string)for i, param := range strings.Split(conn, ";") {key, value, found := strings.Cut(param, "=")@@ -59,17 +60,18 @@ func parseSqlServer(ctx logContext.Context, subname string) (jdbc, error) {}switch strings.ToLower(key) {- case "password":- password = value- case "spring.datasource.password":+ case "password", "spring.datasource.password", "pwd":password = valuecase "server":host = valuecase "port":port = value- case "user", "uid", "user id":+ case "user", "uid", "user id", "userid":user = value-+ case "database", "databasename":+ database = value+ default:+ params[key] = value}}@@ -80,14 +82,23 @@ func parseSqlServer(ctx logContext.Context, subname string) (jdbc, error) {return nil, fmt.Errorf("missing host or password in connection string")}- urlStr := fmt.Sprintf("sqlserver://%s:%s@%s:%s?database=master&connection+timeout=5", user, password, host, port)- jdbcUrl, err := url.Parse(urlStr)- if err != nil {- ctx.Logger().WithName("jdbc").- V(3).- Info("Skipping invalid SQL Server URL", "url", urlStr, "err", err)- return nil, err- }+ return &SqlServerJDBC{+ ConnectionInfo: ConnectionInfo{+ Host: host + ":" + port,+ User: user,+ Password: password,+ Database: database,+ Params: params,+ },+ }, nil+}- return &sqlServerJDBC{connStr: jdbcUrl.String()}, nil+func BuildSQLServerConnectionString(host, user, password, database string, params map[string]string) string {+ conn := fmt.Sprintf("sqlserver://%s:%s@%s?database=%s", user, password, host, database)+ if len(params) > 0 {+ for k, v := range params {+ conn += fmt.Sprintf("&%s=%s", k, v)+ }+ }+ return conn}diff --git a/pkg/detectors/jdbc/sqlserver_test.go b/pkg/detectors/jdbc/sqlserver_test.goindex 7522d105e..f86b80d10 100644--- a/pkg/detectors/jdbc/sqlserver_test.go+++ b/pkg/detectors/jdbc/sqlserver_test.go@@ -2,8 +2,6 @@ package jdbcimport ("context"- "fmt"- "strings""testing"logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context"@@ -56,7 +54,7 @@ func TestParseSqlServerMissingCredentials(t *testing.T) {t.Run(tt.name, func(t *testing.T) {ctx := logContext.AddLogger(context.Background())- j, err := parseSqlServer(ctx, tt.subname)+ j, err := ParseSqlServer(ctx, tt.subname)if tt.shouldBeNil {if j != nil {@@ -103,17 +101,16 @@ func TestParseSqlServerUserIgnoredBug2(t *testing.T) {t.Run(tt.name, func(t *testing.T) {ctx := logContext.AddLogger(context.Background())- j, err := parseSqlServer(ctx, tt.subname)+ j, err := ParseSqlServer(ctx, tt.subname)if err != nil {t.Fatalf("parseSqlServer() error = %v", err)}- sqlServerConn := j.(*sqlServerJDBC)- expectedPrefix := fmt.Sprintf("sqlserver://%s:", tt.wantUsername)+ sqlServerConn := j.(*SqlServerJDBC)- if !strings.Contains(sqlServerConn.connStr, expectedPrefix) {+ if sqlServerConn.User != tt.wantUsername {t.Errorf("Connection string does not contain expected username '%s'\nGot: %s\nExpected to contain: %s",- tt.wantUsername, sqlServerConn.connStr, expectedPrefix)+ tt.wantUsername, sqlServerConn.User, tt.wantUsername)}})}