fix(bonfire): mask secret flag values in the wrong-directory hint
- the Try line replaces the value of every long flag whose name contains password, passwd, secret, token or key with ***, in the --flag value and --flag=value forms; other arguments are unchanged - README and the writing-commands page say so (WR-08)
This commit is contained in:
@@ -218,3 +218,47 @@ func TestWithExitCode(t *testing.T) {
|
||||
t.Errorf("exitCode through a wrapper = %d, want 4", code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWrongDirectoryMasksSecrets (WR-08): the wrong-directory Try line
|
||||
// replaces the value of every flag named like a password, secret, token or
|
||||
// key with ***, in both the "--flag value" and "--flag=value" forms, and
|
||||
// keeps every other argument.
|
||||
func TestWrongDirectoryMasksSecrets(t *testing.T) {
|
||||
plainEnv(t)
|
||||
root := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(root, "summer.yaml"), []byte("binary: acme\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sub := filepath.Join(root, "bin")
|
||||
if err := os.MkdirAll(sub, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fail := errors.New("lagoon: database.dsn is empty")
|
||||
cases := []struct {
|
||||
args []string
|
||||
want string
|
||||
}{
|
||||
{[]string{"admin:create", "--email", "a@b.c", "--password", "hunter2"}, "cd ..; {bin} admin:create --email a@b.c --password ***"},
|
||||
{[]string{"admin:create", "--email", "a@b.c", "--password=hunter2"}, "cd ..; {bin} admin:create --email a@b.c --password=***"},
|
||||
{[]string{"sync", "--client-secret", "s3cr3t", "--verbose"}, "cd ..; {bin} sync --client-secret *** --verbose"},
|
||||
{[]string{"sync", "--api-token=t0k3n"}, "cd ..; {bin} sync --api-token=***"},
|
||||
{[]string{"sync", "--key", "k3y", "acme blog"}, "cd ..; {bin} sync --key *** 'acme blog'"},
|
||||
{[]string{"sync", "--PASSWD", "hunter2"}, "cd ..; {bin} sync --PASSWD ***"},
|
||||
{[]string{"sync", "--token", "--force"}, "cd ..; {bin} sync --token --force"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := wrongDirectory("acme", fail, tc.args, sub)
|
||||
var hint *HintError
|
||||
if !errors.As(got, &hint) {
|
||||
t.Fatalf("%q: no hint: %v", tc.args, got)
|
||||
}
|
||||
if hint.Try != tc.want {
|
||||
t.Errorf("%q: Try = %q, want %q", tc.args, hint.Try, tc.want)
|
||||
}
|
||||
for _, secret := range []string{"hunter2", "s3cr3t", "t0k3n", "k3y"} {
|
||||
if strings.Contains(hint.Try, secret) {
|
||||
t.Errorf("%q: Try line repeats %q: %q", tc.args, secret, hint.Try)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user