mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 12:24:44 +00:00
fix(config): harden DSN password redaction for escaped values and malformed URLs (#656)
Applies #656 (escaped quotes/backslashes in keyword=value DSNs and opaque URLs) and hardens config.Redact to fail closed on malformed postgres:// or postgresql:// URLs instead of falling through to keyword=value redaction. Co-authored-by: Coff0xc <184964491+Coff0xc@users.noreply.github.com>
This commit is contained in:
1 parent
256e16ac16
commit
bfdb026e36
2 files changed
+81
-4
No files matched your search
+55
-4
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user