From ae9754bd624da693aadee716f7a940a64d9fb9fb Mon Sep 17 00:00:00 2001 From: Sinfinal <131638103+Sinfinal@users.noreply.github.com> Date: Sun, 6 Dec 2026 03:23:07 +0800 Subject: [PATCH 1/2] fix(config): fail closed in Redact on split userinfo passwords and malformed queries --- config/config.go | 29 ++++++++++++++++++++++++++++- config/config_test.go | 20 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index dc9272ef..14d57384 100644 --- a/config/config.go +++ b/config/config.go @@ -308,6 +308,27 @@ func redactKeywordPasswords(dsn string) string { } } +// hasSplitUserinfoPassword reports whether url.Parse likely split an unencoded +// '#', '/' or '?' inside userinfo (e.g. "u:1234#secret@host/d" or +// "u:p@ss#secret@host/d") so the trailing '@' landed in Path, RawQuery, or Fragment. +func hasSplitUserinfoPassword(u *url.URL) bool { + if u.User == nil && !strings.Contains(u.Host, ":") { + return false + } + if strings.Contains(u.Fragment, "@") || strings.Contains(u.Path, "@") { + return true + } + if !strings.Contains(u.RawQuery, "@") { + return false + } + if u.Path == "" { + return true + } + at := strings.IndexByte(u.RawQuery, '@') + eq := strings.IndexByte(u.RawQuery, '=') + return eq < 0 || at < eq +} + // Redact is a log-safe view of a PostgreSQL DSN. It masks URL userinfo // passwords, `?password=` and `?sslpassword=` query values, and libpq // `password=` and `sslpassword=` keywords, in any letter case. @@ -321,12 +342,18 @@ func Redact(dsn string) string { if u.Opaque != "" { return u.Scheme + ":[redacted]" } + if hasSplitUserinfoPassword(u) { + return u.Scheme + "://[redacted]" + } if u.User != nil { if _, hasPw := u.User.Password(); hasPw { u.User = url.UserPassword(u.User.Username(), "****") } } - q := u.Query() + q, qerr := url.ParseQuery(u.RawQuery) + if qerr != nil { + return u.Scheme + "://[redacted]" + } masked := false for k := range q { if isDSNSecretParam(k) { diff --git a/config/config_test.go b/config/config_test.go index 3379b229..a414eac5 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -115,6 +115,21 @@ func TestRedactMasksEveryDSNPasswordForm(t *testing.T) { {"postgres://u:pa/ss@localhost/d", "pa/ss"}, {"postgresql://u:50%off@localhost/d", "50%off"}, {"postgres://u:a?b@localhost/d", "a?b"}, + // Numeric or empty prefix before # / ? lets url.Parse succeed with u.Host="u:1234" or "u:". + {"postgres://u:1234#secret@localhost:5432/d?sslmode=disable", "secret"}, + {"postgres://u:#secret@localhost:5432/d?sslmode=disable", "secret"}, + {"postgres://u:1234/secret@localhost/d", "secret"}, + {"postgres://u:/secret@localhost/d", "secret"}, + {"postgres://u:1234?secret@localhost/d", "secret"}, + {"postgres://u:?secret@localhost/d", "secret"}, + {"postgres://u:1234/a?b@localhost/d", "b@localhost"}, + // Password containing '@' before '#' or '/' splits authority prematurely. + {"postgres://u:p@ss#secret@localhost:5432/d", "secret"}, + {"postgres://u:p@ss/secret@localhost/d", "secret"}, + // Raw '%' or ';' in query string makes url.ParseQuery drop the secret parameter. + {"postgres://localhost/d?password=50%off&sslmode=require", "50%off"}, + {"postgres://localhost/d?sslpassword=50%off&sslmode=require", "50%off"}, + {"postgres://localhost/d?sslmode=require;password=secret", "secret"}, } for _, c := range cases { got := Redact(c.in) @@ -122,6 +137,11 @@ func TestRedactMasksEveryDSNPasswordForm(t *testing.T) { t.Errorf("Redact(%q)=%q still contains %q", c.in, got, c.secret) } } + // A benign DSN with an '@' inside a non-secret query value must remain intact. + const benign = "postgres://localhost:5432/d?application_name=team@ops" + if got := Redact(benign); got != benign { + t.Errorf("Redact(%q)=%q, want unchanged", benign, got) + } } func TestMalformedConfigIsAnError(t *testing.T) { From 4a74435d3825ef9ed11752a980f0db80a72a2316 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sat, 5 Dec 2026 22:33:51 +0100 Subject: [PATCH 2/2] fix(config): redact any raw @ outside the userinfo A raw @ in the path, query or fragment has the same shape as a password that an unencoded #, / or ? split out of the userinfo, so it now fails closed to postgres://[redacted]. The benign query test uses the percent-encoded form, and the split-after-= cases are in it. --- config/config.go | 25 +++++++------------------ config/config_test.go | 9 ++++++++- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/config/config.go b/config/config.go index 14d57384..15d80b0f 100644 --- a/config/config.go +++ b/config/config.go @@ -308,25 +308,14 @@ func redactKeywordPasswords(dsn string) string { } } -// hasSplitUserinfoPassword reports whether url.Parse likely split an unencoded -// '#', '/' or '?' inside userinfo (e.g. "u:1234#secret@host/d" or -// "u:p@ss#secret@host/d") so the trailing '@' landed in Path, RawQuery, or Fragment. +// hasSplitUserinfoPassword reports whether a raw '@' landed outside the +// userinfo, in Path, RawQuery or Fragment, where url.Parse puts it when a +// password contains an unencoded '#', '/' or '?'. Any such '@' fails closed: a +// split password cannot be told apart from a harmless '@' in a query value. func hasSplitUserinfoPassword(u *url.URL) bool { - if u.User == nil && !strings.Contains(u.Host, ":") { - return false - } - if strings.Contains(u.Fragment, "@") || strings.Contains(u.Path, "@") { - return true - } - if !strings.Contains(u.RawQuery, "@") { - return false - } - if u.Path == "" { - return true - } - at := strings.IndexByte(u.RawQuery, '@') - eq := strings.IndexByte(u.RawQuery, '=') - return eq < 0 || at < eq + return strings.Contains(u.Fragment, "@") || + strings.Contains(u.Path, "@") || + strings.Contains(u.RawQuery, "@") } // Redact is a log-safe view of a PostgreSQL DSN. It masks URL userinfo diff --git a/config/config_test.go b/config/config_test.go index a414eac5..64950c64 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -130,6 +130,11 @@ func TestRedactMasksEveryDSNPasswordForm(t *testing.T) { {"postgres://localhost/d?password=50%off&sslmode=require", "50%off"}, {"postgres://localhost/d?sslpassword=50%off&sslmode=require", "50%off"}, {"postgres://localhost/d?sslmode=require;password=secret", "secret"}, + // A raw '@' in a query value or in the path fails closed: a split + // password has the same shape and cannot be told apart from it. + {"postgres://u:12/x?a=S3CRETMARK@h/d", "S3CRETMARK"}, + {"postgres://u:12/x?a=b&c=S3CRETMARK@h/d", "S3CRETMARK"}, + {"postgres://u:12/S3CRETMARK?a=b@h/d", "S3CRETMARK"}, } for _, c := range cases { got := Redact(c.in) @@ -138,7 +143,9 @@ func TestRedactMasksEveryDSNPasswordForm(t *testing.T) { } } // A benign DSN with an '@' inside a non-secret query value must remain intact. - const benign = "postgres://localhost:5432/d?application_name=team@ops" + // The '@' is percent-encoded: a raw '@' is indistinguishable from a split + // password and is redacted on purpose. + const benign = "postgres://localhost:5432/d?application_name=team%40ops" if got := Redact(benign); got != benign { t.Errorf("Redact(%q)=%q, want unchanged", benign, got) }