ARTEX/config/redact_escape_test.go

63 lines
3.2 KiB
Go

package config
import (
"strings"
"testing"
)
// sslpassword (the passphrase of an encrypted client key) is as secret as
// password, and an upper-case keyword or query key names the same parameter.
func TestRedactSSLPasswordAndUpperCase(t *testing.T) {
keyword := []struct{ name, in, want string }{
{"sslpassword keyword", `host=h sslmode=verify-full sslpassword=keypass dbname=d`, `host=h sslmode=verify-full sslpassword=**** dbname=d`},
{"quoted sslpassword keyword", `sslpassword='key pass' host=h`, `sslpassword=**** host=h`},
{"upper-case PASSWORD keyword", `host=h PASSWORD=secret dbname=d`, `host=h password=**** dbname=d`},
{"upper-case SSLPASSWORD keyword", `host=h SSLPASSWORD = keypass`, `host=h sslpassword=****`},
{"both keywords", `password=one sslpassword=two host=h`, `password=**** sslpassword=**** host=h`},
{"sslpassword after a parse failure", `postgres://u:pa%zz@h/d sslpassword=keypass`, `postgres://[redacted]`},
}
for _, tc := range keyword {
t.Run(tc.name, func(t *testing.T) {
if got := Redact(tc.in); got != tc.want {
t.Fatalf("Redact(%q) = %q; want %q", tc.in, got, tc.want)
}
})
}
url := []struct{ name, in, secret, keep string }{
{"sslpassword query", "postgres://u@h:5432/d?sslmode=verify-full&sslpassword=keypass", "keypass", "sslmode=verify-full"},
{"upper-case PASSWORD query", "postgresql://u@h/d?PASSWORD=secret&sslmode=require", "secret", "sslmode=require"},
{"upper-case SSLPASSWORD query", "postgres://u@h/d?SSLPASSWORD=keypass", "keypass", "SSLPASSWORD="},
{"userinfo and sslpassword query", "postgres://u:userpass@h/d?sslpassword=keypass", "userpass", "sslpassword=%2A%2A%2A%2A"},
}
for _, tc := range url {
t.Run(tc.name, func(t *testing.T) {
got := Redact(tc.in)
if strings.Contains(got, tc.secret) || !strings.Contains(got, tc.keep) {
t.Fatalf("Redact(%q) = %q; want %q masked and %q kept", tc.in, got, tc.secret, tc.keep)
}
})
}
}
func TestRedactEscapedAndMalformedPasswords(t *testing.T) {
cases := []struct{ name, in, want string }{
{"quoted apostrophe", `host=localhost password='pa\'ss' dbname=d`, `host=localhost password=**** dbname=d`},
{"unquoted space", `host=localhost password=pa\ ss dbname=d`, `host=localhost password=**** dbname=d`},
{"escaped backslash", `password='pa\\ss' host=localhost`, `password=**** host=localhost`},
{"escaped quote", `password="pa\"ss" host=localhost`, `password=**** host=localhost`},
{"unterminated quoted password", `password='pa ss dbname=d`, `password=****`},
{"trailing escape", `password=pa\`, `password=****`},
{"multiple passwords", `password='one\'suffix' password=two\ suffix host=h`, `password=**** password=**** host=h`},
{"opaque URL", `postgres:user:secret@host`, `postgres:[redacted]`},
{"opaque URL query", `postgresql:user:secret@host?password=more`, `postgresql:[redacted]`},
{"malformed url bad percent escape", `postgres://artex:p%ssSecret@db:5432/artex`, `postgres://[redacted]`},
{"malformed postgresql url bad port", `postgresql://artex:TopSecret!@db:badport/artex`, `postgres://[redacted]`},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := Redact(tc.in); got != tc.want {
t.Fatalf("Redact(%q) = %q; want %q", tc.in, got, tc.want)
}
})
}
}