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:
Jakub Zych
2026-10-22 02:22:40 +02:00
parent fc7706dfff
commit 9b62285654
4 changed files with 93 additions and 7 deletions

View File

@@ -205,7 +205,8 @@ const wrongDirectoryExplanation = "This command runs from the application root."
// name other than summer) failed while the working directory dir ("" means
// os.Getwd) has no config directory and a parent directory holds
// summer.yaml: compass then found no configuration, which is why the command
// failed (D-19). The Try line is "cd <relative path>; bin/<name> <args>".
// failed (D-19). The Try line is "cd <relative path>; bin/<name> <args>",
// with the values of secret-looking flags masked (tryArgs).
// Usage errors, Reported errors and summer are left alone; nothing else
// about the failure changes.
func wrongDirectory(name string, err error, args []string, dir string) error {
@@ -239,13 +240,54 @@ func wrongDirectory(name string, err error, args []string, dir string) error {
if rerr != nil {
return err
}
try := "cd " + shellQuote(rel) + "; {bin}"
for _, a := range args {
try += " " + shellQuote(a)
}
try := "cd " + shellQuote(rel) + "; {bin}" + tryArgs(args)
return &HintError{Err: err, Explanation: wrongDirectoryExplanation, Try: try}
}
// secretWords mark a long flag whose value the Try line masks.
var secretWords = []string{"password", "passwd", "secret", "token", "key"}
// secretFlag reports whether the long flag name (without "--") looks like it
// carries a secret.
func secretFlag(name string) bool {
name = strings.ToLower(name)
for _, w := range secretWords {
if strings.Contains(name, w) {
return true
}
}
return false
}
// tryArgs renders args for the wrong-directory Try line, each preceded by a
// space and quoted for a shell. The value of a long flag named like a
// secret becomes ***, in the "--flag=value" form and in the "--flag value"
// form (the next argument, when it does not start with "-"), so a password
// typed on the command line does not reach stderr and CI logs.
func tryArgs(args []string) string {
var b strings.Builder
for i := 0; i < len(args); i++ {
a := args[i]
b.WriteByte(' ')
name, long := strings.CutPrefix(a, "--")
flag, _, inline := strings.Cut(name, "=")
if !long || flag == "" || !secretFlag(flag) {
b.WriteString(shellQuote(a))
continue
}
if inline {
b.WriteString(shellQuote("--"+flag) + "=***")
continue
}
b.WriteString(shellQuote(a))
if i+1 < len(args) && !strings.HasPrefix(args[i+1], "-") {
b.WriteString(" ***")
i++
}
}
return b.String()
}
// shellQuote single-quotes s when a shell would split or expand it.
func shellQuote(s string) string {
if s != "" && !strings.ContainsAny(s, " \t\n'\"\\$`*?[]{}()<>|&;#~!") {