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
@@ -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