diff --git a/config/config.go b/config/config.go index c10458d1..a79e1aa7 100644 --- a/config/config.go +++ b/config/config.go @@ -254,9 +254,51 @@ func (d Database) buildDSN() string { return u.String() } -// libpqPassword matches a libpq keyword DSN password field, including quoted -// values. pgx accepts this form as well as the URL form. -var libpqPassword = regexp.MustCompile(`(?i)\b(password)\s*=\s*(?:'[^']*'|"[^"]*"|\S*)`) +var libpqPassword = regexp.MustCompile(`(?i)\bpassword\s*=\s*`) + +// redactKeywordPasswords consumes escaped characters as part of the password, +// including escaped whitespace and quotes. An unterminated quoted value consumes +// the rest of the input: malformed configuration must not reveal its secret. +func redactKeywordPasswords(dsn string) string { + var out strings.Builder + for { + loc := libpqPassword.FindStringIndex(dsn) + if loc == nil { + out.WriteString(dsn) + return out.String() + } + out.WriteString(dsn[:loc[0]]) + out.WriteString("password=****") + i := loc[1] + var quote byte + if i < len(dsn) && (dsn[i] == '\'' || dsn[i] == '"') { + quote = dsn[i] + i++ + } + for i < len(dsn) { + c := dsn[i] + if c == '\\' { + i++ + if i < len(dsn) { + i++ + } + continue + } + if quote != 0 { + i++ + if c == quote { + break + } + } else { + if strings.ContainsRune(" \t\r\n\v\f", rune(c)) { + break + } + i++ + } + } + dsn = dsn[i:] + } +} // Redact is a log-safe view of a PostgreSQL DSN. It masks URL userinfo // passwords, `?password=` query values, and libpq `password=` keywords. @@ -267,6 +309,9 @@ func Redact(dsn string) string { return dsn } if u, err := url.Parse(dsn); err == nil && (u.Scheme == "postgres" || u.Scheme == "postgresql") { + if u.Opaque != "" { + return u.Scheme + ":[redacted]" + } if u.User != nil { if _, hasPw := u.User.Password(); hasPw { u.User = url.UserPassword(u.User.Username(), "****") @@ -288,5 +333,11 @@ func Redact(dsn string) string { } } } - return libpqPassword.ReplaceAllString(dsn, `${1}=****`) + lower := strings.ToLower(strings.TrimSpace(dsn)) + if strings.HasPrefix(lower, "postgres://") || strings.HasPrefix(lower, "postgresql://") { + // Fail closed: a malformed URL (e.g. raw '%' in password or bad port) may + // carry user:pass@ that keyword=value redaction will not match. + return "postgres://[redacted]" + } + return redactKeywordPasswords(dsn) } diff --git a/config/redact_escape_test.go b/config/redact_escape_test.go new file mode 100644 index 00000000..78b5e807 --- /dev/null +++ b/config/redact_escape_test.go @@ -0,0 +1,26 @@ +package config + +import "testing" + +func TestRedactEscapedAndMalformedPasswords(t *testing.T) { + cases := []struct{ name, in, want string }{ + {"quoted apostrophe", `host=localhost password='pa\'ss' dbname=d`, `host=localhost password=**** dbname=d`}, + {"unquoted space", `host=localhost password=pa\ ss dbname=d`, `host=localhost password=**** dbname=d`}, + {"escaped backslash", `password='pa\\ss' host=localhost`, `password=**** host=localhost`}, + {"escaped quote", `password="pa\"ss" host=localhost`, `password=**** host=localhost`}, + {"unterminated quoted password", `password='pa ss dbname=d`, `password=****`}, + {"trailing escape", `password=pa\`, `password=****`}, + {"multiple passwords", `password='one\'suffix' password=two\ suffix host=h`, `password=**** password=**** host=h`}, + {"opaque URL", `postgres:user:secret@host`, `postgres:[redacted]`}, + {"opaque URL query", `postgresql:user:secret@host?password=more`, `postgresql:[redacted]`}, + {"malformed url bad percent escape", `postgres://artex:p%ssSecret@db:5432/artex`, `postgres://[redacted]`}, + {"malformed postgresql url bad port", `postgresql://artex:TopSecret!@db:badport/artex`, `postgres://[redacted]`}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := Redact(tc.in); got != tc.want { + t.Fatalf("Redact(%q) = %q; want %q", tc.in, got, tc.want) + } + }) + } +}