mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 04:14:15 +00:00
Merge pull request #1014 from Sinfinal/fix/config-redact-fail-closed
fix(config): fail closed in Redact on split userinfo passwords and malformed queries
This commit is contained in:
2 files changed
+44
-1
No files matched your search
+17
-1
@@ -308,6 +308,16 @@ func redactKeywordPasswords(dsn string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
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
|
||||
// passwords, `?password=` and `?sslpassword=` query values, and libpq
|
||||
// `password=` and `sslpassword=` keywords, in any letter case.
|
||||
@@ -321,12 +331,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) {
|
||||
|
||||
@@ -115,6 +115,26 @@ 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"},
|
||||
// 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)
|
||||
@@ -122,6 +142,13 @@ 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.
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMalformedConfigIsAnError(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user