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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
365152d164
commit
8b30ab4e79
2 files changed
+74
-4
No files matched your search
@@ -4,7 +4,10 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TLS modes of services with optional encryption: "require" encrypts
|
// 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
|
// redact shortens err for customers and removes secrets some clients
|
||||||
// repeat in their errors. Secrets shorter than four characters are left:
|
// repeat in their errors, also in URL-encoded form.
|
||||||
// replacing them would garble the message while hiding little.
|
|
||||||
func redact(err error, secrets ...string) string {
|
func redact(err error, secrets ...string) string {
|
||||||
var op *net.OpError
|
var op *net.OpError
|
||||||
if errors.As(err, &op) && op.Op == "dial" {
|
if errors.As(err, &op) && op.Op == "dial" {
|
||||||
@@ -30,9 +32,44 @@ func redact(err error, secrets ...string) string {
|
|||||||
}
|
}
|
||||||
msg := err.Error()
|
msg := err.Error()
|
||||||
for _, s := range secrets {
|
for _, s := range secrets {
|
||||||
if len(s) >= 4 {
|
if s == "" {
|
||||||
msg = strings.ReplaceAll(msg, s, "***")
|
continue
|
||||||
|
}
|
||||||
|
for _, v := range []string{s, url.QueryEscape(s), url.PathEscape(s)} {
|
||||||
|
msg = replaceStandalone(msg, v, "***")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return truncate(msg, 300)
|
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)
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user