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"
|
||||
"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)
|
||||
}
|
||||
Reference in new issue
Block a user