Skip to content

Commit

Permalink
Add support for user:pass@host to postgres JDBC detector (trufflesecu…
Browse files Browse the repository at this point in the history
…rity#2089)

* Add support for user:pass@host to postgres JDBC detector

* Remove ineffectual assignment
  • Loading branch information
mcastorina authored Nov 7, 2023
1 parent 1094190 commit 8e3f6e9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pkg/detectors/jdbc/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"errors"
"fmt"
"github.com/lib/pq"
"strings"

"github.com/lib/pq"
)

type postgresJDBC struct {
Expand Down Expand Up @@ -57,18 +58,34 @@ func joinKeyValues(m map[string]string, sep string) string {
}

func parsePostgres(subname string) (jdbc, error) {
// expected form: //HOST/DB?key=value&key=value
// expected form: [subprotocol:]//[user:password@]HOST[/DB][?key=val[&key=val]]
hostAndDB, paramString, _ := strings.Cut(subname, "?")
if !strings.HasPrefix(hostAndDB, "//") {
return nil, errors.New("expected host to start with //")
}
hostAndDB = strings.TrimPrefix(hostAndDB, "//")
host, database, _ := strings.Cut(hostAndDB, "/")
userPassAndHostAndDB := strings.TrimPrefix(hostAndDB, "//")
userPass, hostAndDB, found := strings.Cut(userPassAndHostAndDB, "@")
var user, pass string
if found {
user, pass, _ = strings.Cut(userPass, ":")
} else {
hostAndDB = userPass
}
host, database, found := strings.Cut(hostAndDB, "/")
if !found {
return nil, errors.New("expected host and database to be separated by /")
}

params := map[string]string{
"host": host,
"dbname": database,
}
if len(user) > 0 {
params["user"] = user
}
if len(pass) > 0 {
params["password"] = pass
}
for _, param := range strings.Split(paramString, "&") {
key, val, _ := strings.Cut(param, "=")
params[key] = val
Expand Down
9 changes: 9 additions & 0 deletions pkg/detectors/jdbc/postgres_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"testing"
"time"
Expand All @@ -33,10 +34,18 @@ func TestPostgres(t *testing.T) {
input: "//localhost:5432/foo?sslmode=disable&password=" + postgresPass,
want: result{pingOk: true, pingDeterminate: true},
},
{
input: fmt.Sprintf("//postgres:%s@localhost:5432/foo?sslmode=disable", postgresPass),
want: result{pingOk: true, pingDeterminate: true},
},
{
input: "//localhost:5432/foo?sslmode=disable&user=" + postgresUser + "&password=" + postgresPass,
want: result{pingOk: true, pingDeterminate: true},
},
{
input: fmt.Sprintf("//%s:%s@localhost:5432/foo?sslmode=disable", postgresUser, postgresPass),
want: result{pingOk: true, pingDeterminate: true},
},
{
input: "//localhost/foo?sslmode=disable&port=5432&password=" + postgresPass,
want: result{pingOk: true, pingDeterminate: true},
Expand Down

0 comments on commit 8e3f6e9

Please sign in to comment.