Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func (cmd *Command) set(fName string, f Flag, val string) error {
cmd.setFlags[f] = struct{}{}
cmd.setMultiValueParsingConfig(f)
if err := f.Set(fName, val); err != nil {
return fmt.Errorf("invalid value %q for flag -%s: %v", val, fName, err)
return fmt.Errorf("invalid value %q for flag -%s: %v", redactValue(f, val, val), fName, redactValue(f, err.Error(), val))
}
return nil
}
Expand Down Expand Up @@ -690,6 +690,43 @@ func (cmd *Command) FlagNames() []string {
return names
}

// SensitiveValues returns the current values of all flags marked as
// sensitive (see [SensitiveFlag]) in this command and all of its parent
// commands, so that they can be removed from logs or other output.
// The value of a flag that was not set is its default value. Empty values
// are left out, and only values that are text (strings, string slices,
// string map values and [fmt.Stringer] values) are returned. The values
// are sorted longest first, so that replacing them in that order also
// works when one value contains another.
func (cmd *Command) SensitiveValues() []string {
seenFlags := map[Flag]struct{}{}
seenVals := map[string]struct{}{}
vals := []string{}

for _, c := range cmd.Lineage() {
for _, f := range c.allFlags() {
if _, ok := seenFlags[f]; ok || !isSensitive(f) {
continue
}
seenFlags[f] = struct{}{}

for _, val := range sensitiveStrings(f.Get()) {
if _, ok := seenVals[val]; ok || val == "" {
continue
}
seenVals[val] = struct{}{}
vals = append(vals, val)
}
}
}

slices.SortStableFunc(vals, func(a, b string) int {
return len(b) - len(a)
})

return vals
}

// Lineage returns *this* command and all of its ancestor commands
// in order from child to parent
func (cmd *Command) Lineage() []*Command {
Expand Down
12 changes: 12 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6756,6 +6756,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "",
"required": false,
"sensitive": false,
"hidden": false,
"deprecated": "",
"hideDefault": false,
Expand All @@ -6778,6 +6779,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "some usage text",
"required": false,
"sensitive": false,
"hidden": false,
"deprecated": "",
"hideDefault": false,
Expand Down Expand Up @@ -6823,6 +6825,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "",
"required": false,
"sensitive": false,
"hidden": false,
"deprecated": "",
"hideDefault": false,
Expand All @@ -6845,6 +6848,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "another usage text",
"required": false,
"sensitive": false,
"hidden": false,
"deprecated": "",
"hideDefault": false,
Expand Down Expand Up @@ -6969,6 +6973,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "",
"required": false,
"sensitive": false,
"hidden": false,
"deprecated": "",
"hideDefault": false,
Expand Down Expand Up @@ -7037,6 +7042,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "some usage text",
"required": false,
"sensitive": false,
"hidden": false,
"deprecated": "",
"hideDefault": false,
Expand Down Expand Up @@ -7082,6 +7088,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "",
"required": false,
"sensitive": false,
"hidden": false,
"deprecated": "",
"hideDefault": false,
Expand All @@ -7104,6 +7111,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "another usage text",
"required": false,
"sensitive": false,
"hidden": false,
"deprecated": "",
"hideDefault": false,
Expand Down Expand Up @@ -7149,6 +7157,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "some 'usage' text",
"required": false,
"sensitive": false,
"hidden": false,
"deprecated": "",
"hideDefault": false,
Expand All @@ -7170,6 +7179,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "",
"required": false,
"sensitive": false,
"hidden": false,
"deprecated": "",
"hideDefault": false,
Expand All @@ -7192,6 +7202,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "another usage text",
"required": false,
"sensitive": false,
"hidden": false,
"deprecated": "",
"hideDefault": false,
Expand All @@ -7213,6 +7224,7 @@ func TestJSONExportCommand(t *testing.T) {
"defaultText": "",
"usage": "",
"required": false,
"sensitive": false,
"hidden": true,
"deprecated": "",
"hideDefault": false,
Expand Down
2 changes: 1 addition & 1 deletion docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func stringifyFlag(f Flag) string {
if df.IsDefaultVisible() {
if s := df.GetDefaultText(); s != "" {
defaultValueString = fmt.Sprintf(formatDefault("%s"), s)
} else if df.TakesValue() && df.GetValue() != "" {
} else if df.TakesValue() && df.GetValue() != "" && !isSensitive(f) {
defaultValueString = fmt.Sprintf(formatDefault("%s"), df.GetValue())
}
}
Expand Down
55 changes: 55 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli_test
import (
"context"
"fmt"
"io"
"log/slog"
"net/mail"
"os"
"strings"
Expand Down Expand Up @@ -490,3 +492,56 @@ func ExampleCommand_Walk_visibleTree() {
// list
// version
}

// redactingWriter replaces secrets in everything written to w. Any logger
// that writes to an io.Writer can use it, such as log, log/slog or zap.
type redactingWriter struct {
w io.Writer
r *strings.Replacer
}

func newRedactingWriter(w io.Writer, cmd *cli.Command) *redactingWriter {
// SensitiveValues sorts longest first, so a secret that contains
// another is replaced as a whole
var oldnew []string
for _, v := range cmd.SensitiveValues() {
oldnew = append(oldnew, v, "[REDACTED]")
}
return &redactingWriter{w: w, r: strings.NewReplacer(oldnew...)}
}

func (rw *redactingWriter) Write(p []byte) (int, error) {
if _, err := io.WriteString(rw.w, rw.r.Replace(string(p))); err != nil {
return 0, err
}
return len(p), nil
}

func ExampleCommand_SensitiveValues() {
cmd := &cli.Command{
Name: "deploy",
Flags: []cli.Flag{
&cli.StringFlag{Name: "token", Sensitive: true},
&cli.StringFlag{Name: "region"},
},
Action: func(_ context.Context, cmd *cli.Command) error {
// Build the logger after parsing, once the values are known
logger := slog.New(slog.NewTextHandler(newRedactingWriter(os.Stdout, cmd), &slog.HandlerOptions{
// leave out the time so the output below is stable
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
},
}))

logger.Info("deploying", "region", cmd.String("region"), "auth", "Bearer "+cmd.String("token"))
return nil
},
}

_ = cmd.Run(context.Background(), []string{"deploy", "--token", "s3cret", "--region", "eu-west-1"})
// Output:
// level=INFO msg=deploying region=eu-west-1 auth="Bearer [REDACTED]"
}
85 changes: 85 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ type DeprecatedFlag interface {
GetDeprecated() string
}

// SensitiveFlag is an interface that allows us to mark flags whose values
// are secrets, such as passwords or tokens. The value of a sensitive flag
// is not shown in help output or error messages, and is returned by
// [Command.SensitiveValues] so that it can be removed from other output.
//
// Encoding a command or a flag as JSON includes the default value of a
// sensitive flag, but never the value it was set to. To keep this so for
// flag types whose value is a pointer, such as [GenericFlag] and [TextFlag],
// a sensitive flag parses into a shallow copy of its default value rather
// than into the default value itself, so the object given as the default
// value keeps its default. Read the parsed value with methods such as
// [Command.Generic] and [Command.Text], or through Destination.
//
// A value that implements [encoding.TextMarshaler] and
// [encoding.TextUnmarshaler], as every TextFlag value does, is copied
// deeply: its text form is unmarshaled into a new zero value. Any other
// value is copied shallowly, so a value whose Set method writes into a map,
// slice or pointer that it shares with the default value also changes the
// default value.
type SensitiveFlag interface {
// IsSensitive returns true if the flag's value must be kept secret
IsSensitive() bool
}

// DocGenerationFlag is an interface that allows documentation generation for the flag
type DocGenerationFlag interface {
// TakesValue returns true if the flag takes a value, otherwise false
Expand Down Expand Up @@ -218,6 +242,67 @@ type LocalFlag interface {
IsLocal() bool
}

// redactedText replaces the value of a sensitive flag in output
const redactedText = "REDACTED"

func isSensitive(f Flag) bool {
sf, ok := f.(SensitiveFlag)
return ok && sf.IsSensitive()
}

// sensitivePartser is implemented by flags that can split a raw value into
// the parts that an error message may quote, such as the items of a slice
// or the values of a map
type sensitivePartser interface {
sensitiveParts(val string) []string
}

// redactValue replaces each occurrence of val, and of each part of val that
// an error message may quote on its own, in s if f is sensitive
func redactValue(f Flag, s, val string) string {
if !isSensitive(f) || val == "" {
return s
}

parts := []string{val}
if p, ok := f.(sensitivePartser); ok {
parts = p.sensitiveParts(val)
}

// Replace longer parts first, so that a part that contains another is
// replaced as a whole
parts = slices.DeleteFunc(slices.Clone(parts), func(p string) bool { return p == "" })
slices.SortStableFunc(parts, func(a, b string) int { return len(b) - len(a) })
parts = slices.Compact(parts)

oldnew := make([]string, 0, 2*len(parts))
for _, p := range parts {
oldnew = append(oldnew, p, redactedText)
}
return strings.NewReplacer(oldnew...).Replace(s)
}

// sensitiveStrings returns the string forms of a sensitive flag's value.
// Only values that are text are returned, as replacing e.g. a number
// everywhere in a log line would hide much more than the secret.
func sensitiveStrings(v any) []string {
switch v := v.(type) {
case string:
return []string{v}
case []string:
return v
case map[string]string:
vals := make([]string, 0, len(v))
for _, val := range v {
vals = append(vals, val)
}
return vals
case fmt.Stringer:
return []string{v.String()}
}
return nil
}

func visibleFlags(fl []Flag) []Flag {
var visible []Flag
for _, f := range fl {
Expand Down
Loading
Loading