Skip to content

Find, verify, and analyze leaked credentials

Narrow Postgres detector to only look for URIs (#2314)

Cody Rosee0a1b11a8e4c
diff --git a/pkg/detectors/postgres/postgres.go b/pkg/detectors/postgres/postgres.go
index cb22536cd..183b37d4c 100644
--- a/pkg/detectors/postgres/postgres.go
+++ b/pkg/detectors/postgres/postgres.go
@@ -3,63 +3,133 @@ package postgres
import (
"context"
"database/sql"
+ "errors"
"fmt"
- "net/url"
+ "net"
"regexp"
+ "strconv"
"strings"
"time"
- _ "github.com/lib/pq" // PostgreSQL driver
+ "github.com/lib/pq"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
)
const (
defaultPort = "5432"
- defaultHost = "localhost"
+
+ pg_connect_timeout = "connect_timeout"
+ pg_dbname = "dbname"
+ pg_host = "host"
+ pg_password = "password"
+ pg_port = "port"
+ pg_requiressl = "requiressl"
+ pg_sslmode = "sslmode"
+ pg_sslmode_allow = "allow"
+ pg_sslmode_disable = "disable"
+ pg_sslmode_prefer = "prefer"
+ pg_sslmode_require = "require"
+ pg_user = "user"
)
+// This detector currently only finds Postgres connection string URIs
+// (https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS) When it finds one, it uses
+// pq.ParseURI to normalize this into space-separated key-value pair Postgres connection string, and then uses a regular
+// expression to transform this connection string into a parameters map. This parameters map is manipulated prior to
+// verification, which operates by transforming the map back into a space-separated kvp connection string. This is kind
+// of clunky overall, but it has the benefit of preserving the connection string as a map when it needs to be modified,
+// which is much nicer than having to patch a space-separated string of kvps.
+
+// Multi-host connection string URIs are currently not supported because pq.ParseURI doesn't parse them correctly. If we
+// happen to run into a case where this matters we can address it then.
var (
- _ detectors.Detector = (*Scanner)(nil)
- uriPattern = regexp.MustCompile(`\b(?i)postgresql://[\S]+\b`)
- hostnamePattern = regexp.MustCompile(`(?i)(?:host|server|address).{0,40}?(\b[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\b)`)
- portPattern = regexp.MustCompile(`(?i)(?:port|p).{0,40}?(\b[0-9]{1,5}\b)`)
- usernamePattern = regexp.MustCompile(`(?im)(?:user|usr)\S{0,40}?[:=\s]{1,3}[ '"=]{0,1}([^:'"\s]{4,40})`)
- passwordPattern = regexp.MustCompile(`(?im)(?:pass)\S{0,40}?[:=\s]{1,3}[ '"=]{0,1}([^:'"\s]{4,40})`)
+ _ detectors.Detector = (*Scanner)(nil)
+ uriPattern = regexp.MustCompile(`\b(?i)postgres(?:ql)?://\S+\b`)
+ connStrPartPattern = regexp.MustCompile(`([[:alpha:]]+)='(.+?)' ?`)
)
-type Scanner struct{}
+type Scanner struct {
+ detectLoopback bool // Automated tests run against localhost, but we want to ignore those results in the wild
+}
func (s Scanner) Keywords() []string {
- return []string{"postgres", "psql", "pghost"}
+ return []string{"postgres"}
}
func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) ([]detectors.Result, error) {
var results []detectors.Result
- var pgURLs []url.URL
- pgURLs = append(pgURLs, findUriMatches(string(data)))
- pgURLs = append(pgURLs, findComponentMatches(verify, string(data))...)
+ candidateParamSets := findUriMatches(data)
+
+ for _, params := range candidateParamSets {
+ user, ok := params[pg_user]
+ if !ok {
+ continue
+ }
- for _, pgURL := range pgURLs {
- if pgURL.User == nil {
+ password, ok := params[pg_password]
+ if !ok {
continue
}
- username := pgURL.User.Username()
- password, _ := pgURL.User.Password()
- hostport := pgURL.Host
+
+ host, ok := params[pg_host]
+ if !ok {
+ continue
+ }
+ if !s.detectLoopback {
+ if host == "localhost" {
+ continue
+ }
+ if ip := net.ParseIP(host); ip != nil && ip.IsLoopback() {
+ continue
+ }
+ }
+
+ port, ok := params[pg_port]
+ if !ok {
+ port = defaultPort
+ params[pg_port] = port
+ }
+
+ raw := []byte(fmt.Sprintf("postgresql://%s:%s@%s:%s", user, password, host, port))
+
result := detectors.Result{
DetectorType: detectorspb.DetectorType_Postgres,
- Raw: []byte(hostport + username + password),
- RawV2: []byte(hostport + username + password),
+ Raw: raw,
+ RawV2: raw,
+ }
+
+ // We don't need to normalize the (deprecated) requiressl option into the (up-to-date) sslmode option - pq can
+ // do it for us - but we will do it anyway here so that when we later capture sslmode into ExtraData we will
+ // capture it post-normalization. (The detector's behavior is undefined for candidate secrets that have both
+ // requiressl and sslmode set.)
+ if requiressl := params[pg_requiressl]; requiressl == "0" {
+ params[pg_sslmode] = pg_sslmode_prefer
+ } else if requiressl == "1" {
+ params[pg_sslmode] = pg_sslmode_require
}
if verify {
- timeoutInSeconds := getDeadlineInSeconds(ctx)
- isVerified, verificationErr := verifyPostgres(&pgURL, timeoutInSeconds)
+ // pq appears to ignore the context deadline, so we copy any timeout that's been set into the connection
+ // parameters themselves.
+ if timeout := getDeadlineInSeconds(ctx); timeout != 0 {
+ params[pg_connect_timeout] = strconv.Itoa(timeout)
+ }
+
+ isVerified, verificationErr := verifyPostgres(params)
result.Verified = isVerified
result.SetVerificationError(verificationErr, password)
}
+ // We gather SSL information into ExtraData in case it's useful for later reporting.
+ sslmode := params[pg_sslmode]
+ if sslmode == "" {
+ sslmode = "<unset>"
+ }
+ result.ExtraData = map[string]string{
+ pg_sslmode: sslmode,
+ }
+
if !result.Verified && detectors.IsKnownFalsePositive(password, detectors.DefaultFalsePositives, true) {
continue
}
@@ -69,184 +139,86 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) ([]dete
return results, nil
}
-func getDeadlineInSeconds(ctx context.Context) int {
- deadline, ok := ctx.Deadline()
- if !ok {
- // Context does not have a deadline
- return 0
- }
-
- duration := time.Until(deadline)
- return int(duration.Seconds())
-}
-
-func findUriMatches(dataStr string) url.URL {
- var pgURL url.URL
- for _, uri := range uriPattern.FindAllString(dataStr, -1) {
- pgURL, err := url.Parse(uri)
+func findUriMatches(data []byte) []map[string]string {
+ var matches []map[string]string
+ for _, uri := range uriPattern.FindAll(data, -1) {
+ connStr, err := pq.ParseURL(string(uri))
if err != nil {
continue
}
- if pgURL.User != nil {
- return *pgURL
- }
- }
- return pgURL
-}
-
-// check if postgres is running
-func postgresRunning(hostname, port string) bool {
- connStr := fmt.Sprintf("host=%s port=%s sslmode=disable", hostname, port)
- db, err := sql.Open("postgres", connStr)
- if err != nil {
- return false
- }
- defer db.Close()
- return true
-}
-func findComponentMatches(verify bool, dataStr string) []url.URL {
- usernameMatches := usernamePattern.FindAllStringSubmatch(dataStr, -1)
- passwordMatches := passwordPattern.FindAllStringSubmatch(dataStr, -1)
- hostnameMatches := hostnamePattern.FindAllStringSubmatch(dataStr, -1)
- portMatches := portPattern.FindAllStringSubmatch(dataStr, -1)
-
- var pgURLs []url.URL
-
- hosts := findHosts(verify, hostnameMatches, portMatches)
-
- for _, username := range dedupMatches(usernameMatches) {
- for _, password := range dedupMatches(passwordMatches) {
- for _, host := range hosts {
- hostname, port := strings.Split(host, ":")[0], strings.Split(host, ":")[1]
- if combinedLength := len(username) + len(password) + len(hostname); combinedLength > 255 {
- continue
- }
- postgresURL := url.URL{
- Scheme: "postgresql",
- User: url.UserPassword(username, password),
- Host: fmt.Sprintf("%s:%s", hostname, port),
- }
- pgURLs = append(pgURLs, postgresURL)
- }
+ parts := connStrPartPattern.FindAllStringSubmatch(connStr, -1)
+ params := make(map[string]string, len(parts))
+ for _, part := range parts {
+ params[part[1]] = part[2]
}
- }
- return pgURLs
-}
-
-// if verification is turned on, and we can confirm that postgres is running on at least one host,
-// return only hosts where it's running. otherwise return all hosts.
-func findHosts(verify bool, hostnameMatches, portMatches [][]string) []string {
- hostnames := dedupMatches(hostnameMatches)
- ports := dedupMatches(portMatches)
- var hosts []string
-
- if len(hostnames) < 1 {
- hostnames = append(hostnames, defaultHost)
- }
- if len(ports) < 1 {
- ports = append(ports, defaultPort)
- }
-
- for _, hostname := range hostnames {
- for _, port := range ports {
- hosts = append(hosts, fmt.Sprintf("%s:%s", hostname, port))
- }
+ matches = append(matches, params)
}
+ return matches
+}
- if verify {
- var verifiedHosts []string
- for _, host := range hosts {
- parts := strings.Split(host, ":")
- hostname, port := parts[0], parts[1]
- if postgresRunning(hostname, port) {
- verifiedHosts = append(verifiedHosts, host)
- }
- }
- if len(verifiedHosts) > 0 {
- return verifiedHosts
- }
+func getDeadlineInSeconds(ctx context.Context) int {
+ deadline, ok := ctx.Deadline()
+ if !ok {
+ // Context does not have a deadline
+ return 0
}
- return hosts
+ duration := time.Until(deadline)
+ return int(duration.Seconds())
}
-// deduplicate matches in order to reduce the number of verification requests
-func dedupMatches(matches [][]string) []string {
- setOfMatches := make(map[string]struct{})
- for _, match := range matches {
- if len(match) > 1 {
- setOfMatches[match[1]] = struct{}{}
- }
+func isErrorDatabaseNotFound(err error, dbName string) bool {
+ if dbName == "" {
+ dbName = "postgres"
}
- var results []string
- for match := range setOfMatches {
- results = append(results, match)
- }
- return results
+ missingDbErrorText := fmt.Sprintf("database \"%s\" does not exist", dbName)
+
+ return strings.Contains(err.Error(), missingDbErrorText)
}
-func verifyPostgres(pgURL *url.URL, timeoutInSeconds int) (bool, error) {
- if pgURL.User == nil {
- return false, nil
- }
- username := pgURL.User.Username()
- password, _ := pgURL.User.Password()
+func verifyPostgres(params map[string]string) (bool, error) {
+ if sslmode := params[pg_sslmode]; sslmode == pg_sslmode_allow || sslmode == pg_sslmode_prefer {
+ // pq doesn't support 'allow' or 'prefer'. If we find either of them, we'll just ignore it. This will trigger
+ // the same logic that is run if no sslmode is set at all (which mimics 'prefer', which is the default).
+ delete(params, pg_sslmode)
- hostname, port := pgURL.Hostname(), pgURL.Port()
- if hostname == "" {
- hostname = defaultHost
- }
- if port == "" {
- port = defaultPort
+ // We still want to save the original sslmode in ExtraData, so we'll re-add it before returning.
+ defer func() {
+ params[pg_sslmode] = sslmode
+ }()
}
- sslmode := determineSSLMode(pgURL)
-
- connStr := fmt.Sprintf("user=%s password=%s host=%s port=%s sslmode=%s", username, password, hostname, port, sslmode)
- if timeoutInSeconds > 0 {
- connStr = fmt.Sprintf("%s connect_timeout=%d", connStr, timeoutInSeconds)
+ var connStr string
+ for key, value := range params {
+ connStr += fmt.Sprintf("%s='%s'", key, value)
}
db, err := sql.Open("postgres", connStr)
if err != nil {
- if strings.Contains(err.Error(), "connection refused") {
- // inactive host
- return false, nil
- }
return false, err
}
defer db.Close()
- ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
- defer cancel()
-
- err = db.PingContext(ctx)
- if err == nil {
+ err = db.Ping()
+ switch {
+ case err == nil:
return true, nil
- } else if strings.Contains(err.Error(), "password authentication failed") || // incorrect username or password
- strings.Contains(err.Error(), "connection refused") { // inactive host
+ case strings.Contains(err.Error(), "password authentication failed"):
return false, nil
+ case errors.Is(err, pq.ErrSSLNotSupported) && params[pg_sslmode] == "":
+ // If the sslmode is unset, then either it was unset in the candidate secret, or we've intentionally unset it
+ // because it was specified as 'allow' or 'prefer', neither of which pq supports. In all of these cases, non-SSL
+ // connections are acceptable, so now we try a connection without SSL.
+ params[pg_sslmode] = pg_sslmode_disable
+ defer delete(params, pg_sslmode) // We want to return with the original params map intact (for ExtraData)
+ return verifyPostgres(params)
+ case isErrorDatabaseNotFound(err, params[pg_dbname]):
+ return true, nil // If we know this, we were able to authenticate
+ default:
+ return false, err
}
-
- // if ssl is not enabled, manually fall-back to sslmode=disable
- if strings.Contains(err.Error(), "SSL is not enabled on the server") {
- pgURL.RawQuery = fmt.Sprintf("sslmode=%s", "disable")
- return verifyPostgres(pgURL, timeoutInSeconds)
- }
- return false, err
-}
-
-func determineSSLMode(pgURL *url.URL) string {
- // default ssl mode is "prefer" per https://www.postgresql.org/docs/current/libpq-ssl.html
- // but is currently not implemented in the driver per https://github.com/lib/pq/issues/1006
- // default for the driver is "require". ideally we would use "allow" but that is also not supported by the driver.
- sslmode := "require"
- if sslQuery, ok := pgURL.Query()["sslmode"]; ok && len(sslQuery) > 0 {
- sslmode = sslQuery[0]
- }
- return sslmode
}
func (s Scanner) Type() detectorspb.DetectorType {
diff --git a/pkg/detectors/postgres/postgres_test.go b/pkg/detectors/postgres/postgres_test.go
index 08da6dd2f..90c460b0a 100644
--- a/pkg/detectors/postgres/postgres_test.go
+++ b/pkg/detectors/postgres/postgres_test.go
@@ -15,6 +15,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
+ "github.com/lib/pq"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
@@ -24,20 +25,35 @@ var postgresDockerHash string
const (
postgresUser = "postgres"
- postgresPass = "23201dabb56ca236f3dc6736c0f9afad"
+ postgresPass = "23201da=b56ca236f3dc6736c0f9afad"
postgresHost = "localhost"
- postgresPort = "5433"
+ postgresPort = "5434" // Do not use 5433, as local dev environments can use it for other things
- inactiveUser = "inactive"
inactivePass = "inactive"
- inactivePort = "61000"
inactiveHost = "192.0.2.0"
)
func TestPostgres_FromChunk(t *testing.T) {
- startPostgres()
+ if err := startPostgres(); err != nil {
+ if exitErr, ok := err.(*exec.ExitError); ok {
+ t.Fatalf("could not start local postgres: %v w/stderr:\n%s", err, string(exitErr.Stderr))
+ } else {
+ t.Fatalf("could not start local postgres: %v", err)
+ }
+ }
defer stopPostgres()
+ // The detector is written to connect to the database 'postgres' if no explicit database is found in the candidate
+ // secret (because pq uses 'postgres' as a default if no database is specified). If the target cluster doesn't
+ // actually have a database with this name, but our credentials are good, then Postgres will give us a "missing
+ // database" error message instead of an authentication failure.
+ //
+ // Unfortunately, directly validating this in the automated tests is awkward because the docker image's POSTGRES_DB
+ // environment variable doesn't appear to work: The database created is always named 'postgres', no matter what
+ // POSTGRES_DB is set to. This means that we can't replicate a cluster that has no database named 'postgres', so we
+ // can't directly test what happens if we see one. To work around this, all the automated tests try to connect to
+ // the nonexistent database 'postgres2'. In this way, we test the logic of attempting to connect to a non-existent
+ // database, even though the test cases are the inverse of what we'd see in the wild.
type args struct {
ctx context.Context
data []byte
@@ -62,177 +78,200 @@ func TestPostgres_FromChunk(t *testing.T) {
wantErr: false,
},
{
- name: "found with seperated credentials, verified",
- s: Scanner{},
+ name: "found connection URI with ssl mode unset, verified",
+ s: Scanner{detectLoopback: true},
args: args{
- ctx: context.Background(),
- data: []byte(fmt.Sprintf(`
- POSTGRES_USER=%s
- POSTGRES_PASSWORD=%s
- POSTGRES_ADDRESS=%s
- POSTGRES_PORT=%s
- `, postgresUser, postgresPass, postgresHost, postgresPort)),
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres2`, postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Postgres,
Verified: true,
+ Raw: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ RawV2: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ ExtraData: map[string]string{"sslmode": "<unset>"},
},
},
wantErr: false,
},
{
- name: "found with single line credentials, verified",
- s: Scanner{},
+ name: "found connection URI with ssl mode 'prefer', verified",
+ s: Scanner{detectLoopback: true},
args: args{
ctx: context.Background(),
- data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres`, postgresUser, postgresPass, postgresHost, postgresPort)),
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres2?sslmode=prefer`, postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Postgres,
Verified: true,
+ Raw: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ RawV2: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ ExtraData: map[string]string{"sslmode": "prefer"},
},
},
wantErr: false,
},
{
- name: "found with json credentials, verified",
- s: Scanner{},
+ name: "found connection URI with ssl mode 'allow', verified",
+ s: Scanner{detectLoopback: true},
args: args{
- ctx: context.Background(),
- data: []byte(fmt.Sprintf(
- `DB_CONFIG={"user": "%s", "password": "%s", "host": "%s", "port": "%s", "database": "postgres"}`, postgresUser, postgresPass, postgresHost, postgresPort)),
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres2?sslmode=allow`, postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Postgres,
Verified: true,
+ Raw: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ RawV2: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ ExtraData: map[string]string{"sslmode": "allow"},
},
},
wantErr: false,
},
{
- name: "found with seperated credentials, unverified",
- s: Scanner{},
+ name: "found connection URI with requiressl=0, verified",
+ s: Scanner{detectLoopback: true},
args: args{
- ctx: context.Background(),
- data: []byte(fmt.Sprintf(`
- POSTGRES_USER=%s
- POSTGRES_PASSWORD=%s
- POSTGRES_ADDRESS=%s
- POSTGRES_PORT=%s
- `, postgresUser, inactivePass, postgresHost, postgresPort)),
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres2?requiressl=0`, postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Postgres,
- Verified: false,
+ Verified: true,
+ Raw: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ RawV2: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ ExtraData: map[string]string{"sslmode": "prefer"},
},
},
wantErr: false,
},
{
- name: "found with seperated credentials - no port, unverified",
- s: Scanner{},
+ name: "found connection URI without database, verified",
+ s: Scanner{detectLoopback: true},
args: args{
- ctx: context.Background(),
- data: []byte(fmt.Sprintf(`
- POSTGRES_USER=%s
- POSTGRES_PASSWORD=%s
- POSTGRES_ADDRESS=%s
- `, postgresUser, inactivePass, postgresHost)),
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/`, postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Postgres,
- Verified: false,
+ Verified: true,
+ Raw: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ RawV2: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ ExtraData: map[string]string{"sslmode": "<unset>"},
},
},
wantErr: false,
},
{
- name: "found with single line credentials, unverified",
- s: Scanner{},
+ name: "found connection URI, unverified",
+ s: Scanner{detectLoopback: true},
args: args{
ctx: context.Background(),
- data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres`, postgresUser, inactivePass, postgresHost, postgresPort)),
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres2`, postgresUser, inactivePass, postgresHost, postgresPort)),
verify: true,
},
want: []detectors.Result{
{
DetectorType: detectorspb.DetectorType_Postgres,
Verified: false,
+ Raw: []byte("postgresql://postgres:inactive@localhost:5434"),
+ RawV2: []byte("postgresql://postgres:inactive@localhost:5434"),
+ ExtraData: map[string]string{"sslmode": "<unset>"},
},
},
wantErr: false,
},
{
- name: "found with json credentials, unverified - inactive password",
+ name: "ignored localhost",
s: Scanner{},
args: args{
- ctx: context.Background(),
- data: []byte(fmt.Sprintf(
- `DB_CONFIG={"user": "%s", "password": "%s", "host": "%s", "port": "%s", "database": "postgres"}`, postgresUser, inactivePass, postgresHost, postgresPort)),
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres2`, postgresUser, postgresPass, "localhost", postgresPort)),
verify: true,
},
- want: []detectors.Result{
- {
- DetectorType: detectorspb.DetectorType_Postgres,
- Verified: false,
- },
- },
+ want: nil,
wantErr: false,
},
{
- name: "found with json credentials, unverified - inactive user",
+ name: "ignored 127.0.0.1",
s: Scanner{},
args: args{
- ctx: context.Background(),
- data: []byte(fmt.Sprintf(
- `DB_CONFIG={"user": "%s", "password": "%s", "host": "%s", "port": "%s", "database": "postgres"}`, inactiveUser, postgresPass, postgresHost, postgresPort)),
+ ctx: context.Background(),
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres2`, postgresUser, postgresPass, "127.0.0.1", postgresPort)),
verify: true,
},
- want: []detectors.Result{
- {
+ want: nil,
+ wantErr: false,
+ },
+ {
+ name: "found connection URI, unverified due to error - inactive host",
+ s: Scanner{},
+ args: func() args {
+ ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
+ defer cancel()
+ return args{
+ ctx: ctx,
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres2`, postgresUser, postgresPass, inactiveHost, postgresPort)),
+ verify: true,
+ }
+ }(),
+ want: func() []detectors.Result {
+ r := detectors.Result{
DetectorType: detectorspb.DetectorType_Postgres,
Verified: false,
- },
- },
+ Raw: []byte("postgresql://postgres:[email protected]:5434"),
+ RawV2: []byte("postgresql://postgres:[email protected]:5434"),
+ ExtraData: map[string]string{"sslmode": "<unset>"},
+ }
+ r.SetVerificationError(errors.New("i/o timeout"))
+ return []detectors.Result{r}
+ }(),
wantErr: false,
},
{
- name: "found, unverified due to error - inactive port",
- s: Scanner{},
- args: args{
- ctx: context.Background(),
- data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres`, postgresUser, postgresPass, postgresHost, inactivePort)),
- verify: true,
- },
+ name: "found connection URI, unverified due to error - wrong port",
+ s: Scanner{detectLoopback: true},
+ args: func() args {
+ ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
+ defer cancel()
+ return args{
+ ctx: ctx,
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s/postgres2`, postgresUser, postgresPass, postgresHost)),
+ verify: true,
+ }
+ }(),
want: func() []detectors.Result {
r := detectors.Result{
DetectorType: detectorspb.DetectorType_Postgres,
Verified: false,
+ Raw: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5432"),
+ RawV2: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5432"),
+ ExtraData: map[string]string{"sslmode": "<unset>"},
}
+ r.SetVerificationError(errors.New("connection refused"))
return []detectors.Result{r}
}(),
wantErr: false,
},
- // This test seems take a long time to run (70s+) even with the timeout set to 1s. It's not clear why.
{
- name: "found, unverified due to error - inactive host",
- s: Scanner{},
+ name: "found connection URI, unverified due to error - ssl not supported (using sslmode)",
+ s: Scanner{detectLoopback: true},
args: func() args {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
return args{
ctx: ctx,
- data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres`, postgresUser, postgresPass, inactiveHost, postgresPort)),
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres2?sslmode=require`, postgresUser, postgresPass, postgresHost, postgresPort)),
verify: true,
}
}(),
@@ -240,8 +279,36 @@ func TestPostgres_FromChunk(t *testing.T) {
r := detectors.Result{
DetectorType: detectorspb.DetectorType_Postgres,
Verified: false,
+ Raw: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ RawV2: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ ExtraData: map[string]string{"sslmode": "require"},
}
- r.SetVerificationError(errors.New("i/o timeout"))
+ r.SetVerificationError(pq.ErrSSLNotSupported)
+ return []detectors.Result{r}
+ }(),
+ wantErr: false,
+ },
+ {
+ name: "found connection URI, unverified due to error - ssl not supported (using requiressl)",
+ s: Scanner{detectLoopback: true},
+ args: func() args {
+ ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
+ defer cancel()
+ return args{
+ ctx: ctx,
+ data: []byte(fmt.Sprintf(`postgresql://%s:%s@%s:%s/postgres2?requiressl=1`, postgresUser, postgresPass, postgresHost, postgresPort)),
+ verify: true,
+ }
+ }(),
+ want: func() []detectors.Result {
+ r := detectors.Result{
+ DetectorType: detectorspb.DetectorType_Postgres,
+ Verified: false,
+ Raw: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ RawV2: []byte("postgresql://postgres:23201da=b56ca236f3dc6736c0f9afad@localhost:5434"),
+ ExtraData: map[string]string{"sslmode": "require"},
+ }
+ r.SetVerificationError(pq.ErrSSLNotSupported)
return []detectors.Result{r}
}(),
wantErr: false,
@@ -249,8 +316,7 @@ func TestPostgres_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("postgres.FromData() error = %v, wantErr %v", err, tt.wantErr)
return
@@ -271,7 +337,7 @@ func TestPostgres_FromChunk(t *testing.T) {
t.Fatalf("wantVerificationError = %v, verification error = %v", tt.want[i].VerificationError(), got[i].VerificationError())
}
}
- ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "Raw", "RawV2", "verificationError")
+ ignoreOpts := cmpopts.IgnoreFields(detectors.Result{}, "verificationError")
if diff := cmp.Diff(got, tt.want, ignoreOpts); diff != "" {
t.Errorf("Postgres.FromData() %s diff: (-got +want)\n%s", tt.name, diff)
}
diff --git a/pkg/engine/defaults.go b/pkg/engine/defaults.go
index 0cd901ab9..16682b867 100644
--- a/pkg/engine/defaults.go
+++ b/pkg/engine/defaults.go
@@ -524,6 +524,7 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/positionstack"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/postageapp"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/postbacks"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/postgres"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/posthog"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/postman"
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors/postmark"
@@ -1496,8 +1497,7 @@ func DefaultDetectors() []detectors.Detector {
speechtextai.Scanner{},
databox.Scanner{},
postbacks.Scanner{},
- // Too noisy, needs attention
- // postgres.Scanner{},
+ postgres.Scanner{},
collect2.Scanner{},
uclassify.Scanner{},
holistic.Scanner{},