Merge pull request #354 from Coff0xc/fix/dsn-redact

fix(config): mask keyword-value and query DSN passwords
This commit is contained in:
BaaRai
2026-11-19 14:25:37 +01:00
committed by GitHub
3 changed files with 39 additions and 7 deletions
+1
View File
@@ -49,6 +49,7 @@ All notable changes to this project are documented in this file. The format is b
### Fixed
- **DSN password redaction**: `config.Redact` now masks libpq `password=` keywords and `?password=` query values as well as URL userinfo, so a ping-failure log cannot print the database password.
- **Asset DSL nesting**: `ParseDSL` refuses queries longer than 4 KiB or nested more than 32 parentheses deep, so a crafted `dsl` cannot grow the goroutine stack without bound.
- **Retry-After overflow**: a 429/503 `Retry-After` of huge seconds no longer wraps `time.Duration` into a tiny delay. Values above `MaxDelay` are not retried.
- **Conversation message pages**: `GET /api/conversations/{id}/messages` clamps `limit` (200 for history, 1000 for `?since=`), so a huge query cannot load the whole transcript in one response.
+22 -7
View File
@@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
@@ -209,16 +210,30 @@ func (d Database) buildDSN() string {
return u.String()
}
// String is a redacted view of the resolved DSN (password masked) for logging.
// 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*)`)
// Redact is a log-safe view of a PostgreSQL DSN. It masks URL userinfo
// passwords, `?password=` query values, and libpq `password=` keywords.
// Unrecognised input still runs the keyword mask so a parse failure cannot
// print the secret.
func Redact(dsn string) string {
u, err := url.Parse(dsn)
if err != nil {
if dsn == "" {
return dsn
}
if u.User != nil {
if _, hasPw := u.User.Password(); hasPw {
u.User = url.UserPassword(u.User.Username(), "****")
if u, err := url.Parse(dsn); err == nil && (u.Scheme == "postgres" || u.Scheme == "postgresql") {
if u.User != nil {
if _, hasPw := u.User.Password(); hasPw {
u.User = url.UserPassword(u.User.Username(), "****")
}
}
q := u.Query()
if q.Has("password") {
q.Set("password", "****")
u.RawQuery = q.Encode()
}
return u.String()
}
return fmt.Sprintf("%s", u.String())
return libpqPassword.ReplaceAllString(dsn, `${1}=****`)
}
+16
View File
@@ -44,6 +44,22 @@ func TestPostgresDSNPrecedence(t *testing.T) {
// A config file that exists but is not valid JSON must fail loudly, not run with
// defaults (or report "no database config") as if the file were absent.
func TestRedactMasksEveryDSNPasswordForm(t *testing.T) {
cases := []struct{ in, secret string }{
{"postgres://u:secret@localhost:5432/d?sslmode=require", "secret"},
{"postgresql://localhost/d?password=secret&sslmode=require", "secret"},
{"host=localhost user=u password=secret dbname=d", "secret"},
{"host=localhost password='p a ss' dbname=d", "p a ss"},
{"not a dsn password=stillsecret", "stillsecret"},
}
for _, c := range cases {
got := Redact(c.in)
if strings.Contains(got, c.secret) {
t.Errorf("Redact(%q)=%q still contains %q", c.in, got, c.secret)
}
}
}
func TestMalformedConfigIsAnError(t *testing.T) {
cfgPath := filepath.Join(t.TempDir(), "config.json")
// What install.sh used to write for a password containing a double quote.