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
76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package check
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"net"
|
|
"net/url"
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// TLS modes of services with optional encryption: "require" encrypts
|
|
// without verifying the certificate, "verify" also verifies it.
|
|
func validTLSMode(m string) bool { return m == "disable" || m == "require" || m == "verify" }
|
|
|
|
// tlsFor returns the client TLS settings for mode, or nil for "disable".
|
|
func tlsFor(mode, target string) *tls.Config {
|
|
if mode == "disable" {
|
|
return nil
|
|
}
|
|
host, _, _ := net.SplitHostPort(target)
|
|
return &tls.Config{ServerName: host, InsecureSkipVerify: mode == "require"} //nolint:gosec // "require" encrypts without verifying, as chosen per monitor
|
|
}
|
|
|
|
// redact shortens err for customers and removes secrets some clients
|
|
// 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" {
|
|
return describeNetErr(err)
|
|
}
|
|
msg := err.Error()
|
|
for _, s := range secrets {
|
|
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)
|
|
}
|