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
34 lines
1.4 KiB
Go
34 lines
1.4 KiB
Go
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)
|
|
}
|
|
}
|
|
}
|