From 8b30ab4e79226077b0bc205513e59fba815bea7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sun, 22 Nov 2026 17:03:37 +0100 Subject: [PATCH] fix(check): redact secrets of any length without garbling messages Skipping secrets under four characters left short passwords readable whenever a client repeated them. Secrets are now replaced wherever they stand alone, as clients print them (password=pw, user:pw@host, 'pw'), but not inside longer words, and their URL-encoded forms are replaced too, which connection URLs in errors carry. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3 --- internal/check/secure.go | 45 +++++++++++++++++++++++++++++++---- internal/check/secure_test.go | 33 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 internal/check/secure_test.go diff --git a/internal/check/secure.go b/internal/check/secure.go index 717acda..f473539 100644 --- a/internal/check/secure.go +++ b/internal/check/secure.go @@ -4,7 +4,10 @@ import ( "crypto/tls" "errors" "net" + "net/url" "strings" + "unicode" + "unicode/utf8" ) // TLS modes of services with optional encryption: "require" encrypts @@ -21,8 +24,7 @@ func tlsFor(mode, target string) *tls.Config { } // redact shortens err for customers and removes secrets some clients -// repeat in their errors. Secrets shorter than four characters are left: -// replacing them would garble the message while hiding little. +// repeat in their errors, also in URL-encoded form. func redact(err error, secrets ...string) string { var op *net.OpError if errors.As(err, &op) && op.Op == "dial" { @@ -30,9 +32,44 @@ func redact(err error, secrets ...string) string { } msg := err.Error() for _, s := range secrets { - if len(s) >= 4 { - msg = strings.ReplaceAll(msg, s, "***") + if s == "" { + continue + } + for _, v := range []string{s, url.QueryEscape(s), url.PathEscape(s)} { + msg = replaceStandalone(msg, v, "***") } } return truncate(msg, 300) } + +// replaceStandalone replaces occurrences of s in msg that are not part of +// a longer word. Clients print secrets as values ("password=pw", +// "user:pw@host", "'pw'"), so this hides secrets of any length without +// tearing apart words that merely contain them. +func replaceStandalone(msg, s, with string) string { + var b strings.Builder + done, from := 0, 0 + for { + i := strings.Index(msg[from:], s) + if i < 0 { + b.WriteString(msg[done:]) + return b.String() + } + i += from + before, _ := utf8.DecodeLastRuneInString(msg[:i]) + after, _ := utf8.DecodeRuneInString(msg[i+len(s):]) + if isWordRune(before) || isWordRune(after) { + _, n := utf8.DecodeRuneInString(msg[i:]) + from = i + n + continue + } + b.WriteString(msg[done:i]) + b.WriteString(with) + done = i + len(s) + from = done + } +} + +func isWordRune(r rune) bool { + return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r) +} diff --git a/internal/check/secure_test.go b/internal/check/secure_test.go new file mode 100644 index 0000000..d1d2ee8 --- /dev/null +++ b/internal/check/secure_test.go @@ -0,0 +1,33 @@ +package check + +import ( + "errors" + "testing" +) + +func TestRedact(t *testing.T) { + tests := []struct { + msg string + secrets []string + want string + }{ + // Short secrets are hidden where they stand alone ... + {`failed to connect: password=pw user=app`, []string{"pw"}, `failed to connect: password=*** user=app`}, + {`dial postgres://app:x@db:5432/shop`, []string{"x"}, `dial postgres://app:***@db:5432/shop`}, + {`auth failed for 'ab'`, []string{"ab"}, `auth failed for '***'`}, + // ... without tearing apart words that contain them. + {`destination resolves to a non-public address`, []string{"s"}, `destination resolves to a non-public address`}, + {`access denied for user probe`, []string{"p", "e"}, `access denied for user probe`}, + // Long secrets and secrets with symbols, also URL-encoded. + {`login "Sup3r-Secret!" rejected`, []string{"Sup3r-Secret!"}, `login "***" rejected`}, + {`dial mongodb://root:p%40ss%2Fw@db/admin`, []string{"p@ss/w"}, `dial mongodb://root:***@db/admin`}, + {`dial amqp://u:a+b@mq/`, []string{"a b"}, `dial amqp://u:***@mq/`}, + {`token=abc abc_def xabc abc`, []string{"abc"}, `token=*** abc_def xabc ***`}, + {`umlaut ä and äb`, []string{"ä"}, `umlaut *** and äb`}, + } + for _, tc := range tests { + if got := redact(errors.New(tc.msg), tc.secrets...); got != tc.want { + t.Errorf("redact(%q, %q)\n got %q\nwant %q", tc.msg, tc.secrets, got, tc.want) + } + } +}