Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

fix(config): mask keyword-value and query DSN passwords

Coff0xc7d28638980ce
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7f48566..35c377e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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.
diff --git a/config/config.go b/config/config.go
index b966429..d17cb9a 100644
--- a/config/config.go
+++ b/config/config.go
@@ -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}=****`)
}
diff --git a/config/config_test.go b/config/config_test.go
index e30c7bc..7be1683 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -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.