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) } } }