Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
fix(postgres): recognise a missing database by SQLSTATE (#5312)
Bernát Gáborff8d20c4e054
diff --git a/pkg/detectors/postgres/postgres.go b/pkg/detectors/postgres/postgres.goindex fb5c6dabb..68e4df53c 100644--- a/pkg/detectors/postgres/postgres.go+++ b/pkg/detectors/postgres/postgres.go@@ -293,13 +293,31 @@ func getDeadlineInSeconds(ctx context.Context) (int, bool) {return int(duration.Seconds()), true}-func isErrorDatabaseNotFound(err error, dbName string) bool {+// The server looks the database up only after authenticating, so this confirms the credentials.+const invalidCatalogName = "3D000"++// Message text is only a fallback, for proxies that relay a failure without a SQLSTATE; the server+// translates messages per lc_messages. Postgres substitutes the user name, not "postgres", when a+// connection string names no database (src/backend/tcop/backend_startup.c).+func isErrorDatabaseNotFound(err error, params map[string]string) bool {+ var pqErr *pq.Error+ if errors.As(err, &pqErr) && pqErr.Code == invalidCatalogName {+ return true+ }+ var pgErr *pgconn.PgError+ if errors.As(err, &pgErr) && pgErr.Code == invalidCatalogName {+ return true+ }++ dbName := params[pgDbname]+ if dbName == "" {+ dbName = params[pgUser]+ }if dbName == "" {- dbName = "postgres"+ return false}- missingDbErrorText := fmt.Sprintf("database \"%s\" does not exist", dbName)- return strings.Contains(err.Error(), missingDbErrorText)+ return strings.Contains(err.Error(), fmt.Sprintf("database %q does not exist", dbName))}func verifyPostgres(ctx context.Context, params map[string]string) (bool, error) {@@ -317,7 +335,7 @@ func verifyPostgres(ctx context.Context, params map[string]string) (bool, error)func verifyPostgresPgx(ctx context.Context, params map[string]string) (bool, error) {conn, err := pgx.Connect(ctx, pgxConnString(params))if err != nil {- return classifyPostgresVerifyError(err, params[pgDbname])+ return classifyPostgresVerifyError(err, params)}defer func() {// Best-effort close after verification; the verify outcome is already decided.@@ -327,7 +345,7 @@ func verifyPostgresPgx(ctx context.Context, params map[string]string) (bool, err}()if err := conn.Ping(ctx); err != nil {- return classifyPostgresVerifyError(err, params[pgDbname])+ return classifyPostgresVerifyError(err, params)}return true, nil}@@ -346,7 +364,7 @@ func pgxConnString(params map[string]string) string {return connStr.String()}-func classifyPostgresVerifyError(err error, dbName string) (bool, error) {+func classifyPostgresVerifyError(err error, params map[string]string) (bool, error) {var pgErr *pgconn.PgErrorif errors.As(err, &pgErr) {switch pgErr.Code {@@ -359,7 +377,7 @@ func classifyPostgresVerifyError(err error, dbName string) (bool, error) {if strings.Contains(err.Error(), "password authentication failed") {return false, nil}- if isErrorDatabaseNotFound(err, dbName) {+ if isErrorDatabaseNotFound(err, params) {return true, nil}return false, err@@ -413,7 +431,7 @@ func verifyPostgresPq(params map[string]string) (bool, error) {params[pgSslmode] = pgSslmodeDisabledefer delete(params, pgSslmode) // We want to return with the original params map intact (for ExtraData)return verifyPostgresPq(params)- case isErrorDatabaseNotFound(err, params[pgDbname]):+ case isErrorDatabaseNotFound(err, params):return true, nil // If we know this, we were able to authenticatedefault:return false, errdiff --git a/pkg/detectors/postgres/postgres_test.go b/pkg/detectors/postgres/postgres_test.goindex 022c7b09b..465682972 100644--- a/pkg/detectors/postgres/postgres_test.go+++ b/pkg/detectors/postgres/postgres_test.go@@ -8,6 +8,7 @@ import ("github.com/google/go-cmp/cmp""github.com/jackc/pgx/v5/pgconn"+ "github.com/lib/pq""github.com/stretchr/testify/assert""github.com/stretchr/testify/require"@@ -179,7 +180,7 @@ func TestClassifyPostgresVerifyError(t *testing.T) {tests := []struct {name stringerr error- dbName string+ params map[string]stringwantVerified boolwantErr bool}{@@ -192,7 +193,19 @@ func TestClassifyPostgresVerifyError(t *testing.T) {{name: "missing database code",err: &pgconn.PgError{Code: "3D000", Message: `database "app" does not exist`},- dbName: "app",+ params: map[string]string{pgDbname: "app"},+ wantVerified: true,+ wantErr: false,+ },+ {+ name: "missing database code needs no parameters",+ err: &pgconn.PgError{Code: "3D000", Message: "la base de données n'existe pas"},+ wantVerified: true,+ wantErr: false,+ },+ {+ name: "missing database code from lib/pq",+ err: &pq.Error{Code: "3D000", Message: `database "app" does not exist`},wantVerified: true,wantErr: false,},@@ -203,8 +216,30 @@ func TestClassifyPostgresVerifyError(t *testing.T) {wantErr: false,},{- name: "missing database by message",+ name: "missing database by message names the given database",+ err: errors.New(`database "app" does not exist`),+ params: map[string]string{pgDbname: "app"},+ wantVerified: true,+ wantErr: false,+ },+ {+ name: "missing database by message names the user when no database is given",+ err: errors.New(`database "svc_reports" does not exist`),+ params: map[string]string{pgUser: "svc_reports"},+ wantVerified: true,+ wantErr: false,+ },+ {+ name: "missing database by message for a database nobody asked for",err: errors.New(`database "postgres" does not exist`),+ params: map[string]string{pgUser: "svc_reports"},+ wantVerified: false,+ wantErr: true,+ },+ {+ name: "proxy relays the failure without a sqlstate",+ err: errors.New(`server login has been failing, cached error: database "svc_reports" does not exist`),+ params: map[string]string{pgUser: "svc_reports"},wantVerified: true,wantErr: false,},@@ -219,7 +254,7 @@ func TestClassifyPostgresVerifyError(t *testing.T) {for _, tc := range tests {t.Run(tc.name, func(t *testing.T) {t.Parallel()- verified, err := classifyPostgresVerifyError(tc.err, tc.dbName)+ verified, err := classifyPostgresVerifyError(tc.err, tc.params)assert.Equal(t, tc.wantVerified, verified)if tc.wantErr {assert.Error(t, err)